]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Shaarli\Bookmark; | |
4 | ||
5 | use Shaarli\TestCase; | |
6 | ||
7 | /** | |
8 | * Class BookmarkArrayTest | |
9 | */ | |
10 | class BookmarkArrayTest extends TestCase | |
11 | { | |
12 | /** | |
13 | * Test the constructor and make sure that the instance is properly initialized | |
14 | */ | |
15 | public function testArrayConstructorEmpty() | |
16 | { | |
17 | $array = new BookmarkArray(); | |
18 | $this->assertTrue(is_iterable($array)); | |
19 | $this->assertEmpty($array); | |
20 | } | |
21 | ||
22 | /** | |
23 | * Test adding entries to the array, specifying the key offset or not. | |
24 | */ | |
25 | public function testArrayAccessAddEntries() | |
26 | { | |
27 | $array = new BookmarkArray(); | |
28 | $bookmark = new Bookmark(); | |
29 | $bookmark->setId(11)->validate(); | |
30 | $array[] = $bookmark; | |
31 | $this->assertCount(1, $array); | |
32 | $this->assertTrue(isset($array[11])); | |
33 | $this->assertNull($array[0]); | |
34 | $this->assertEquals($bookmark, $array[11]); | |
35 | ||
36 | $bookmark = new Bookmark(); | |
37 | $bookmark->setId(14)->validate(); | |
38 | $array[14] = $bookmark; | |
39 | $this->assertCount(2, $array); | |
40 | $this->assertTrue(isset($array[14])); | |
41 | $this->assertNull($array[0]); | |
42 | $this->assertEquals($bookmark, $array[14]); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Test adding a bad entry: wrong type | |
47 | */ | |
48 | public function testArrayAccessAddBadEntryInstance() | |
49 | { | |
50 | $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); | |
51 | ||
52 | $array = new BookmarkArray(); | |
53 | $array[] = 'nope'; | |
54 | } | |
55 | ||
56 | /** | |
57 | * Test adding a bad entry: no id | |
58 | */ | |
59 | public function testArrayAccessAddBadEntryNoId() | |
60 | { | |
61 | $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); | |
62 | ||
63 | $array = new BookmarkArray(); | |
64 | $bookmark = new Bookmark(); | |
65 | $array[] = $bookmark; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Test adding a bad entry: no url | |
70 | */ | |
71 | public function testArrayAccessAddBadEntryNoUrl() | |
72 | { | |
73 | $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); | |
74 | ||
75 | $array = new BookmarkArray(); | |
76 | $bookmark = (new Bookmark())->setId(11); | |
77 | $array[] = $bookmark; | |
78 | } | |
79 | ||
80 | /** | |
81 | * Test adding a bad entry: invalid offset | |
82 | */ | |
83 | public function testArrayAccessAddBadEntryOffset() | |
84 | { | |
85 | $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); | |
86 | ||
87 | $array = new BookmarkArray(); | |
88 | $bookmark = (new Bookmark())->setId(11); | |
89 | $bookmark->validate(); | |
90 | $array['nope'] = $bookmark; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Test adding a bad entry: ID/offset not consistent | |
95 | */ | |
96 | public function testArrayAccessAddBadEntryIdOffset() | |
97 | { | |
98 | $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); | |
99 | ||
100 | $array = new BookmarkArray(); | |
101 | $bookmark = (new Bookmark())->setId(11); | |
102 | $bookmark->validate(); | |
103 | $array[14] = $bookmark; | |
104 | } | |
105 | ||
106 | /** | |
107 | * Test update entries through array access. | |
108 | */ | |
109 | public function testArrayAccessUpdateEntries() | |
110 | { | |
111 | $array = new BookmarkArray(); | |
112 | $bookmark = new Bookmark(); | |
113 | $bookmark->setId(11)->validate(); | |
114 | $bookmark->setTitle('old'); | |
115 | $array[] = $bookmark; | |
116 | $bookmark = new Bookmark(); | |
117 | $bookmark->setId(11)->validate(); | |
118 | $bookmark->setTitle('test'); | |
119 | $array[] = $bookmark; | |
120 | $this->assertCount(1, $array); | |
121 | $this->assertEquals('test', $array[11]->getTitle()); | |
122 | ||
123 | $bookmark = new Bookmark(); | |
124 | $bookmark->setId(11)->validate(); | |
125 | $bookmark->setTitle('test2'); | |
126 | $array[11] = $bookmark; | |
127 | $this->assertCount(1, $array); | |
128 | $this->assertEquals('test2', $array[11]->getTitle()); | |
129 | } | |
130 | ||
131 | /** | |
132 | * Test delete entries through array access. | |
133 | */ | |
134 | public function testArrayAccessDeleteEntries() | |
135 | { | |
136 | $array = new BookmarkArray(); | |
137 | $bookmark11 = new Bookmark(); | |
138 | $bookmark11->setId(11)->validate(); | |
139 | $array[] = $bookmark11; | |
140 | $bookmark14 = new Bookmark(); | |
141 | $bookmark14->setId(14)->validate(); | |
142 | $array[] = $bookmark14; | |
143 | $bookmark23 = new Bookmark(); | |
144 | $bookmark23->setId(23)->validate(); | |
145 | $array[] = $bookmark23; | |
146 | $bookmark0 = new Bookmark(); | |
147 | $bookmark0->setId(0)->validate(); | |
148 | $array[] = $bookmark0; | |
149 | $this->assertCount(4, $array); | |
150 | ||
151 | unset($array[14]); | |
152 | $this->assertCount(3, $array); | |
153 | $this->assertEquals($bookmark11, $array[11]); | |
154 | $this->assertEquals($bookmark23, $array[23]); | |
155 | $this->assertEquals($bookmark0, $array[0]); | |
156 | ||
157 | unset($array[23]); | |
158 | $this->assertCount(2, $array); | |
159 | $this->assertEquals($bookmark11, $array[11]); | |
160 | $this->assertEquals($bookmark0, $array[0]); | |
161 | ||
162 | unset($array[11]); | |
163 | $this->assertCount(1, $array); | |
164 | $this->assertEquals($bookmark0, $array[0]); | |
165 | ||
166 | unset($array[0]); | |
167 | $this->assertCount(0, $array); | |
168 | } | |
169 | ||
170 | /** | |
171 | * Test iterating through array access. | |
172 | */ | |
173 | public function testArrayAccessIterate() | |
174 | { | |
175 | $array = new BookmarkArray(); | |
176 | $bookmark11 = new Bookmark(); | |
177 | $bookmark11->setId(11)->validate(); | |
178 | $array[] = $bookmark11; | |
179 | $bookmark14 = new Bookmark(); | |
180 | $bookmark14->setId(14)->validate(); | |
181 | $array[] = $bookmark14; | |
182 | $bookmark23 = new Bookmark(); | |
183 | $bookmark23->setId(23)->validate(); | |
184 | $array[] = $bookmark23; | |
185 | $this->assertCount(3, $array); | |
186 | ||
187 | foreach ($array as $id => $bookmark) { | |
188 | $this->assertEquals(${'bookmark'. $id}, $bookmark); | |
189 | } | |
190 | } | |
191 | ||
192 | /** | |
193 | * Test reordering the array. | |
194 | */ | |
195 | public function testReorder() | |
196 | { | |
197 | $refDB = new \ReferenceLinkDB(); | |
198 | $refDB->write('sandbox/datastore.php'); | |
199 | ||
200 | ||
201 | $bookmarks = $refDB->getLinks(); | |
202 | $bookmarks->reorder('ASC'); | |
203 | $this->assertInstanceOf(BookmarkArray::class, $bookmarks); | |
204 | ||
205 | $stickyIds = [11, 10]; | |
206 | $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41]; | |
207 | $linkIds = array_merge($stickyIds, $standardIds); | |
208 | $cpt = 0; | |
209 | foreach ($bookmarks as $key => $value) { | |
210 | $this->assertEquals($linkIds[$cpt++], $key); | |
211 | } | |
212 | ||
213 | $bookmarks = $refDB->getLinks(); | |
214 | $bookmarks->reorder('DESC'); | |
215 | $this->assertInstanceOf(BookmarkArray::class, $bookmarks); | |
216 | ||
217 | $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds)); | |
218 | $cpt = 0; | |
219 | foreach ($bookmarks as $key => $value) { | |
220 | $this->assertEquals($linkIds[$cpt++], $key); | |
221 | } | |
222 | } | |
223 | } |