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/composer.json b/composer.json index 59a4bd7..9981069 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": "^2.8|^3.0" }, "autoload": { "psr-4": { 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/Extension/Symfony.php b/src/Extension/Symfony.php new file mode 100644 index 0000000..efb79f9 --- /dev/null +++ b/src/Extension/Symfony.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace EnricoStahn\JsonAssert\Extension; + +use EnricoStahn\JsonAssert\Assert; +use Symfony\Component\HttpFoundation\Response; + +trait Symfony +{ + /** + * Asserts that json content is valid according to the provided schema file. + * + * Example: + * + * static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json') + * + * @param string $schema Path to the schema file + * @param Response $response JSON array or object + */ + public static function assertJsonMatchesSchema($schema, Response $response) + { + Assert::assertJsonMatchesSchema($schema, json_decode($response->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())); + } +} diff --git a/src/Extension/SymfonyClass.php b/src/Extension/SymfonyClass.php new file mode 100644 index 0000000..ef86108 --- /dev/null +++ b/src/Extension/SymfonyClass.php @@ -0,0 +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; +} diff --git a/tests/AssertTraitTest.php b/tests/AssertTraitTest.php index 37830b8..37b8d96 100644 --- a/tests/AssertTraitTest.php +++ b/tests/AssertTraitTest.php @@ -13,41 +13,31 @@ class AssertTraitTest extends \PHPUnit_Framework_TestCase { - private static function getSchema($filename) - { - return implode(DIRECTORY_SEPARATOR, [__DIR__, 'schemas', $filename]); - } - - private static function getJson($filename) - { - return implode(DIRECTORY_SEPARATOR, [__DIR__, 'json', $filename]); - } - - public function testAssertJsonSchema() + public function testAssertJsonMatchesSchema() { $content = json_decode('{"foo":123}'); - AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('test.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('test.schema.json'), $content); } /** * @expectedException \PHPUnit_Framework_ExpectationFailedException */ - public function testAssertJsonSchemaFail() + public function testAssertJsonMatchesSchemaFail() { $content = json_decode('{"foo":"123"}'); - AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('test.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('test.schema.json'), $content); } - public function testAssertJsonSchemaFailMessage() + public function testAssertJsonMatchesSchemaFailMessage() { $content = json_decode('{"foo":"123"}'); $exception = null; try { - AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('test.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('test.schema.json'), $content); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { self::assertContains('- Property: foo, Contraint: type, Message: String value found, but an integer is required', $exception->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..03e2569 --- /dev/null +++ b/tests/Extension/SymfonyTest.php @@ -0,0 +1,42 @@ + + * + * 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; +use EnricoStahn\JsonAssert\Tests\Utils; +use Symfony\Component\HttpFoundation\Response; + +class SymfonyTest extends \PHPUnit_Framework_TestCase +{ + public function testAssertJsonMatchesSchema() + { + $schema = Utils::getSchemaPath('test.schema.json'); + $response = new Response(file_get_contents(Utils::getJsonPath('simple.json'))); + + Symfony::assertJsonMatchesSchema($schema, $response); + } + + public function testAssertJsonMatchesSchemaString() + { + $schema = file_get_contents(Utils::getSchemaPath('test.schema.json')); + $response = new Response(file_get_contents(Utils::getJsonPath('simple.json'))); + + Symfony::assertJsonMatchesSchemaString($schema, $response); + } + + public function testAssertJsonValueEquals() + { + $response = new Response(file_get_contents(Utils::getJsonPath('simple.json'))); + + Symfony::assertJsonValueEquals(123, 'foo', $response); + } +} diff --git a/tests/Utils.php b/tests/Utils.php new file mode 100644 index 0000000..0ad108c --- /dev/null +++ b/tests/Utils.php @@ -0,0 +1,39 @@ + + * + * 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. + * + * @param string $filename The filename of the schema file + * + * @return string + */ + public static function getSchemaPath($filename) + { + return implode(DIRECTORY_SEPARATOR, [__DIR__, 'schemas', $filename]); + } + + /** + * 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]); + } +} diff --git a/tests/json/simple.json b/tests/json/simple.json new file mode 100644 index 0000000..933fa69 --- /dev/null +++ b/tests/json/simple.json @@ -0,0 +1 @@ +{ "foo" : 123 } \ No newline at end of file