Passed
Pull Request — master (#32)
by Wilmer
15:17
created

anonymous//tests/Conveyor/SchemaConveyorTest.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 4
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
wmc 1
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Tests\Conveyor;
4
5
use Cycle\Schema\GeneratorInterface;
6
use PHPUnit\Framework\TestCase;
7
use Yiisoft\Yii\Cycle\Conveyor\SchemaConveyor;
8
use Yiisoft\Yii\Cycle\Exception\BadGeneratorDeclarationException;
9
use Yiisoft\Yii\Cycle\Tests\Conveyor\Stub\FakeContainer;
10
use Yiisoft\Yii\Cycle\Tests\Conveyor\Stub\FakeGenerator;
11
12
class SchemaConveyorTest extends TestCase
13
{
14
    public function testDefaultGeneratorsList(): void
15
    {
16
        $conveyor = $this->createConveyor();
17
18
        // get generators list
19
        /** @var string[] $generators */
20
        $generators = array_map(
21
            fn ($value) => $value instanceof FakeGenerator ? $value->originClass() : get_class($value),
22
            $conveyor->getGenerators()
23
        );
24
25
        $this->assertSame([
26
            'Cycle\Schema\Generator\ResetTables',
27
            'Cycle\Schema\Generator\GenerateRelations',
28
            'Cycle\Schema\Generator\ValidateEntities',
29
            'Cycle\Schema\Generator\RenderTables',
30
            'Cycle\Schema\Generator\RenderRelations',
31
            'Cycle\Schema\Generator\GenerateTypecast',
32
        ], $generators);
33
    }
34
35
    public function testAddCustomGenerators(): void
36
    {
37
        $conveyor = $this->createConveyor();
38
        $conveyor->addGenerator(
39
            $conveyor::STAGE_POSTPROCESS,
40
            new class() {
41
                public function __invoke(): GeneratorInterface
42
                {
43
                    return new FakeGenerator('FakeGenerator-from-invocable-object');
44
                }
45
            }
46
        );
47
        $conveyor->addGenerator($conveyor::STAGE_USERLAND, static function () {
48
            return new FakeGenerator('FakeGenerator-from-closure');
49
        });
50
        $conveyor->addGenerator($conveyor::STAGE_RENDER, \Cycle\Schema\Generator\SyncTables::class);
51
        $conveyor->addGenerator($conveyor::STAGE_INDEX, new FakeGenerator('FakeGenerator-object'));
52
53
        // get generators list
54
        /** @var string[] $generators */
55
        $generators = array_map(
56
            fn ($value) => $value instanceof FakeGenerator ? $value->originClass() : get_class($value),
57
            $conveyor->getGenerators()
58
        );
59
60
        $this->assertSame([
61
            'Cycle\Schema\Generator\ResetTables',
62
            'FakeGenerator-object',
63
            'Cycle\Schema\Generator\GenerateRelations',
64
            'Cycle\Schema\Generator\ValidateEntities',
65
            'Cycle\Schema\Generator\RenderTables',
66
            'Cycle\Schema\Generator\RenderRelations',
67
            \Cycle\Schema\Generator\SyncTables::class,
68
            'FakeGenerator-from-closure',
69
            'Cycle\Schema\Generator\GenerateTypecast',
70
            'FakeGenerator-from-invocable-object',
71
        ], $generators);
72
    }
73
74
    public function testAddCustomGeneratorObject(): void
75
    {
76
        $conveyor = $this->createConveyor();
77
        $conveyor->addGenerator($conveyor::STAGE_POSTPROCESS, \Cycle\Schema\Generator\GenerateTypecast::class);
78
        $conveyor->addGenerator($conveyor::STAGE_USERLAND, \Cycle\Schema\Generator\RenderTables::class);
79
        $conveyor->addGenerator($conveyor::STAGE_RENDER, \Cycle\Schema\Generator\SyncTables::class);
80
        $conveyor->addGenerator($conveyor::STAGE_INDEX, \Cycle\Annotated\MergeIndexes::class);
81
82
        // get generators list
83
        /** @var string[] $generators */
84
        $generators = array_map(
85
            fn ($value) => $value instanceof FakeGenerator ? $value->originClass() : get_class($value),
86
            $conveyor->getGenerators()
87
        );
88
89
        $this->assertSame([
90
            'Cycle\Schema\Generator\ResetTables',
91
            \Cycle\Annotated\MergeIndexes::class,
92
            'Cycle\Schema\Generator\GenerateRelations',
93
            'Cycle\Schema\Generator\ValidateEntities',
94
            'Cycle\Schema\Generator\RenderTables',
95
            'Cycle\Schema\Generator\RenderRelations',
96
            \Cycle\Schema\Generator\SyncTables::class,
97
            \Cycle\Schema\Generator\RenderTables::class,
98
            'Cycle\Schema\Generator\GenerateTypecast',
99
            \Cycle\Schema\Generator\GenerateTypecast::class,
100
        ], $generators);
101
    }
102
103
    public function badGeneratorProvider(): array
104
    {
105
        return [
106
            [\stdClass::class],
107
            [new \DateTimeImmutable()],
108
            [fn () => new \DateTime()],
109
        ];
110
    }
111
112
    /**
113
     * @dataProvider badGeneratorProvider
114
     */
115
    public function testAddWrongGenerator($badGenerator): void
116
    {
117
        $conveyor = $this->createConveyor();
118
        $conveyor->addGenerator($conveyor::STAGE_USERLAND, $badGenerator);
119
120
        $this->expectException(BadGeneratorDeclarationException::class);
121
122
        $conveyor->getGenerators();
123
    }
124
125
    public function createConveyor(): SchemaConveyor
126
    {
127
        return new SchemaConveyor(new FakeContainer($this));
128
    }
129
}
130