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

ExtensionTest::test yaml configurations()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
cc 6
eloc 20
nc 10
nop 2
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 PHPUnit\Framework\TestCase;
13
use Pierstoval\Bundle\CharacterManagerBundle\DependencyInjection\Compiler\StepsPass;
14
use Pierstoval\Bundle\CharacterManagerBundle\DependencyInjection\PierstovalCharacterManagerExtension;
15
use Pierstoval\Bundle\CharacterManagerBundle\Tests\Fixtures\Stubs\Action\ConcreteAbstractActionStub;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\Yaml\Yaml;
18
19
/**
20
 * Test the extension and the compiler pass.
21
 */
22
class ExtensionTest extends TestCase
23
{
24
    /**
25
     * @dataProvider provideYamlConfiguration
26
     *
27
     * @param $config
28
     * @param $expected
29
     */
30
    public function test yaml configurations($config, $expected)
31
    {
32
        $container = new ContainerBuilder();
33
        $ext       = new PierstovalCharacterManagerExtension();
34
        $stepsPass = new StepsPass();
35
36
        // Add the default step service
37
        $container
38
            ->register('steps.default')
39
            ->setClass(ConcreteAbstractActionStub::class)
40
        ;
41
42
        $ext->load($config, $container);
43
        $stepsPass->process($container);
44
45
        // Sorting the arrays by key name avoid issues with PHP7 and Yaml parsing that sometimes store the keys in the wrong order
46
47
        foreach ($expected['pierstoval_character_manager'] as $key => $expectedValue) {
48
            if (is_array($expectedValue)) {
49
                ksort($expectedValue);
50
                if (is_array(current($expectedValue))) {
51
                    $expectedValue = array_map('ksort', $expectedValue);
52
                }
53
            }
54
            $parameterValue = $container->getParameter($parameter = "pierstoval_character_manager.$key");
55
            if (is_array($parameterValue)) {
56
                ksort($parameterValue);
57
                if (is_array(current($parameterValue))) {
58
                    $parameterValue = array_map('ksort', $parameterValue);
59
                }
60
            }
61
            static::assertSame($expectedValue, $parameterValue, "$parameter is not the same as expected");
62
        }
63
    }
64
65
    /**
66
     * Provide all "extension_test" directory configs and test them through the extension.
67
     */
68
    public function provideYamlConfiguration(): \Generator
69
    {
70
        $dir = __DIR__.'/../Fixtures/App/extension_test/';
71
72
        $configFiles = glob($dir.'config_*.yaml');
73
74
        sort($configFiles);
75
76
        foreach ($configFiles as $k => $file) {
77
            $content = Yaml::parse(file_get_contents($file));
78
            yield basename($file) => [
79
                $content['input'],
80
                $content['output'],
81
            ];
82
        }
83
    }
84
}
85