-
-
Notifications
You must be signed in to change notification settings - Fork 10
Add symfony integration #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5579bf6
7e90d3d
4889695
7156dd9
f243e6b
739fc04
28411dd
104e97f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the phpunit-json-assertions package. | ||
* | ||
* (c) Enrico Stahn <enrico.stahn@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace EnricoStahn\JsonAssert\Extension; | ||
|
||
use EnricoStahn\JsonAssert\Assert; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
trait Symfony | ||
{ | ||
/** | ||
* Asserts that json content is valid according to the provided schema file. | ||
* | ||
* Example: | ||
* | ||
* static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json') | ||
* | ||
* @param string $schema Path to the schema file | ||
* @param Response $response JSON array or object | ||
*/ | ||
public static function assertJsonMatchesSchema($schema, Response $response) | ||
{ | ||
Assert::assertJsonMatchesSchema($schema, json_decode($response->getContent())); | ||
} | ||
|
||
/** | ||
* Asserts that json content is valid according to the provided schema string. | ||
* | ||
* @param string $schema Schema data | ||
* @param Response $response JSON content | ||
*/ | ||
public static function assertJsonMatchesSchemaString($schema, Response $response) | ||
{ | ||
Assert::assertJsonMatchesSchemaString($schema, json_decode($response->getContent())); | ||
} | ||
|
||
/** | ||
* Asserts if the value retrieved with the expression equals the expected value. | ||
* | ||
* Example: | ||
* | ||
* static::assertJsonValueEquals(33, 'foo.bar[0]', $json); | ||
* | ||
* @param mixed $expected Expected value | ||
* @param string $expression Expression to retrieve the result | ||
* (e.g. locations[?state == 'WA'].name | sort(@)) | ||
* @param Response $response JSON Content | ||
*/ | ||
public static function assertJsonValueEquals($expected, $expression, $response) | ||
{ | ||
Assert::assertJsonValueEquals($expected, $expression, json_decode($response->getContent())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the phpunit-json-assertions package. | ||
* | ||
* (c) Enrico Stahn <enrico.stahn@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace EnricoStahn\JsonAssert\Extension; | ||
|
||
class SymfonyClass extends \PHPUnit_Framework_TestCase | ||
{ | ||
use Symfony; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,41 +13,31 @@ | |
|
||
class AssertTraitTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private static function getSchema($filename) | ||
{ | ||
return implode(DIRECTORY_SEPARATOR, [__DIR__, 'schemas', $filename]); | ||
} | ||
|
||
private static function getJson($filename) | ||
{ | ||
return implode(DIRECTORY_SEPARATOR, [__DIR__, 'json', $filename]); | ||
} | ||
|
||
public function testAssertJsonSchema() | ||
public function testAssertJsonMatchesSchema() | ||
{ | ||
$content = json_decode('{"foo":123}'); | ||
|
||
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('test.schema.json'), $content); | ||
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('test.schema.json'), $content); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
/** | ||
* @expectedException \PHPUnit_Framework_ExpectationFailedException | ||
*/ | ||
public function testAssertJsonSchemaFail() | ||
public function testAssertJsonMatchesSchemaFail() | ||
{ | ||
$content = json_decode('{"foo":"123"}'); | ||
|
||
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('test.schema.json'), $content); | ||
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('test.schema.json'), $content); | ||
} | ||
|
||
public function testAssertJsonSchemaFailMessage() | ||
public function testAssertJsonMatchesSchemaFailMessage() | ||
{ | ||
$content = json_decode('{"foo":"123"}'); | ||
|
||
$exception = null; | ||
|
||
try { | ||
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('test.schema.json'), $content); | ||
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('test.schema.json'), $content); | ||
} catch (\PHPUnit_Framework_ExpectationFailedException $exception) { | ||
self::assertContains('- Property: foo, Contraint: type, Message: String value found, but an integer is required', $exception->getMessage()); | ||
self::assertContains('- Response: {"foo":"123"}', $exception->getMessage()); | ||
|
@@ -59,27 +49,27 @@ public function testAssertJsonSchemaFailMessage() | |
/** | ||
* Tests if referenced schemas are loaded automatically. | ||
*/ | ||
public function testAssertJsonSchemaWithRefs() | ||
public function testAssertJsonMatchesSchemaWithRefs() | ||
{ | ||
$content = json_decode('{"code":123, "message":"Nothing works."}'); | ||
|
||
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('error.schema.json'), $content); | ||
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('error.schema.json'), $content); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
/** | ||
* @expectedException \PHPUnit_Framework_ExpectationFailedException | ||
*/ | ||
public function testAssertJsonSchemaWithRefsFails() | ||
public function testAssertJsonMatchesSchemaWithRefsFails() | ||
{ | ||
$content = json_decode('{"code":"123", "message":"Nothing works."}'); | ||
|
||
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('error.schema.json'), $content); | ||
AssertTraitImpl::assertJsonMatchesSchema(Utils::getSchemaPath('error.schema.json'), $content); | ||
} | ||
|
||
public function testAssertJsonMatchesSchemaString() | ||
{ | ||
$content = json_decode('{"foo":123}'); | ||
$schema = file_get_contents(self::getSchema('test.schema.json')); | ||
$schema = file_get_contents(Utils::getSchemaPath('test.schema.json')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
AssertTraitImpl::assertJsonMatchesSchemaString($schema, $content); | ||
} | ||
|
@@ -94,7 +84,7 @@ public function testAssertJsonMatchesSchemaString() | |
*/ | ||
public function testAssertJsonValueEquals($expression, $value) | ||
{ | ||
$content = json_decode(file_get_contents(self::getJson('testAssertJsonValueEquals.json'))); | ||
$content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json'))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
AssertTraitImpl::assertJsonValueEquals($value, $expression, $content); | ||
} | ||
|
@@ -112,7 +102,7 @@ public function assertJsonValueEqualsProvider() | |
*/ | ||
public function testAssertJsonValueEqualsFailsOnWrongDataType() | ||
{ | ||
$content = json_decode(file_get_contents(self::getJson('testAssertJsonValueEquals.json'))); | ||
$content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json'))); | ||
|
||
AssertTraitImpl::assertJsonValueEquals($content, 'a.b.c[0].d[1][0]', '1'); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the phpunit-json-assertions package. | ||
* | ||
* (c) Enrico Stahn <enrico.stahn@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace EnricoStahn\JsonAssert\Tests\Extension; | ||
|
||
use EnricoStahn\JsonAssert\Extension\Symfony; | ||
use EnricoStahn\JsonAssert\Tests\Utils; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class SymfonyTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testAssertJsonMatchesSchema() | ||
{ | ||
$schema = Utils::getSchemaPath('test.schema.json'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json'))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
Symfony::assertJsonMatchesSchema($schema, $response); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
public function testAssertJsonMatchesSchemaString() | ||
{ | ||
$schema = file_get_contents(Utils::getSchemaPath('test.schema.json')); | ||
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json'))); | ||
|
||
Symfony::assertJsonMatchesSchemaString($schema, $response); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
public function testAssertJsonValueEquals() | ||
{ | ||
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json'))); | ||
|
||
Symfony::assertJsonValueEquals(123, 'foo', $response); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the phpunit-json-assertions package. | ||
* | ||
* (c) Enrico Stahn <enrico.stahn@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace EnricoStahn\JsonAssert\Tests; | ||
|
||
class Utils | ||
{ | ||
/** | ||
* Returns the full path of the schema file. | ||
* | ||
* @param string $filename The filename of the schema file | ||
* | ||
* @return string | ||
*/ | ||
public static function getSchemaPath($filename) | ||
{ | ||
return implode(DIRECTORY_SEPARATOR, [__DIR__, 'schemas', $filename]); | ||
} | ||
|
||
/** | ||
* Returns the full path of the schema file. | ||
* | ||
* @param string $filename The filename of the json file | ||
* | ||
* @return string | ||
*/ | ||
public static function getJsonPath($filename) | ||
{ | ||
return implode(DIRECTORY_SEPARATOR, [__DIR__, 'json', $filename]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "foo" : 123 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.