diff --git a/.travis.yml b/.travis.yml index 4b169d7..ac4dc62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,3 +27,4 @@ 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 diff --git a/README.md b/README.md index b6d4cd6..43cfbed 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](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 | +| assertJsonResponse | Asserts that a response is successful and of type json | Symfony | ## Usage diff --git a/src/Extension/Symfony.php b/src/Extension/Symfony.php index efb79f9..af8e73f 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 + ); + } } diff --git a/tests/AssertTraitTest.php b/tests/AssertTraitTest.php index 37b8d96..ef2f1f3 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}'); diff --git a/tests/Extension/SymfonyTest.php b/tests/Extension/SymfonyTest.php index 03e2569..ad9c95d 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); + } } 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