Completed
Push — master ( e32202...083db4 )
by Alex
08:17
created

StepsPassTest::test abstract class service definitions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 29
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the PierstovalCharacterManagerBundle package.
5
 *
6
 * (c) Alexandre Rock Ancelet <[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
use Doctrine\Common\Persistence\ObjectManager;
13
use PHPUnit\Framework\TestCase;
14
use Pierstoval\Bundle\CharacterManagerBundle\DependencyInjection\Compiler\StepsPass;
15
use Pierstoval\Bundle\CharacterManagerBundle\Registry\ActionsRegistry;
16
use Pierstoval\Bundle\CharacterManagerBundle\Tests\Fixtures\Stubs\Action\ConcreteAbstractActionStub;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\Routing\RouterInterface;
19
use Symfony\Component\Translation\TranslatorInterface;
20
use Symfony\Component\Yaml\Yaml;
21
use Twig\Environment;
22
23
/**
24
 * Test the extension and the compiler pass.
25
 */
26
class StepsPassTest extends TestCase
27
{
28
    /**
29
     * @dataProvider provideNonWorkingConfigurations
30
     */
31
    public function test compiler pass should not work if extension not processed(
32
        array $config,
33
        string $expectedException,
34
        string $expectedExceptionMessage,
35
        string $stepClass
36
    ) {
37
        $this->expectException($expectedException);
38
        $this->expectExceptionMessage($expectedExceptionMessage);
39
40
        $stepsPass = new StepsPass();
41
42
        $container = new ContainerBuilder();
43
        $container
44
            ->register('steps.default')
45
            ->setClass($stepClass)
46
        ;
47
48
        $container->setParameter('pierstoval_character_manager.managers', $config);
49
50
        $stepsPass->process($container);
51
    }
52
53
    public function provideNonWorkingConfigurations()
54
    {
55
        $dir = __DIR__.'/../Fixtures/App/compiler_pass_test_non_working/';
56
57
        $configFiles = glob($dir.'compiler_config_*.yaml');
58
59
        sort($configFiles);
60
61
        foreach ($configFiles as $k => $file) {
62
            $config = Yaml::parse(file_get_contents($file));
63
            yield basename($file) => [
64
                $config['config'],
65
                $config['expected_exception'],
66
                $config['expected_exception_message'],
67
                $config['step_class'],
68
            ];
69
        }
70
    }
71
72
    public function test abstract class service definitions()
73
    {
74
        $stepsPass = new StepsPass();
75
76
        $container = new ContainerBuilder();
77
        $container
78
            ->register('steps.default')
79
            ->setClass(ConcreteAbstractActionStub::class)
80
        ;
81
82
        $container->register(ActionsRegistry::class);
83
        $container->register(ObjectManager::class);
84
        $container->register(Environment::class);
85
        $container->register(RouterInterface::class);
86
        $container->register(TranslatorInterface::class);
87
88
        // Empty config here, we just test definition tags
89
        $container->setParameter('pierstoval_character_manager.managers', [
90
            'main' => [
91
                'character_class' => 'test_abstract',
92
                'steps' => [
93
                    '01' => [
94
                        'action' => 'steps.default',
95
                        'label' => '',
96
                        'onchange_clear' => [],
97
                        'dependencies' => [],
98
                    ],
99
                ],
100
            ],
101
        ]);
102
103
        $stepsPass->process($container);
104
105
        // Test references calls are correct.
106
        $definition = $container->getDefinition('steps.default');
107
108
        $calls = $definition->getMethodCalls();
109
110
        static::assertCount(5, $calls);
111
        static::assertSame('configure', $calls[0][0]);
112
        static::assertSame('setObjectManager', $calls[1][0]);
113
        static::assertSame('setTwig', $calls[2][0]);
114
        static::assertSame('setRouter', $calls[3][0]);
115
        static::assertSame('setTranslator', $calls[4][0]);
116
    }
117
118
    public function test simple classes are automatically registered as services()
119
    {
120
        $stepsPass = new StepsPass();
121
122
        $container = new ContainerBuilder();
123
124
        $container->register(ActionsRegistry::class);
125
        $container->register(ObjectManager::class);
126
        $container->register(Environment::class);
127
        $container->register(RouterInterface::class);
128
        $container->register(TranslatorInterface::class);
129
130
        // Empty config here, we just test definition tags
131
        $container->setParameter('pierstoval_character_manager.managers', [
132
            'main' => [
133
                'character_class' => 'test_abstract',
134
                'steps' => [
135
                    '01' => [
136
                        'action'         => ConcreteAbstractActionStub::class,
137
                        'name'           => 'step_1',
138
                        'label'          => 'Step 1',
139
                        'dependencies'     => [],
140
                        'onchange_clear' => [],
141
                        'number'         => 1,
142
                    ],
143
                ],
144
            ],
145
        ]);
146
147
        $stepsPass->process($container);
148
149
        // Test references calls are correct.
150
        static::assertTrue($container->hasDefinition(ConcreteAbstractActionStub::class));
151
152
        $definition = $container->getDefinition(ConcreteAbstractActionStub::class);
153
154
        // These should be set by default on every action class not already registered as service
155
        static::assertTrue($definition->isPrivate());
156
        static::assertTrue($definition->isAutowired());
157
        static::assertTrue($definition->isLazy());
158
        static::assertSame(ConcreteAbstractActionStub::class, $definition->getClass());
159
    }
160
161
    public function test steps order starts from one()
162
    {
163
        $stepsPass = new StepsPass();
164
        $container = new ContainerBuilder();
165
166
        $container->register(ActionsRegistry::class);
167
168
        $inlineStub1 = new class extends ConcreteAbstractActionStub
169
        {
170
        };
171
        $inlineStub2 = new class extends ConcreteAbstractActionStub
172
        {
173
        };
174
175
        $container->setParameter('pierstoval_character_manager.managers', [
176
            'main' => [
177
                'character_class' => 'test_abstract',
178
                'steps' => [
179
                    '01' => [
180
                        'action'         => ConcreteAbstractActionStub::class,
181
                        'label'          => 'Step 1',
182
                        'dependencies'     => [],
183
                        'onchange_clear' => [],
184
                    ],
185
                    '02' => [
186
                        'action'         => ConcreteAbstractActionStub::class,
187
                        'label'          => 'Step 1',
188
                        'dependencies'     => [],
189
                        'onchange_clear' => [],
190
                    ],
191
                ],
192
            ],
193
            'another_manager' => [
194
                'character_class' => 'test_abstract',
195
                'steps' => [
196
                    '01' => [
197
                        'action'         => \get_class($inlineStub1),
198
                        'label'          => 'Step 1',
199
                        'dependencies'     => [],
200
                        'onchange_clear' => [],
201
                    ],
202
                    '02' => [
203
                        'action'         => \get_class($inlineStub2),
204
                        'label'          => 'Step 1',
205
                        'dependencies'     => [],
206
                        'onchange_clear' => [],
207
                    ],
208
                ],
209
            ],
210
        ]);
211
212
        $stepsPass->process($container);
213
214
        $managersConfiguration = $container->getParameter('pierstoval_character_manager.managers');
215
216
        static::assertSame(1, $managersConfiguration['main']['steps']['01']['number']);
217
        static::assertSame(2, $managersConfiguration['main']['steps']['02']['number']);
218
        static::assertSame(1, $managersConfiguration['another_manager']['steps']['01']['number']);
219
        static::assertSame(2, $managersConfiguration['another_manager']['steps']['02']['number']);
220
    }
221
}
222