aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/bookmark/BookmarkArrayTest.php
blob: 0f8f04c5ad2d9e7e5aab348c4704f764f7fdacda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php

namespace Shaarli\Bookmark;

use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\Exception\InvalidBookmarkException;
use Shaarli\Config\ConfigManager;
use Shaarli\History;

/**
 * Class BookmarkArrayTest
 */
class BookmarkArrayTest extends TestCase
{
    /**
     * Test the constructor and make sure that the instance is properly initialized
     */
    public function testArrayConstructorEmpty()
    {
        $array = new BookmarkArray();
        $this->assertTrue(is_iterable($array));
        $this->assertEmpty($array);
    }

    /**
     * Test adding entries to the array, specifying the key offset or not.
     */
    public function testArrayAccessAddEntries()
    {
        $array = new BookmarkArray();
        $bookmark = new Bookmark();
        $bookmark->setId(11)->validate();
        $array[] = $bookmark;
        $this->assertCount(1, $array);
        $this->assertTrue(isset($array[11]));
        $this->assertNull($array[0]);
        $this->assertEquals($bookmark, $array[11]);

        $bookmark = new Bookmark();
        $bookmark->setId(14)->validate();
        $array[14] = $bookmark;
        $this->assertCount(2, $array);
        $this->assertTrue(isset($array[14]));
        $this->assertNull($array[0]);
        $this->assertEquals($bookmark, $array[14]);
    }

    /**
     * Test adding a bad entry: wrong type
     *
     * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
     */
    public function testArrayAccessAddBadEntryInstance()
    {
        $array = new BookmarkArray();
        $array[] = 'nope';
    }

    /**
     * Test adding a bad entry: no id
     *
     * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
     */
    public function testArrayAccessAddBadEntryNoId()
    {
        $array = new BookmarkArray();
        $bookmark = new Bookmark();
        $array[] = $bookmark;
    }

    /**
     * Test adding a bad entry: no url
     *
     * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
     */
    public function testArrayAccessAddBadEntryNoUrl()
    {
        $array = new BookmarkArray();
        $bookmark = (new Bookmark())->setId(11);
        $array[] = $bookmark;
    }

    /**
     * Test adding a bad entry: invalid offset
     *
     * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
     */
    public function testArrayAccessAddBadEntryOffset()
    {
        $array = new BookmarkArray();
        $bookmark = (new Bookmark())->setId(11);
        $bookmark->validate();
        $array['nope'] = $bookmark;
    }

    /**
     * Test adding a bad entry: invalid ID type
     *
     * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
     */
    public function testArrayAccessAddBadEntryIdType()
    {
        $array = new BookmarkArray();
        $bookmark = (new Bookmark())->setId('nope');
        $bookmark->validate();
        $array[] = $bookmark;
    }

    /**
     * Test adding a bad entry: ID/offset not consistent
     *
     * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
     */
    public function testArrayAccessAddBadEntryIdOffset()
    {
        $array = new BookmarkArray();
        $bookmark = (new Bookmark())->setId(11);
        $bookmark->validate();
        $array[14] = $bookmark;
    }

    /**
     * Test update entries through array access.
     */
    public function testArrayAccessUpdateEntries()
    {
        $array = new BookmarkArray();
        $bookmark = new Bookmark();
        $bookmark->setId(11)->validate();
        $bookmark->setTitle('old');
        $array[] = $bookmark;
        $bookmark = new Bookmark();
        $bookmark->setId(11)->validate();
        $bookmark->setTitle('test');
        $array[] = $bookmark;
        $this->assertCount(1, $array);
        $this->assertEquals('test', $array[11]->getTitle());

        $bookmark = new Bookmark();
        $bookmark->setId(11)->validate();
        $bookmark->setTitle('test2');
        $array[11] = $bookmark;
        $this->assertCount(1, $array);
        $this->assertEquals('test2', $array[11]->getTitle());
    }

    /**
     * Test delete entries through array access.
     */
    public function testArrayAccessDeleteEntries()
    {
        $array = new BookmarkArray();
        $bookmark11 = new Bookmark();
        $bookmark11->setId(11)->validate();
        $array[] = $bookmark11;
        $bookmark14 = new Bookmark();
        $bookmark14->setId(14)->validate();
        $array[] = $bookmark14;
        $bookmark23 = new Bookmark();
        $bookmark23->setId(23)->validate();
        $array[] = $bookmark23;
        $bookmark0 = new Bookmark();
        $bookmark0->setId(0)->validate();
        $array[] = $bookmark0;
        $this->assertCount(4, $array);

        unset($array[14]);
        $this->assertCount(3, $array);
        $this->assertEquals($bookmark11, $array[11]);
        $this->assertEquals($bookmark23, $array[23]);
        $this->assertEquals($bookmark0, $array[0]);

        unset($array[23]);
        $this->assertCount(2, $array);
        $this->assertEquals($bookmark11, $array[11]);
        $this->assertEquals($bookmark0, $array[0]);

        unset($array[11]);
        $this->assertCount(1, $array);
        $this->assertEquals($bookmark0, $array[0]);

        unset($array[0]);
        $this->assertCount(0, $array);
    }

    /**
     * Test iterating through array access.
     */
    public function testArrayAccessIterate()
    {
        $array = new BookmarkArray();
        $bookmark11 = new Bookmark();
        $bookmark11->setId(11)->validate();
        $array[] = $bookmark11;
        $bookmark14 = new Bookmark();
        $bookmark14->setId(14)->validate();
        $array[] = $bookmark14;
        $bookmark23 = new Bookmark();
        $bookmark23->setId(23)->validate();
        $array[] = $bookmark23;
        $this->assertCount(3, $array);

        foreach ($array as $id => $bookmark) {
            $this->assertEquals(${'bookmark'. $id}, $bookmark);
        }
    }

    /**
     * Test reordering the array.
     */
    public function testReorder()
    {
        $refDB = new \ReferenceLinkDB();
        $refDB->write('sandbox/datastore.php');


        $bookmarks = $refDB->getLinks();
        $bookmarks->reorder('ASC');
        $this->assertInstanceOf(BookmarkArray::class, $bookmarks);

        $stickyIds = [11, 10];
        $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41];
        $linkIds = array_merge($stickyIds, $standardIds);
        $cpt = 0;
        foreach ($bookmarks as $key => $value) {
            $this->assertEquals($linkIds[$cpt++], $key);
        }

        $bookmarks = $refDB->getLinks();
        $bookmarks->reorder('DESC');
        $this->assertInstanceOf(BookmarkArray::class, $bookmarks);

        $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds));
        $cpt = 0;
        foreach ($bookmarks as $key => $value) {
            $this->assertEquals($linkIds[$cpt++], $key);
        }
    }
}