-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(json): adds checks for json formats
- Loading branch information
Showing
10 changed files
with
245 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
|
||
namespace Pitchart\Phlunit\Checks; | ||
|
||
|
||
use PHPUnit\Framework\Assert; | ||
use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck; | ||
use Pitchart\Phlunit\Checks\Mixin\TypeCheck; | ||
use Pitchart\Phlunit\Checks\Mixin\WithMessage; | ||
use Pitchart\Phlunit\Constraint\Json\MatchesSchema; | ||
|
||
class JsonCheck implements FluentCheck | ||
{ | ||
use WithMessage, ConstraintCheck, TypeCheck; | ||
|
||
private $value; | ||
|
||
/** | ||
* JsonCheck constructor. | ||
* | ||
* @param $value | ||
*/ | ||
public function __construct(string $value) | ||
{ | ||
Assert::assertJson($value); | ||
$this->value = $value; | ||
} | ||
|
||
public function isEqualTo(string $expected): self | ||
{ | ||
Assert::assertJsonStringEqualsJsonString($expected, $this->value, $this->message); | ||
return $this; | ||
} | ||
|
||
public function isNotEqualTo(string $expected): self | ||
{ | ||
Assert::assertJsonStringNotEqualsJsonString($expected, $this->value, $this->message); | ||
return $this; | ||
} | ||
|
||
public function isEqualToFileContent(string $path): self | ||
{ | ||
Assert::assertJsonStringEqualsJsonFile($path, $this->value, $this->message); | ||
return $this; | ||
} | ||
|
||
public function isNotEqualToFileContent(string $path): self | ||
{ | ||
Assert::assertJsonStringNotEqualsJsonFile($path, $this->value, $this->message); | ||
return $this; | ||
} | ||
|
||
public function matchesSchema($schema) | ||
{ | ||
Assert::assertThat($this->value, new MatchesSchema($schema), $this->message); | ||
return $this; | ||
} | ||
|
||
} |
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,62 @@ | ||
<?php | ||
|
||
|
||
namespace Pitchart\Phlunit\Constraint\Json; | ||
|
||
|
||
use JsonSchema\Validator; | ||
use PHPUnit\Framework\Constraint\Constraint; | ||
|
||
class MatchesSchema extends Constraint | ||
{ | ||
private $schema; | ||
|
||
public function __construct($schema) | ||
{ | ||
$this->schema = $this->convertToObject($schema); | ||
} | ||
|
||
|
||
public function toString(): string | ||
{ | ||
return sprintf('matches Json Schema %s', json_encode($this->schema)); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function matches($other): bool | ||
{ | ||
$other = $this->convertToObject($other); | ||
|
||
$validator = new Validator(); | ||
$validator->check($other, $this->schema); | ||
|
||
return $validator->isValid(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function additionalFailureDescription($other): string | ||
{ | ||
$other = $this->convertToObject($other); | ||
|
||
$validator = new Validator(); | ||
$validator->check($other, $this->schema); | ||
|
||
return implode("\n", array_map(function ($error) { | ||
return sprintf("[%s] %s", $error['property'], $error['message']); | ||
}, $validator->getErrors())); | ||
} | ||
|
||
private function convertToObject($value): \stdClass | ||
{ | ||
if (is_string($value)) { | ||
return json_decode($value); | ||
} | ||
|
||
return json_decode(json_encode($value)); | ||
} | ||
|
||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace Tests\Pitchart\Phlunit\Constraint\Json; | ||
|
||
use Pitchart\Phlunit\Constraint\Json\MatchesSchema; | ||
use PHPUnit\Framework\TestCase; | ||
use Tests\Pitchart\Phlunit\Constraint\ConstraintTestCase; | ||
use Tests\Pitchart\Phlunit\Fixture\Person; | ||
|
||
class MatchesSchemaTest extends ConstraintTestCase | ||
{ | ||
/** | ||
* @var MatchesSchema | ||
*/ | ||
protected $constraint; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->constraint = new MatchesSchema(['type' => 'object', 'required' => ['name'], 'properties' => ['name' => ['type' => 'string']]]); | ||
} | ||
|
||
public function test_succeeds_when_sut_is_a_string_matching_provided_format() | ||
{ | ||
$this->assertTrue($this->constraint->evaluate('{"name": "Batman"}', '', true)); | ||
} | ||
|
||
public function test_succeeds_when_sut_is_an_array_matching_provided_format() | ||
{ | ||
$this->assertTrue($this->constraint->evaluate(['name' => 'Batman'], '', true)); | ||
} | ||
|
||
public function test_succeeds_when_sut_is_an_object_matching_provided_format() | ||
{ | ||
$this->assertTrue($this->constraint->evaluate(new Person('Batman'), '', true)); | ||
} | ||
|
||
public function test_succeeds_when_sut_is_is_more_than_provided_format() | ||
{ | ||
$this->assertTrue($this->constraint->evaluate(['name' => 'Batman', 'city' => 'Gotham City'], '', true)); | ||
} | ||
|
||
public function test_fails_when_sut_has_a_property_missing() | ||
{ | ||
$this->assertFalse($this->constraint->evaluate(['lastname' => 'Batman'], '', true)); | ||
} | ||
|
||
public function test_fails_when_sut_do_not_respect_format() | ||
{ | ||
$this->assertFalse($this->constraint->evaluate(['name' => null], '', true)); | ||
} | ||
|
||
public function test_fails_with_a_clear_and_complete_error_message() | ||
{ | ||
$json = '{"name":null}'; | ||
|
||
$this->assertHasFailingMessage( | ||
<<<EOF | ||
Failed asserting that '{"name":null}' matches Json Schema {"type":"object","required":["name"],"properties":{"name":{"type":"string"}}}. | ||
[name] NULL value found, but a string is required | ||
EOF | ||
, $json); | ||
} | ||
|
||
} |
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,3 @@ | ||
{ | ||
"name": "Batman" | ||
} |
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,3 @@ | ||
{ | ||
"name":"Robin" | ||
} |
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,37 @@ | ||
<?php | ||
|
||
|
||
namespace Tests\Pitchart\Phlunit; | ||
|
||
|
||
use PHPUnit\Framework\TestCase; | ||
use Pitchart\Phlunit\Check; | ||
use Pitchart\Phlunit\Checks\JsonCheck; | ||
|
||
class JsonCheckTest extends TestCase | ||
{ | ||
/** | ||
* @param string $sut | ||
* @param string $method | ||
* @param array $arguments | ||
* @dataProvider methodsAreFulentProvider | ||
*/ | ||
public function test_has_fluent_methods(string $sut, string $method, array $arguments = []) | ||
{ | ||
Check::that(call_user_func_array([Check::that($sut)->asJson(), $method], $arguments)) | ||
->isAnInstanceOf(JsonCheck::class); | ||
} | ||
|
||
public function methodsAreFulentProvider() | ||
{ | ||
$batman = file_get_contents(TEST_FILES_PATH.'batman.json'); | ||
yield from [ | ||
'isEqualTo' => ['{"name":"Batman"}', 'isEqualTo', [$batman]], | ||
'isNotEqualTo' => ['{"name":"Batman"}', 'isNotEqualTo', ['{"name":"Robin"}']], | ||
'isEqualToFileContent' => ['{"name":"Batman"}', 'isEqualToFileContent', [TEST_FILES_PATH.'batman.json']], | ||
'isNotEqualToFileContent' => ['{"name":"Batman"}', 'isNotEqualToFileContent', [TEST_FILES_PATH.'robin.json']], | ||
'matchesSchema' => ['{"name":"Batman"}', 'matchesSchema', [['type' => 'object', 'required' => ['name'], 'properties' => ['name' => ['type' => 'string']]], | ||
]] | ||
]; | ||
} | ||
} |
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,7 @@ | ||
<?php declare(strict_types=1); | ||
|
||
if (!\defined('TEST_FILES_PATH')) { | ||
\define('TEST_FILES_PATH', __DIR__ . \DIRECTORY_SEPARATOR . 'Fixture' . \DIRECTORY_SEPARATOR. 'files' . \DIRECTORY_SEPARATOR); | ||
} | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; |