-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from estahn/add_symfony_integration
Add symfony integration
- Loading branch information
Showing
9 changed files
with
210 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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'); | ||
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json'))); | ||
|
||
Symfony::assertJsonMatchesSchema($schema, $response); | ||
} | ||
|
||
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); | ||
} | ||
|
||
public function testAssertJsonValueEquals() | ||
{ | ||
$response = new Response(file_get_contents(Utils::getJsonPath('simple.json'))); | ||
|
||
Symfony::assertJsonValueEquals(123, 'foo', $response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "foo" : 123 } |