|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\GBProd\AlgoliaSpecificationBundle\DependencyInjection\Compiler; |
|
4
|
|
|
|
|
5
|
|
|
use GBProd\AlgoliaSpecification\Handler; |
|
6
|
|
|
use GBProd\AlgoliaSpecificationBundle\DependencyInjection\Compiler\QueryFactoryPass; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Tests for QueryFactoryPass |
|
13
|
|
|
* |
|
14
|
|
|
* @author gbprod <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class QueryFactoryPassTest extends \PHPUnit_Framework_TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function testThrowExceptionIfNoHandlerDefinition() |
|
19
|
|
|
{ |
|
20
|
|
|
$pass = new QueryFactoryPass(); |
|
21
|
|
|
|
|
22
|
|
|
$this->expectException(\Exception::class); |
|
23
|
|
|
|
|
24
|
|
|
$pass->process(new ContainerBuilder()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testDoNothingIfNoTaggedServices() |
|
28
|
|
|
{ |
|
29
|
|
|
$pass = new QueryFactoryPass(); |
|
30
|
|
|
$container = $this->createContainerWithHandler(); |
|
31
|
|
|
|
|
32
|
|
|
$pass->process($container); |
|
33
|
|
|
|
|
34
|
|
|
$calls = $container |
|
35
|
|
|
->getDefinition('gbprod.algolia_specification_handler') |
|
36
|
|
|
->getMethodCalls() |
|
37
|
|
|
; |
|
38
|
|
|
|
|
39
|
|
|
$this->assertEmpty($calls); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function createContainerWithHandler() |
|
43
|
|
|
{ |
|
44
|
|
|
$container = new ContainerBuilder(); |
|
45
|
|
|
|
|
46
|
|
|
$container->setDefinition( |
|
47
|
|
|
'gbprod.algolia_specification_handler', |
|
48
|
|
|
new Definition(Handler::class) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
return $container; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testThrowExceptionIfTagHasNoSpecification() |
|
55
|
|
|
{ |
|
56
|
|
|
$pass = new QueryFactoryPass(); |
|
57
|
|
|
|
|
58
|
|
|
$container = $this->createContainerWithHandler(); |
|
59
|
|
|
$container |
|
60
|
|
|
->register('factory', \stdClass::class) |
|
61
|
|
|
->addTag('algolia.query_factory') |
|
62
|
|
|
; |
|
63
|
|
|
|
|
64
|
|
|
$this->expectException(\Exception::class); |
|
65
|
|
|
$pass->process($container); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testAddMethodCalls() |
|
69
|
|
|
{ |
|
70
|
|
|
$pass = new QueryFactoryPass(); |
|
71
|
|
|
|
|
72
|
|
|
$container = $this->createContainerWithHandler(); |
|
73
|
|
|
$container |
|
74
|
|
|
->register('factory1', 'Factory1') |
|
75
|
|
|
->addTag('algolia.query_factory', ['specification' => 'Specification1']) |
|
76
|
|
|
; |
|
77
|
|
|
|
|
78
|
|
|
$container |
|
79
|
|
|
->register('factory2', 'Factory2') |
|
80
|
|
|
->addTag('algolia.query_factory', ['specification' => 'Specification2']) |
|
81
|
|
|
; |
|
82
|
|
|
|
|
83
|
|
|
$pass->process($container); |
|
84
|
|
|
|
|
85
|
|
|
$calls = $container |
|
86
|
|
|
->getDefinition('gbprod.algolia_specification_handler') |
|
87
|
|
|
->getMethodCalls() |
|
88
|
|
|
; |
|
89
|
|
|
|
|
90
|
|
|
$this->assertCount(2, $calls); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertEquals('registerFactory', $calls[0][0]); |
|
93
|
|
|
$this->assertEquals('Specification1', $calls[0][1][0]); |
|
94
|
|
|
$this->assertInstanceOf(Reference::class, $calls[0][1][1]); |
|
95
|
|
|
$this->assertEquals('factory1', $calls[0][1][1]); |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$this->assertEquals('registerFactory', $calls[1][0]); |
|
99
|
|
|
$this->assertEquals('Specification2', $calls[1][1][0]); |
|
100
|
|
|
$this->assertInstanceOf(Reference::class, $calls[1][1][1]); |
|
101
|
|
|
$this->assertEquals('factory2', $calls[1][1][1]); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|