Completed
Push — master ( 336c64...8fa798 )
by Łukasz
25:31
created

testLoadBookmarkDataByUserIdAndLocationId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
rs 9.6666
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->connection
45
            ->executeQuery('SELECT * FROM ezcontentbrowsebookmark WHERE id = :id', ['id' => $id])
46
            ->fetch(PDO::FETCH_ASSOC);
47
48
        $this->assertEquals([
49
            'id' => $id,
50
            'name' => 'Lorem ipsum dolor...',
51
            'node_id' => '54',
52
            'user_id' => '14',
53
        ], $data);
54
    }
55
56
    /**
57
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::deleteBookmark
58
     */
59
    public function testDeleteBookmark()
60
    {
61
        $this->getGateway()->deleteBookmark(self::EXISTING_BOOKMARK_ID);
62
63
        $data = $this->connection
64
            ->executeQuery('SELECT * FROM ezcontentbrowsebookmark WHERE id = :id', ['id' => self::EXISTING_BOOKMARK_ID])
65
            ->fetch(PDO::FETCH_ASSOC);
66
67
        $this->assertEmpty($data);
68
    }
69
70
    /**
71
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::loadBookmarkDataById
72
     */
73
    public function testLoadBookmarkDataById()
74
    {
75
        $this->assertEquals(
76
            [self::EXISTING_BOOKMARK_DATA],
77
            $this->getGateway()->loadBookmarkDataById(self::EXISTING_BOOKMARK_ID)
78
        );
79
    }
80
81
    /**
82
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::loadBookmarkDataByUserIdAndLocationId
83
     */
84
    public function testLoadBookmarkDataByUserIdAndLocationId()
85
    {
86
        $data = $this->getGateway()->loadBookmarkDataByUserIdAndLocationId(
87
            (int) self::EXISTING_BOOKMARK_DATA['user_id'],
88
            [(int) self::EXISTING_BOOKMARK_DATA['node_id']]
89
        );
90
91
        $this->assertEquals([self::EXISTING_BOOKMARK_DATA], $data);
92
    }
93
94
    /**
95
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::loadUserBookmarks
96
     * @dataProvider dataProviderForLoadUserBookmarks
97
     */
98
    public function testLoadUserBookmarks(int $userId, int $offset, int $limit, array $expected)
99
    {
100
        $this->assertEquals($expected, $this->getGateway()->loadUserBookmarks($userId, $offset, $limit));
101
    }
102
103
    /**
104
     * @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::countUserBookmarks
105
     * @dataProvider dataProviderForLoadUserBookmarks
106
     */
107
    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...
108
    {
109
        $this->assertEquals(count($expected), $this->getGateway()->countUserBookmarks($userId));
110
    }
111
112
    public function dataProviderForLoadUserBookmarks(): array
113
    {
114
        $fixtures = (require __DIR__ . '/../_fixtures/bookmarks.php')[DoctrineDatabase::TABLE_BOOKMARKS];
115
116
        $expectedRows = function ($userId) use ($fixtures) {
117
            $rows = array_filter($fixtures, function (array $row) use ($userId) {
118
                return $row['user_id'] == $userId;
119
            });
120
121
            usort($rows, function ($a, $b) {
122
                return $a['id'] < $b['id'];
123
            });
124
125
            return $rows;
126
        };
127
128
        $userId = self::EXISTING_BOOKMARK_DATA['user_id'];
129
130
        return [
131
            [
132
                $userId, 0, 10, $expectedRows($userId),
133
            ],
134
        ];
135
    }
136
137
    /**
138
     * Return a ready to test DoctrineStorage gateway.
139
     *
140
     * @return \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase
141
     */
142
    protected function getGateway(): DoctrineDatabase
143
    {
144
        return new DoctrineDatabase(
145
            $this->getDatabaseHandler()->getConnection()
146
        );
147
    }
148
}
149