1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests\Mapping; |
4
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Mapping\ContainerAwareEntityListenerResolver; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
7
|
|
|
|
8
|
|
|
class ContainerAwareEntityListenerResolverTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var ContainerAwareEntityListenerResolver |
12
|
|
|
*/ |
13
|
|
|
private $resolver; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject |
17
|
|
|
*/ |
18
|
|
|
private $container; |
19
|
|
|
|
20
|
|
|
protected function setUp() |
21
|
|
|
{ |
22
|
|
|
if (!interface_exists('\Doctrine\ORM\Mapping\EntityListenerResolver')) { |
23
|
|
|
$this->markTestSkipped('Entity listeners are not supported in this Doctrine version'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
parent::setUp(); |
27
|
|
|
|
28
|
|
|
$this->container = $this->getMockForAbstractClass('\Symfony\Component\DependencyInjection\ContainerInterface'); |
29
|
|
|
$this->resolver = new ContainerAwareEntityListenerResolver($this->container); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testResolveClass() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
35
|
|
|
$object = $this->resolver->resolve($className); |
36
|
|
|
|
37
|
|
|
$this->assertInstanceOf($className, $object); |
38
|
|
|
$this->assertSame($object, $this->resolver->resolve($className)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testRegisterClassAndResolve() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
44
|
|
|
$object = new $className(); |
45
|
|
|
|
46
|
|
|
$this->resolver->register($object); |
47
|
|
|
|
48
|
|
|
$this->assertSame($object, $this->resolver->resolve($className)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testRegisterServiceAndResolve() |
52
|
|
|
{ |
53
|
|
|
$className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
54
|
|
|
$serviceId = 'app.entity_listener'; |
55
|
|
|
$object = new $className(); |
56
|
|
|
|
57
|
|
|
$this->resolver->registerService($className, $serviceId); |
58
|
|
|
$this->container |
|
|
|
|
59
|
|
|
->expects($this->any()) |
60
|
|
|
->method('has') |
61
|
|
|
->with($serviceId) |
62
|
|
|
->will($this->returnValue(true)) |
63
|
|
|
; |
64
|
|
|
$this->container |
65
|
|
|
->expects($this->any()) |
66
|
|
|
->method('get') |
67
|
|
|
->with($serviceId) |
68
|
|
|
->will($this->returnValue($object)) |
69
|
|
|
; |
70
|
|
|
|
71
|
|
|
$this->assertInstanceOf($className, $this->resolver->resolve($className)); |
72
|
|
|
$this->assertSame($object, $this->resolver->resolve($className)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @expectedException \RuntimeException |
77
|
|
|
* @expectedExceptionMessage There is no service named |
78
|
|
|
*/ |
79
|
|
|
public function testRegisterMissingServiceAndResolve() |
80
|
|
|
{ |
81
|
|
|
$className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
82
|
|
|
$serviceId = 'app.entity_listener'; |
83
|
|
|
|
84
|
|
|
$this->resolver->registerService($className, $serviceId); |
85
|
|
|
$this->container |
|
|
|
|
86
|
|
|
->expects($this->any()) |
87
|
|
|
->method('has') |
88
|
|
|
->with($serviceId) |
89
|
|
|
->will($this->returnValue(false)) |
90
|
|
|
; |
91
|
|
|
|
92
|
|
|
$this->resolver->resolve($className); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function testClearOne() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$className1 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
98
|
|
|
$className2 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener2'; |
99
|
|
|
|
100
|
|
|
$obj1 = $this->resolver->resolve($className1); |
101
|
|
|
$obj2 = $this->resolver->resolve($className2); |
102
|
|
|
|
103
|
|
|
$this->assertInstanceOf($className1, $obj1); |
104
|
|
|
$this->assertInstanceOf($className2, $obj2); |
105
|
|
|
|
106
|
|
|
$this->assertSame($obj1, $this->resolver->resolve($className1)); |
107
|
|
|
$this->assertSame($obj2, $this->resolver->resolve($className2)); |
108
|
|
|
|
109
|
|
|
$this->resolver->clear($className1); |
110
|
|
|
|
111
|
|
|
$this->assertInstanceOf($className1, $this->resolver->resolve($className1)); |
112
|
|
|
$this->assertInstanceOf($className2, $this->resolver->resolve($className2)); |
113
|
|
|
|
114
|
|
|
$this->assertNotSame($obj1, $this->resolver->resolve($className1)); |
115
|
|
|
$this->assertSame($obj2, $this->resolver->resolve($className2)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
public function testClearAll() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$className1 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1'; |
121
|
|
|
$className2 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener2'; |
122
|
|
|
|
123
|
|
|
$obj1 = $this->resolver->resolve($className1); |
124
|
|
|
$obj2 = $this->resolver->resolve($className2); |
125
|
|
|
|
126
|
|
|
$this->assertInstanceOf($className1, $obj1); |
127
|
|
|
$this->assertInstanceOf($className2, $obj2); |
128
|
|
|
|
129
|
|
|
$this->assertSame($obj1, $this->resolver->resolve($className1)); |
130
|
|
|
$this->assertSame($obj2, $this->resolver->resolve($className2)); |
131
|
|
|
|
132
|
|
|
$this->resolver->clear(); |
133
|
|
|
|
134
|
|
|
$this->assertInstanceOf($className1, $this->resolver->resolve($className1)); |
135
|
|
|
$this->assertInstanceOf($className2, $this->resolver->resolve($className2)); |
136
|
|
|
|
137
|
|
|
$this->assertNotSame($obj1, $this->resolver->resolve($className1)); |
138
|
|
|
$this->assertNotSame($obj2, $this->resolver->resolve($className2)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @expectedException \InvalidArgumentException |
143
|
|
|
* @expectedExceptionMessage An object was expected, but got "string". |
144
|
|
|
*/ |
145
|
|
|
public function testRegisterStringException() |
146
|
|
|
{ |
147
|
|
|
$this->resolver->register('CompanyContractListener'); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
class EntityListener1 |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
class EntityListener2 |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
} |
158
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.