Completed
Push — master ( 06c0f8...7da7c2 )
by Dmitry
03:39
created

IncidentEventSubscriberTest::testOnFlush()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 46
rs 8.9411
cc 1
eloc 32
nc 1
nop 0
1
<?php
2
3
namespace TonicHealthCheck\Test\Incident;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\Event\OnFlushEventArgs;
7
use Doctrine\ORM\Events;
8
use Doctrine\ORM\UnitOfWork;
9
use PHPUnit_Framework_TestCase;
10
use TonicHealthCheck\Incident\IncidentEventSubscriber;
11
use TonicHealthCheck\Incident\IncidentInterface;
12
use TonicHealthCheck\Incident\Siren\IncidentSiren;
13
use TonicHealthCheck\Incident\Siren\IncidentSirenCollection;
14
use TonicHealthCheck\Incident\Siren\NotificationType\EmailNotificationType;
15
use TonicHealthCheck\Incident\Siren\NotificationType\FileNotificationType;
16
use TonicHealthCheck\Incident\Siren\NotificationType\RequestNotificationType;
17
18
/**
19
 * Class IncidentEventSubscriber.
20
 */
21
class IncidentEventSubscriberTest extends PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var IncidentEventSubscriber;
25
     */
26
    private $incidentEventSubscriber;
27
28
    /**
29
     * @var IncidentSirenCollection;
30
     */
31
    private $incidentSirenC;
32
33
    /**
34
     * set up base dependency.
35
     */
36
    public function setUp()
37
    {
38
        $this->setIncidentSirenC(new IncidentSirenCollection());
39
40
        $this->getIncidentSirenC()->add($this->createSirenMock(FileNotificationType::class));
41
        $this->getIncidentSirenC()->add($this->createSirenMock(EmailNotificationType::class));
42
        $this->getIncidentSirenC()->add($this->createSirenMock(RequestNotificationType::class));
43
44
        $this->setIncidentEventSubscriber(
45
            new IncidentEventSubscriber($this->getIncidentSirenC())
46
        );
47
    }
48
49
    /**
50
     * Test onFlush.
51
     */
52
    public function testOnFlush()
53
    {
54
        $argsMock = $this->createEventArgsMock(OnFlushEventArgs::class);
55
56
        $entity = $this->getMockBuilder(IncidentInterface::class)->getMock();
57
58
        $entityMinor = $this->getMockBuilder(IncidentInterface::class)->getMock();
59
60
        $entityMinor
61
            ->expects($this->any())
62
            ->method('getType')
63
            ->willReturn(IncidentInterface::TYPE_MINOR);
64
65
66
        $unitOfWork = $this->getMockBuilder(UnitOfWork::class)
67
            ->disableOriginalConstructor()
68
            ->getMock();
69
70
        $unitOfWork
71
            ->expects($this->once())
72
            ->method('getScheduledEntityUpdates')
73
            ->willReturn([$entity, $entityMinor]);
74
75
        $unitOfWork
76
            ->expects($this->any())
77
            ->method('getEntityChangeSet')
78
            ->willReturn(['status' => 234]);
79
80
        $entityManager = $this->getMockBuilder(EntityManager::class)
81
            ->disableOriginalConstructor()
82
            ->getMock();
83
84
        $entityManager
85
            ->expects($this->once())
86
            ->method('getUnitOfWork')
87
            ->willReturn($unitOfWork);
88
89
        $argsMock
90
            ->expects($this->once())
91
            ->method('getEntityManager')
92
            ->willReturn($entityManager);
93
94
        $this->setUpExpectsForEntity($entity);
95
96
        $this->getIncidentEventSubscriber()->onFlush($argsMock);
97
    }
98
99
    /**
100
     * Test get subscribed events.
101
     */
102
    public function testGetSubscribedEvents()
103
    {
104
        $this->assertEquals(
105
            $this->getIncidentEventSubscriber()->getSubscribedEvents(),
106
            [
107
                Events::onFlush,
108
            ]
109
        );
110
    }
111
112
    /**
113
     * @return IncidentEventSubscriber
114
     */
115
    protected function getIncidentEventSubscriber()
116
    {
117
        return $this->incidentEventSubscriber;
118
    }
119
120
    /**
121
     * @param IncidentEventSubscriber $incidentEventSub
122
     */
123
    protected function setIncidentEventSubscriber(IncidentEventSubscriber $incidentEventSub)
124
    {
125
        $this->incidentEventSubscriber = $incidentEventSub;
126
    }
127
128
    /**
129
     * @return IncidentSirenCollection
130
     */
131
    protected function getIncidentSirenC()
132
    {
133
        return $this->incidentSirenC;
134
    }
135
136
    /**
137
     * @param IncidentSirenCollection $incidentSirenC
138
     */
139
    protected function setIncidentSirenC(IncidentSirenCollection $incidentSirenC)
140
    {
141
        $this->incidentSirenC = $incidentSirenC;
142
    }
143
144
    /**
145
     * @param $sirenNType
146
     *
147
     * @return \PHPUnit_Framework_MockObject_MockObject|IncidentSiren
148
     */
149
    private function createSirenMock($sirenNType)
150
    {
151
        $incidentSiren = $this->getMockBuilder(IncidentSiren::class)
152
            ->disableOriginalConstructor()->getMock();
153
154
        $incidentSiren
155
            ->expects($this->any())
156
            ->method('getNotificationTypeI')
157
            ->willReturn(
158
                $this
159
                    ->getMockBuilder($sirenNType)
160
                    ->disableOriginalConstructor()
161
                    ->getMock()
162
            );
163
164
        return $incidentSiren;
165
    }
166
167
    /**
168
     * @param $eventArgsClassName
169
     *
170
     * @return \PHPUnit_Framework_MockObject_MockObject
171
     */
172
    protected function createEventArgsMock($eventArgsClassName)
173
    {
174
        $argsMock = $this->getMockBuilder($eventArgsClassName)
175
            ->disableOriginalConstructor()
176
            ->getMock();
177
178
        return $argsMock;
179
    }
180
181
    /**
182
     * @param $entity
183
     */
184
    protected function setUpExpectsForEntity($entity)
185
    {
186
        $entity
187
            ->expects($this->any())
188
            ->method('getId')
189
            ->willReturn(null);
190
191
        $entity->expects($this->once())->method('notify');
192
        $entity
193
            ->expects($this->any())
194
            ->method('getType')
195
            ->willReturn(IncidentInterface::TYPE_URGENT);
196
    }
197
}
198