|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gorynych\Tests\Util; |
|
6
|
|
|
|
|
7
|
|
|
use Cake\Collection\Collection; |
|
8
|
|
|
use DG\BypassFinals; |
|
9
|
|
|
use Gorynych\Tests\Util\Resource\TestSchema; |
|
10
|
|
|
use Gorynych\Util\SchemaFactory; |
|
11
|
|
|
use Gorynych\Util\SchemaStorage; |
|
12
|
|
|
use OpenApi\Annotations\Property; |
|
13
|
|
|
use OpenApi\Annotations\Schema; |
|
14
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
class SchemaFactoryTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var SchemaStorage|MockObject */ |
|
20
|
|
|
private $schemaStorageMock; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp(): void |
|
23
|
|
|
{ |
|
24
|
|
|
BypassFinals::enable(); |
|
25
|
|
|
$this->schemaStorageMock = $this->createMock(SchemaStorage::class); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @dataProvider provideSchemas |
|
30
|
|
|
* @param mixed[] $schemas |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testCreatesSchema(array $schemas, string $schemaClassName, string $expectedSchema): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->schemaStorageMock->method('getSchemas')->willReturn(new Collection($schemas)); |
|
35
|
|
|
$schema = $this->setUpFactory()->create($schemaClassName); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertSame($expectedSchema, $schema); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return \Generator<array> |
|
42
|
|
|
*/ |
|
43
|
|
|
public function provideSchemas(): \Generator |
|
44
|
|
|
{ |
|
45
|
|
|
$relatedSchema = new Schema([]); |
|
46
|
|
|
$relatedSchema->schema = 'RelatedSchema'; |
|
47
|
|
|
|
|
48
|
|
|
$property = new Property([]); |
|
49
|
|
|
$property->property = 'related'; |
|
50
|
|
|
$property->ref = '#/components/schemas/RelatedSchema'; |
|
51
|
|
|
|
|
52
|
|
|
$schema = new Schema([]); |
|
53
|
|
|
$schema->schema = 'TestSchema'; |
|
54
|
|
|
$schema->properties = [$property]; |
|
55
|
|
|
|
|
56
|
|
|
yield 'one schema with one related' => [ |
|
57
|
|
|
[$schema, $relatedSchema], |
|
58
|
|
|
TestSchema::class, |
|
59
|
|
|
'{"schema":"TestSchema","properties":{"related":{"$ref":"#\/components\/schemas\/RelatedSchema"}},"components":{"schemas":{"RelatedSchema":{"schema":"RelatedSchema"}}}}', |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function setUpFactory(): SchemaFactory |
|
64
|
|
|
{ |
|
65
|
|
|
return new SchemaFactory($this->schemaStorageMock); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|