|
| 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\JsonSchema\Tests; |
| 15 | + |
| 16 | +use ApiPlatform\JsonSchema\BackwardCompatibleSchemaFactory; |
| 17 | +use ApiPlatform\JsonSchema\Schema; |
| 18 | +use ApiPlatform\JsonSchema\SchemaFactoryInterface; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class BackwardCompatibleSchemaFactoryTest extends TestCase |
| 22 | +{ |
| 23 | + public function testWithSingleType(): void |
| 24 | + { |
| 25 | + $schema = new Schema(); |
| 26 | + $schema->setDefinitions(new \ArrayObject([ |
| 27 | + 'a' => new \ArrayObject([ |
| 28 | + 'properties' => new \ArrayObject([ |
| 29 | + 'foo' => new \ArrayObject(['type' => 'integer', 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]), |
| 30 | + ]), |
| 31 | + ]), |
| 32 | + ])); |
| 33 | + $schemaFactory = $this->createMock(SchemaFactoryInterface::class); |
| 34 | + $schemaFactory->method('buildSchema')->willReturn($schema); |
| 35 | + $schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory); |
| 36 | + $schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]); |
| 37 | + $schema = $schema->getDefinitions()['a']; |
| 38 | + |
| 39 | + $this->assertTrue($schema['properties']['foo']['exclusiveMinimum']); |
| 40 | + $this->assertTrue($schema['properties']['foo']['exclusiveMaximum']); |
| 41 | + $this->assertEquals($schema['properties']['foo']['minimum'], 0); |
| 42 | + $this->assertEquals($schema['properties']['foo']['maximum'], 1); |
| 43 | + } |
| 44 | + |
| 45 | + public function testWithMultipleType(): void |
| 46 | + { |
| 47 | + $schema = new Schema(); |
| 48 | + $schema->setDefinitions(new \ArrayObject([ |
| 49 | + 'a' => new \ArrayObject([ |
| 50 | + 'properties' => new \ArrayObject([ |
| 51 | + 'foo' => new \ArrayObject(['type' => ['number', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]), |
| 52 | + ]), |
| 53 | + ]), |
| 54 | + ])); |
| 55 | + $schemaFactory = $this->createMock(SchemaFactoryInterface::class); |
| 56 | + $schemaFactory->method('buildSchema')->willReturn($schema); |
| 57 | + $schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory); |
| 58 | + $schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]); |
| 59 | + $schema = $schema->getDefinitions()['a']; |
| 60 | + |
| 61 | + $this->assertTrue($schema['properties']['foo']['exclusiveMinimum']); |
| 62 | + $this->assertTrue($schema['properties']['foo']['exclusiveMaximum']); |
| 63 | + $this->assertEquals($schema['properties']['foo']['minimum'], 0); |
| 64 | + $this->assertEquals($schema['properties']['foo']['maximum'], 1); |
| 65 | + } |
| 66 | + |
| 67 | + public function testWithoutNumber(): void |
| 68 | + { |
| 69 | + $schema = new Schema(); |
| 70 | + $schema->setDefinitions(new \ArrayObject([ |
| 71 | + 'a' => new \ArrayObject([ |
| 72 | + 'properties' => new \ArrayObject([ |
| 73 | + 'foo' => new \ArrayObject(['type' => ['string', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]), |
| 74 | + ]), |
| 75 | + ]), |
| 76 | + ])); |
| 77 | + $schemaFactory = $this->createMock(SchemaFactoryInterface::class); |
| 78 | + $schemaFactory->method('buildSchema')->willReturn($schema); |
| 79 | + $schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory); |
| 80 | + $schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]); |
| 81 | + $schema = $schema->getDefinitions()['a']; |
| 82 | + |
| 83 | + $this->assertEquals($schema['properties']['foo']['exclusiveMinimum'], 0); |
| 84 | + $this->assertEquals($schema['properties']['foo']['exclusiveMaximum'], 1); |
| 85 | + } |
| 86 | + |
| 87 | + public function testWithoutFlag(): void |
| 88 | + { |
| 89 | + $schema = new Schema(); |
| 90 | + $schema->setDefinitions(new \ArrayObject([ |
| 91 | + 'a' => new \ArrayObject([ |
| 92 | + 'properties' => new \ArrayObject([ |
| 93 | + 'foo' => new \ArrayObject(['type' => ['string', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]), |
| 94 | + ]), |
| 95 | + ]), |
| 96 | + ])); |
| 97 | + $schemaFactory = $this->createMock(SchemaFactoryInterface::class); |
| 98 | + $schemaFactory->method('buildSchema')->willReturn($schema); |
| 99 | + $schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory); |
| 100 | + $schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => false]); |
| 101 | + $schema = $schema->getDefinitions()['a']; |
| 102 | + |
| 103 | + $this->assertEquals($schema['properties']['foo']['exclusiveMinimum'], 0); |
| 104 | + $this->assertEquals($schema['properties']['foo']['exclusiveMaximum'], 1); |
| 105 | + } |
| 106 | +} |
0 commit comments