1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Tests\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use FOS\ElasticaBundle\DependencyInjection\Compiler\RegisterPagerProvidersPass; |
6
|
|
|
use FOS\ElasticaBundle\Provider\PagerProviderInterface; |
7
|
|
|
use FOS\ElasticaBundle\Provider\PagerProviderRegistry; |
8
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
|
12
|
|
View Code Duplication |
class RegisterPagerProvidersPassTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
public function testShouldImplementCompilerPassInterface() |
15
|
|
|
{ |
16
|
|
|
$rc = new \ReflectionClass(RegisterPagerProvidersPass::class); |
17
|
|
|
|
18
|
|
|
$this->assertTrue($rc->implementsInterface(CompilerPassInterface::class)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testCouldBeConstructedWithoutAnyArguments() |
22
|
|
|
{ |
23
|
|
|
new RegisterPagerProvidersPass(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testShouldDoNothingIfPagerProviderRegistryServiceIsMissing() |
27
|
|
|
{ |
28
|
|
|
$container = new ContainerBuilder(); |
29
|
|
|
$pass = new RegisterPagerProvidersPass(); |
30
|
|
|
|
31
|
|
|
$container->setDefinition('foo_provider', $this->createProviderDefinition(array('type' => 'a'))); |
32
|
|
|
$container->setDefinition('bar_provider', $this->createProviderDefinition(array('index' => 'foo', 'type' => 'b'))); |
33
|
|
|
$container->setDefinition('baz_provider', $this->createProviderDefinition(array('index' => 'bar', 'type' => 'a'))); |
34
|
|
|
|
35
|
|
|
$pass->process($container); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testShouldRegisterTaggedPagerProviders() |
39
|
|
|
{ |
40
|
|
|
$container = new ContainerBuilder(); |
41
|
|
|
$pass = new RegisterPagerProvidersPass(); |
42
|
|
|
|
43
|
|
|
$registry = new Definition(PagerProviderRegistry::class); |
44
|
|
|
$registry->addArgument([]); |
45
|
|
|
|
46
|
|
|
$container->setParameter('fos_elastica.default_index', 'foo'); |
47
|
|
|
$container->setDefinition('fos_elastica.pager_provider_registry', $registry); |
48
|
|
|
|
49
|
|
|
$container->setDefinition('foo_provider', $this->createProviderDefinition(array('type' => 'a'))); |
50
|
|
|
$container->setDefinition('bar_provider', $this->createProviderDefinition(array('index' => 'foo', 'type' => 'b'))); |
51
|
|
|
$container->setDefinition('baz_provider', $this->createProviderDefinition(array('index' => 'bar', 'type' => 'a'))); |
52
|
|
|
|
53
|
|
|
$pass->process($container); |
54
|
|
|
|
55
|
|
|
$this->assertEquals([ |
56
|
|
|
'foo' => [ |
57
|
|
|
'a' => 'foo_provider', |
58
|
|
|
'b' => 'bar_provider', |
59
|
|
|
], |
60
|
|
|
'bar' => [ |
61
|
|
|
'a' => 'baz_provider' |
62
|
|
|
], |
63
|
|
|
], $registry->getArgument(0)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testThrowsIfTagMissesTypeAttribute() |
67
|
|
|
{ |
68
|
|
|
$container = new ContainerBuilder(); |
69
|
|
|
$pass = new RegisterPagerProvidersPass(); |
70
|
|
|
|
71
|
|
|
$container->setDefinition('fos_elastica.pager_provider_registry', new Definition()); |
72
|
|
|
$container->setParameter('fos_elastica.default_index', 'foo'); |
73
|
|
|
|
74
|
|
|
$container->setDefinition('a_provider', $this->createProviderDefinition([])); |
75
|
|
|
|
76
|
|
|
$this->setExpectedException(\InvalidArgumentException::class, 'Elastica provider "a_provider" must specify the "type" attribute.'); |
|
|
|
|
77
|
|
|
|
78
|
|
|
$pass->process($container); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testThrowsIfProviderForSuchIndexTypeHasBeenAlreadyRegistered() |
82
|
|
|
{ |
83
|
|
|
$container = new ContainerBuilder(); |
84
|
|
|
$pass = new RegisterPagerProvidersPass(); |
85
|
|
|
|
86
|
|
|
$container->setDefinition('fos_elastica.pager_provider_registry', new Definition()); |
87
|
|
|
$container->setParameter('fos_elastica.default_index', 'foo'); |
88
|
|
|
|
89
|
|
|
$container->setDefinition('a_foo_provider', $this->createProviderDefinition(['index' => 'foo', 'type' => 'bar'])); |
90
|
|
|
$container->setDefinition('a_bar_provider', $this->createProviderDefinition(['index' => 'foo', 'type' => 'bar'])); |
91
|
|
|
|
92
|
|
|
$this->setExpectedException(\InvalidArgumentException::class, 'Cannot register provider "a_bar_provider". The provider "a_foo_provider" has been registered for same index "foo" and type "bar"'); |
|
|
|
|
93
|
|
|
$pass->process($container); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testThrowsIfProviderServiceDoesNotImplementPagerProviderInterface() |
97
|
|
|
{ |
98
|
|
|
$container = new ContainerBuilder(); |
99
|
|
|
$pass = new RegisterPagerProvidersPass(); |
100
|
|
|
|
101
|
|
|
$container->setDefinition('fos_elastica.pager_provider_registry', new Definition()); |
102
|
|
|
$container->setParameter('fos_elastica.default_index', 'foo'); |
103
|
|
|
|
104
|
|
|
$provider = $this->createProviderDefinition(['index' => 'foo', 'type' => 'bar']); |
105
|
|
|
$provider->setClass(\stdClass::class); |
106
|
|
|
|
107
|
|
|
$container->setDefinition('a_foo_provider', $provider); |
108
|
|
|
|
109
|
|
|
$this->setExpectedException(\InvalidArgumentException::class, 'Elastica provider "a_foo_provider" with class "stdClass" must implement "FOS\ElasticaBundle\Provider\PagerProviderInterface".'); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$pass->process($container); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testShouldSkipClassCheckIfDefinitionHasFactory() |
115
|
|
|
{ |
116
|
|
|
$container = new ContainerBuilder(); |
117
|
|
|
$pass = new RegisterPagerProvidersPass(); |
118
|
|
|
|
119
|
|
|
$registry = new Definition(PagerProviderRegistry::class); |
120
|
|
|
$registry->addArgument([]); |
121
|
|
|
|
122
|
|
|
$container->setParameter('fos_elastica.default_index', 'foo'); |
123
|
|
|
$container->setDefinition('fos_elastica.pager_provider_registry', $registry); |
124
|
|
|
|
125
|
|
|
$provider = $this->createProviderDefinition(['index' => 'foo', 'type' => 'bar']); |
126
|
|
|
$provider->setClass(\stdClass::class); |
127
|
|
|
$provider->setFactory('a_factory_function'); |
128
|
|
|
|
129
|
|
|
$container->setDefinition('a_foo_provider', $provider); |
130
|
|
|
|
131
|
|
|
$pass->process($container); |
132
|
|
|
|
133
|
|
|
$this->assertEquals(['foo' => ['bar' => 'a_foo_provider']], $registry->getArgument(0)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $attributes |
138
|
|
|
* |
139
|
|
|
* @return Definition |
140
|
|
|
*/ |
141
|
|
|
private function createProviderDefinition(array $attributes = array()) |
142
|
|
|
{ |
143
|
|
|
$definition = new Definition(PagerProviderInterface::class); |
144
|
|
|
$definition->addTag('fos_elastica.pager_provider', $attributes); |
145
|
|
|
|
146
|
|
|
return $definition; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.