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