Skip to content

Commit f17b899

Browse files
committed
Merge pull request #7 from estahn/symfony_assertJsonResponse
Add assertJsonResponse to Symfony extension
2 parents f319623 + 47a6a40 commit f17b899

7 files changed

+74
-5
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ after_script:
2727
- php vendor/bin/codacycoverage clover build/logs/clover.xml
2828
- wget https://scrutinizer-ci.com/ocular.phar
2929
- php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml
30+
- bash <(curl -s https://codecov.io/bash) -f build/logs/clover.xml

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ or in your `composer.json`:
3737

3838
## Asserts
3939

40-
| Assert | Description |
41-
| ----------------------------- | ---------------------------------------------------------------------------- |
42-
| assertJsonMatchesSchema | Asserts that json content is valid according to the provided schema file |
43-
| assertJsonMatchesSchemaString | Asserts that json content is valid according to the provided schema string |
44-
| assertJsonValueEquals | Asserts if the value retrieved with the expression equals the expected value |
40+
| Assert | Description | Available in |
41+
| ----------------------------- | ---------------------------------------------------------------------------- | ------------ |
42+
| [assertJsonMatchesSchema](https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema) | Asserts that json content is valid according to the provided schema file | All |
43+
| assertJsonMatchesSchemaString | Asserts that json content is valid according to the provided schema string | All |
44+
| assertJsonValueEquals | Asserts if the value retrieved with the expression equals the expected value | All |
45+
| assertJsonValueEquals | Asserts if the value retrieved with the expression equals the expected value | All |
46+
| assertJsonResponse | Asserts that a response is successful and of type json | Symfony |
4547

4648
## Usage
4749

src/Extension/Symfony.php

+21
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,25 @@ public static function assertJsonValueEquals($expected, $expression, $response)
5858
{
5959
Assert::assertJsonValueEquals($expected, $expression, json_decode($response->getContent()));
6060
}
61+
62+
/**
63+
* Asserts that a response is successful and of type json.
64+
*
65+
* @param Response $response Response object
66+
* @param int $statusCode Expected status code (default 200)
67+
*
68+
* @see \Bazinga\Bundle\RestExtraBundle\Test\WebTestCase::assertJsonResponse()
69+
*/
70+
public static function assertJsonResponse(Response $response, $statusCode = 200)
71+
{
72+
\PHPUnit_Framework_Assert::assertEquals(
73+
$statusCode,
74+
$response->getStatusCode(),
75+
$response->getContent()
76+
);
77+
\PHPUnit_Framework_Assert::assertTrue(
78+
$response->headers->contains('Content-Type', 'application/json'),
79+
$response->headers
80+
);
81+
}
6182
}

tests/AssertTraitTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313

1414
class AssertTraitTest extends \PHPUnit_Framework_TestCase
1515
{
16+
/**
17+
* Showcase for the Wiki.
18+
*
19+
* @see https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema
20+
*/
21+
public function testAssertJsonMatchesSchemaSimple()
22+
{
23+
$content = json_decode(file_get_contents(Utils::getJsonPath('assertJsonMatchesSchema_simple.json')));
24+
25+
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json'), $content);
26+
}
27+
1628
public function testAssertJsonMatchesSchema()
1729
{
1830
$content = json_decode('{"foo":123}');

tests/Extension/SymfonyTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ public function testAssertJsonValueEquals()
3939

4040
Symfony::assertJsonValueEquals(123, 'foo', $response);
4141
}
42+
43+
public function testAssertJsonResponse()
44+
{
45+
$response = new Response('{}', 200, ['Content-Type' => 'application/json']);
46+
47+
Symfony::assertJsonResponse($response);
48+
}
4249
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id" : 33,
3+
"title" : "This is blog title #33",
4+
"created_at" : "2009-03-24 16:24:32",
5+
"tags" : [
6+
"foo",
7+
"bar"
8+
]
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema" : "http://json-schema.org/draft-04/schema#",
3+
"type" : "object",
4+
"required" : [ "id", "title" ],
5+
"additionalProperties" : false,
6+
"properties" : {
7+
"id" : { "type" : "integer" },
8+
"title" : { "type" : "string" },
9+
"created_at" : { "type" : "string", "pattern" : "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}" },
10+
"tags" : {
11+
"type" : "array",
12+
"minItems" : 1,
13+
"items" : { "type" : "string" },
14+
"uniqueItems" : true
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)