-
Notifications
You must be signed in to change notification settings - Fork 0
IBX-10120: Added ability to declare JSON Schema for tests #15
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
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,35 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\Test\Rest\Schema; | ||
|
||
use LogicException; | ||
|
||
abstract class AbstractFileSchemaProvider implements SchemaProviderInterface | ||
{ | ||
/** | ||
* @throws \JsonException | ||
*/ | ||
final protected function decodeFile(string $file): object | ||
{ | ||
$basicTypes = file_get_contents($file); | ||
if ($basicTypes === false) { | ||
throw new LogicException('Failed to load basic types schema from file: ' . $file); | ||
} | ||
|
||
$schema = json_decode($basicTypes, false, 512, JSON_THROW_ON_ERROR); | ||
if (!is_object($schema)) { | ||
throw new LogicException(sprintf( | ||
'Failed to decode basic types schema from file: %s. Schema is not an object.', | ||
$file, | ||
)); | ||
} | ||
|
||
return $schema; | ||
} | ||
} |
This file contains hidden or 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,21 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\Test\Rest\Schema; | ||
|
||
interface SchemaProviderInterface | ||
{ | ||
/** | ||
* Return schema objects, indexed by their "$id". | ||
* | ||
* Will be automatically prefixed with "internal://". | ||
* | ||
* @return iterable<non-empty-string, object> | ||
*/ | ||
public function provideSchemas(): iterable; | ||
} |
This file contains hidden or 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,33 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Test\Rest\Schema; | ||
|
||
use JsonSchema\SchemaStorage; | ||
use JsonSchema\UriRetrieverInterface; | ||
|
||
final class SchemaStorageFactory | ||
{ | ||
/** | ||
* @param iterable<\Ibexa\Contracts\Test\Rest\Schema\SchemaProviderInterface> $schemas | ||
*/ | ||
public function create( | ||
UriRetrieverInterface $uriRetriever, | ||
iterable $schemas | ||
): SchemaStorage { | ||
$storage = new SchemaStorage($uriRetriever); | ||
|
||
foreach ($schemas as $schemaProvider) { | ||
foreach ($schemaProvider->provideSchemas() as $id => $schema) { | ||
$storage->addSchema("internal://$id", $schema); | ||
} | ||
} | ||
|
||
return $storage; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 @@ | ||
services: | ||
_defaults: | ||
public: false | ||
autowire: true | ||
autoconfigure: true | ||
|
||
Ibexa\Tests\Integration\Test\Rest\Schema\TestSchemaProvider: ~ |
This file contains hidden or 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,22 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Integration\Test\Rest\Schema; | ||
|
||
use Ibexa\Contracts\Test\Rest\Schema\AbstractFileSchemaProvider; | ||
|
||
final class TestSchemaProvider extends AbstractFileSchemaProvider | ||
{ | ||
/** | ||
* @throws \JsonException | ||
*/ | ||
public function provideSchemas(): iterable | ||
{ | ||
yield 'ibexa/test-rest/basic_types' => $this->decodeFile(__DIR__ . '/../basic_types.json'); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
tests/integration/Schema/Validator/JsonSchemaValidatorTest.php
This file contains hidden or 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,82 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Integration\Test\Rest\Schema\Validator; | ||
|
||
use Ibexa\Contracts\Test\Core\IbexaKernelTestCase; | ||
use Ibexa\Test\Rest\Schema\Validator\JsonSchemaValidator; | ||
use JsonSchema\SchemaStorageInterface; | ||
use LogicException; | ||
use PHPUnit\Framework\ExpectationFailedException; | ||
|
||
final class JsonSchemaValidatorTest extends IbexaKernelTestCase | ||
{ | ||
private JsonSchemaValidator $validator; | ||
|
||
private SchemaStorageInterface $storage; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$core = self::getIbexaTestCore(); | ||
$this->validator = $core->getServiceByClassName(JsonSchemaValidator::class); | ||
$this->storage = $core->getServiceByClassName(SchemaStorageInterface::class, 'ibexa.test.rest.json_schema.schema_storage'); | ||
} | ||
|
||
public function testValidate(): void | ||
{ | ||
$json = '{"data": {"xyz": false}}'; | ||
|
||
$this->expectException(ExpectationFailedException::class); | ||
$this->expectExceptionMessage(<<<MESSAGE | ||
property: [data], constraint: type, error: Object value found, but a string is required | ||
property: [data], constraint: type, error: Object value found, but an integer is required | ||
property: [data], constraint: oneOf, error: Failed to match exactly one schema | ||
|
||
Failed asserting that false is true. | ||
Steveb-p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MESSAGE); | ||
$this->validator->validate($json, __DIR__ . '/../../json_schema'); | ||
} | ||
|
||
public function testValidateWithInternal(): void | ||
{ | ||
$this->storage->addSchema( | ||
'internal://ibexa/test-rest/json_schema', | ||
$this->decodeJsonObject($this->loadFile(__DIR__ . '/../../json_schema.json')) | ||
); | ||
|
||
$json = '{"data": {"xyz": false}}'; | ||
$this->expectException(ExpectationFailedException::class); | ||
$this->expectExceptionMessage(<<<MESSAGE | ||
property: [data], constraint: type, error: Object value found, but a string is required | ||
property: [data], constraint: type, error: Object value found, but an integer is required | ||
property: [data], constraint: oneOf, error: Failed to match exactly one schema | ||
|
||
Failed asserting that false is true. | ||
Steveb-p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MESSAGE); | ||
$this->validator->validate($json, 'internal://ibexa/test-rest/json_schema'); | ||
} | ||
|
||
private function decodeJsonObject(string $content): object | ||
{ | ||
return json_decode($content, false, 512, JSON_THROW_ON_ERROR); | ||
} | ||
|
||
private function loadFile(string $location): string | ||
{ | ||
$contents = file_get_contents($location); | ||
if (empty($contents)) { | ||
throw new LogicException(sprintf( | ||
'Unable to load file: %s', | ||
$location, | ||
)); | ||
} | ||
|
||
return $contents; | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.