Skip to content

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

Merged
merged 8 commits into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class MyTestCase extends \PHPUnit_Framework_TestCase

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

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

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

JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $json);
JsonAssert::assertJsonMatchesSchema('./my-schema.json', $json);
JsonAssert::assertJsonValueEquals(1, '* | [0]', $json);
}
}
```

## Extensions

`phpunit-json-assertions` provides extensions for simpler handling in different use cases.

### Symfony HttpFoundation Component

The extension `EnricoStahn\JsonAssert\Extension\Symfony` allows to pass in the actual response object generated
by the symfony framework and takes care of the decoding part.

BEFORE:
```php
use EnricoStahn\JsonAssert\Assert as JsonAssert;

// ...

$content = $response->getContent();
$json = json_decode($content);
JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $json);
```

AFTER:
```php
use EnricoStahn\JsonAssert\Assert\Extension\Symfony as JsonAssert;

// ...

JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $response);
```

## Tests

To run the test suite, you need [composer](http://getcomposer.org).
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"require-dev": {
"phpunit/phpunit": "~4.8|~5.2",
"codacy/coverage": "dev-master"
"codacy/coverage": "dev-master",
"symfony/http-foundation": "^2.8|^3.0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 4 additions & 4 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trait Assert
*
* Example:
*
* static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json')
* static::assertJsonMatchesSchema('./schema.json', json_decode('{"foo":1}'))
*
* @param string $schema Path to the schema file
* @param array|object $content JSON array or object
Expand All @@ -53,7 +53,7 @@ public static function assertJsonMatchesSchema($schema, $content)
}, $validator->getErrors());
$messages[] = '- Response: '.json_encode($content);

self::assertTrue($validator->isValid(), implode("\n", $messages));
\PHPUnit_Framework_Assert::assertTrue($validator->isValid(), implode("\n", $messages));
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

/**
Expand Down Expand Up @@ -86,8 +86,8 @@ public static function assertJsonValueEquals($expected, $expression, $json)
{
$result = \JmesPath\Env::search($expression, $json);

self::assertEquals($expected, $result);
self::assertInternalType(gettype($expected), $result);
\PHPUnit_Framework_Assert::assertEquals($expected, $result);
\PHPUnit_Framework_Assert::assertInternalType(gettype($expected), $result);
}

/**
Expand Down
61 changes: 61 additions & 0 deletions src/Extension/Symfony.php
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()));
}
}
17 changes: 17 additions & 0 deletions src/Extension/SymfonyClass.php
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;
}
36 changes: 13 additions & 23 deletions tests/AssertTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Owner Author

Choose a reason for hiding this comment

The 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());
Expand All @@ -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);
Copy link
Owner Author

Choose a reason for hiding this comment

The 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'));
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


AssertTraitImpl::assertJsonMatchesSchemaString($schema, $content);
}
Expand All @@ -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')));
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


AssertTraitImpl::assertJsonValueEquals($value, $expression, $content);
}
Expand All @@ -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');
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Extension/SymfonyTest.php
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');
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$response = new Response(file_get_contents(Utils::getJsonPath('simple.json')));
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Symfony::assertJsonMatchesSchema($schema, $response);
Copy link
Owner Author

Choose a reason for hiding this comment

The 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);
Copy link
Owner Author

Choose a reason for hiding this comment

The 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);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
39 changes: 39 additions & 0 deletions tests/Utils.php
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]);
}
}
1 change: 1 addition & 0 deletions tests/json/simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "foo" : 123 }