diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-02 17:50:59 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 13:50:11 +0200 |
commit | efb7d21b52eb033530e80e5e49d175e6e3b031f4 (patch) | |
tree | 4f34052788a08be1a30cb88c3339ae14e0b7c4da /tests/bookmark | |
parent | 29c31b7ec6ca48ba37b7eb6da650931fd0cb7164 (diff) | |
download | Shaarli-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/bookmark')
-rw-r--r-- | tests/bookmark/BookmarkArrayTest.php | 13 | ||||
-rw-r--r-- | tests/bookmark/BookmarkFileServiceTest.php | 44 | ||||
-rw-r--r-- | tests/bookmark/BookmarkTest.php | 38 |
3 files changed, 0 insertions, 95 deletions
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() |