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\Publish\Core\MVC\Symfony\View\Tests; |
10
|
|
|
|
11
|
|
|
use ArrayIterator; |
12
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
13
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\GenericVariableProviderRegistry; |
14
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\View; |
15
|
|
|
use eZ\Publish\SPI\MVC\View\VariableProvider; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
final class VariableProviderRegistryTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
private function getRegistry(array $providers): GenericVariableProviderRegistry |
21
|
|
|
{ |
22
|
|
|
return new GenericVariableProviderRegistry( |
23
|
|
|
new ArrayIterator($providers) |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
private function getProvider(string $identifier): VariableProvider |
28
|
|
|
{ |
29
|
|
|
return new class($identifier) implements VariableProvider { |
30
|
|
|
private $identifier; |
31
|
|
|
|
32
|
|
|
public function __construct(string $identifier) |
33
|
|
|
{ |
34
|
|
|
$this->identifier = $identifier; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getIdentifier(): string |
38
|
|
|
{ |
39
|
|
|
return $this->identifier; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getTwigVariables(View $view, array $options = []): object |
43
|
|
|
{ |
44
|
|
|
return (object)[ |
45
|
|
|
$this->identifier . '_parameter' => $this->identifier . '_value', |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
}; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function testParameterProviderGetter(): void |
52
|
|
|
{ |
53
|
|
|
$registry = $this->getRegistry([ |
54
|
|
|
$this->getProvider('provider_a'), |
55
|
|
|
$this->getProvider('provider_b'), |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$providerA = $registry->getTwigVariableProvider('provider_a'); |
59
|
|
|
$providerB = $registry->getTwigVariableProvider('provider_b'); |
60
|
|
|
|
61
|
|
|
$this->assertEquals($providerA->getIdentifier(), 'provider_a'); |
62
|
|
|
$this->assertEquals($providerB->getIdentifier(), 'provider_b'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testParameterNotFoundProviderGetter(): void |
66
|
|
|
{ |
67
|
|
|
$this->expectException(NotFoundException::class); |
68
|
|
|
|
69
|
|
|
$registry = $this->getRegistry([ |
70
|
|
|
$this->getProvider('provider_a'), |
71
|
|
|
$this->getProvider('provider_b'), |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
$registry->getTwigVariableProvider('provider_c'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testParameterProviderSetter(): void |
78
|
|
|
{ |
79
|
|
|
$registry = $this->getRegistry([ |
80
|
|
|
$this->getProvider('provider_a'), |
81
|
|
|
$this->getProvider('provider_b'), |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$hasProviderC = $registry->hasTwigVariableProvider('provider_c'); |
85
|
|
|
|
86
|
|
|
$this->assertFalse($hasProviderC); |
87
|
|
|
|
88
|
|
|
$registry->setTwigVariableProvider($this->getProvider('provider_c')); |
89
|
|
|
|
90
|
|
|
$providerC = $registry->getTwigVariableProvider('provider_c'); |
91
|
|
|
$this->assertEquals($providerC->getIdentifier(), 'provider_c'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testParameterProviderChecker(): void |
95
|
|
|
{ |
96
|
|
|
$registry = $this->getRegistry([ |
97
|
|
|
$this->getProvider('provider_a'), |
98
|
|
|
$this->getProvider('provider_b'), |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$this->assertTrue($registry->hasTwigVariableProvider('provider_a')); |
102
|
|
|
$this->assertTrue($registry->hasTwigVariableProvider('provider_b')); |
103
|
|
|
$this->assertFalse($registry->hasTwigVariableProvider('provider_c')); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|