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/api/controllers/links/DeleteLinkTest.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'tests/api/controllers/links/DeleteLinkTest.php') diff --git a/tests/api/controllers/links/DeleteLinkTest.php b/tests/api/controllers/links/DeleteLinkTest.php index 90193e28..6c2b3698 100644 --- a/tests/api/controllers/links/DeleteLinkTest.php +++ b/tests/api/controllers/links/DeleteLinkTest.php @@ -3,7 +3,7 @@ namespace Shaarli\Api\Controllers; -use Shaarli\Bookmark\LinkDB; +use Shaarli\Bookmark\BookmarkFileService; use Shaarli\Config\ConfigManager; use Shaarli\History; use Slim\Container; @@ -34,9 +34,9 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase protected $refDB = null; /** - * @var LinkDB instance. + * @var BookmarkFileService instance. */ - protected $linkDB; + protected $bookmarkService; /** * @var HistoryController instance. @@ -54,20 +54,22 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase protected $controller; /** - * Before each test, instantiate a new Api with its config, plugins and links. + * Before each test, instantiate a new Api with its config, plugins and bookmarks. */ public function setUp() { $this->conf = new ConfigManager('tests/utils/config/configJson'); + $this->conf->set('resource.datastore', self::$testDatastore); $this->refDB = new \ReferenceLinkDB(); $this->refDB->write(self::$testDatastore); - $this->linkDB = new LinkDB(self::$testDatastore, true, false); $refHistory = new \ReferenceHistory(); $refHistory->write(self::$testHistory); $this->history = new History(self::$testHistory); + $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); + $this->container = new Container(); $this->container['conf'] = $this->conf; - $this->container['db'] = $this->linkDB; + $this->container['db'] = $this->bookmarkService; $this->container['history'] = $this->history; $this->controller = new Links($this->container); @@ -88,7 +90,7 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase public function testDeleteLinkValid() { $id = '41'; - $this->assertTrue(isset($this->linkDB[$id])); + $this->assertTrue($this->bookmarkService->exists($id)); $env = Environment::mock([ 'REQUEST_METHOD' => 'DELETE', ]); @@ -98,8 +100,8 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase $this->assertEquals(204, $response->getStatusCode()); $this->assertEmpty((string) $response->getBody()); - $this->linkDB = new LinkDB(self::$testDatastore, true, false); - $this->assertFalse(isset($this->linkDB[$id])); + $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); + $this->assertFalse($this->bookmarkService->exists($id)); $historyEntry = $this->history->getHistory()[0]; $this->assertEquals(History::DELETED, $historyEntry['event']); @@ -117,7 +119,7 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase public function testDeleteLink404() { $id = -1; - $this->assertFalse(isset($this->linkDB[$id])); + $this->assertFalse($this->bookmarkService->exists($id)); $env = Environment::mock([ 'REQUEST_METHOD' => 'DELETE', ]); -- cgit v1.2.3 From 8f60e1206e45e67c96a7630d4ff94e72fe875f09 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 26 Sep 2020 15:08:39 +0200 Subject: Comply with PHPUnit V8: setup/teardown functions must return void --- tests/api/controllers/links/DeleteLinkTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/api/controllers/links/DeleteLinkTest.php') diff --git a/tests/api/controllers/links/DeleteLinkTest.php b/tests/api/controllers/links/DeleteLinkTest.php index 6c2b3698..10dfe8bc 100644 --- a/tests/api/controllers/links/DeleteLinkTest.php +++ b/tests/api/controllers/links/DeleteLinkTest.php @@ -56,7 +56,7 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase /** * Before each test, instantiate a new Api with its config, plugins and bookmarks. */ - public function setUp() + protected function setUp(): void { $this->conf = new ConfigManager('tests/utils/config/configJson'); $this->conf->set('resource.datastore', self::$testDatastore); @@ -78,7 +78,7 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase /** * After each test, remove the test datastore. */ - public function tearDown() + protected function tearDown(): void { @unlink(self::$testDatastore); @unlink(self::$testHistory); -- 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/api/controllers/links/DeleteLinkTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/api/controllers/links/DeleteLinkTest.php') diff --git a/tests/api/controllers/links/DeleteLinkTest.php b/tests/api/controllers/links/DeleteLinkTest.php index 10dfe8bc..bd8403cf 100644 --- a/tests/api/controllers/links/DeleteLinkTest.php +++ b/tests/api/controllers/links/DeleteLinkTest.php @@ -113,11 +113,11 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase /** * Test DELETE link endpoint: reach not existing ID. - * - * @expectedException \Shaarli\Api\Exceptions\ApiLinkNotFoundException */ public function testDeleteLink404() { + $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class); + $id = -1; $this->assertFalse($this->bookmarkService->exists($id)); $env = Environment::mock([ -- 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/api/controllers/links/DeleteLinkTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/api/controllers/links/DeleteLinkTest.php') diff --git a/tests/api/controllers/links/DeleteLinkTest.php b/tests/api/controllers/links/DeleteLinkTest.php index bd8403cf..cf9464f0 100644 --- a/tests/api/controllers/links/DeleteLinkTest.php +++ b/tests/api/controllers/links/DeleteLinkTest.php @@ -11,7 +11,7 @@ use Slim\Http\Environment; use Slim\Http\Request; use Slim\Http\Response; -class DeleteLinkTest extends \PHPUnit\Framework\TestCase +class DeleteLinkTest extends \Shaarli\TestCase { /** * @var string datastore to test write operations -- cgit v1.2.3