3 namespace Shaarli\Bookmark
;
5 use PHPUnit\Framework\TestCase
;
6 use Shaarli\Bookmark\Exception\InvalidBookmarkException
;
7 use Shaarli\Config\ConfigManager
;
11 * Class BookmarkArrayTest
13 class BookmarkArrayTest
extends TestCase
16 * Test the constructor and make sure that the instance is properly initialized
18 public function testArrayConstructorEmpty()
20 $array = new BookmarkArray();
21 $this->assertTrue(is_iterable($array));
22 $this->assertEmpty($array);
26 * Test adding entries to the array, specifying the key offset or not.
28 public function testArrayAccessAddEntries()
30 $array = new BookmarkArray();
31 $bookmark = new Bookmark();
32 $bookmark->setId(11)->validate();
34 $this->assertCount(1, $array);
35 $this->assertTrue(isset($array[11]));
36 $this->assertNull($array[0]);
37 $this->assertEquals($bookmark, $array[11]);
39 $bookmark = new Bookmark();
40 $bookmark->setId(14)->validate();
41 $array[14] = $bookmark;
42 $this->assertCount(2, $array);
43 $this->assertTrue(isset($array[14]));
44 $this->assertNull($array[0]);
45 $this->assertEquals($bookmark, $array[14]);
49 * Test adding a bad entry: wrong type
51 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
53 public function testArrayAccessAddBadEntryInstance()
55 $array = new BookmarkArray();
60 * Test adding a bad entry: no id
62 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
64 public function testArrayAccessAddBadEntryNoId()
66 $array = new BookmarkArray();
67 $bookmark = new Bookmark();
72 * Test adding a bad entry: no url
74 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
76 public function testArrayAccessAddBadEntryNoUrl()
78 $array = new BookmarkArray();
79 $bookmark = (new Bookmark())->setId(11);
84 * Test adding a bad entry: invalid offset
86 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
88 public function testArrayAccessAddBadEntryOffset()
90 $array = new BookmarkArray();
91 $bookmark = (new Bookmark())->setId(11);
92 $bookmark->validate();
93 $array['nope'] = $bookmark;
97 * Test adding a bad entry: invalid ID type
99 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
101 public function testArrayAccessAddBadEntryIdType()
103 $array = new BookmarkArray();
104 $bookmark = (new Bookmark())->setId('nope');
105 $bookmark->validate();
106 $array[] = $bookmark;
110 * Test adding a bad entry: ID/offset not consistent
112 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
114 public function testArrayAccessAddBadEntryIdOffset()
116 $array = new BookmarkArray();
117 $bookmark = (new Bookmark())->setId(11);
118 $bookmark->validate();
119 $array[14] = $bookmark;
123 * Test update entries through array access.
125 public function testArrayAccessUpdateEntries()
127 $array = new BookmarkArray();
128 $bookmark = new Bookmark();
129 $bookmark->setId(11)->validate();
130 $bookmark->setTitle('old');
131 $array[] = $bookmark;
132 $bookmark = new Bookmark();
133 $bookmark->setId(11)->validate();
134 $bookmark->setTitle('test');
135 $array[] = $bookmark;
136 $this->assertCount(1, $array);
137 $this->assertEquals('test', $array[11]->getTitle());
139 $bookmark = new Bookmark();
140 $bookmark->setId(11)->validate();
141 $bookmark->setTitle('test2');
142 $array[11] = $bookmark;
143 $this->assertCount(1, $array);
144 $this->assertEquals('test2', $array[11]->getTitle());
148 * Test delete entries through array access.
150 public function testArrayAccessDeleteEntries()
152 $array = new BookmarkArray();
153 $bookmark11 = new Bookmark();
154 $bookmark11->setId(11)->validate();
155 $array[] = $bookmark11;
156 $bookmark14 = new Bookmark();
157 $bookmark14->setId(14)->validate();
158 $array[] = $bookmark14;
159 $bookmark23 = new Bookmark();
160 $bookmark23->setId(23)->validate();
161 $array[] = $bookmark23;
162 $bookmark0 = new Bookmark();
163 $bookmark0->setId(0)->validate();
164 $array[] = $bookmark0;
165 $this->assertCount(4, $array);
168 $this->assertCount(3, $array);
169 $this->assertEquals($bookmark11, $array[11]);
170 $this->assertEquals($bookmark23, $array[23]);
171 $this->assertEquals($bookmark0, $array[0]);
174 $this->assertCount(2, $array);
175 $this->assertEquals($bookmark11, $array[11]);
176 $this->assertEquals($bookmark0, $array[0]);
179 $this->assertCount(1, $array);
180 $this->assertEquals($bookmark0, $array[0]);
183 $this->assertCount(0, $array);
187 * Test iterating through array access.
189 public function testArrayAccessIterate()
191 $array = new BookmarkArray();
192 $bookmark11 = new Bookmark();
193 $bookmark11->setId(11)->validate();
194 $array[] = $bookmark11;
195 $bookmark14 = new Bookmark();
196 $bookmark14->setId(14)->validate();
197 $array[] = $bookmark14;
198 $bookmark23 = new Bookmark();
199 $bookmark23->setId(23)->validate();
200 $array[] = $bookmark23;
201 $this->assertCount(3, $array);
203 foreach ($array as $id => $bookmark) {
204 $this->assertEquals(${'bookmark'. $id}, $bookmark);
209 * Test reordering the array.
211 public function testReorder()
213 $refDB = new \
ReferenceLinkDB();
214 $refDB->write('sandbox/datastore.php');
217 $bookmarks = $refDB->getLinks();
218 $bookmarks->reorder('ASC');
219 $this->assertInstanceOf(BookmarkArray
::class, $bookmarks);
221 $stickyIds = [11, 10];
222 $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41];
223 $linkIds = array_merge($stickyIds, $standardIds);
225 foreach ($bookmarks as $key => $value) {
226 $this->assertEquals($linkIds[$cpt++
], $key);
229 $bookmarks = $refDB->getLinks();
230 $bookmarks->reorder('DESC');
231 $this->assertInstanceOf(BookmarkArray
::class, $bookmarks);
233 $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds));
235 foreach ($bookmarks as $key => $value) {
236 $this->assertEquals($linkIds[$cpt++
], $key);