1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\GBProd\ElasticsearchDataProviderBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use GBProd\ElasticsearchDataProviderBundle\DependencyInjection\Compiler\DataProviderCompilerPass; |
6
|
|
|
use GBProd\ElasticsearchDataProviderBundle\DataProvider\DataProviderInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Compiler path to register DataProviders |
12
|
|
|
* |
13
|
|
|
* @author gbprod <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class DataProviderCompilerPassTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
private $testedInstance; |
18
|
|
|
|
19
|
|
|
private $container; |
20
|
|
|
|
21
|
|
|
private $registryDefinition; |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->testedInstance = new DataProviderCompilerPass(); |
26
|
|
|
$this->container = new ContainerBuilder(); |
27
|
|
|
$this->registryDefinition = new Definition(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testShouldRegisterTaggedDataProviders() |
31
|
|
|
{ |
32
|
|
|
$this->container->setDefinition( |
33
|
|
|
'gbprod.elasticsearch_dataprovider.registry', |
34
|
|
|
$this->registryDefinition |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$this->container->setDefinition( |
38
|
|
|
'data_provider.foo.bar', |
39
|
|
|
$this->newDataProviderDefinition('foo', 'bar') |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$this->container->setDefinition( |
43
|
|
|
'data_provider.bar.foo', |
44
|
|
|
$this->newDataProviderDefinition('fizz', 'buzz') |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$this->testedInstance->process($this->container); |
48
|
|
|
|
49
|
|
|
$calls = $this->registryDefinition->getMethodCalls(); |
50
|
|
|
|
51
|
|
|
$this->assertEquals('add', $calls[0][0]); |
52
|
|
|
$this->assertInstanceOf(Definition::class, $calls[0][1][0]); |
53
|
|
|
|
54
|
|
|
$this->assertEquals('add', $calls[1][0]); |
55
|
|
|
$this->assertInstanceOf(Definition::class, $calls[1][1][0]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function newDataProviderDefinition($index, $type) |
59
|
|
|
{ |
60
|
|
|
$definition = new Definition(DataProviderInterface::class); |
61
|
|
|
$definition->addTag( |
62
|
|
|
'elasticsearch.dataprovider', |
63
|
|
|
['index' => $index, 'type' => $type] |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
return $definition; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testThrowsExceptionIfNotDataProvider() |
70
|
|
|
{ |
71
|
|
|
$this->container->setDefinition( |
72
|
|
|
'gbprod.elasticsearch_dataprovider.registry', |
73
|
|
|
$this->registryDefinition |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$definition = new Definition(\stdClass::class); |
77
|
|
|
$definition->addTag( |
78
|
|
|
'elasticsearch.dataprovider', |
79
|
|
|
['index' => 'foo', 'type' => 'bar'] |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$this->container->setDefinition( |
83
|
|
|
'data_provider.foo.bar', |
84
|
|
|
$definition |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$this->setExpectedException(\InvalidArgumentException::class); |
88
|
|
|
|
89
|
|
|
$this->testedInstance->process($this->container); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testThrowsExceptionIfBadTag() |
93
|
|
|
{ |
94
|
|
|
$this->container->setDefinition( |
95
|
|
|
'gbprod.elasticsearch_dataprovider.registry', |
96
|
|
|
$this->registryDefinition |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$definition = new Definition(DataProviderInterface::class); |
100
|
|
|
$definition->addTag( |
101
|
|
|
'elasticsearch.dataprovider', |
102
|
|
|
['type' => 'my-type'] |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$this->container->setDefinition( |
106
|
|
|
'data_provider.foo.bar', |
107
|
|
|
$definition |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$this->setExpectedException(\InvalidArgumentException::class); |
111
|
|
|
|
112
|
|
|
$this->testedInstance->process($this->container); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testNeverCallGetDefinitionIfServiceNotSet() |
116
|
|
|
{ |
117
|
|
|
$container = $this->getMock(ContainerBuilder::class); |
118
|
|
|
|
119
|
|
|
$container |
120
|
|
|
->expects($this->any()) |
121
|
|
|
->method('hasDefinition') |
122
|
|
|
->with('gbprod.elasticsearch_dataprovider.registry') |
123
|
|
|
->willReturn(false) |
124
|
|
|
; |
125
|
|
|
|
126
|
|
|
$container |
127
|
|
|
->expects($this->never()) |
128
|
|
|
->method('getDefinition') |
129
|
|
|
->with('gbprod.elasticsearch_dataprovider.registry') |
130
|
|
|
; |
131
|
|
|
|
132
|
|
|
$this->testedInstance->process($container); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|