Skip to content

Commit f319623

Browse files
committed
Merge pull request #6 from estahn/add_symfony_integration
Add symfony integration
2 parents f5fe643 + 104e97f commit f319623

File tree

9 files changed

+210
-30
lines changed

9 files changed

+210
-30
lines changed

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class MyTestCase extends \PHPUnit_Framework_TestCase
7676

7777
$json = json_decode('{"foo":1}');
7878

79-
$this->assertJsonMatchesSchemaString('./my-schema.json', $json);
79+
$this->assertJsonMatchesSchema('./my-schema.json', $json);
8080
$this->assertJsonValueEquals(1, '* | [0]', $json);
8181
}
8282
}
@@ -112,12 +112,41 @@ class MyTestCase extends \PHPUnit_Framework_TestCase
112112

113113
$json = json_decode('{"foo":1}');
114114

115-
JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $json);
115+
JsonAssert::assertJsonMatchesSchema('./my-schema.json', $json);
116116
JsonAssert::assertJsonValueEquals(1, '* | [0]', $json);
117117
}
118118
}
119119
```
120120

121+
## Extensions
122+
123+
`phpunit-json-assertions` provides extensions for simpler handling in different use cases.
124+
125+
### Symfony HttpFoundation Component
126+
127+
The extension `EnricoStahn\JsonAssert\Extension\Symfony` allows to pass in the actual response object generated
128+
by the symfony framework and takes care of the decoding part.
129+
130+
BEFORE:
131+
```php
132+
use EnricoStahn\JsonAssert\Assert as JsonAssert;
133+
134+
// ...
135+
136+
$content = $response->getContent();
137+
$json = json_decode($content);
138+
JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $json);
139+
```
140+
141+
AFTER:
142+
```php
143+
use EnricoStahn\JsonAssert\Assert\Extension\Symfony as JsonAssert;
144+
145+
// ...
146+
147+
JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $response);
148+
```
149+
121150
## Tests
122151

123152
To run the test suite, you need [composer](http://getcomposer.org).

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
},
2020
"require-dev": {
2121
"phpunit/phpunit": "~4.8|~5.2",
22-
"codacy/coverage": "dev-master"
22+
"codacy/coverage": "dev-master",
23+
"symfony/http-foundation": "^2.8|^3.0"
2324
},
2425
"autoload": {
2526
"psr-4": {

src/Assert.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ trait Assert
2929
*
3030
* Example:
3131
*
32-
* static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json')
32+
* static::assertJsonMatchesSchema('./schema.json', json_decode('{"foo":1}'))
3333
*
3434
* @param string $schema Path to the schema file
3535
* @param array|object $content JSON array or object
@@ -53,7 +53,7 @@ public static function assertJsonMatchesSchema($schema, $content)
5353
}, $validator->getErrors());
5454
$messages[] = '- Response: '.json_encode($content);
5555

56-
self::assertTrue($validator->isValid(), implode("\n", $messages));
56+
\PHPUnit_Framework_Assert::assertTrue($validator->isValid(), implode("\n", $messages));
5757
}
5858

5959
/**
@@ -86,8 +86,8 @@ public static function assertJsonValueEquals($expected, $expression, $json)
8686
{
8787
$result = \JmesPath\Env::search($expression, $json);
8888

89-
self::assertEquals($expected, $result);
90-
self::assertInternalType(gettype($expected), $result);
89+
\PHPUnit_Framework_Assert::assertEquals($expected, $result);
90+
\PHPUnit_Framework_Assert::assertInternalType(gettype($expected), $result);
9191
}
9292

9393
/**

src/Extension/Symfony.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpunit-json-assertions package.
5+
*
6+
* (c) Enrico Stahn <enrico.stahn@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace EnricoStahn\JsonAssert\Extension;
13+
14+
use EnricoStahn\JsonAssert\Assert;
15+
use Symfony\Component\HttpFoundation\Response;
16+
17+
trait Symfony
18+
{
19+
/**
20+
* Asserts that json content is valid according to the provided schema file.
21+
*
22+
* Example:
23+
*
24+
* static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json')
25+
*
26+
* @param string $schema Path to the schema file
27+
* @param Response $response JSON array or object
28+
*/
29+
public static function assertJsonMatchesSchema($schema, Response $response)
30+
{
31+
Assert::assertJsonMatchesSchema($schema, json_decode($response->getContent()));
32+
}
33+
34+
/**
35+
* Asserts that json content is valid according to the provided schema string.
36+
*
37+
* @param string $schema Schema data
38+
* @param Response $response JSON content
39+
*/
40+
public static function assertJsonMatchesSchemaString($schema, Response $response)
41+
{
42+
Assert::assertJsonMatchesSchemaString($schema, json_decode($response->getContent()));
43+
}
44+
45+
/**
46+
* Asserts if the value retrieved with the expression equals the expected value.
47+
*
48+
* Example:
49+
*
50+
* static::assertJsonValueEquals(33, 'foo.bar[0]', $json);
51+
*
52+
* @param mixed $expected Expected value
53+
* @param string $expression Expression to retrieve the result
54+
* (e.g. locations[?state == 'WA'].name | sort(@))
55+
* @param Response $response JSON Content
56+
*/
57+
public static function assertJsonValueEquals($expected, $expression, $response)
58+
{
59+
Assert::assertJsonValueEquals($expected, $expression, json_decode($response->getContent()));
60+
}
61+
}

src/Extension/SymfonyClass.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpunit-json-assertions package.
5+
*
6+
* (c) Enrico Stahn <enrico.stahn@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace EnricoStahn\JsonAssert\Extension;
13+
14+
class SymfonyClass extends \PHPUnit_Framework_TestCase
15+
{
16+
use Symfony;
17+
}

tests/AssertTraitTest.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,31 @@
1313

1414
class AssertTraitTest extends \PHPUnit_Framework_TestCase
1515
{
16-
private static function getSchema($filename)
17-
{
18-
return implode(DIRECTORY_SEPARATOR, [__DIR__, 'schemas', $filename]);
19-
}
20-
21-
private static function getJson($filename)
22-
{
23-
return implode(DIRECTORY_SEPARATOR, [__DIR__, 'json', $filename]);
24-
}
25-
26-
public function testAssertJsonSchema()
16+
public function testAssertJsonMatchesSchema()
2717
{
2818
$content = json_decode('{"foo":123}');
2919

30-
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('test.schema.json'), $content);
20+
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('test.schema.json'), $content);
3121
}
3222

3323
/**
3424
* @expectedException \PHPUnit_Framework_ExpectationFailedException
3525
*/
36-
public function testAssertJsonSchemaFail()
26+
public function testAssertJsonMatchesSchemaFail()
3727
{
3828
$content = json_decode('{"foo":"123"}');
3929

40-
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('test.schema.json'), $content);
30+
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('test.schema.json'), $content);
4131
}
4232

43-
public function testAssertJsonSchemaFailMessage()
33+
public function testAssertJsonMatchesSchemaFailMessage()
4434
{
4535
$content = json_decode('{"foo":"123"}');
4636

4737
$exception = null;
4838

4939
try {
50-
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('test.schema.json'), $content);
40+
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('test.schema.json'), $content);
5141
} catch (\PHPUnit_Framework_ExpectationFailedException $exception) {
5242
self::assertContains('- Property: foo, Contraint: type, Message: String value found, but an integer is required', $exception->getMessage());
5343
self::assertContains('- Response: {"foo":"123"}', $exception->getMessage());
@@ -59,27 +49,27 @@ public function testAssertJsonSchemaFailMessage()
5949
/**
6050
* Tests if referenced schemas are loaded automatically.
6151
*/
62-
public function testAssertJsonSchemaWithRefs()
52+
public function testAssertJsonMatchesSchemaWithRefs()
6353
{
6454
$content = json_decode('{"code":123, "message":"Nothing works."}');
6555

66-
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('error.schema.json'), $content);
56+
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('error.schema.json'), $content);
6757
}
6858

6959
/**
7060
* @expectedException \PHPUnit_Framework_ExpectationFailedException
7161
*/
72-
public function testAssertJsonSchemaWithRefsFails()
62+
public function testAssertJsonMatchesSchemaWithRefsFails()
7363
{
7464
$content = json_decode('{"code":"123", "message":"Nothing works."}');
7565

76-
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('error.schema.json'), $content);
66+
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('error.schema.json'), $content);
7767
}
7868

7969
public function testAssertJsonMatchesSchemaString()
8070
{
8171
$content = json_decode('{"foo":123}');
82-
$schema = file_get_contents(self::getSchema('test.schema.json'));
72+
$schema = file_get_contents(Utils::getSchemaPath('test.schema.json'));
8373

8474
AssertTraitImpl::assertJsonMatchesSchemaString($schema, $content);
8575
}
@@ -94,7 +84,7 @@ public function testAssertJsonMatchesSchemaString()
9484
*/
9585
public function testAssertJsonValueEquals($expression, $value)
9686
{
97-
$content = json_decode(file_get_contents(self::getJson('testAssertJsonValueEquals.json')));
87+
$content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json')));
9888

9989
AssertTraitImpl::assertJsonValueEquals($value, $expression, $content);
10090
}
@@ -112,7 +102,7 @@ public function assertJsonValueEqualsProvider()
112102
*/
113103
public function testAssertJsonValueEqualsFailsOnWrongDataType()
114104
{
115-
$content = json_decode(file_get_contents(self::getJson('testAssertJsonValueEquals.json')));
105+
$content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json')));
116106

117107
AssertTraitImpl::assertJsonValueEquals($content, 'a.b.c[0].d[1][0]', '1');
118108
}

tests/Extension/SymfonyTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpunit-json-assertions package.
5+
*
6+
* (c) Enrico Stahn <enrico.stahn@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace EnricoStahn\JsonAssert\Tests\Extension;
13+
14+
use EnricoStahn\JsonAssert\Extension\Symfony;
15+
use EnricoStahn\JsonAssert\Tests\Utils;
16+
use Symfony\Component\HttpFoundation\Response;
17+
18+
class SymfonyTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function testAssertJsonMatchesSchema()
21+
{
22+
$schema = Utils::getSchemaPath('test.schema.json');
23+
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json')));
24+
25+
Symfony::assertJsonMatchesSchema($schema, $response);
26+
}
27+
28+
public function testAssertJsonMatchesSchemaString()
29+
{
30+
$schema = file_get_contents(Utils::getSchemaPath('test.schema.json'));
31+
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json')));
32+
33+
Symfony::assertJsonMatchesSchemaString($schema, $response);
34+
}
35+
36+
public function testAssertJsonValueEquals()
37+
{
38+
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json')));
39+
40+
Symfony::assertJsonValueEquals(123, 'foo', $response);
41+
}
42+
}

tests/Utils.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpunit-json-assertions package.
5+
*
6+
* (c) Enrico Stahn <enrico.stahn@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace EnricoStahn\JsonAssert\Tests;
13+
14+
class Utils
15+
{
16+
/**
17+
* Returns the full path of the schema file.
18+
*
19+
* @param string $filename The filename of the schema file
20+
*
21+
* @return string
22+
*/
23+
public static function getSchemaPath($filename)
24+
{
25+
return implode(DIRECTORY_SEPARATOR, [__DIR__, 'schemas', $filename]);
26+
}
27+
28+
/**
29+
* Returns the full path of the schema file.
30+
*
31+
* @param string $filename The filename of the json file
32+
*
33+
* @return string
34+
*/
35+
public static function getJsonPath($filename)
36+
{
37+
return implode(DIRECTORY_SEPARATOR, [__DIR__, 'json', $filename]);
38+
}
39+
}

tests/json/simple.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "foo" : 123 }

0 commit comments

Comments
 (0)