Skip to content

Commit f243e6b

Browse files
committed
* Rename Integration to Extension
* Create tests for symfony integration * Refactor tests * Add documentation for symfony extension
1 parent 7156dd9 commit f243e6b

File tree

8 files changed

+119
-30
lines changed

8 files changed

+119
-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).

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/Integration/Symfony.php renamed to src/Extension/Symfony.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace EnricoStahn\JsonAssert\Integration;
12+
namespace EnricoStahn\JsonAssert\Extension;
1313

1414
use EnricoStahn\JsonAssert\Assert;
1515
use Symfony\Component\HttpFoundation\Response;

src/Extension/SymfonyClass.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace EnricoStahn\JsonAssert\Extension;
4+
5+
class SymfonyClass extends \PHPUnit_Framework_TestCase
6+
{
7+
use Symfony;
8+
}

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace EnricoStahn\JsonAssert\Tests\Extension;
4+
5+
use EnricoStahn\JsonAssert\Extension\Symfony;
6+
use EnricoStahn\JsonAssert\Tests\Utils;
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
class SymfonyTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testAssertJsonMatchesSchema()
12+
{
13+
$schema = Utils::getSchemaPath('test.schema.json');
14+
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json')));
15+
16+
Symfony::assertJsonMatchesSchema($schema, $response);
17+
}
18+
19+
public function testAssertJsonMatchesSchemaString()
20+
{
21+
$schema = file_get_contents(Utils::getSchemaPath('test.schema.json'));
22+
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json')));
23+
24+
Symfony::assertJsonMatchesSchemaString($schema, $response);
25+
}
26+
27+
public function testAssertJsonValueEquals()
28+
{
29+
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json')));
30+
31+
Symfony::assertJsonValueEquals(123, 'foo', $response);
32+
}
33+
}

tests/Utils.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace EnricoStahn\JsonAssert\Tests;
4+
5+
class Utils
6+
{
7+
/**
8+
* Returns the full path of the schema file
9+
*
10+
* @param string $filename The filename of the schema file
11+
* @return string
12+
*/
13+
public static function getSchemaPath($filename)
14+
{
15+
return implode(DIRECTORY_SEPARATOR, [__DIR__, 'schemas', $filename]);
16+
}
17+
18+
/**
19+
* Returns the full path of the schema file
20+
*
21+
* @param string $filename The filename of the json file
22+
* @return string
23+
*/
24+
public static function getJsonPath($filename)
25+
{
26+
return implode(DIRECTORY_SEPARATOR, [__DIR__, 'json', $filename]);
27+
}
28+
}

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)