SchemaFactoryTest::provideSchemas()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.8666
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