aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/bookmark/BookmarkArrayTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/bookmark/BookmarkArrayTest.php')
-rw-r--r--tests/bookmark/BookmarkArrayTest.php239
1 files changed, 239 insertions, 0 deletions
diff --git a/tests/bookmark/BookmarkArrayTest.php b/tests/bookmark/BookmarkArrayTest.php
new file mode 100644
index 00000000..0f8f04c5
--- /dev/null
+++ b/tests/bookmark/BookmarkArrayTest.php
@@ -0,0 +1,239 @@
1<?php
2
3namespace Shaarli\Bookmark;
4
5use PHPUnit\Framework\TestCase;
6use Shaarli\Bookmark\Exception\InvalidBookmarkException;
7use Shaarli\Config\ConfigManager;
8use Shaarli\History;
9
10/**
11 * Class BookmarkArrayTest
12 */
13class BookmarkArrayTest extends TestCase
14{
15 /**
16 * Test the constructor and make sure that the instance is properly initialized
17 */
18 public function testArrayConstructorEmpty()
19 {
20 $array = new BookmarkArray();
21 $this->assertTrue(is_iterable($array));
22 $this->assertEmpty($array);
23 }
24
25 /**
26 * Test adding entries to the array, specifying the key offset or not.
27 */
28 public function testArrayAccessAddEntries()
29 {
30 $array = new BookmarkArray();
31 $bookmark = new Bookmark();
32 $bookmark->setId(11)->validate();
33 $array[] = $bookmark;
34 $this->assertCount(1, $array);
35 $this->assertTrue(isset($array[11]));
36 $this->assertNull($array[0]);
37 $this->assertEquals($bookmark, $array[11]);
38
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]);
46 }
47
48 /**
49 * Test adding a bad entry: wrong type
50 *
51 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
52 */
53 public function testArrayAccessAddBadEntryInstance()
54 {
55 $array = new BookmarkArray();
56 $array[] = 'nope';
57 }
58
59 /**
60 * Test adding a bad entry: no id
61 *
62 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
63 */
64 public function testArrayAccessAddBadEntryNoId()
65 {
66 $array = new BookmarkArray();
67 $bookmark = new Bookmark();
68 $array[] = $bookmark;
69 }
70
71 /**
72 * Test adding a bad entry: no url
73 *
74 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
75 */
76 public function testArrayAccessAddBadEntryNoUrl()
77 {
78 $array = new BookmarkArray();
79 $bookmark = (new Bookmark())->setId(11);
80 $array[] = $bookmark;
81 }
82
83 /**
84 * Test adding a bad entry: invalid offset
85 *
86 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
87 */
88 public function testArrayAccessAddBadEntryOffset()
89 {
90 $array = new BookmarkArray();
91 $bookmark = (new Bookmark())->setId(11);
92 $bookmark->validate();
93 $array['nope'] = $bookmark;
94 }
95
96 /**
97 * Test adding a bad entry: invalid ID type
98 *
99 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
100 */
101 public function testArrayAccessAddBadEntryIdType()
102 {
103 $array = new BookmarkArray();
104 $bookmark = (new Bookmark())->setId('nope');
105 $bookmark->validate();
106 $array[] = $bookmark;
107 }
108
109 /**
110 * Test adding a bad entry: ID/offset not consistent
111 *
112 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
113 */
114 public function testArrayAccessAddBadEntryIdOffset()
115 {
116 $array = new BookmarkArray();
117 $bookmark = (new Bookmark())->setId(11);
118 $bookmark->validate();
119 $array[14] = $bookmark;
120 }
121
122 /**
123 * Test update entries through array access.
124 */
125 public function testArrayAccessUpdateEntries()
126 {
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());
138
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());
145 }
146
147 /**
148 * Test delete entries through array access.
149 */
150 public function testArrayAccessDeleteEntries()
151 {
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);
166
167 unset($array[14]);
168 $this->assertCount(3, $array);
169 $this->assertEquals($bookmark11, $array[11]);
170 $this->assertEquals($bookmark23, $array[23]);
171 $this->assertEquals($bookmark0, $array[0]);
172
173 unset($array[23]);
174 $this->assertCount(2, $array);
175 $this->assertEquals($bookmark11, $array[11]);
176 $this->assertEquals($bookmark0, $array[0]);
177
178 unset($array[11]);
179 $this->assertCount(1, $array);
180 $this->assertEquals($bookmark0, $array[0]);
181
182 unset($array[0]);
183 $this->assertCount(0, $array);
184 }
185
186 /**
187 * Test iterating through array access.
188 */
189 public function testArrayAccessIterate()
190 {
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);
202
203 foreach ($array as $id => $bookmark) {
204 $this->assertEquals(${'bookmark'. $id}, $bookmark);
205 }
206 }
207
208 /**
209 * Test reordering the array.
210 */
211 public function testReorder()
212 {
213 $refDB = new \ReferenceLinkDB();
214 $refDB->write('sandbox/datastore.php');
215
216
217 $bookmarks = $refDB->getLinks();
218 $bookmarks->reorder('ASC');
219 $this->assertInstanceOf(BookmarkArray::class, $bookmarks);
220
221 $stickyIds = [11, 10];
222 $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41];
223 $linkIds = array_merge($stickyIds, $standardIds);
224 $cpt = 0;
225 foreach ($bookmarks as $key => $value) {
226 $this->assertEquals($linkIds[$cpt++], $key);
227 }
228
229 $bookmarks = $refDB->getLinks();
230 $bookmarks->reorder('DESC');
231 $this->assertInstanceOf(BookmarkArray::class, $bookmarks);
232
233 $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds));
234 $cpt = 0;
235 foreach ($bookmarks as $key => $value) {
236 $this->assertEquals($linkIds[$cpt++], $key);
237 }
238 }
239}