aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-02 17:50:59 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-13 13:50:11 +0200
commitefb7d21b52eb033530e80e5e49d175e6e3b031f4 (patch)
tree4f34052788a08be1a30cb88c3339ae14e0b7c4da /tests
parent29c31b7ec6ca48ba37b7eb6da650931fd0cb7164 (diff)
downloadShaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.tar.gz
Shaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.tar.zst
Shaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.zip
Add strict types for bookmarks management
Parameters typing and using strict types overall increase the codebase quality by enforcing the a given parameter will have the expected type. It also removes the need to unnecessary unit tests checking methods behavior with invalid input.
Diffstat (limited to 'tests')
-rw-r--r--tests/HistoryTest.php8
-rw-r--r--tests/bookmark/BookmarkArrayTest.php13
-rw-r--r--tests/bookmark/BookmarkFileServiceTest.php44
-rw-r--r--tests/bookmark/BookmarkTest.php38
-rw-r--r--tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php4
-rw-r--r--tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php20
-rw-r--r--tests/front/controller/admin/ThumbnailsControllerTest.php4
7 files changed, 22 insertions, 109 deletions
diff --git a/tests/HistoryTest.php b/tests/HistoryTest.php
index 6dc0e5b7..e810104e 100644
--- a/tests/HistoryTest.php
+++ b/tests/HistoryTest.php
@@ -89,14 +89,6 @@ class HistoryTest extends \Shaarli\TestCase
89 $this->assertEquals(History::CREATED, $actual['event']); 89 $this->assertEquals(History::CREATED, $actual['event']);
90 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 90 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
91 $this->assertEquals(1, $actual['id']); 91 $this->assertEquals(1, $actual['id']);
92
93 $history = new History(self::$historyFilePath);
94 $bookmark = (new Bookmark())->setId('str');
95 $history->addLink($bookmark);
96 $actual = $history->getHistory()[0];
97 $this->assertEquals(History::CREATED, $actual['event']);
98 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
99 $this->assertEquals('str', $actual['id']);
100 } 92 }
101 93
102// /** 94// /**
diff --git a/tests/bookmark/BookmarkArrayTest.php b/tests/bookmark/BookmarkArrayTest.php
index ebed9bfc..1953078c 100644
--- a/tests/bookmark/BookmarkArrayTest.php
+++ b/tests/bookmark/BookmarkArrayTest.php
@@ -91,19 +91,6 @@ class BookmarkArrayTest extends TestCase
91 } 91 }
92 92
93 /** 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 94 * Test adding a bad entry: ID/offset not consistent
108 */ 95 */
109 public function testArrayAccessAddBadEntryIdOffset() 96 public function testArrayAccessAddBadEntryIdOffset()
diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php
index 6c56dfaa..59c0608c 100644
--- a/tests/bookmark/BookmarkFileServiceTest.php
+++ b/tests/bookmark/BookmarkFileServiceTest.php
@@ -265,17 +265,6 @@ class BookmarkFileServiceTest extends TestCase
265 } 265 }
266 266
267 /** 267 /**
268 * Test add() method with an entry which is not a bookmark instance
269 */
270 public function testAddNotABookmark()
271 {
272 $this->expectException(\Exception::class);
273 $this->expectExceptionMessage('Provided data is invalid');
274
275 $this->privateLinkDB->add(['title' => 'hi!']);
276 }
277
278 /**
279 * Test add() method with a Bookmark already containing an ID 268 * Test add() method with a Bookmark already containing an ID
280 */ 269 */
281 public function testAddWithId() 270 public function testAddWithId()
@@ -413,17 +402,6 @@ class BookmarkFileServiceTest extends TestCase
413 } 402 }
414 403
415 /** 404 /**
416 * Test set() method with an entry which is not a bookmark instance
417 */
418 public function testSetNotABookmark()
419 {
420 $this->expectException(\Exception::class);
421 $this->expectExceptionMessage('Provided data is invalid');
422
423 $this->privateLinkDB->set(['title' => 'hi!']);
424 }
425
426 /**
427 * Test set() method with a Bookmark without an ID defined. 405 * Test set() method with a Bookmark without an ID defined.
428 */ 406 */
429 public function testSetWithoutId() 407 public function testSetWithoutId()
@@ -497,17 +475,6 @@ class BookmarkFileServiceTest extends TestCase
497 } 475 }
498 476
499 /** 477 /**
500 * Test addOrSet() method with an entry which is not a bookmark instance
501 */
502 public function testAddOrSetNotABookmark()
503 {
504 $this->expectException(\Exception::class);
505 $this->expectExceptionMessage('Provided data is invalid');
506
507 $this->privateLinkDB->addOrSet(['title' => 'hi!']);
508 }
509
510 /**
511 * Test addOrSet() method for a bookmark without any field set and without writing the data store 478 * Test addOrSet() method for a bookmark without any field set and without writing the data store
512 */ 479 */
513 public function testAddOrSetMinimalNoWrite() 480 public function testAddOrSetMinimalNoWrite()
@@ -565,17 +532,6 @@ class BookmarkFileServiceTest extends TestCase
565 } 532 }
566 533
567 /** 534 /**
568 * Test remove() method with an entry which is not a bookmark instance
569 */
570 public function testRemoveNotABookmark()
571 {
572 $this->expectException(\Exception::class);
573 $this->expectExceptionMessage('Provided data is invalid');
574
575 $this->privateLinkDB->remove(['title' => 'hi!']);
576 }
577
578 /**
579 * Test remove() method with a Bookmark with an unknown ID 535 * Test remove() method with a Bookmark with an unknown ID
580 */ 536 */
581 public function testRemoveWithUnknownId() 537 public function testRemoveWithUnknownId()
diff --git a/tests/bookmark/BookmarkTest.php b/tests/bookmark/BookmarkTest.php
index afec2440..4c7ae4c0 100644
--- a/tests/bookmark/BookmarkTest.php
+++ b/tests/bookmark/BookmarkTest.php
@@ -154,25 +154,6 @@ class BookmarkTest extends TestCase
154 } 154 }
155 155
156 /** 156 /**
157 * Test validate() with a a bookmark with a non integer ID.
158 */
159 public function testValidateNotValidStringId()
160 {
161 $bookmark = new Bookmark();
162 $bookmark->setId('str');
163 $bookmark->setShortUrl('abc');
164 $bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102'));
165 $exception = null;
166 try {
167 $bookmark->validate();
168 } catch (InvalidBookmarkException $e) {
169 $exception = $e;
170 }
171 $this->assertNotNull($exception);
172 $this->assertContainsPolyfill('- ID: str'. PHP_EOL, $exception->getMessage());
173 }
174
175 /**
176 * Test validate() with a a bookmark without short url. 157 * Test validate() with a a bookmark without short url.
177 */ 158 */
178 public function testValidateNotValidNoShortUrl() 159 public function testValidateNotValidNoShortUrl()
@@ -211,25 +192,6 @@ class BookmarkTest extends TestCase
211 } 192 }
212 193
213 /** 194 /**
214 * Test validate() with a a bookmark with a bad created datetime.
215 */
216 public function testValidateNotValidBadCreated()
217 {
218 $bookmark = new Bookmark();
219 $bookmark->setId(1);
220 $bookmark->setShortUrl('abc');
221 $bookmark->setCreated('hi!');
222 $exception = null;
223 try {
224 $bookmark->validate();
225 } catch (InvalidBookmarkException $e) {
226 $exception = $e;
227 }
228 $this->assertNotNull($exception);
229 $this->assertContainsPolyfill('- Created: Not a DateTime object'. PHP_EOL, $exception->getMessage());
230 }
231
232 /**
233 * Test setId() and make sure that default fields are generated. 195 * Test setId() and make sure that default fields are generated.
234 */ 196 */
235 public function testSetIdEmptyGeneratedFields() 197 public function testSetIdEmptyGeneratedFields()
diff --git a/tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php b/tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php
index ba774e21..83bbee7c 100644
--- a/tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php
+++ b/tests/front/controller/admin/ManageShaareControllerTest/DeleteBookmarkTest.php
@@ -356,6 +356,10 @@ class DeleteBookmarkTest extends TestCase
356 ; 356 ;
357 $response = new Response(); 357 $response = new Response();
358 358
359 $this->container->bookmarkService->method('get')->with('123')->willReturn(
360 (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123')
361 );
362
359 $this->container->formatterFactory = $this->createMock(FormatterFactory::class); 363 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
360 $this->container->formatterFactory 364 $this->container->formatterFactory
361 ->expects(static::once()) 365 ->expects(static::once())
diff --git a/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php b/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
index f7a68226..37542c26 100644
--- a/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
+++ b/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php
@@ -66,23 +66,27 @@ class SaveBookmarkTest extends TestCase
66 $this->container->bookmarkService 66 $this->container->bookmarkService
67 ->expects(static::once()) 67 ->expects(static::once())
68 ->method('addOrSet') 68 ->method('addOrSet')
69 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void { 69 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): Bookmark {
70 static::assertFalse($save); 70 static::assertFalse($save);
71 71
72 $checkBookmark($bookmark); 72 $checkBookmark($bookmark);
73 73
74 $bookmark->setId($id); 74 $bookmark->setId($id);
75
76 return $bookmark;
75 }) 77 })
76 ; 78 ;
77 $this->container->bookmarkService 79 $this->container->bookmarkService
78 ->expects(static::once()) 80 ->expects(static::once())
79 ->method('set') 81 ->method('set')
80 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void { 82 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): Bookmark {
81 static::assertTrue($save); 83 static::assertTrue($save);
82 84
83 $checkBookmark($bookmark); 85 $checkBookmark($bookmark);
84 86
85 static::assertSame($id, $bookmark->getId()); 87 static::assertSame($id, $bookmark->getId());
88
89 return $bookmark;
86 }) 90 })
87 ; 91 ;
88 92
@@ -155,21 +159,25 @@ class SaveBookmarkTest extends TestCase
155 $this->container->bookmarkService 159 $this->container->bookmarkService
156 ->expects(static::once()) 160 ->expects(static::once())
157 ->method('addOrSet') 161 ->method('addOrSet')
158 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void { 162 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): Bookmark {
159 static::assertFalse($save); 163 static::assertFalse($save);
160 164
161 $checkBookmark($bookmark); 165 $checkBookmark($bookmark);
166
167 return $bookmark;
162 }) 168 })
163 ; 169 ;
164 $this->container->bookmarkService 170 $this->container->bookmarkService
165 ->expects(static::once()) 171 ->expects(static::once())
166 ->method('set') 172 ->method('set')
167 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): void { 173 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($checkBookmark, $id): Bookmark {
168 static::assertTrue($save); 174 static::assertTrue($save);
169 175
170 $checkBookmark($bookmark); 176 $checkBookmark($bookmark);
171 177
172 static::assertSame($id, $bookmark->getId()); 178 static::assertSame($id, $bookmark->getId());
179
180 return $bookmark;
173 }) 181 })
174 ; 182 ;
175 183
@@ -230,8 +238,10 @@ class SaveBookmarkTest extends TestCase
230 $this->container->bookmarkService 238 $this->container->bookmarkService
231 ->expects(static::once()) 239 ->expects(static::once())
232 ->method('addOrSet') 240 ->method('addOrSet')
233 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($thumb): void { 241 ->willReturnCallback(function (Bookmark $bookmark, bool $save) use ($thumb): Bookmark {
234 static::assertSame($thumb, $bookmark->getThumbnail()); 242 static::assertSame($thumb, $bookmark->getThumbnail());
243
244 return $bookmark;
235 }) 245 })
236 ; 246 ;
237 247
diff --git a/tests/front/controller/admin/ThumbnailsControllerTest.php b/tests/front/controller/admin/ThumbnailsControllerTest.php
index f4a8acff..e5749654 100644
--- a/tests/front/controller/admin/ThumbnailsControllerTest.php
+++ b/tests/front/controller/admin/ThumbnailsControllerTest.php
@@ -89,8 +89,10 @@ class ThumbnailsControllerTest extends TestCase
89 $this->container->bookmarkService 89 $this->container->bookmarkService
90 ->expects(static::once()) 90 ->expects(static::once())
91 ->method('set') 91 ->method('set')
92 ->willReturnCallback(function (Bookmark $bookmark) use ($thumb) { 92 ->willReturnCallback(function (Bookmark $bookmark) use ($thumb): Bookmark {
93 static::assertSame($thumb, $bookmark->getThumbnail()); 93 static::assertSame($thumb, $bookmark->getThumbnail());
94
95 return $bookmark;
94 }) 96 })
95 ; 97 ;
96 98