From e26e2060f5470ce8bf4c5973284bae07b8af170a Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 17 Jan 2020 21:34:12 +0100 Subject: Add and update unit test for the new system (Bookmark + Service) See #1307 --- tests/bookmark/BookmarkArrayTest.php | 239 +++++++++++++++++++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 tests/bookmark/BookmarkArrayTest.php (limited to 'tests/bookmark/BookmarkArrayTest.php') 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 @@ +assertTrue(is_iterable($array)); + $this->assertEmpty($array); + } + + /** + * Test adding entries to the array, specifying the key offset or not. + */ + public function testArrayAccessAddEntries() + { + $array = new BookmarkArray(); + $bookmark = new Bookmark(); + $bookmark->setId(11)->validate(); + $array[] = $bookmark; + $this->assertCount(1, $array); + $this->assertTrue(isset($array[11])); + $this->assertNull($array[0]); + $this->assertEquals($bookmark, $array[11]); + + $bookmark = new Bookmark(); + $bookmark->setId(14)->validate(); + $array[14] = $bookmark; + $this->assertCount(2, $array); + $this->assertTrue(isset($array[14])); + $this->assertNull($array[0]); + $this->assertEquals($bookmark, $array[14]); + } + + /** + * Test adding a bad entry: wrong type + * + * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException + */ + public function testArrayAccessAddBadEntryInstance() + { + $array = new BookmarkArray(); + $array[] = 'nope'; + } + + /** + * Test adding a bad entry: no id + * + * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException + */ + public function testArrayAccessAddBadEntryNoId() + { + $array = new BookmarkArray(); + $bookmark = new Bookmark(); + $array[] = $bookmark; + } + + /** + * Test adding a bad entry: no url + * + * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException + */ + public function testArrayAccessAddBadEntryNoUrl() + { + $array = new BookmarkArray(); + $bookmark = (new Bookmark())->setId(11); + $array[] = $bookmark; + } + + /** + * Test adding a bad entry: invalid offset + * + * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException + */ + public function testArrayAccessAddBadEntryOffset() + { + $array = new BookmarkArray(); + $bookmark = (new Bookmark())->setId(11); + $bookmark->validate(); + $array['nope'] = $bookmark; + } + + /** + * Test adding a bad entry: invalid ID type + * + * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException + */ + public function testArrayAccessAddBadEntryIdType() + { + $array = new BookmarkArray(); + $bookmark = (new Bookmark())->setId('nope'); + $bookmark->validate(); + $array[] = $bookmark; + } + + /** + * Test adding a bad entry: ID/offset not consistent + * + * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException + */ + public function testArrayAccessAddBadEntryIdOffset() + { + $array = new BookmarkArray(); + $bookmark = (new Bookmark())->setId(11); + $bookmark->validate(); + $array[14] = $bookmark; + } + + /** + * Test update entries through array access. + */ + public function testArrayAccessUpdateEntries() + { + $array = new BookmarkArray(); + $bookmark = new Bookmark(); + $bookmark->setId(11)->validate(); + $bookmark->setTitle('old'); + $array[] = $bookmark; + $bookmark = new Bookmark(); + $bookmark->setId(11)->validate(); + $bookmark->setTitle('test'); + $array[] = $bookmark; + $this->assertCount(1, $array); + $this->assertEquals('test', $array[11]->getTitle()); + + $bookmark = new Bookmark(); + $bookmark->setId(11)->validate(); + $bookmark->setTitle('test2'); + $array[11] = $bookmark; + $this->assertCount(1, $array); + $this->assertEquals('test2', $array[11]->getTitle()); + } + + /** + * Test delete entries through array access. + */ + public function testArrayAccessDeleteEntries() + { + $array = new BookmarkArray(); + $bookmark11 = new Bookmark(); + $bookmark11->setId(11)->validate(); + $array[] = $bookmark11; + $bookmark14 = new Bookmark(); + $bookmark14->setId(14)->validate(); + $array[] = $bookmark14; + $bookmark23 = new Bookmark(); + $bookmark23->setId(23)->validate(); + $array[] = $bookmark23; + $bookmark0 = new Bookmark(); + $bookmark0->setId(0)->validate(); + $array[] = $bookmark0; + $this->assertCount(4, $array); + + unset($array[14]); + $this->assertCount(3, $array); + $this->assertEquals($bookmark11, $array[11]); + $this->assertEquals($bookmark23, $array[23]); + $this->assertEquals($bookmark0, $array[0]); + + unset($array[23]); + $this->assertCount(2, $array); + $this->assertEquals($bookmark11, $array[11]); + $this->assertEquals($bookmark0, $array[0]); + + unset($array[11]); + $this->assertCount(1, $array); + $this->assertEquals($bookmark0, $array[0]); + + unset($array[0]); + $this->assertCount(0, $array); + } + + /** + * Test iterating through array access. + */ + public function testArrayAccessIterate() + { + $array = new BookmarkArray(); + $bookmark11 = new Bookmark(); + $bookmark11->setId(11)->validate(); + $array[] = $bookmark11; + $bookmark14 = new Bookmark(); + $bookmark14->setId(14)->validate(); + $array[] = $bookmark14; + $bookmark23 = new Bookmark(); + $bookmark23->setId(23)->validate(); + $array[] = $bookmark23; + $this->assertCount(3, $array); + + foreach ($array as $id => $bookmark) { + $this->assertEquals(${'bookmark'. $id}, $bookmark); + } + } + + /** + * Test reordering the array. + */ + public function testReorder() + { + $refDB = new \ReferenceLinkDB(); + $refDB->write('sandbox/datastore.php'); + + + $bookmarks = $refDB->getLinks(); + $bookmarks->reorder('ASC'); + $this->assertInstanceOf(BookmarkArray::class, $bookmarks); + + $stickyIds = [11, 10]; + $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41]; + $linkIds = array_merge($stickyIds, $standardIds); + $cpt = 0; + foreach ($bookmarks as $key => $value) { + $this->assertEquals($linkIds[$cpt++], $key); + } + + $bookmarks = $refDB->getLinks(); + $bookmarks->reorder('DESC'); + $this->assertInstanceOf(BookmarkArray::class, $bookmarks); + + $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds)); + $cpt = 0; + foreach ($bookmarks as $key => $value) { + $this->assertEquals($linkIds[$cpt++], $key); + } + } +} -- cgit v1.2.3 From b1baca99f280570d0336b4d71ad1f9dca213a35b Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 27 Sep 2020 14:07:08 +0200 Subject: Convert legacy PHPUnit @expected* to new ->expect* Converted automatically using https://github.com/ArthurHoaro/convert-legacy-phpunit-expect --- tests/bookmark/BookmarkArrayTest.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'tests/bookmark/BookmarkArrayTest.php') diff --git a/tests/bookmark/BookmarkArrayTest.php b/tests/bookmark/BookmarkArrayTest.php index 0f8f04c5..bad3af8d 100644 --- a/tests/bookmark/BookmarkArrayTest.php +++ b/tests/bookmark/BookmarkArrayTest.php @@ -47,22 +47,22 @@ class BookmarkArrayTest extends TestCase /** * Test adding a bad entry: wrong type - * - * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException */ public function testArrayAccessAddBadEntryInstance() { + $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); + $array = new BookmarkArray(); $array[] = 'nope'; } /** * Test adding a bad entry: no id - * - * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException */ public function testArrayAccessAddBadEntryNoId() { + $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); + $array = new BookmarkArray(); $bookmark = new Bookmark(); $array[] = $bookmark; @@ -70,11 +70,11 @@ class BookmarkArrayTest extends TestCase /** * Test adding a bad entry: no url - * - * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException */ public function testArrayAccessAddBadEntryNoUrl() { + $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); + $array = new BookmarkArray(); $bookmark = (new Bookmark())->setId(11); $array[] = $bookmark; @@ -82,11 +82,11 @@ class BookmarkArrayTest extends TestCase /** * Test adding a bad entry: invalid offset - * - * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException */ public function testArrayAccessAddBadEntryOffset() { + $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); + $array = new BookmarkArray(); $bookmark = (new Bookmark())->setId(11); $bookmark->validate(); @@ -95,11 +95,11 @@ class BookmarkArrayTest extends TestCase /** * Test adding a bad entry: invalid ID type - * - * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException */ public function testArrayAccessAddBadEntryIdType() { + $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); + $array = new BookmarkArray(); $bookmark = (new Bookmark())->setId('nope'); $bookmark->validate(); @@ -108,11 +108,11 @@ class BookmarkArrayTest extends TestCase /** * Test adding a bad entry: ID/offset not consistent - * - * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException */ public function testArrayAccessAddBadEntryIdOffset() { + $this->expectException(\Shaarli\Bookmark\Exception\InvalidBookmarkException::class); + $array = new BookmarkArray(); $bookmark = (new Bookmark())->setId(11); $bookmark->validate(); -- cgit v1.2.3 From a5a9cf23acd1248585173aa32757d9720b5f2d62 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 29 Sep 2020 14:41:40 +0200 Subject: Compatibility with PHPUnit 9 --- tests/bookmark/BookmarkArrayTest.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'tests/bookmark/BookmarkArrayTest.php') diff --git a/tests/bookmark/BookmarkArrayTest.php b/tests/bookmark/BookmarkArrayTest.php index bad3af8d..ebed9bfc 100644 --- a/tests/bookmark/BookmarkArrayTest.php +++ b/tests/bookmark/BookmarkArrayTest.php @@ -2,10 +2,7 @@ namespace Shaarli\Bookmark; -use PHPUnit\Framework\TestCase; -use Shaarli\Bookmark\Exception\InvalidBookmarkException; -use Shaarli\Config\ConfigManager; -use Shaarli\History; +use Shaarli\TestCase; /** * Class BookmarkArrayTest -- cgit v1.2.3