TagSubscriberTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 92
dl 0
loc 149
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetTags() 0 17 1
A testOnFlushEntityWithoutTagsUpdate() 0 26 1
A testGetSubscribedEvents() 0 6 1
A testOnFlushEntityWithoutTagsInsert() 0 26 1
A testOnFlush() 0 28 1
A testOnFlushWithPurge() 0 20 1
A testInvalidClass() 0 5 1
A testNonexistentClass() 0 5 1
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;
12
use Doctrine\ORM\EntityManager;
13
use Doctrine\ORM\Event\OnFlushEventArgs;
14
use Doctrine\ORM\Mapping\ClassMetadata;
15
use PHPUnit\Framework\TestCase;
16
17
/**
18
 * @group unit
19
 */
20
final class TagSubscriberTest extends TestCase
21
{
22
    public function testNonexistentClass(): void
23
    {
24
        $this->expectException(MappingException::class);
25
26
        $subscriber = new TagSubscriber('ClassDoesNotExist');
0 ignored issues
show
Unused Code introduced by Massimiliano Arione
The assignment to $subscriber is dead and can be removed.
Loading history...
27
    }
28
29
    public function testInvalidClass(): void
30
    {
31
        $this->expectException(\InvalidArgumentException::class);
32
33
        $subscriber = new TagSubscriber(NonTaggableStub::class);
0 ignored issues
show
Unused Code introduced by Massimiliano Arione
The assignment to $subscriber is dead and can be removed.
Loading history...
34
    }
35
36
    public function testGetSubscribedEvents(): void
37
    {
38
        $tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock();
39
        $subscriber = new TagSubscriber(\get_class($tag));
40
41
        $this->assertContains('onFlush', $subscriber->getSubscribedEvents());
42
    }
43
44
    public function testOnFlush(): void
45
    {
46
        $tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock();
47
        /** @var OnFlushEventArgs&\PHPUnit\Framework\MockObject\MockObject $args */
48
        $args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock();
49
        $manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
50
        $repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->getMock();
51
        $uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock();
52
        $metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
53
54
        $args->expects($this->once())->method('getEntityManager')->willReturn($manager);
55
        $manager->expects($this->once())->method('getUnitOfWork')->willReturn($uow);
56
        $manager->expects($this->any())->method('getRepository')->willReturn($repo);
57
        $manager->expects($this->any())->method('getClassMetadata')->willReturn($metadata);
58
        $uow
59
            ->expects($this->once())
60
            ->method('getScheduledEntityInsertions')
61
            ->willReturn([new TaggableStub(), new NonTaggableStub()])
62
        ;
63
        $uow
64
            ->expects($this->once())
65
            ->method('getScheduledEntityUpdates')
66
            ->willReturn([new TaggableStub2()])
67
        ;
68
        $uow->expects($this->never())->method('getScheduledEntityDeletions');
69
70
        $subscriber = new TagSubscriber(\get_class($tag));
71
        $subscriber->onFlush($args);
72
    }
73
74
    public function testOnFlushEntityWithoutTagsUpdate(): void
75
    {
76
        $tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock();
77
        /** @var OnFlushEventArgs&\PHPUnit\Framework\MockObject\MockObject $args */
78
        $args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock();
79
        $manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
80
        $uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock();
81
        $metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
82
83
        $args->expects($this->once())->method('getEntityManager')->willReturn($manager);
84
        $manager->expects($this->once())->method('getUnitOfWork')->willReturn($uow);
85
        $manager->expects($this->any())->method('getClassMetadata')->willReturn($metadata);
86
        $uow
87
            ->expects($this->once())
88
            ->method('getScheduledEntityInsertions')
89
            ->willReturn([])
90
        ;
91
        $uow
92
            ->expects($this->once())
93
            ->method('getScheduledEntityUpdates')
94
            ->willReturn([new TaggableStub3()])
95
        ;
96
        $uow->expects($this->never())->method('getScheduledEntityDeletions');
97
98
        $subscriber = new TagSubscriber(\get_class($tag));
99
        $subscriber->onFlush($args);
100
    }
101
102
    public function testOnFlushEntityWithoutTagsInsert(): void
103
    {
104
        $tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock();
105
        /** @var OnFlushEventArgs&\PHPUnit\Framework\MockObject\MockObject $args */
106
        $args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock();
107
        $manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
108
        $uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock();
109
        $metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
110
111
        $args->expects($this->once())->method('getEntityManager')->willReturn($manager);
112
        $manager->expects($this->once())->method('getUnitOfWork')->willReturn($uow);
113
        $manager->expects($this->any())->method('getClassMetadata')->willReturn($metadata);
114
        $uow
115
            ->expects($this->once())
116
            ->method('getScheduledEntityInsertions')
117
            ->willReturn([new TaggableStub3()])
118
        ;
119
        $uow
120
            ->expects($this->once())
121
            ->method('getScheduledEntityUpdates')
122
            ->willReturn([])
123
        ;
124
        $uow->expects($this->never())->method('getScheduledEntityDeletions');
125
126
        $subscriber = new TagSubscriber(\get_class($tag));
127
        $subscriber->onFlush($args);
128
    }
129
130
    public function testOnFlushWithPurge(): void
131
    {
132
        $tag = new TagStub();
133
        /** @var OnFlushEventArgs&\PHPUnit\Framework\MockObject\MockObject $args */
134
        $args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock();
135
        $manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
136
        $uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock();
137
138
        $args->expects($this->once())->method('getEntityManager')->willReturn($manager);
139
        $manager->expects($this->once())->method('getUnitOfWork')->willReturn($uow);
140
        $uow->expects($this->once())->method('getScheduledEntityInsertions')->willReturn([]);
141
        $uow->expects($this->once())->method('getScheduledEntityUpdates')->willReturn([]);
142
        $uow
143
            ->expects($this->once())
144
            ->method('getScheduledEntityDeletions')
145
            ->willReturn([new TaggableStub()])
146
        ;
147
148
        $subscriber = new TagSubscriber(\get_class($tag), true);
149
        $subscriber->onFlush($args);
150
    }
151
152
    public function testSetTags(): void
153
    {
154
        $tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock();
155
        /** @var OnFlushEventArgs&\PHPUnit\Framework\MockObject\MockObject $args */
156
        $args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock();
157
        $manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
158
        $uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock();
159
160
        $args->expects($this->once())->method('getEntityManager')->willReturn($manager);
161
        $manager->expects($this->once())->method('getUnitOfWork')->willReturn($uow);
162
        // TODO create some stubs of taggable entities and non-taggable entities...
163
        $uow->expects($this->once())->method('getScheduledEntityInsertions')->willReturn([$tag]);
164
        $uow->expects($this->once())->method('getScheduledEntityUpdates')->willReturn([]);
165
        $uow->expects($this->once())->method('getScheduledEntityDeletions')->willReturn([]);
166
167
        $subscriber = new TagSubscriber(\get_class($tag), true);
168
        $subscriber->onFlush($args);
169
    }
170
}
171