|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests\Mapping; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Mapping\ContainerEntityListenerResolver; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
|
|
10
|
|
|
class ContainerEntityListenerResolverTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var ContainerEntityListenerResolver */ |
|
13
|
|
|
private $resolver; |
|
14
|
|
|
|
|
15
|
|
|
/** @var ContainerInterface|PHPUnit_Framework_MockObject_MockObject */ |
|
16
|
|
|
private $container; |
|
17
|
|
|
|
|
18
|
|
|
protected function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
parent::setUp(); |
|
21
|
|
|
|
|
22
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
23
|
|
|
$this->resolver = new ContainerEntityListenerResolver($this->container); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
View Code Duplication |
public function testResolveClass() |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
$className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
|
29
|
|
|
$object = $this->resolver->resolve($className); |
|
30
|
|
|
|
|
31
|
|
|
$this->assertInstanceOf($className, $object); |
|
32
|
|
|
$this->assertSame($object, $this->resolver->resolve($className)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
public function testRegisterClassAndResolve() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
|
38
|
|
|
$object = new $className(); |
|
39
|
|
|
|
|
40
|
|
|
$this->resolver->register($object); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertSame($object, $this->resolver->resolve($className)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testRegisterServiceAndResolve() |
|
46
|
|
|
{ |
|
47
|
|
|
$className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
|
48
|
|
|
$serviceId = 'app.entity_listener'; |
|
49
|
|
|
$object = new $className(); |
|
50
|
|
|
|
|
51
|
|
|
$this->resolver->registerService($className, $serviceId); |
|
52
|
|
|
$this->container |
|
|
|
|
|
|
53
|
|
|
->expects($this->any()) |
|
54
|
|
|
->method('has') |
|
55
|
|
|
->with($serviceId) |
|
56
|
|
|
->will($this->returnValue(true)); |
|
57
|
|
|
$this->container |
|
58
|
|
|
->expects($this->any()) |
|
59
|
|
|
->method('get') |
|
60
|
|
|
->with($serviceId) |
|
61
|
|
|
->will($this->returnValue($object)); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertInstanceOf($className, $this->resolver->resolve($className)); |
|
64
|
|
|
$this->assertSame($object, $this->resolver->resolve($className)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @expectedException \RuntimeException |
|
69
|
|
|
* @expectedExceptionMessage There is no service named |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testRegisterMissingServiceAndResolve() |
|
72
|
|
|
{ |
|
73
|
|
|
$className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
|
74
|
|
|
$serviceId = 'app.entity_listener'; |
|
75
|
|
|
|
|
76
|
|
|
$this->resolver->registerService($className, $serviceId); |
|
77
|
|
|
$this->container |
|
|
|
|
|
|
78
|
|
|
->expects($this->any()) |
|
79
|
|
|
->method('has') |
|
80
|
|
|
->with($serviceId) |
|
81
|
|
|
->will($this->returnValue(false)); |
|
82
|
|
|
|
|
83
|
|
|
$this->resolver->resolve($className); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
View Code Duplication |
public function testClearOne() |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
$className1 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
|
89
|
|
|
$className2 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener2'; |
|
90
|
|
|
|
|
91
|
|
|
$obj1 = $this->resolver->resolve($className1); |
|
92
|
|
|
$obj2 = $this->resolver->resolve($className2); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertInstanceOf($className1, $obj1); |
|
95
|
|
|
$this->assertInstanceOf($className2, $obj2); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertSame($obj1, $this->resolver->resolve($className1)); |
|
98
|
|
|
$this->assertSame($obj2, $this->resolver->resolve($className2)); |
|
99
|
|
|
|
|
100
|
|
|
$this->resolver->clear($className1); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertInstanceOf($className1, $this->resolver->resolve($className1)); |
|
103
|
|
|
$this->assertInstanceOf($className2, $this->resolver->resolve($className2)); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertNotSame($obj1, $this->resolver->resolve($className1)); |
|
106
|
|
|
$this->assertSame($obj2, $this->resolver->resolve($className2)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
View Code Duplication |
public function testClearAll() |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$className1 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
|
112
|
|
|
$className2 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener2'; |
|
113
|
|
|
|
|
114
|
|
|
$obj1 = $this->resolver->resolve($className1); |
|
115
|
|
|
$obj2 = $this->resolver->resolve($className2); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertInstanceOf($className1, $obj1); |
|
118
|
|
|
$this->assertInstanceOf($className2, $obj2); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertSame($obj1, $this->resolver->resolve($className1)); |
|
121
|
|
|
$this->assertSame($obj2, $this->resolver->resolve($className2)); |
|
122
|
|
|
|
|
123
|
|
|
$this->resolver->clear(); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertInstanceOf($className1, $this->resolver->resolve($className1)); |
|
126
|
|
|
$this->assertInstanceOf($className2, $this->resolver->resolve($className2)); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertNotSame($obj1, $this->resolver->resolve($className1)); |
|
129
|
|
|
$this->assertNotSame($obj2, $this->resolver->resolve($className2)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @expectedException \InvalidArgumentException |
|
134
|
|
|
* @expectedExceptionMessage An object was expected, but got "string". |
|
135
|
|
|
*/ |
|
136
|
|
|
public function testRegisterStringException() |
|
137
|
|
|
{ |
|
138
|
|
|
$this->resolver->register('CompanyContractListener'); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
class EntityListener1 |
|
143
|
|
|
{ |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
class EntityListener2 |
|
147
|
|
|
{ |
|
148
|
|
|
} |
|
149
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: