1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Beelab\TagBundle\Tests\Listener; |
4
|
|
|
|
5
|
|
|
use Beelab\TagBundle\Listener\TagSubscriber; |
6
|
|
|
use Beelab\TagBundle\Test\NonTaggableStub; |
7
|
|
|
use Beelab\TagBundle\Test\TagStub; |
8
|
|
|
use Beelab\TagBundle\Test\TaggableStub; |
9
|
|
|
use Beelab\TagBundle\Test\TaggableStub2; |
10
|
|
|
use Beelab\TagBundle\Test\TaggableStub3; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @group unit |
14
|
|
|
*/ |
15
|
|
|
class TagSubscriberTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @expectedException Doctrine\Common\Persistence\Mapping\MappingException |
19
|
|
|
*/ |
20
|
|
|
public function testNonexistentClass() |
21
|
|
|
{ |
22
|
|
|
$subscriber = new TagSubscriber('ClassDoesNotExist'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @expectedException InvalidArgumentException |
27
|
|
|
*/ |
28
|
|
|
public function testInvalidClass() |
29
|
|
|
{ |
30
|
|
|
$subscriber = new TagSubscriber('Beelab\TagBundle\Test\NonTaggableStub'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testGetSubscribedEvents() |
34
|
|
|
{ |
35
|
|
|
$tag = $this->getMock('Beelab\TagBundle\Tag\TagInterface'); |
36
|
|
|
$subscriber = new TagSubscriber(get_class($tag)); |
37
|
|
|
|
38
|
|
|
$this->assertContains('onFlush', $subscriber->getSubscribedEvents()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testOnFlush() |
42
|
|
|
{ |
43
|
|
|
$tag = $this->getMock('Beelab\TagBundle\Tag\TagInterface'); |
44
|
|
|
$args = $this->getMockBuilder('Doctrine\ORM\Event\OnFlushEventArgs')->disableOriginalConstructor()->getMock(); |
45
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
46
|
|
|
$repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->getMock(); |
47
|
|
|
$uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock(); |
48
|
|
|
$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock(); |
49
|
|
|
$tagsCollection = $this->getMockBuilder('Doctrine\Common\Collections\Collection')->disableOriginalConstructor()->getMock(); |
50
|
|
|
|
51
|
|
|
$args->expects($this->once())->method('getEntityManager')->will($this->returnValue($em)); |
52
|
|
|
$em->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow)); |
53
|
|
|
$em->expects($this->any())->method('getRepository')->will($this->returnValue($repo)); |
54
|
|
|
$em->expects($this->any())->method('getClassMetadata')->will($this->returnValue($metadata)); |
55
|
|
|
$uow |
56
|
|
|
->expects($this->once()) |
57
|
|
|
->method('getScheduledEntityInsertions') |
58
|
|
|
->will($this->returnValue(array(new TaggableStub(), new NonTaggableStub()))) |
59
|
|
|
; |
60
|
|
|
$uow |
61
|
|
|
->expects($this->once()) |
62
|
|
|
->method('getScheduledEntityUpdates') |
63
|
|
|
->will($this->returnValue(array(new TaggableStub2()))) |
64
|
|
|
; |
65
|
|
|
$uow->expects($this->never())->method('getScheduledEntityDeletions'); |
66
|
|
|
$tag->expects($this->any())->method('getTags')->will($this->returnValue($tagsCollection)); |
67
|
|
|
|
68
|
|
|
$subscriber = new TagSubscriber(get_class($tag)); |
69
|
|
|
$subscriber->onFlush($args); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testOnFlushEntityWithoutTagsUpdate() |
73
|
|
|
{ |
74
|
|
|
$tag = $this->getMock('Beelab\TagBundle\Tag\TagInterface'); |
75
|
|
|
$args = $this->getMockBuilder('Doctrine\ORM\Event\OnFlushEventArgs')->disableOriginalConstructor()->getMock(); |
76
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
77
|
|
|
$uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock(); |
78
|
|
|
$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock(); |
79
|
|
|
|
80
|
|
|
$args->expects($this->once())->method('getEntityManager')->will($this->returnValue($em)); |
81
|
|
|
$em->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow)); |
82
|
|
|
$em->expects($this->any())->method('getClassMetadata')->will($this->returnValue($metadata)); |
83
|
|
|
$uow |
84
|
|
|
->expects($this->once()) |
85
|
|
|
->method('getScheduledEntityInsertions') |
86
|
|
|
->will($this->returnValue(array())) |
87
|
|
|
; |
88
|
|
|
$uow |
89
|
|
|
->expects($this->once()) |
90
|
|
|
->method('getScheduledEntityUpdates') |
91
|
|
|
->will($this->returnValue(array(new TaggableStub3()))) |
92
|
|
|
; |
93
|
|
|
$uow->expects($this->never())->method('getScheduledEntityDeletions'); |
94
|
|
|
|
95
|
|
|
$subscriber = new TagSubscriber(get_class($tag)); |
96
|
|
|
$subscriber->onFlush($args); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testOnFlushEntityWithoutTagsInsert() |
100
|
|
|
{ |
101
|
|
|
$tag = $this->getMock('Beelab\TagBundle\Tag\TagInterface'); |
102
|
|
|
$args = $this->getMockBuilder('Doctrine\ORM\Event\OnFlushEventArgs')->disableOriginalConstructor()->getMock(); |
103
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
104
|
|
|
$uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock(); |
105
|
|
|
$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock(); |
106
|
|
|
|
107
|
|
|
$args->expects($this->once())->method('getEntityManager')->will($this->returnValue($em)); |
108
|
|
|
$em->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow)); |
109
|
|
|
$em->expects($this->any())->method('getClassMetadata')->will($this->returnValue($metadata)); |
110
|
|
|
$uow |
111
|
|
|
->expects($this->once()) |
112
|
|
|
->method('getScheduledEntityInsertions') |
113
|
|
|
->will($this->returnValue(array(new TaggableStub3()))) |
114
|
|
|
; |
115
|
|
|
$uow |
116
|
|
|
->expects($this->once()) |
117
|
|
|
->method('getScheduledEntityUpdates') |
118
|
|
|
->will($this->returnValue(array())) |
119
|
|
|
; |
120
|
|
|
$uow->expects($this->never())->method('getScheduledEntityDeletions'); |
121
|
|
|
|
122
|
|
|
$subscriber = new TagSubscriber(get_class($tag)); |
123
|
|
|
$subscriber->onFlush($args); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testOnFlushWithPurge() |
127
|
|
|
{ |
128
|
|
|
$tag = new TagStub(); |
129
|
|
|
$args = $this->getMockBuilder('Doctrine\ORM\Event\OnFlushEventArgs')->disableOriginalConstructor()->getMock(); |
130
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
131
|
|
|
$uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock(); |
132
|
|
|
|
133
|
|
|
$args->expects($this->once())->method('getEntityManager')->will($this->returnValue($em)); |
134
|
|
|
$em->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow)); |
135
|
|
|
$uow->expects($this->once())->method('getScheduledEntityInsertions')->will($this->returnValue(array())); |
136
|
|
|
$uow->expects($this->once())->method('getScheduledEntityUpdates')->will($this->returnValue(array())); |
137
|
|
|
$uow |
138
|
|
|
->expects($this->once()) |
139
|
|
|
->method('getScheduledEntityDeletions') |
140
|
|
|
->will($this->returnValue(array(new TaggableStub()))) |
141
|
|
|
; |
142
|
|
|
|
143
|
|
|
$subscriber = new TagSubscriber(get_class($tag), true); |
144
|
|
|
$subscriber->onFlush($args); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testSetTags() |
148
|
|
|
{ |
149
|
|
|
$tag = $this->getMock('Beelab\TagBundle\Tag\TagInterface'); |
150
|
|
|
$args = $this->getMockBuilder('Doctrine\ORM\Event\OnFlushEventArgs')->disableOriginalConstructor()->getMock(); |
151
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
152
|
|
|
$uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock(); |
153
|
|
|
|
154
|
|
|
$args->expects($this->once())->method('getEntityManager')->will($this->returnValue($em)); |
155
|
|
|
$em->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow)); |
156
|
|
|
// TODO create some stubs of taggable entities and non-taggable entities... |
157
|
|
|
$uow->expects($this->once())->method('getScheduledEntityInsertions')->will($this->returnValue(array($tag))); |
158
|
|
|
$uow->expects($this->once())->method('getScheduledEntityUpdates')->will($this->returnValue(array())); |
159
|
|
|
$uow->expects($this->once())->method('getScheduledEntityDeletions')->will($this->returnValue(array())); |
160
|
|
|
|
161
|
|
|
$subscriber = new TagSubscriber(get_class($tag), true); |
162
|
|
|
$subscriber->onFlush($args); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|