Completed
Push — chain-config-resolver-tests ( f53a81 )
by
unknown
16:09
created

ConfigResolverTest::parameterProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Configuration\ConfigResolver;
10
11
use eZ\Publish\Core\MVC\ConfigResolverInterface;
12
use eZ\Publish\Core\MVC\Exception\ParameterNotFoundException;
13
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use PHPUnit\Framework\TestCase;
16
17
abstract class ConfigResolverTest extends TestCase
18
{
19
    protected const EXISTING_SA_NAME = 'existing_sa';
20
    protected const UNDEFINED_SA_NAME = 'undefined_sa';
21
    protected const SA_GROUP = 'sa_group';
22
23
    protected const DEFAULT_NAMESPACE = 'ezsettings';
24
25
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess */
26
    protected $siteAccess;
27
28
    /** @var \PHPUnit\Framework\MockObject\MockObject|\Symfony\Component\DependencyInjection\ContainerInterface */
29
    protected $containerMock;
30
31
    protected function setUp(): void
32
    {
33
        parent::setUp();
34
        $this->siteAccess = new SiteAccess('test');
35
        $this->containerMock = $this->createMock(ContainerInterface::class);
36
    }
37
38
    abstract protected function getResolver(string $defaultNamespace = 'ezsettings'): ConfigResolverInterface;
39
40
    abstract protected function getScope(): string;
41
42
    protected function getNamespace(): string
43
    {
44
        return self::DEFAULT_NAMESPACE;
45
    }
46
47
    public function testGetParameterFailedWithException(): void
48
    {
49
        $resolver = $this->getResolver(self::DEFAULT_NAMESPACE);
50
        $this->containerMock
51
            ->expects($this->once())
52
            ->method('hasParameter')
53
            ->with(sprintf('%s.%s.undefined', $this->getNamespace(), $this->getScope()))
54
            ->willReturn(false);
55
56
        $this->expectException(ParameterNotFoundException::class);
57
58
        $resolver->getParameter('undefined');
59
    }
60
61
    /**
62
     * @dataProvider parameterProvider
63
     */
64
    public function testGetParameterGlobalScope(string $paramName, $expectedValue): void
65
    {
66
        $globalScopeParameter = sprintf('%s.%s.%s', $this->getNamespace(), $this->getScope(), $paramName);
67
        $this->containerMock
68
            ->expects($this->once())
69
            ->method('hasParameter')
70
            ->with($globalScopeParameter)
71
            ->willReturn(true);
72
        $this->containerMock
73
            ->expects($this->once())
74
            ->method('getParameter')
75
            ->with($globalScopeParameter)
76
            ->willReturn($expectedValue);
77
78
        $this->assertSame($expectedValue, $this->getResolver()->getParameter($paramName));
79
    }
80
81
    public function parameterProvider(): array
82
    {
83
        return [
84
            ['foo', 'bar'],
85
            ['some.parameter', true],
86
            ['some.other.parameter', ['foo', 'bar', 'baz']],
87
            ['a.hash.parameter', ['foo' => 'bar', 'tata' => 'toto']],
88
            [
89
                'a.deep.hash', [
90
                    'foo' => 'bar',
91
                    'tata' => 'toto',
92
                    'deeper_hash' => [
93
                        'likeStarWars' => true,
94
                        'jedi' => ['Obi-Wan Kenobi', 'Mace Windu', 'Luke Skywalker', 'Leïa Skywalker (yes! Read episodes 7-8-9!)'],
95
                        'sith' => ['Darth Vader', 'Darth Maul', 'Palpatine'],
96
                        'roles' => [
97
                            'Amidala' => ['Queen'],
98
                            'Palpatine' => ['Senator', 'Emperor', 'Villain'],
99
                            'C3PO' => ['Droid', 'Annoying guy'],
100
                            'Jar-Jar' => ['Still wondering his role', 'Annoying guy'],
101
                        ],
102
                    ],
103
                ],
104
            ],
105
        ];
106
    }
107
108
    public function testGetSetDefaultNamespace(): void
109
    {
110
        $newDefaultNamespace = 'new';
111
        $configResolver = $this->getResolver();
112
        $this->assertSame(self::DEFAULT_NAMESPACE, $configResolver->getDefaultNamespace());
113
        $configResolver->setDefaultNamespace($newDefaultNamespace);
114
        $this->assertSame($newDefaultNamespace, $configResolver->getDefaultNamespace());
115
    }
116
}
117