Passed
Pull Request — master (#13)
by
unknown
02:35
created

php$0 ➔ testAddCustomGeneratorObject()   A

Complexity

Conditions 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 27
rs 9.488
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Tests\Generator;
4
5
use Cycle\Schema\GeneratorInterface;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Yii\Cycle\Exception\BadGeneratorDeclarationException;
9
use Yiisoft\Yii\Cycle\Generator\SchemaConveyor;
10
use Yiisoft\Yii\Cycle\Tests\Generator\Stub\FakeContainer;
11
use Yiisoft\Yii\Cycle\Tests\Generator\Stub\FakeGenerator;
12
13
class SchemaConveyorTest extends TestCase
14
{
15
    public function testDefaultGeneratorsList()
16
    {
17
        $conveyor = $this->createConveyor();
18
19
        // get generators list
20
        /** @var string[] $generators */
21
        $generators = array_map(
22
            fn($value) => $value instanceof FakeGenerator ? $value->originClass() : get_class($value),
23
            $conveyor->getGenerators()
24
        );
25
26
        $this->assertEquals([
27
            'Cycle\Schema\Generator\ResetTables',
28
            'Cycle\Schema\Generator\GenerateRelations',
29
            'Cycle\Schema\Generator\ValidateEntities',
30
            'Cycle\Schema\Generator\RenderTables',
31
            'Cycle\Schema\Generator\RenderRelations',
32
            'Cycle\Schema\Generator\GenerateTypecast',
33
        ], $generators);
34
    }
35
36
    public function testAddCustomGenerators(): void
37
    {
38
        $conveyor = $this->createConveyor();
39
        $conveyor->addGenerator($conveyor::STAGE_POSTPROCESS, new class {
40
            public function __invoke(ContainerInterface $container): GeneratorInterface
41
            {
42
                return new FakeGenerator('FakeGenerator-from-invocable-object');
43
            }
44
        });
45
        $conveyor->addGenerator($conveyor::STAGE_USERLAND, function (ContainerInterface $container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

45
        $conveyor->addGenerator($conveyor::STAGE_USERLAND, function (/** @scrutinizer ignore-unused */ ContainerInterface $container) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
            return new FakeGenerator('FakeGenerator-from-closure');
47
        });
48
        $conveyor->addGenerator($conveyor::STAGE_RENDER, \Cycle\Schema\Generator\SyncTables::class);
49
        $conveyor->addGenerator($conveyor::STAGE_INDEX, new FakeGenerator('FakeGenerator-from-object'));
50
51
        // get generators list
52
        /** @var string[] $generators */
53
        $generators = array_map(
54
            fn($value) => $value instanceof FakeGenerator ? $value->originClass() : get_class($value),
55
            $conveyor->getGenerators()
56
        );
57
58
        $this->assertEquals([
59
            'Cycle\Schema\Generator\ResetTables',
60
            'FakeGenerator-from-object',
61
            'Cycle\Schema\Generator\GenerateRelations',
62
            'Cycle\Schema\Generator\ValidateEntities',
63
            'Cycle\Schema\Generator\RenderTables',
64
            'Cycle\Schema\Generator\RenderRelations',
65
            \Cycle\Schema\Generator\SyncTables::class,
66
            'FakeGenerator-from-closure',
67
            'Cycle\Schema\Generator\GenerateTypecast',
68
            'FakeGenerator-from-invocable-object',
69
        ], $generators);
70
    }
71
72
    public function testAddCustomGeneratorObject(): void
73
    {
74
        $conveyor = $this->createConveyor();
75
        $conveyor->addGenerator($conveyor::STAGE_POSTPROCESS, \Cycle\Schema\Generator\GenerateTypecast::class);
76
        $conveyor->addGenerator($conveyor::STAGE_USERLAND, \Cycle\Schema\Generator\RenderTables::class);
77
        $conveyor->addGenerator($conveyor::STAGE_RENDER, \Cycle\Schema\Generator\SyncTables::class);
78
        $conveyor->addGenerator($conveyor::STAGE_INDEX, \Cycle\Annotated\MergeIndexes::class);
79
80
        // get generators list
81
        /** @var string[] $generators */
82
        $generators = array_map(
83
            fn($value) => $value instanceof FakeGenerator ? $value->originClass() : get_class($value),
84
            $conveyor->getGenerators()
85
        );
86
87
        $this->assertEquals([
88
            'Cycle\Schema\Generator\ResetTables',
89
            \Cycle\Annotated\MergeIndexes::class,
90
            'Cycle\Schema\Generator\GenerateRelations',
91
            'Cycle\Schema\Generator\ValidateEntities',
92
            'Cycle\Schema\Generator\RenderTables',
93
            'Cycle\Schema\Generator\RenderRelations',
94
            \Cycle\Schema\Generator\SyncTables::class,
95
            \Cycle\Schema\Generator\RenderTables::class,
96
            'Cycle\Schema\Generator\GenerateTypecast',
97
            \Cycle\Schema\Generator\GenerateTypecast::class,
98
        ], $generators);
99
    }
100
101
    public function badGeneratorProvider(): array
102
    {
103
        return [
104
            [\stdClass::class],
105
            [new \DateTimeImmutable()],
106
            [fn(ContainerInterface $container) => new \DateTime()],
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

106
            [fn(/** @scrutinizer ignore-unused */ ContainerInterface $container) => new \DateTime()],

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
        ];
108
    }
109
110
    /**
111
     * @dataProvider badGeneratorProvider
112
     */
113
    public function testAddWrongGenerator($badGenerator): void
114
    {
115
        $conveyor = $this->createConveyor();
116
        $conveyor->addGenerator($conveyor::STAGE_USERLAND, $badGenerator);
117
118
        $this->expectException(BadGeneratorDeclarationException::class);
119
120
        $conveyor->getGenerators();
121
    }
122
123
    public function createConveyor($entityPaths = ['@test-dir']): SchemaConveyor
0 ignored issues
show
Unused Code introduced by
The parameter $entityPaths is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

123
    public function createConveyor(/** @scrutinizer ignore-unused */ $entityPaths = ['@test-dir']): SchemaConveyor

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
    {
125
        $conveyor = new SchemaConveyor(new FakeContainer($this));
126
127
        return $conveyor;
128
    }
129
}
130