|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Tactician\Bundle\Tests\DependencyInjection\Compiler; |
|
4
|
|
|
|
|
5
|
|
|
use League\Tactician\Bundle\DependencyInjection\Compiler\DoctrineMiddlewarePass; |
|
6
|
|
|
use League\Tactician\Doctrine\ORM\TransactionMiddleware; |
|
7
|
|
|
use Mockery\MockInterface; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
12
|
|
|
|
|
13
|
|
|
class DoctrineMiddlewarePassTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ContainerBuilder|MockInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $container; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var DoctrineMiddlewarePass |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $compiler; |
|
24
|
|
|
|
|
25
|
|
|
protected function setUp() |
|
26
|
|
|
{ |
|
27
|
|
|
parent::setUp(); |
|
28
|
|
|
$this->container = \Mockery::mock(ContainerBuilder::class); |
|
29
|
|
|
|
|
30
|
|
|
$this->compiler = new DoctrineMiddlewarePass(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testProcess() |
|
34
|
|
|
{ |
|
35
|
|
|
if (!class_exists(TransactionMiddleware::class)) { |
|
36
|
|
|
$this->markTestSkipped('"league/tactician-doctrine" is not installed'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->container->shouldReceive('hasParameter') |
|
|
|
|
|
|
40
|
|
|
->with('doctrine.entity_managers') |
|
41
|
|
|
->once() |
|
42
|
|
|
->andReturn(true); |
|
43
|
|
|
|
|
44
|
|
|
$this->container->shouldReceive('getParameter') |
|
45
|
|
|
->with('doctrine.entity_managers') |
|
46
|
|
|
->once() |
|
47
|
|
|
->andReturn([ |
|
48
|
|
|
'default' => 'doctrine.orm.default_entity_manager', |
|
49
|
|
|
'second' => 'doctrine.orm.second_entity_manager', |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
$this->container->shouldReceive('getParameter') |
|
53
|
|
|
->with('doctrine.default_entity_manager') |
|
54
|
|
|
->once() |
|
55
|
|
|
->andReturn('default'); |
|
56
|
|
|
|
|
57
|
|
|
$this->container->shouldReceive('setDefinition') |
|
58
|
|
|
->andReturnUsing(function($name, Definition $def) { |
|
59
|
|
|
$this->assertEquals('tactician.middleware.doctrine.default', $name); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertEquals(TransactionMiddleware::class, $def->getClass()); |
|
62
|
|
|
$this->assertCount(1, $def->getArguments()); |
|
63
|
|
|
$this->assertInstanceOf(Reference::class, $def->getArgument(0)); |
|
64
|
|
|
$this->assertEquals('doctrine.orm.default_entity_manager', (string)$def->getArgument(0)); |
|
65
|
|
|
}) |
|
66
|
|
|
->once(); |
|
67
|
|
|
|
|
68
|
|
|
$this->container->shouldReceive('setDefinition') |
|
|
|
|
|
|
69
|
|
|
->andReturnUsing(function($name, Definition $def) { |
|
70
|
|
|
$this->assertEquals('tactician.middleware.doctrine.second', $name); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertEquals(TransactionMiddleware::class, $def->getClass()); |
|
73
|
|
|
$this->assertCount(1, $def->getArguments()); |
|
74
|
|
|
$this->assertInstanceOf(Reference::class, $def->getArgument(0)); |
|
75
|
|
|
$this->assertEquals('doctrine.orm.second_entity_manager', (string)$def->getArgument(0)); |
|
76
|
|
|
}) |
|
77
|
|
|
->once(); |
|
78
|
|
|
|
|
79
|
|
|
$this->container->shouldReceive('setDefinition') |
|
|
|
|
|
|
80
|
|
|
->with('tactician.middleware.doctrine.second') |
|
81
|
|
|
->once(); |
|
82
|
|
|
|
|
83
|
|
|
$this->container->shouldReceive('setAlias') |
|
84
|
|
|
->once() |
|
85
|
|
|
->with('tactician.middleware.doctrine', 'tactician.middleware.doctrine.default'); |
|
86
|
|
|
|
|
87
|
|
|
$this->compiler->process($this->container); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testDoNotProcessWhenThereAreNoEntityManagers() |
|
91
|
|
|
{ |
|
92
|
|
|
if (!class_exists(TransactionMiddleware::class)) { |
|
93
|
|
|
$this->markTestSkipped('"league/tactician-doctrine" is not installed'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->container->shouldReceive('hasParameter') |
|
|
|
|
|
|
97
|
|
|
->with('doctrine.entity_managers') |
|
98
|
|
|
->once() |
|
99
|
|
|
->andReturn(false); |
|
100
|
|
|
|
|
101
|
|
|
$this->container->shouldNotReceive('getParameter') |
|
|
|
|
|
|
102
|
|
|
->withAnyArgs(); |
|
103
|
|
|
|
|
104
|
|
|
$this->container->shouldNotReceive('setDefinition') |
|
|
|
|
|
|
105
|
|
|
->withAnyArgs(); |
|
106
|
|
|
|
|
107
|
|
|
$this->container->shouldNotReceive('setAlias') |
|
|
|
|
|
|
108
|
|
|
->withAnyArgs(); |
|
109
|
|
|
|
|
110
|
|
|
$this->compiler->process($this->container); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testDoNotProcessWhenTacticianDoctrineIsNotInstalled() |
|
114
|
|
|
{ |
|
115
|
|
|
if (class_exists(TransactionMiddleware::class)) { |
|
116
|
|
|
$this->markTestSkipped('"league/tactician-doctrine" is installed'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->container->shouldReceive('hasParameter') |
|
|
|
|
|
|
120
|
|
|
->with('doctrine.entity_managers') |
|
121
|
|
|
->andReturn(true); |
|
122
|
|
|
|
|
123
|
|
|
$this->container->shouldNotReceive('getParameter') |
|
|
|
|
|
|
124
|
|
|
->withAnyArgs(); |
|
125
|
|
|
|
|
126
|
|
|
$this->container->shouldNotReceive('setDefinition') |
|
|
|
|
|
|
127
|
|
|
->withAnyArgs(); |
|
128
|
|
|
|
|
129
|
|
|
$this->container->shouldNotReceive('setAlias') |
|
|
|
|
|
|
130
|
|
|
->withAnyArgs(); |
|
131
|
|
|
|
|
132
|
|
|
$this->compiler->process($this->container); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: