diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
commit | b6f678a5a1d15acf284ebcec16c905e976671ce1 (patch) | |
tree | 33c7da831482ed79c44896ef19c73c72ada84f2e /tests/bookmark/BookmarkArrayTest.php | |
parent | b14687036b9b800681197f51fdc47e62f0c88e2e (diff) | |
parent | 1c1520b6b98ab20201bfe15577782a52320339df (diff) | |
download | Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip |
Merge branch 'v0.12' into latest
Diffstat (limited to 'tests/bookmark/BookmarkArrayTest.php')
-rw-r--r-- | tests/bookmark/BookmarkArrayTest.php | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/tests/bookmark/BookmarkArrayTest.php b/tests/bookmark/BookmarkArrayTest.php new file mode 100644 index 00000000..ebed9bfc --- /dev/null +++ b/tests/bookmark/BookmarkArrayTest.php | |||
@@ -0,0 +1,236 @@ | |||
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: invalid ID type | ||
95 | */ | ||
96 | public function testArrayAccessAddBadEntryIdType() | ||
97 | { | ||
98 | $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); | ||
99 | |||
100 | $array = new BookmarkArray(); | ||
101 | $bookmark = (new Bookmark())->setId('nope'); | ||
102 | $bookmark->validate(); | ||
103 | $array[] = $bookmark; | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * Test adding a bad entry: ID/offset not consistent | ||
108 | */ | ||
109 | public function testArrayAccessAddBadEntryIdOffset() | ||
110 | { | ||
111 | $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); | ||
112 | |||
113 | $array = new BookmarkArray(); | ||
114 | $bookmark = (new Bookmark())->setId(11); | ||
115 | $bookmark->validate(); | ||
116 | $array[14] = $bookmark; | ||
117 | } | ||
118 | |||
119 | /** | ||
120 | * Test update entries through array access. | ||
121 | */ | ||
122 | public function testArrayAccessUpdateEntries() | ||
123 | { | ||
124 | $array = new BookmarkArray(); | ||
125 | $bookmark = new Bookmark(); | ||
126 | $bookmark->setId(11)->validate(); | ||
127 | $bookmark->setTitle('old'); | ||
128 | $array[] = $bookmark; | ||
129 | $bookmark = new Bookmark(); | ||
130 | $bookmark->setId(11)->validate(); | ||
131 | $bookmark->setTitle('test'); | ||
132 | $array[] = $bookmark; | ||
133 | $this->assertCount(1, $array); | ||
134 | $this->assertEquals('test', $array[11]->getTitle()); | ||
135 | |||
136 | $bookmark = new Bookmark(); | ||
137 | $bookmark->setId(11)->validate(); | ||
138 | $bookmark->setTitle('test2'); | ||
139 | $array[11] = $bookmark; | ||
140 | $this->assertCount(1, $array); | ||
141 | $this->assertEquals('test2', $array[11]->getTitle()); | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * Test delete entries through array access. | ||
146 | */ | ||
147 | public function testArrayAccessDeleteEntries() | ||
148 | { | ||
149 | $array = new BookmarkArray(); | ||
150 | $bookmark11 = new Bookmark(); | ||
151 | $bookmark11->setId(11)->validate(); | ||
152 | $array[] = $bookmark11; | ||
153 | $bookmark14 = new Bookmark(); | ||
154 | $bookmark14->setId(14)->validate(); | ||
155 | $array[] = $bookmark14; | ||
156 | $bookmark23 = new Bookmark(); | ||
157 | $bookmark23->setId(23)->validate(); | ||
158 | $array[] = $bookmark23; | ||
159 | $bookmark0 = new Bookmark(); | ||
160 | $bookmark0->setId(0)->validate(); | ||
161 | $array[] = $bookmark0; | ||
162 | $this->assertCount(4, $array); | ||
163 | |||
164 | unset($array[14]); | ||
165 | $this->assertCount(3, $array); | ||
166 | $this->assertEquals($bookmark11, $array[11]); | ||
167 | $this->assertEquals($bookmark23, $array[23]); | ||
168 | $this->assertEquals($bookmark0, $array[0]); | ||
169 | |||
170 | unset($array[23]); | ||
171 | $this->assertCount(2, $array); | ||
172 | $this->assertEquals($bookmark11, $array[11]); | ||
173 | $this->assertEquals($bookmark0, $array[0]); | ||
174 | |||
175 | unset($array[11]); | ||
176 | $this->assertCount(1, $array); | ||
177 | $this->assertEquals($bookmark0, $array[0]); | ||
178 | |||
179 | unset($array[0]); | ||
180 | $this->assertCount(0, $array); | ||
181 | } | ||
182 | |||
183 | /** | ||
184 | * Test iterating through array access. | ||
185 | */ | ||
186 | public function testArrayAccessIterate() | ||
187 | { | ||
188 | $array = new BookmarkArray(); | ||
189 | $bookmark11 = new Bookmark(); | ||
190 | $bookmark11->setId(11)->validate(); | ||
191 | $array[] = $bookmark11; | ||
192 | $bookmark14 = new Bookmark(); | ||
193 | $bookmark14->setId(14)->validate(); | ||
194 | $array[] = $bookmark14; | ||
195 | $bookmark23 = new Bookmark(); | ||
196 | $bookmark23->setId(23)->validate(); | ||
197 | $array[] = $bookmark23; | ||
198 | $this->assertCount(3, $array); | ||
199 | |||
200 | foreach ($array as $id => $bookmark) { | ||
201 | $this->assertEquals(${'bookmark'. $id}, $bookmark); | ||
202 | } | ||
203 | } | ||
204 | |||
205 | /** | ||
206 | * Test reordering the array. | ||
207 | */ | ||
208 | public function testReorder() | ||
209 | { | ||
210 | $refDB = new \ReferenceLinkDB(); | ||
211 | $refDB->write('sandbox/datastore.php'); | ||
212 | |||
213 | |||
214 | $bookmarks = $refDB->getLinks(); | ||
215 | $bookmarks->reorder('ASC'); | ||
216 | $this->assertInstanceOf(BookmarkArray::class, $bookmarks); | ||
217 | |||
218 | $stickyIds = [11, 10]; | ||
219 | $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41]; | ||
220 | $linkIds = array_merge($stickyIds, $standardIds); | ||
221 | $cpt = 0; | ||
222 | foreach ($bookmarks as $key => $value) { | ||
223 | $this->assertEquals($linkIds[$cpt++], $key); | ||
224 | } | ||
225 | |||
226 | $bookmarks = $refDB->getLinks(); | ||
227 | $bookmarks->reorder('DESC'); | ||
228 | $this->assertInstanceOf(BookmarkArray::class, $bookmarks); | ||
229 | |||
230 | $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds)); | ||
231 | $cpt = 0; | ||
232 | foreach ($bookmarks as $key => $value) { | ||
233 | $this->assertEquals($linkIds[$cpt++], $key); | ||
234 | } | ||
235 | } | ||
236 | } | ||