Completed
Push — master ( b2a1ff...4b823c )
by Łukasz
19:14
created

testLoadByUserIdAndLocationIdNonExistingBookmark()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
rs 9.4285
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Bookmark;
12
13
use eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway;
14
use eZ\Publish\Core\Persistence\Legacy\Bookmark\Handler;
15
use eZ\Publish\Core\Persistence\Legacy\Bookmark\Mapper;
16
use eZ\Publish\SPI\Persistence\Bookmark\Bookmark;
17
use eZ\Publish\SPI\Persistence\Bookmark\CreateStruct;
18
use PHPUnit\Framework\TestCase;
19
20
class HandlerTest extends TestCase
21
{
22
    const BOOKMARK_ID = 7;
23
24
    /** @var \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway|\PHPUnit\Framework\MockObject\MockObject */
25
    private $gateway;
26
27
    /** @var \eZ\Publish\Core\Persistence\Legacy\Bookmark\Mapper|\PHPUnit\Framework\MockObject\MockObject */
28
    private $mapper;
29
30
    /** @var \eZ\Publish\Core\Persistence\Legacy\Bookmark\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\Bookmark\Handler::create
42
     */
43
    public function testCreate()
44
    {
45
        $createStruct = new CreateStruct([
46
            'name' => 'Contact',
47
            'locationId' => 54,
48
            'userId' => 87,
49
        ]);
50
51
        $bookmark = new Bookmark([
52
            'name' => 'Contact',
53
            'locationId' => 54,
54
            'userId' => 87,
55
        ]);
56
57
        $this->mapper
58
            ->expects($this->once())
59
            ->method('createBookmarkFromCreateStruct')
60
            ->with($createStruct)
61
            ->willReturn($bookmark);
62
63
        $this->gateway
64
            ->expects($this->once())
65
            ->method('insertBookmark')
66
            ->with($bookmark)
67
            ->willReturn(self::BOOKMARK_ID);
68
69
        $this->handler->create($createStruct);
70
71
        $this->assertEquals($bookmark->id, self::BOOKMARK_ID);
72
    }
73
74
    /**
75
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Handler::delete
76
     */
77
    public function testDelete()
78
    {
79
        $this->gateway
80
            ->expects($this->once())
81
            ->method('deleteBookmark')
82
            ->with(self::BOOKMARK_ID);
83
84
        $this->handler->delete(self::BOOKMARK_ID);
85
    }
86
87
    /**
88
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Handler::loadByUserIdAndLocationId
89
     */
90
    public function testLoadByUserIdAndLocationIdExistingBookmark()
91
    {
92
        $userId = 87;
93
        $locationId = 54;
94
95
        $rows = [
96
            [
97
                'name' => 'Contact',
98
                'node_id' => $locationId,
99
                'user_id' => $userId,
100
            ],
101
        ];
102
103
        $object = new Bookmark([
104
            'name' => 'Contact',
105
            'locationId' => $locationId,
106
            'userId' => $userId,
107
        ]);
108
109
        $this->gateway
110
            ->expects($this->once())
111
            ->method('loadBookmarkDataByUserIdAndLocationId')
112
            ->with($userId, [$locationId])
113
            ->willReturn($rows);
114
115
        $this->mapper
116
            ->expects($this->once())
117
            ->method('extractBookmarksFromRows')
118
            ->with($rows)
119
            ->willReturn([$object]);
120
121
        $this->assertEquals([$locationId => $object], $this->handler->loadByUserIdAndLocationId($userId, [$locationId]));
122
    }
123
124
    /**
125
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Handler::loadByUserIdAndLocationId
126
     */
127
    public function testLoadByUserIdAndLocationIdNonExistingBookmark()
128
    {
129
        $userId = 87;
130
        $locationId = 54;
131
132
        $this->gateway
133
            ->expects($this->once())
134
            ->method('loadBookmarkDataByUserIdAndLocationId')
135
            ->with($userId, [$locationId])
136
            ->willReturn([]);
137
138
        $this->mapper
139
            ->expects($this->once())
140
            ->method('extractBookmarksFromRows')
141
            ->with([])
142
            ->willReturn([]);
143
144
        $this->assertEmpty($this->handler->loadByUserIdAndLocationId($userId, [$locationId]));
145
    }
146
147
    /**
148
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Handler::loadUserBookmarks
149
     */
150
    public function testLoadUserBookmarks()
151
    {
152
        $userId = 87;
153
        $offset = 50;
154
        $limit = 25;
155
156
        $rows = [
157
            [
158
                'id' => '12',
159
                'name' => 'Home',
160
                'node_id' => '2',
161
                'user_id' => $userId,
162
            ],
163
            [
164
                'id' => '75',
165
                'name' => 'Contact',
166
                'node_id' => '54',
167
                'user_id' => $userId,
168
            ],
169
        ];
170
171
        $objects = [
172
            new Bookmark([
173
                'id' => 12,
174
                'name' => 'Home',
175
                'locationId' => 2,
176
                'userId' => 78,
177
            ]),
178
            new Bookmark([
179
                'id' => 75,
180
                'name' => 'Contact',
181
                'locationId' => 54,
182
                'userId' => 87,
183
            ]),
184
        ];
185
186
        $this->gateway
187
            ->expects($this->once())
188
            ->method('loadUserBookmarks')
189
            ->with($userId, $offset, $limit)
190
            ->willReturn($rows);
191
192
        $this->mapper
193
            ->expects($this->once())
194
            ->method('extractBookmarksFromRows')
195
            ->with($rows)
196
            ->willReturn($objects);
197
198
        $this->assertEquals($objects, $this->handler->loadUserBookmarks($userId, $offset, $limit));
199
    }
200
201
    /**
202
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Handler::locationSwapped
203
     */
204
    public function testLocationSwapped()
205
    {
206
        $location1Id = 1;
207
        $location2Id = 2;
208
209
        $this->gateway
210
            ->expects($this->once())
211
            ->method('locationSwapped')
212
            ->with($location1Id, $location2Id);
213
214
        $this->handler->locationSwapped($location1Id, $location2Id);
215
    }
216
}
217