From 5579bf6b46388c7d96e1a6a6e6f6104465e81bbb Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Wed, 16 Mar 2016 18:25:17 +1100 Subject: [PATCH 1/5] Add integration for symfony HttpFoundation component --- composer.json | 3 ++- src/Integration/Symfony.php | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/Integration/Symfony.php diff --git a/composer.json b/composer.json index 59a4bd7..cf2f53c 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ }, "require-dev": { "phpunit/phpunit": "~4.8|~5.2", - "codacy/coverage": "dev-master" + "codacy/coverage": "dev-master", + "symfony/http-foundation": "^3.0" }, "autoload": { "psr-4": { diff --git a/src/Integration/Symfony.php b/src/Integration/Symfony.php new file mode 100644 index 0000000..2b7b316 --- /dev/null +++ b/src/Integration/Symfony.php @@ -0,0 +1,52 @@ +getContent())); + } + + /** + * Asserts that json content is valid according to the provided schema string. + * + * @param string $schema Schema data + * @param Response $response JSON content + */ + public static function assertJsonMatchesSchemaString($schema, Response $response) + { + Assert::assertJsonMatchesSchemaString($schema, json_decode($response->getContent())); + } + + /** + * Asserts if the value retrieved with the expression equals the expected value. + * + * Example: + * + * static::assertJsonValueEquals(33, 'foo.bar[0]', $json); + * + * @param mixed $expected Expected value + * @param string $expression Expression to retrieve the result + * (e.g. locations[?state == 'WA'].name | sort(@)) + * @param Response $response JSON Content + */ + public static function assertJsonValueEquals($expected, $expression, $response) + { + Assert::assertJsonValueEquals($expected, $expression, json_decode($response->getContent())); + } +} \ No newline at end of file From 7e90d3da8c8480947b12a5995cca1895352931df Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Wed, 16 Mar 2016 03:25:25 -0400 Subject: [PATCH 2/5] Applied fixes from StyleCI --- src/Integration/Symfony.php | 11 ++++++++++- tests/AssertTraitTest.php | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Integration/Symfony.php b/src/Integration/Symfony.php index 2b7b316..441cb9c 100644 --- a/src/Integration/Symfony.php +++ b/src/Integration/Symfony.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace EnricoStahn\JsonAssert\Integration; use EnricoStahn\JsonAssert\Assert; @@ -49,4 +58,4 @@ public static function assertJsonValueEquals($expected, $expression, $response) { Assert::assertJsonValueEquals($expected, $expression, json_decode($response->getContent())); } -} \ No newline at end of file +} diff --git a/tests/AssertTraitTest.php b/tests/AssertTraitTest.php index f854a99..37830b8 100644 --- a/tests/AssertTraitTest.php +++ b/tests/AssertTraitTest.php @@ -57,7 +57,7 @@ public function testAssertJsonSchemaFailMessage() } /** - * Tests if referenced schemas are loaded automatically + * Tests if referenced schemas are loaded automatically. */ public function testAssertJsonSchemaWithRefs() { From 7156dd9b01e6b6b0bec7db7f36fa32a11722b796 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Thu, 17 Mar 2016 00:18:35 +1100 Subject: [PATCH 3/5] Add Symfony HttpFoundation Component 2.8.x for PHP 5.4 support --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index cf2f53c..9981069 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "require-dev": { "phpunit/phpunit": "~4.8|~5.2", "codacy/coverage": "dev-master", - "symfony/http-foundation": "^3.0" + "symfony/http-foundation": "^2.8|^3.0" }, "autoload": { "psr-4": { From f243e6b8fad39cef3705281e16512b6e3629f94d Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Thu, 17 Mar 2016 00:51:08 +1100 Subject: [PATCH 4/5] * Rename Integration to Extension * Create tests for symfony integration * Refactor tests * Add documentation for symfony extension --- README.md | 33 ++++++++++++++++++-- src/Assert.php | 8 ++--- src/{Integration => Extension}/Symfony.php | 2 +- src/Extension/SymfonyClass.php | 8 +++++ tests/AssertTraitTest.php | 36 ++++++++-------------- tests/Extension/SymfonyTest.php | 33 ++++++++++++++++++++ tests/Utils.php | 28 +++++++++++++++++ tests/json/simple.json | 1 + 8 files changed, 119 insertions(+), 30 deletions(-) rename src/{Integration => Extension}/Symfony.php (97%) create mode 100644 src/Extension/SymfonyClass.php create mode 100644 tests/Extension/SymfonyTest.php create mode 100644 tests/Utils.php create mode 100644 tests/json/simple.json diff --git a/README.md b/README.md index 6421e14..b6d4cd6 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ class MyTestCase extends \PHPUnit_Framework_TestCase $json = json_decode('{"foo":1}'); - $this->assertJsonMatchesSchemaString('./my-schema.json', $json); + $this->assertJsonMatchesSchema('./my-schema.json', $json); $this->assertJsonValueEquals(1, '* | [0]', $json); } } @@ -112,12 +112,41 @@ class MyTestCase extends \PHPUnit_Framework_TestCase $json = json_decode('{"foo":1}'); - JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $json); + JsonAssert::assertJsonMatchesSchema('./my-schema.json', $json); JsonAssert::assertJsonValueEquals(1, '* | [0]', $json); } } ``` +## Extensions + +`phpunit-json-assertions` provides extensions for simpler handling in different use cases. + +### Symfony HttpFoundation Component + +The extension `EnricoStahn\JsonAssert\Extension\Symfony` allows to pass in the actual response object generated +by the symfony framework and takes care of the decoding part. + +BEFORE: +```php +use EnricoStahn\JsonAssert\Assert as JsonAssert; + +// ... + +$content = $response->getContent(); +$json = json_decode($content); +JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $json); +``` + +AFTER: +```php +use EnricoStahn\JsonAssert\Assert\Extension\Symfony as JsonAssert; + +// ... + +JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $response); +``` + ## Tests To run the test suite, you need [composer](http://getcomposer.org). diff --git a/src/Assert.php b/src/Assert.php index 40bc760..2abd8d6 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -29,7 +29,7 @@ trait Assert * * Example: * - * static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json') + * static::assertJsonMatchesSchema('./schema.json', json_decode('{"foo":1}')) * * @param string $schema Path to the schema file * @param array|object $content JSON array or object @@ -53,7 +53,7 @@ public static function assertJsonMatchesSchema($schema, $content) }, $validator->getErrors()); $messages[] = '- Response: '.json_encode($content); - self::assertTrue($validator->isValid(), implode("\n", $messages)); + \PHPUnit_Framework_Assert::assertTrue($validator->isValid(), implode("\n", $messages)); } /** @@ -86,8 +86,8 @@ public static function assertJsonValueEquals($expected, $expression, $json) { $result = \JmesPath\Env::search($expression, $json); - self::assertEquals($expected, $result); - self::assertInternalType(gettype($expected), $result); + \PHPUnit_Framework_Assert::assertEquals($expected, $result); + \PHPUnit_Framework_Assert::assertInternalType(gettype($expected), $result); } /** diff --git a/src/Integration/Symfony.php b/src/Extension/Symfony.php similarity index 97% rename from src/Integration/Symfony.php rename to src/Extension/Symfony.php index 441cb9c..efb79f9 100644 --- a/src/Integration/Symfony.php +++ b/src/Extension/Symfony.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace EnricoStahn\JsonAssert\Integration; +namespace EnricoStahn\JsonAssert\Extension; use EnricoStahn\JsonAssert\Assert; use Symfony\Component\HttpFoundation\Response; diff --git a/src/Extension/SymfonyClass.php b/src/Extension/SymfonyClass.php new file mode 100644 index 0000000..78295de --- /dev/null +++ b/src/Extension/SymfonyClass.php @@ -0,0 +1,8 @@ +getMessage()); self::assertContains('- Response: {"foo":"123"}', $exception->getMessage()); @@ -59,27 +49,27 @@ public function testAssertJsonSchemaFailMessage() /** * Tests if referenced schemas are loaded automatically. */ - public function testAssertJsonSchemaWithRefs() + public function testAssertJsonMatchesSchemaWithRefs() { $content = json_decode('{"code":123, "message":"Nothing works."}'); - AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('error.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('error.schema.json'), $content); } /** * @expectedException \PHPUnit_Framework_ExpectationFailedException */ - public function testAssertJsonSchemaWithRefsFails() + public function testAssertJsonMatchesSchemaWithRefsFails() { $content = json_decode('{"code":"123", "message":"Nothing works."}'); - AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('error.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('error.schema.json'), $content); } public function testAssertJsonMatchesSchemaString() { $content = json_decode('{"foo":123}'); - $schema = file_get_contents(self::getSchema('test.schema.json')); + $schema = file_get_contents(Utils::getSchemaPath('test.schema.json')); AssertTraitImpl::assertJsonMatchesSchemaString($schema, $content); } @@ -94,7 +84,7 @@ public function testAssertJsonMatchesSchemaString() */ public function testAssertJsonValueEquals($expression, $value) { - $content = json_decode(file_get_contents(self::getJson('testAssertJsonValueEquals.json'))); + $content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json'))); AssertTraitImpl::assertJsonValueEquals($value, $expression, $content); } @@ -112,7 +102,7 @@ public function assertJsonValueEqualsProvider() */ public function testAssertJsonValueEqualsFailsOnWrongDataType() { - $content = json_decode(file_get_contents(self::getJson('testAssertJsonValueEquals.json'))); + $content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json'))); AssertTraitImpl::assertJsonValueEquals($content, 'a.b.c[0].d[1][0]', '1'); } diff --git a/tests/Extension/SymfonyTest.php b/tests/Extension/SymfonyTest.php new file mode 100644 index 0000000..116f154 --- /dev/null +++ b/tests/Extension/SymfonyTest.php @@ -0,0 +1,33 @@ + Date: Wed, 16 Mar 2016 09:51:17 -0400 Subject: [PATCH 5/5] Applied fixes from StyleCI --- src/Extension/SymfonyClass.php | 11 ++++++++++- tests/Extension/SymfonyTest.php | 11 ++++++++++- tests/Utils.php | 17 ++++++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/Extension/SymfonyClass.php b/src/Extension/SymfonyClass.php index 78295de..ef86108 100644 --- a/src/Extension/SymfonyClass.php +++ b/src/Extension/SymfonyClass.php @@ -1,8 +1,17 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace EnricoStahn\JsonAssert\Extension; class SymfonyClass extends \PHPUnit_Framework_TestCase { use Symfony; -} \ No newline at end of file +} diff --git a/tests/Extension/SymfonyTest.php b/tests/Extension/SymfonyTest.php index 116f154..03e2569 100644 --- a/tests/Extension/SymfonyTest.php +++ b/tests/Extension/SymfonyTest.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace EnricoStahn\JsonAssert\Tests\Extension; use EnricoStahn\JsonAssert\Extension\Symfony; @@ -30,4 +39,4 @@ public function testAssertJsonValueEquals() Symfony::assertJsonValueEquals(123, 'foo', $response); } -} \ No newline at end of file +} diff --git a/tests/Utils.php b/tests/Utils.php index 76904bf..0ad108c 100644 --- a/tests/Utils.php +++ b/tests/Utils.php @@ -1,13 +1,23 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace EnricoStahn\JsonAssert\Tests; class Utils { /** - * Returns the full path of the schema file + * Returns the full path of the schema file. * * @param string $filename The filename of the schema file + * * @return string */ public static function getSchemaPath($filename) @@ -16,13 +26,14 @@ public static function getSchemaPath($filename) } /** - * Returns the full path of the schema file + * Returns the full path of the schema file. * * @param string $filename The filename of the json file + * * @return string */ public static function getJsonPath($filename) { return implode(DIRECTORY_SEPARATOR, [__DIR__, 'json', $filename]); } -} \ No newline at end of file +}