1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Beelab\TagBundle\Tests\Listener; |
4
|
|
|
|
5
|
|
|
use Beelab\TagBundle\Listener\TagSubscriber; |
6
|
|
|
use Beelab\TagBundle\Tests\NonTaggableStub; |
7
|
|
|
use Beelab\TagBundle\Tests\TaggableStub; |
8
|
|
|
use Beelab\TagBundle\Tests\TaggableStub2; |
9
|
|
|
use Beelab\TagBundle\Tests\TaggableStub3; |
10
|
|
|
use Beelab\TagBundle\Tests\TagStub; |
11
|
|
|
use Doctrine\Common\Persistence\Mapping\MappingException as LegacyMappingException; |
|
|
|
|
12
|
|
|
use Doctrine\ORM\EntityManager; |
13
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
14
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
15
|
|
|
use Doctrine\Persistence\Mapping\MappingException; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @group unit |
20
|
|
|
*/ |
21
|
|
|
final class TagSubscriberTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
public function testNonexistentClass(): void |
24
|
|
|
{ |
25
|
|
|
if (\class_exists('Doctrine\Common\Persistence\Mapping\MappingException')) { |
26
|
|
|
$this->expectException(LegacyMappingException::class); |
27
|
|
|
} else { |
28
|
|
|
$this->expectException(MappingException::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$subscriber = new TagSubscriber('ClassDoesNotExist'); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testInvalidClass(): void |
35
|
|
|
{ |
36
|
|
|
$this->expectException(\InvalidArgumentException::class); |
37
|
|
|
|
38
|
|
|
$subscriber = new TagSubscriber(NonTaggableStub::class); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testGetSubscribedEvents(): void |
42
|
|
|
{ |
43
|
|
|
$tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock(); |
44
|
|
|
$subscriber = new TagSubscriber(\get_class($tag)); |
45
|
|
|
|
46
|
|
|
$this->assertContains('onFlush', $subscriber->getSubscribedEvents()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testOnFlush(): void |
50
|
|
|
{ |
51
|
|
|
$tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock(); |
52
|
|
|
/** @var OnFlushEventArgs&\PHPUnit\Framework\MockObject\MockObject $args */ |
53
|
|
|
$args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock(); |
54
|
|
|
$manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock(); |
55
|
|
|
$repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->getMock(); |
56
|
|
|
$uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock(); |
57
|
|
|
$metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock(); |
58
|
|
|
|
59
|
|
|
$args->expects($this->once())->method('getEntityManager')->willReturn($manager); |
60
|
|
|
$manager->expects($this->once())->method('getUnitOfWork')->willReturn($uow); |
61
|
|
|
$manager->expects($this->any())->method('getRepository')->willReturn($repo); |
62
|
|
|
$manager->expects($this->any())->method('getClassMetadata')->willReturn($metadata); |
63
|
|
|
$uow |
64
|
|
|
->expects($this->once()) |
65
|
|
|
->method('getScheduledEntityInsertions') |
66
|
|
|
->willReturn([new TaggableStub(), new NonTaggableStub()]) |
67
|
|
|
; |
68
|
|
|
$uow |
69
|
|
|
->expects($this->once()) |
70
|
|
|
->method('getScheduledEntityUpdates') |
71
|
|
|
->willReturn([new TaggableStub2()]) |
72
|
|
|
; |
73
|
|
|
$uow->expects($this->never())->method('getScheduledEntityDeletions'); |
74
|
|
|
|
75
|
|
|
$subscriber = new TagSubscriber(\get_class($tag)); |
76
|
|
|
$subscriber->onFlush($args); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testOnFlushEntityWithoutTagsUpdate(): void |
80
|
|
|
{ |
81
|
|
|
$tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock(); |
82
|
|
|
/** @var OnFlushEventArgs&\PHPUnit\Framework\MockObject\MockObject $args */ |
83
|
|
|
$args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock(); |
84
|
|
|
$manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock(); |
85
|
|
|
$uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock(); |
86
|
|
|
$metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock(); |
87
|
|
|
|
88
|
|
|
$args->expects($this->once())->method('getEntityManager')->willReturn($manager); |
89
|
|
|
$manager->expects($this->once())->method('getUnitOfWork')->willReturn($uow); |
90
|
|
|
$manager->expects($this->any())->method('getClassMetadata')->willReturn($metadata); |
91
|
|
|
$uow |
92
|
|
|
->expects($this->once()) |
93
|
|
|
->method('getScheduledEntityInsertions') |
94
|
|
|
->willReturn([]) |
95
|
|
|
; |
96
|
|
|
$uow |
97
|
|
|
->expects($this->once()) |
98
|
|
|
->method('getScheduledEntityUpdates') |
99
|
|
|
->willReturn([new TaggableStub3()]) |
100
|
|
|
; |
101
|
|
|
$uow->expects($this->never())->method('getScheduledEntityDeletions'); |
102
|
|
|
|
103
|
|
|
$subscriber = new TagSubscriber(\get_class($tag)); |
104
|
|
|
$subscriber->onFlush($args); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testOnFlushEntityWithoutTagsInsert(): void |
108
|
|
|
{ |
109
|
|
|
$tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock(); |
110
|
|
|
/** @var OnFlushEventArgs&\PHPUnit\Framework\MockObject\MockObject $args */ |
111
|
|
|
$args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock(); |
112
|
|
|
$manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock(); |
113
|
|
|
$uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock(); |
114
|
|
|
$metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock(); |
115
|
|
|
|
116
|
|
|
$args->expects($this->once())->method('getEntityManager')->willReturn($manager); |
117
|
|
|
$manager->expects($this->once())->method('getUnitOfWork')->willReturn($uow); |
118
|
|
|
$manager->expects($this->any())->method('getClassMetadata')->willReturn($metadata); |
119
|
|
|
$uow |
120
|
|
|
->expects($this->once()) |
121
|
|
|
->method('getScheduledEntityInsertions') |
122
|
|
|
->willReturn([new TaggableStub3()]) |
123
|
|
|
; |
124
|
|
|
$uow |
125
|
|
|
->expects($this->once()) |
126
|
|
|
->method('getScheduledEntityUpdates') |
127
|
|
|
->willReturn([]) |
128
|
|
|
; |
129
|
|
|
$uow->expects($this->never())->method('getScheduledEntityDeletions'); |
130
|
|
|
|
131
|
|
|
$subscriber = new TagSubscriber(\get_class($tag)); |
132
|
|
|
$subscriber->onFlush($args); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testOnFlushWithPurge(): void |
136
|
|
|
{ |
137
|
|
|
$tag = new TagStub(); |
138
|
|
|
/** @var OnFlushEventArgs&\PHPUnit\Framework\MockObject\MockObject $args */ |
139
|
|
|
$args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock(); |
140
|
|
|
$manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock(); |
141
|
|
|
$uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock(); |
142
|
|
|
|
143
|
|
|
$args->expects($this->once())->method('getEntityManager')->willReturn($manager); |
144
|
|
|
$manager->expects($this->once())->method('getUnitOfWork')->willReturn($uow); |
145
|
|
|
$uow->expects($this->once())->method('getScheduledEntityInsertions')->willReturn([]); |
146
|
|
|
$uow->expects($this->once())->method('getScheduledEntityUpdates')->willReturn([]); |
147
|
|
|
$uow |
148
|
|
|
->expects($this->once()) |
149
|
|
|
->method('getScheduledEntityDeletions') |
150
|
|
|
->willReturn([new TaggableStub()]) |
151
|
|
|
; |
152
|
|
|
|
153
|
|
|
$subscriber = new TagSubscriber(\get_class($tag), true); |
154
|
|
|
$subscriber->onFlush($args); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testSetTags(): void |
158
|
|
|
{ |
159
|
|
|
$tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock(); |
160
|
|
|
/** @var OnFlushEventArgs&\PHPUnit\Framework\MockObject\MockObject $args */ |
161
|
|
|
$args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock(); |
162
|
|
|
$manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock(); |
163
|
|
|
$uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock(); |
164
|
|
|
|
165
|
|
|
$args->expects($this->once())->method('getEntityManager')->willReturn($manager); |
166
|
|
|
$manager->expects($this->once())->method('getUnitOfWork')->willReturn($uow); |
167
|
|
|
// TODO create some stubs of taggable entities and non-taggable entities... |
168
|
|
|
$uow->expects($this->once())->method('getScheduledEntityInsertions')->willReturn([$tag]); |
169
|
|
|
$uow->expects($this->once())->method('getScheduledEntityUpdates')->willReturn([]); |
170
|
|
|
$uow->expects($this->once())->method('getScheduledEntityDeletions')->willReturn([]); |
171
|
|
|
|
172
|
|
|
$subscriber = new TagSubscriber(\get_class($tag), true); |
173
|
|
|
$subscriber->onFlush($args); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths