|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Functional; |
| 15 | + |
| 16 | +use ApiPlatform\JsonSchema\Schema; |
| 17 | +use ApiPlatform\JsonSchema\SchemaFactoryInterface; |
| 18 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 19 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5793\BagOfTests; |
| 20 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5793\TestEntity; |
| 21 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 22 | +use JsonSchema\Validator; |
| 23 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 24 | + |
| 25 | +class SchemaTest extends ApiTestCase |
| 26 | +{ |
| 27 | + use SetupClassResourcesTrait; |
| 28 | + private static ?SchemaFactoryInterface $schemaFactory = null; |
| 29 | + |
| 30 | + /** |
| 31 | + * @return class-string[] |
| 32 | + */ |
| 33 | + public static function getResources(): array |
| 34 | + { |
| 35 | + return [BagOfTests::class, TestEntity::class]; |
| 36 | + } |
| 37 | + |
| 38 | + private static function getSchemaFactory(): SchemaFactoryInterface |
| 39 | + { |
| 40 | + if (static::$schemaFactory) { |
| 41 | + return static::$schemaFactory; |
| 42 | + } |
| 43 | + |
| 44 | + $container = static::getContainer(); |
| 45 | + /** @var SchemaFactoryInterface $schemaFactory */ |
| 46 | + $schemaFactory = $container->get('api_platform.json_schema.schema_factory'); |
| 47 | + |
| 48 | + return static::$schemaFactory = $schemaFactory; |
| 49 | + } |
| 50 | + |
| 51 | + #[DataProvider('getInvalidSchemas')] |
| 52 | + public function testSchemaIsNotValid(string $json, Schema $schema): void |
| 53 | + { |
| 54 | + $validator = new Validator(); |
| 55 | + $json = json_decode($json, null, 512, \JSON_THROW_ON_ERROR); |
| 56 | + $validator->validate($json, $schema->getArrayCopy()); |
| 57 | + $this->assertFalse($validator->isValid()); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + #[DataProvider('getSchemas')] |
| 62 | + public function testSchemaIsValid(string $json, Schema $schema): void |
| 63 | + { |
| 64 | + $validator = new Validator(); |
| 65 | + $json = json_decode($json, null, 512, \JSON_THROW_ON_ERROR); |
| 66 | + $validator->validate($json, $schema->getArrayCopy()); |
| 67 | + $this->assertTrue($validator->isValid()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return array<string, array{string, string}> |
| 72 | + */ |
| 73 | + public static function getSchemas(): array |
| 74 | + { |
| 75 | + return [ |
| 76 | + 'json-ld' => [ |
| 77 | + '{"@context":"/contexts/BagOfTests","@id":"/bag_of_tests/1","@type":"BagOfTests","id":1,"description":"string","tests":"a string","nonResourceTests":[{"id":1,"nullableString":"string","nullableInt":0}],"type":{"@id":"/test_entities/1","@type":"TestEntity","id":1,"nullableString":"string","nullableInt":0}}', |
| 78 | + static::getSchemaFactory()->buildSchema(BagOfTests::class, 'jsonld'), |
| 79 | + ], |
| 80 | + 'json-ld Collection' => [ |
| 81 | + '{"@context":"/contexts/BagOfTests","@id":"/bag_of_tests","@type":"hydra:Collection","hydra:totalItems":1,"hydra:member":[{"@id":"/bag_of_tests/1","@type":"BagOfTests","id":1,"description":"string","nonResourceTests":[],"type":{"@id":"/test_entities/1","@type":"TestEntity","id":1,"nullableString":"string","nullableInt":0}}]}', |
| 82 | + static::getSchemaFactory()->buildSchema(BagOfTests::class, 'jsonld', forceCollection: true), |
| 83 | + ] |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return array<string, array{string, string}> |
| 89 | + */ |
| 90 | + public static function getInvalidSchemas(): array |
| 91 | + { |
| 92 | + return [ |
| 93 | + 'json-ld' => [ |
| 94 | + '{"@context":"/contexts/BagOfTests","@id":"/bag_of_tests/1","@type":"BagOfTests","id":1,"description":"string","tests":"a string","nonResourceTests":[{"id":1,"nullableString":"string","nullableInt":0}],"type":{"@type":"TestEntity","id":1,"nullableString":"string","nullableInt":0}}', |
| 95 | + static::getSchemaFactory()->buildSchema(BagOfTests::class, 'jsonld'), |
| 96 | + ], |
| 97 | + ]; |
| 98 | + } |
| 99 | +} |
0 commit comments