From 057e1b6f9ea385d1aec5bbd5469472e77201edc2 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Mon, 21 Mar 2016 17:54:30 +1100 Subject: [PATCH 1/9] Add coveralls --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 4b169d7..f658f18 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,3 +27,6 @@ after_script: - php vendor/bin/codacycoverage clover build/logs/clover.xml - wget https://scrutinizer-ci.com/ocular.phar - php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml + - bash <(curl -s https://codecov.io/bash) -f build/logs/clover.xml + - wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.0/coveralls.phar + - php coveralls.phar --coverage_clover=build/logs/clover.xml \ No newline at end of file From ca28c2e56fd4aa04416ee02e7cc2cb8eb73f5dc4 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Mon, 21 Mar 2016 18:19:19 +1100 Subject: [PATCH 2/9] Add test examples for wiki --- tests/json/assertJsonMatchesSchema_simple.json | 9 +++++++++ .../assertJsonMatchesSchema_simple.schema.json | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tests/json/assertJsonMatchesSchema_simple.json create mode 100644 tests/schemas/assertJsonMatchesSchema_simple.schema.json diff --git a/tests/json/assertJsonMatchesSchema_simple.json b/tests/json/assertJsonMatchesSchema_simple.json new file mode 100644 index 0000000..12e2cff --- /dev/null +++ b/tests/json/assertJsonMatchesSchema_simple.json @@ -0,0 +1,9 @@ +{ + "id" : 33, + "title" : "This is blog title #33", + "created_at" : "2009-03-24 16:24:32", + "tags" : [ + "foo", + "bar" + ] +} \ No newline at end of file diff --git a/tests/schemas/assertJsonMatchesSchema_simple.schema.json b/tests/schemas/assertJsonMatchesSchema_simple.schema.json new file mode 100644 index 0000000..acef993 --- /dev/null +++ b/tests/schemas/assertJsonMatchesSchema_simple.schema.json @@ -0,0 +1,17 @@ +{ + "$schema" : "http://json-schema.org/draft-04/schema#", + "type" : "object", + "required" : [ "id", "title" ], + "additionalProperties" : false, + "properties" : { + "id" : { "type" : "integer" }, + "title" : { "type" : "string" }, + "created_at" : { "type" : "string", "pattern" : "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}" }, + "tags" : { + "type" : "array", + "minItems" : 1, + "items" : { "type" : "string" }, + "uniqueItems" : true + } + } +} \ No newline at end of file From 7db355b5895d344c6f6e147171e8944581b65e3e Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Mon, 21 Mar 2016 18:19:32 +1100 Subject: [PATCH 3/9] Add test examples for wiki --- tests/AssertTraitTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/AssertTraitTest.php b/tests/AssertTraitTest.php index 37b8d96..c3c626d 100644 --- a/tests/AssertTraitTest.php +++ b/tests/AssertTraitTest.php @@ -13,6 +13,18 @@ class AssertTraitTest extends \PHPUnit_Framework_TestCase { + /** + * Showcase for the Wiki + * + * @see https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema + */ + public function testAssertJsonMatchesSchemaSimple() + { + $content = json_decode(file_get_contents(Utils::getJsonPath('assertJsonMatchesSchema_simple.json'))); + + AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json'), $content); + } + public function testAssertJsonMatchesSchema() { $content = json_decode('{"foo":123}'); From 5a2d68a47b377dda9a6c35192a181d669eadb1bf Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Mon, 21 Mar 2016 18:22:43 +1100 Subject: [PATCH 4/9] Add Symfony::assertJsonResponse --- src/Extension/Symfony.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Extension/Symfony.php b/src/Extension/Symfony.php index efb79f9..1d73b7a 100644 --- a/src/Extension/Symfony.php +++ b/src/Extension/Symfony.php @@ -58,4 +58,25 @@ public static function assertJsonValueEquals($expected, $expression, $response) { Assert::assertJsonValueEquals($expected, $expression, json_decode($response->getContent())); } + + /** + * Asserts that a response is successful and of type json + * + * @param Response $response Response object + * @param int $statusCode Expected status code (default 200) + * + * @see \Bazinga\Bundle\RestExtraBundle\Test\WebTestCase::assertJsonResponse() + */ + public static function assertJsonResponse(Response $response, $statusCode = 200) + { + \PHPUnit_Framework_Assert::assertEquals( + $statusCode, + $response->getStatusCode(), + $response->getContent() + ); + \PHPUnit_Framework_Assert::assertTrue( + $response->headers->contains('Content-Type', 'application/json'), + $response->headers + ); + } } From 6b503699f1dd3244655bba45804400e45dec6d2f Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Mon, 21 Mar 2016 03:22:58 -0400 Subject: [PATCH 5/9] Applied fixes from StyleCI --- src/Extension/Symfony.php | 2 +- tests/AssertTraitTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Extension/Symfony.php b/src/Extension/Symfony.php index 1d73b7a..af8e73f 100644 --- a/src/Extension/Symfony.php +++ b/src/Extension/Symfony.php @@ -60,7 +60,7 @@ public static function assertJsonValueEquals($expected, $expression, $response) } /** - * Asserts that a response is successful and of type json + * Asserts that a response is successful and of type json. * * @param Response $response Response object * @param int $statusCode Expected status code (default 200) diff --git a/tests/AssertTraitTest.php b/tests/AssertTraitTest.php index c3c626d..ef2f1f3 100644 --- a/tests/AssertTraitTest.php +++ b/tests/AssertTraitTest.php @@ -14,7 +14,7 @@ class AssertTraitTest extends \PHPUnit_Framework_TestCase { /** - * Showcase for the Wiki + * Showcase for the Wiki. * * @see https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema */ From 6a217ebc03b2e15c947fe67a26d1031c7f66b277 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Mon, 21 Mar 2016 18:42:44 +1100 Subject: [PATCH 6/9] Add tests and more details in README --- README.md | 12 +++++++----- tests/Extension/SymfonyTest.php | 7 +++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b6d4cd6..68124be 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,13 @@ or in your `composer.json`: ## Asserts -| Assert | Description | -| ----------------------------- | ---------------------------------------------------------------------------- | -| assertJsonMatchesSchema | Asserts that json content is valid according to the provided schema file | -| assertJsonMatchesSchemaString | Asserts that json content is valid according to the provided schema string | -| assertJsonValueEquals | Asserts if the value retrieved with the expression equals the expected value | +| Assert | Description | Available in | +| ----------------------------- | ---------------------------------------------------------------------------- | ------------ | +| assertJsonMatchesSchema | Asserts that json content is valid according to the provided schema file | All | +| assertJsonMatchesSchemaString | Asserts that json content is valid according to the provided schema string | All | +| assertJsonValueEquals | Asserts if the value retrieved with the expression equals the expected value | All | +| assertJsonValueEquals | Asserts if the value retrieved with the expression equals the expected value | All | +| assertJsonResponse | Asserts that a response is successful and of type json | Symfony | ## Usage diff --git a/tests/Extension/SymfonyTest.php b/tests/Extension/SymfonyTest.php index 03e2569..24b3abf 100644 --- a/tests/Extension/SymfonyTest.php +++ b/tests/Extension/SymfonyTest.php @@ -39,4 +39,11 @@ public function testAssertJsonValueEquals() Symfony::assertJsonValueEquals(123, 'foo', $response); } + + public function testAssertJsonResponse() + { + $response = new Response('{}', 200, ['Content-Type' => 'application/json']); + + Symfony::assertJsonResponse($response); + } } From face51a10a98f4bffad63c41fedb54c6cba7ca57 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Mon, 21 Mar 2016 03:42:51 -0400 Subject: [PATCH 7/9] Applied fixes from StyleCI --- tests/Extension/SymfonyTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Extension/SymfonyTest.php b/tests/Extension/SymfonyTest.php index 24b3abf..ad9c95d 100644 --- a/tests/Extension/SymfonyTest.php +++ b/tests/Extension/SymfonyTest.php @@ -43,7 +43,7 @@ public function testAssertJsonValueEquals() public function testAssertJsonResponse() { $response = new Response('{}', 200, ['Content-Type' => 'application/json']); - + Symfony::assertJsonResponse($response); } } From 596895296fde669703df9f50e69262c651e9f9ec Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Mon, 21 Mar 2016 18:53:15 +1100 Subject: [PATCH 8/9] Remove coveralls --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index f658f18..ac4dc62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,5 +28,3 @@ after_script: - wget https://scrutinizer-ci.com/ocular.phar - php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml - bash <(curl -s https://codecov.io/bash) -f build/logs/clover.xml - - wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.0/coveralls.phar - - php coveralls.phar --coverage_clover=build/logs/clover.xml \ No newline at end of file From 47a6a40650e022df86d92daa30ba8ca6c7a0e0f2 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Mon, 21 Mar 2016 19:20:17 +1100 Subject: [PATCH 9/9] Add link to wiki page for assertJsonMatchesSchema --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 68124be..43cfbed 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ or in your `composer.json`: | Assert | Description | Available in | | ----------------------------- | ---------------------------------------------------------------------------- | ------------ | -| assertJsonMatchesSchema | Asserts that json content is valid according to the provided schema file | All | +| [assertJsonMatchesSchema](https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema) | Asserts that json content is valid according to the provided schema file | All | | assertJsonMatchesSchemaString | Asserts that json content is valid according to the provided schema string | All | | assertJsonValueEquals | Asserts if the value retrieved with the expression equals the expected value | All | | assertJsonValueEquals | Asserts if the value retrieved with the expression equals the expected value | All |