Skip to content

Commit

Permalink
Merge pull request #7 from estahn/symfony_assertJsonResponse
Browse files Browse the repository at this point in the history
Add assertJsonResponse to Symfony extension
  • Loading branch information
estahn committed Mar 21, 2016
2 parents f319623 + 47a6a40 commit f17b899
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 5 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions src/Extension/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
12 changes: 12 additions & 0 deletions tests/AssertTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
Expand Down
7 changes: 7 additions & 0 deletions tests/Extension/SymfonyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 9 additions & 0 deletions tests/json/assertJsonMatchesSchema_simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id" : 33,
"title" : "This is blog title #33",
"created_at" : "2009-03-24 16:24:32",
"tags" : [
"foo",
"bar"
]
}
17 changes: 17 additions & 0 deletions tests/schemas/assertJsonMatchesSchema_simple.schema.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit f17b899

Please sign in to comment.