Completed
Push — master ( b9b9e5...bada62 )
by Dmitry
02:47
created

IncidentEventSubscriberTest::testPreUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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