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

DoctrineDatabaseTest::testLoadUserBookmarks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
rs 10
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\Bookmark\Gateway;
10
11
use eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase;
12
use eZ\Publish\Core\Persistence\Legacy\Tests\TestCase;
13
use eZ\Publish\SPI\Persistence\Bookmark\Bookmark;
14
use PDO;
15
16
class DoctrineDatabaseTest extends TestCase
17
{
18
    const EXISTING_BOOKMARK_ID = 1;
19
    const EXISTING_BOOKMARK_DATA = [
20
        'id' => '1',
21
        'name' => 'Lorem ipsum dolor',
22
        'node_id' => '5',
23
        'user_id' => '14',
24
    ];
25
26
    public function setUp()
27
    {
28
        parent::setUp();
29
30
        $this->insertDatabaseFixture(__DIR__ . '/../_fixtures/bookmarks.php');
31
    }
32
33
    /**
34
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::insertBookmark
35
     */
36
    public function testInsertBookmark()
37
    {
38
        $id = $this->getGateway()->insertBookmark(new Bookmark([
39
            'userId' => 14,
40
            'locationId' => 54,
41
            'name' => 'Lorem ipsum dolor...',
42
        ]));
43
44
        $data = $this->loadBookmark($id);
45
46
        $this->assertEquals([
47
            'id' => $id,
48
            'name' => 'Lorem ipsum dolor...',
49
            'node_id' => '54',
50
            'user_id' => '14',
51
        ], $data);
52
    }
53
54
    /**
55
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::deleteBookmark
56
     */
57
    public function testDeleteBookmark()
58
    {
59
        $this->getGateway()->deleteBookmark(self::EXISTING_BOOKMARK_ID);
60
61
        $this->assertEmpty($this->loadBookmark(self::EXISTING_BOOKMARK_ID));
62
    }
63
64
    /**
65
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::loadBookmarkDataById
66
     */
67
    public function testLoadBookmarkDataById()
68
    {
69
        $this->assertEquals(
70
            [self::EXISTING_BOOKMARK_DATA],
71
            $this->getGateway()->loadBookmarkDataById(self::EXISTING_BOOKMARK_ID)
72
        );
73
    }
74
75
    /**
76
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::loadBookmarkDataByUserIdAndLocationId
77
     */
78
    public function testLoadBookmarkDataByUserIdAndLocationId()
79
    {
80
        $data = $this->getGateway()->loadBookmarkDataByUserIdAndLocationId(
81
            (int) self::EXISTING_BOOKMARK_DATA['user_id'],
82
            [(int) self::EXISTING_BOOKMARK_DATA['node_id']]
83
        );
84
85
        $this->assertEquals([self::EXISTING_BOOKMARK_DATA], $data);
86
    }
87
88
    /**
89
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::loadUserBookmarks
90
     * @dataProvider dataProviderForLoadUserBookmarks
91
     */
92
    public function testLoadUserBookmarks(int $userId, int $offset, int $limit, array $expected)
93
    {
94
        $this->assertEquals($expected, $this->getGateway()->loadUserBookmarks($userId, $offset, $limit));
95
    }
96
97
    /**
98
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::countUserBookmarks
99
     * @dataProvider dataProviderForLoadUserBookmarks
100
     */
101
    public function testCountUserBookmarks(int $userId, int $offset, int $limit, array $expected)
0 ignored issues
show
Unused Code introduced by
The parameter $offset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $limit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
        $this->assertEquals(count($expected), $this->getGateway()->countUserBookmarks($userId));
104
    }
105
106
    public function dataProviderForLoadUserBookmarks(): array
107
    {
108
        $fixtures = (require __DIR__ . '/../_fixtures/bookmarks.php')[DoctrineDatabase::TABLE_BOOKMARKS];
109
110
        $expectedRows = function ($userId) use ($fixtures) {
111
            $rows = array_filter($fixtures, function (array $row) use ($userId) {
112
                return $row['user_id'] == $userId;
113
            });
114
115
            usort($rows, function ($a, $b) {
116
                return $a['id'] < $b['id'];
117
            });
118
119
            return $rows;
120
        };
121
122
        $userId = self::EXISTING_BOOKMARK_DATA['user_id'];
123
124
        return [
125
            [
126
                $userId, 0, 10, $expectedRows($userId),
127
            ],
128
        ];
129
    }
130
131
    /**
132
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::locationSwapped
133
     */
134
    public function testLocationSwapped()
135
    {
136
        $bookmark1Id = 3;
137
        $bookmark2Id = 4;
138
139
        $bookmark1BeforeSwap = $this->loadBookmark($bookmark1Id);
140
        $bookmark2BeforeSwap = $this->loadBookmark($bookmark2Id);
141
142
        $this->getGateway()->locationSwapped(
143
            (int) $bookmark1BeforeSwap['node_id'],
144
            (int) $bookmark2BeforeSwap['node_id']
145
        );
146
147
        $bookmark1AfterSwap = $this->loadBookmark($bookmark1Id);
148
        $bookmark2AfterSwap = $this->loadBookmark($bookmark2Id);
149
150
        $this->assertEquals($bookmark1BeforeSwap['node_id'], $bookmark2AfterSwap['node_id']);
151
        $this->assertEquals($bookmark2BeforeSwap['node_id'], $bookmark1AfterSwap['node_id']);
152
    }
153
154
    /**
155
     * Return a ready to test DoctrineStorage gateway.
156
     *
157
     * @return \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase
158
     */
159
    protected function getGateway(): DoctrineDatabase
160
    {
161
        return new DoctrineDatabase(
162
            $this->getDatabaseHandler()->getConnection()
163
        );
164
    }
165
166
    private function loadBookmark(int $id): array
167
    {
168
        $data = $this->connection
169
            ->executeQuery('SELECT * FROM ezcontentbrowsebookmark WHERE id = :id', ['id' => $id])
170
            ->fetch(PDO::FETCH_ASSOC);
171
172
        return is_array($data) ? $data : [];
173
    }
174
}
175