Completed
Push — master ( 7d9afb...4e0ac6 )
by Łukasz
49:46 queued 21:48
created

HandlerTest::testCreateNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Notification;
10
11
use eZ\Publish\API\Repository\Values\Notification\Notification as APINotification;
12
use eZ\Publish\Core\Persistence\Legacy\Notification\Gateway;
13
use eZ\Publish\Core\Persistence\Legacy\Notification\Mapper;
14
use eZ\Publish\Core\Persistence\Legacy\Notification\Handler;
15
use eZ\Publish\SPI\Persistence\Notification\CreateStruct;
16
use eZ\Publish\SPI\Persistence\Notification\Notification;
17
use eZ\Publish\SPI\Persistence\Notification\UpdateStruct;
18
use PHPUnit\Framework\TestCase;
19
20
class HandlerTest extends TestCase
21
{
22
    const NOTIFICATION_ID = 1;
23
24
    /** @var \eZ\Publish\Core\Persistence\Legacy\Notification\Gateway|\PHPUnit\Framework\MockObject\MockObject */
25
    private $gateway;
26
27
    /** @var \eZ\Publish\Core\Persistence\Legacy\Notification\Mapper|\PHPUnit\Framework\MockObject\MockObject */
28
    private $mapper;
29
30
    /** @var \eZ\Publish\Core\Persistence\Legacy\Notification\Handler */
31
    private $handler;
32
33
    protected function setUp()
34
    {
35
        $this->gateway = $this->createMock(Gateway::class);
36
        $this->mapper = $this->createMock(Mapper::class);
37
        $this->handler = new Handler($this->gateway, $this->mapper);
38
    }
39
40
    /**
41
     * @covers \eZ\Publish\Core\Persistence\Legacy\Notification\Handler::createNotification
42
     */
43
    public function testCreateNotification()
44
    {
45
        $createStruct = new CreateStruct([
46
            'ownerId' => 5,
47
            'type' => 'TEST',
48
            'isPending' => true,
49
            'data' => [],
50
            'created' => 0,
51
        ]);
52
53
        $this->gateway
54
            ->expects($this->once())
55
            ->method('insert')
56
            ->with($createStruct)
57
            ->willReturn(self::NOTIFICATION_ID);
58
59
        $this->mapper
60
            ->expects($this->once())
61
            ->method('extractNotificationsFromRows')
62
            ->willReturn([new Notification([
63
                'id' => self::NOTIFICATION_ID,
64
            ])]);
65
66
        $notification = $this->handler->createNotification($createStruct);
67
68
        $this->assertEquals($notification->id, self::NOTIFICATION_ID);
69
    }
70
71
    /**
72
     * @covers \eZ\Publish\Core\Persistence\Legacy\Notification\Handler::countPendingNotifications
73
     */
74 View Code Duplication
    public function testCountPendingNotifications()
75
    {
76
        $ownerId = 10;
77
        $expectedCount = 12;
78
79
        $this->gateway
80
            ->expects($this->once())
81
            ->method('countUserPendingNotifications')
82
            ->with($ownerId)
83
            ->willReturn($expectedCount);
84
85
        $this->assertEquals($expectedCount, $this->handler->countPendingNotifications($ownerId));
86
    }
87
88
    /**
89
     * @covers \eZ\Publish\Core\Persistence\Legacy\Notification\Handler::getNotificationById
90
     */
91
    public function testGetNotificationById()
92
    {
93
        $rows = [
94
            [
95
                'id' => 1, /* ... */
96
            ],
97
        ];
98
99
        $object = new Notification([
100
            'id' => 1, /* ... */
101
        ]);
102
103
        $this->gateway
104
            ->expects($this->once())
105
            ->method('getNotificationById')
106
            ->with(self::NOTIFICATION_ID)
107
            ->willReturn($rows);
108
109
        $this->mapper
110
            ->expects($this->once())
111
            ->method('extractNotificationsFromRows')
112
            ->with($rows)
113
            ->willReturn([$object]);
114
115
        $this->assertEquals($object, $this->handler->getNotificationById(self::NOTIFICATION_ID));
116
    }
117
118
    /**
119
     * @covers \eZ\Publish\Core\Persistence\Legacy\Notification\Handler::updateNotification
120
     */
121
    public function testUpdateNotification()
122
    {
123
        $updateStruct = new UpdateStruct([
124
            'isPending' => false,
125
        ]);
126
127
        $data = [
128
            'id' => self::NOTIFICATION_ID,
129
            'ownerId' => null,
130
            'isPending' => true,
131
            'type' => null,
132
            'created' => null,
133
            'data' => [],
134
        ];
135
136
        $apiNotification = new APINotification($data);
137
        $spiNotification = new Notification($data);
138
139
        $this->mapper
140
            ->expects($this->once())
141
            ->method('createNotificationFromUpdateStruct')
142
            ->with($updateStruct)
143
            ->willReturn($spiNotification);
144
145
        $this->gateway
146
            ->expects($this->once())
147
            ->method('updateNotification')
148
            ->with($spiNotification);
149
150
        $this->mapper
151
            ->expects($this->once())
152
            ->method('extractNotificationsFromRows')
153
            ->willReturn([new Notification([
154
                'id' => self::NOTIFICATION_ID,
155
            ])]);
156
157
        $this->handler->updateNotification($apiNotification, $updateStruct);
158
    }
159
160
    /**
161
     * @covers \eZ\Publish\Core\Persistence\Legacy\Notification\Handler::countNotifications
162
     */
163 View Code Duplication
    public function testCountNotifications()
164
    {
165
        $ownerId = 10;
166
        $expectedCount = 12;
167
168
        $this->gateway
169
            ->expects($this->once())
170
            ->method('countUserNotifications')
171
            ->with($ownerId)
172
            ->willReturn($expectedCount);
173
174
        $this->assertEquals($expectedCount, $this->handler->countNotifications($ownerId));
175
    }
176
177
    /**
178
     * @covers \eZ\Publish\Core\Persistence\Legacy\Notification\Handler::loadUserNotifications
179
     */
180
    public function testLoadUserNotifications()
181
    {
182
        $ownerId = 9;
183
        $limit = 5;
184
        $offset = 0;
185
186
        $rows = [
187
            ['id' => 1/* ... */],
188
            ['id' => 2/* ... */],
189
            ['id' => 3/* ... */],
190
        ];
191
192
        $objects = [
193
            new Notification(['id' => 1/* ... */]),
194
            new Notification(['id' => 2/* ... */]),
195
            new Notification(['id' => 3/* ... */]),
196
        ];
197
198
        $this->gateway
199
            ->expects($this->once())
200
            ->method('loadUserNotifications')
201
            ->with($ownerId, $offset, $limit)
202
            ->willReturn($rows);
203
204
        $this->mapper
205
            ->expects($this->once())
206
            ->method('extractNotificationsFromRows')
207
            ->with($rows)
208
            ->willReturn($objects);
209
210
        $this->assertEquals($objects, $this->handler->loadUserNotifications($ownerId, $offset, $limit));
211
    }
212
213
    public function testDelete()
214
    {
215
        $notification = new APINotification([
216
            'id' => self::NOTIFICATION_ID, /* ... */
217
        ]);
218
219
        $this->gateway
220
            ->expects($this->once())
221
            ->method('delete')
222
            ->with($notification->id);
223
224
        $this->handler->delete($notification);
225
    }
226
}
227