diff options
Diffstat (limited to 'tests/api/controllers/links')
-rw-r--r-- | tests/api/controllers/links/DeleteLinkTest.php | 32 | ||||
-rw-r--r-- | tests/api/controllers/links/GetLinkIdTest.php | 25 | ||||
-rw-r--r-- | tests/api/controllers/links/GetLinksTest.php | 23 | ||||
-rw-r--r-- | tests/api/controllers/links/PostLinkTest.php | 40 | ||||
-rw-r--r-- | tests/api/controllers/links/PutLinkTest.php | 40 |
5 files changed, 94 insertions, 66 deletions
diff --git a/tests/api/controllers/links/DeleteLinkTest.php b/tests/api/controllers/links/DeleteLinkTest.php index 90193e28..cf9464f0 100644 --- a/tests/api/controllers/links/DeleteLinkTest.php +++ b/tests/api/controllers/links/DeleteLinkTest.php | |||
@@ -3,7 +3,7 @@ | |||
3 | 3 | ||
4 | namespace Shaarli\Api\Controllers; | 4 | namespace Shaarli\Api\Controllers; |
5 | 5 | ||
6 | use Shaarli\Bookmark\LinkDB; | 6 | use Shaarli\Bookmark\BookmarkFileService; |
7 | use Shaarli\Config\ConfigManager; | 7 | use Shaarli\Config\ConfigManager; |
8 | use Shaarli\History; | 8 | use Shaarli\History; |
9 | use Slim\Container; | 9 | use Slim\Container; |
@@ -11,7 +11,7 @@ use Slim\Http\Environment; | |||
11 | use Slim\Http\Request; | 11 | use Slim\Http\Request; |
12 | use Slim\Http\Response; | 12 | use Slim\Http\Response; |
13 | 13 | ||
14 | class DeleteLinkTest extends \PHPUnit\Framework\TestCase | 14 | class DeleteLinkTest extends \Shaarli\TestCase |
15 | { | 15 | { |
16 | /** | 16 | /** |
17 | * @var string datastore to test write operations | 17 | * @var string datastore to test write operations |
@@ -34,9 +34,9 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase | |||
34 | protected $refDB = null; | 34 | protected $refDB = null; |
35 | 35 | ||
36 | /** | 36 | /** |
37 | * @var LinkDB instance. | 37 | * @var BookmarkFileService instance. |
38 | */ | 38 | */ |
39 | protected $linkDB; | 39 | protected $bookmarkService; |
40 | 40 | ||
41 | /** | 41 | /** |
42 | * @var HistoryController instance. | 42 | * @var HistoryController instance. |
@@ -54,20 +54,22 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase | |||
54 | protected $controller; | 54 | protected $controller; |
55 | 55 | ||
56 | /** | 56 | /** |
57 | * Before each test, instantiate a new Api with its config, plugins and links. | 57 | * Before each test, instantiate a new Api with its config, plugins and bookmarks. |
58 | */ | 58 | */ |
59 | public function setUp() | 59 | protected function setUp(): void |
60 | { | 60 | { |
61 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | 61 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
62 | $this->conf->set('resource.datastore', self::$testDatastore); | ||
62 | $this->refDB = new \ReferenceLinkDB(); | 63 | $this->refDB = new \ReferenceLinkDB(); |
63 | $this->refDB->write(self::$testDatastore); | 64 | $this->refDB->write(self::$testDatastore); |
64 | $this->linkDB = new LinkDB(self::$testDatastore, true, false); | ||
65 | $refHistory = new \ReferenceHistory(); | 65 | $refHistory = new \ReferenceHistory(); |
66 | $refHistory->write(self::$testHistory); | 66 | $refHistory->write(self::$testHistory); |
67 | $this->history = new History(self::$testHistory); | 67 | $this->history = new History(self::$testHistory); |
68 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | ||
69 | |||
68 | $this->container = new Container(); | 70 | $this->container = new Container(); |
69 | $this->container['conf'] = $this->conf; | 71 | $this->container['conf'] = $this->conf; |
70 | $this->container['db'] = $this->linkDB; | 72 | $this->container['db'] = $this->bookmarkService; |
71 | $this->container['history'] = $this->history; | 73 | $this->container['history'] = $this->history; |
72 | 74 | ||
73 | $this->controller = new Links($this->container); | 75 | $this->controller = new Links($this->container); |
@@ -76,7 +78,7 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase | |||
76 | /** | 78 | /** |
77 | * After each test, remove the test datastore. | 79 | * After each test, remove the test datastore. |
78 | */ | 80 | */ |
79 | public function tearDown() | 81 | protected function tearDown(): void |
80 | { | 82 | { |
81 | @unlink(self::$testDatastore); | 83 | @unlink(self::$testDatastore); |
82 | @unlink(self::$testHistory); | 84 | @unlink(self::$testHistory); |
@@ -88,7 +90,7 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase | |||
88 | public function testDeleteLinkValid() | 90 | public function testDeleteLinkValid() |
89 | { | 91 | { |
90 | $id = '41'; | 92 | $id = '41'; |
91 | $this->assertTrue(isset($this->linkDB[$id])); | 93 | $this->assertTrue($this->bookmarkService->exists($id)); |
92 | $env = Environment::mock([ | 94 | $env = Environment::mock([ |
93 | 'REQUEST_METHOD' => 'DELETE', | 95 | 'REQUEST_METHOD' => 'DELETE', |
94 | ]); | 96 | ]); |
@@ -98,8 +100,8 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase | |||
98 | $this->assertEquals(204, $response->getStatusCode()); | 100 | $this->assertEquals(204, $response->getStatusCode()); |
99 | $this->assertEmpty((string) $response->getBody()); | 101 | $this->assertEmpty((string) $response->getBody()); |
100 | 102 | ||
101 | $this->linkDB = new LinkDB(self::$testDatastore, true, false); | 103 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); |
102 | $this->assertFalse(isset($this->linkDB[$id])); | 104 | $this->assertFalse($this->bookmarkService->exists($id)); |
103 | 105 | ||
104 | $historyEntry = $this->history->getHistory()[0]; | 106 | $historyEntry = $this->history->getHistory()[0]; |
105 | $this->assertEquals(History::DELETED, $historyEntry['event']); | 107 | $this->assertEquals(History::DELETED, $historyEntry['event']); |
@@ -111,13 +113,13 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase | |||
111 | 113 | ||
112 | /** | 114 | /** |
113 | * Test DELETE link endpoint: reach not existing ID. | 115 | * Test DELETE link endpoint: reach not existing ID. |
114 | * | ||
115 | * @expectedException \Shaarli\Api\Exceptions\ApiLinkNotFoundException | ||
116 | */ | 116 | */ |
117 | public function testDeleteLink404() | 117 | public function testDeleteLink404() |
118 | { | 118 | { |
119 | $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class); | ||
120 | |||
119 | $id = -1; | 121 | $id = -1; |
120 | $this->assertFalse(isset($this->linkDB[$id])); | 122 | $this->assertFalse($this->bookmarkService->exists($id)); |
121 | $env = Environment::mock([ | 123 | $env = Environment::mock([ |
122 | 'REQUEST_METHOD' => 'DELETE', | 124 | 'REQUEST_METHOD' => 'DELETE', |
123 | ]); | 125 | ]); |
diff --git a/tests/api/controllers/links/GetLinkIdTest.php b/tests/api/controllers/links/GetLinkIdTest.php index cb9b7f6a..99dc606f 100644 --- a/tests/api/controllers/links/GetLinkIdTest.php +++ b/tests/api/controllers/links/GetLinkIdTest.php | |||
@@ -2,7 +2,10 @@ | |||
2 | 2 | ||
3 | namespace Shaarli\Api\Controllers; | 3 | namespace Shaarli\Api\Controllers; |
4 | 4 | ||
5 | use Shaarli\Bookmark\Bookmark; | ||
6 | use Shaarli\Bookmark\BookmarkFileService; | ||
5 | use Shaarli\Config\ConfigManager; | 7 | use Shaarli\Config\ConfigManager; |
8 | use Shaarli\History; | ||
6 | use Slim\Container; | 9 | use Slim\Container; |
7 | use Slim\Http\Environment; | 10 | use Slim\Http\Environment; |
8 | use Slim\Http\Request; | 11 | use Slim\Http\Request; |
@@ -17,7 +20,7 @@ use Slim\Http\Response; | |||
17 | * | 20 | * |
18 | * @package Shaarli\Api\Controllers | 21 | * @package Shaarli\Api\Controllers |
19 | */ | 22 | */ |
20 | class GetLinkIdTest extends \PHPUnit\Framework\TestCase | 23 | class GetLinkIdTest extends \Shaarli\TestCase |
21 | { | 24 | { |
22 | /** | 25 | /** |
23 | * @var string datastore to test write operations | 26 | * @var string datastore to test write operations |
@@ -50,17 +53,19 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase | |||
50 | const NB_FIELDS_LINK = 9; | 53 | const NB_FIELDS_LINK = 9; |
51 | 54 | ||
52 | /** | 55 | /** |
53 | * Before each test, instantiate a new Api with its config, plugins and links. | 56 | * Before each test, instantiate a new Api with its config, plugins and bookmarks. |
54 | */ | 57 | */ |
55 | public function setUp() | 58 | protected function setUp(): void |
56 | { | 59 | { |
57 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | 60 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
61 | $this->conf->set('resource.datastore', self::$testDatastore); | ||
58 | $this->refDB = new \ReferenceLinkDB(); | 62 | $this->refDB = new \ReferenceLinkDB(); |
59 | $this->refDB->write(self::$testDatastore); | 63 | $this->refDB->write(self::$testDatastore); |
64 | $history = new History('sandbox/history.php'); | ||
60 | 65 | ||
61 | $this->container = new Container(); | 66 | $this->container = new Container(); |
62 | $this->container['conf'] = $this->conf; | 67 | $this->container['conf'] = $this->conf; |
63 | $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); | 68 | $this->container['db'] = new BookmarkFileService($this->conf, $history, true); |
64 | $this->container['history'] = null; | 69 | $this->container['history'] = null; |
65 | 70 | ||
66 | $this->controller = new Links($this->container); | 71 | $this->controller = new Links($this->container); |
@@ -69,7 +74,7 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase | |||
69 | /** | 74 | /** |
70 | * After each test, remove the test datastore. | 75 | * After each test, remove the test datastore. |
71 | */ | 76 | */ |
72 | public function tearDown() | 77 | protected function tearDown(): void |
73 | { | 78 | { |
74 | @unlink(self::$testDatastore); | 79 | @unlink(self::$testDatastore); |
75 | } | 80 | } |
@@ -97,7 +102,7 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase | |||
97 | $this->assertEquals($id, $data['id']); | 102 | $this->assertEquals($id, $data['id']); |
98 | 103 | ||
99 | // Check link elements | 104 | // Check link elements |
100 | $this->assertEquals('http://domain.tld/?WDWyig', $data['url']); | 105 | $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']); |
101 | $this->assertEquals('WDWyig', $data['shorturl']); | 106 | $this->assertEquals('WDWyig', $data['shorturl']); |
102 | $this->assertEquals('Link title: @website', $data['title']); | 107 | $this->assertEquals('Link title: @website', $data['title']); |
103 | $this->assertEquals( | 108 | $this->assertEquals( |
@@ -107,7 +112,7 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase | |||
107 | $this->assertEquals('sTuff', $data['tags'][0]); | 112 | $this->assertEquals('sTuff', $data['tags'][0]); |
108 | $this->assertEquals(false, $data['private']); | 113 | $this->assertEquals(false, $data['private']); |
109 | $this->assertEquals( | 114 | $this->assertEquals( |
110 | \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), | 115 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), |
111 | $data['created'] | 116 | $data['created'] |
112 | ); | 117 | ); |
113 | $this->assertEmpty($data['updated']); | 118 | $this->assertEmpty($data['updated']); |
@@ -115,12 +120,12 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase | |||
115 | 120 | ||
116 | /** | 121 | /** |
117 | * Test basic getLink service: get non existent link => ApiLinkNotFoundException. | 122 | * Test basic getLink service: get non existent link => ApiLinkNotFoundException. |
118 | * | ||
119 | * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException | ||
120 | * @expectedExceptionMessage Link not found | ||
121 | */ | 123 | */ |
122 | public function testGetLink404() | 124 | public function testGetLink404() |
123 | { | 125 | { |
126 | $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class); | ||
127 | $this->expectExceptionMessage('Link not found'); | ||
128 | |||
124 | $env = Environment::mock([ | 129 | $env = Environment::mock([ |
125 | 'REQUEST_METHOD' => 'GET', | 130 | 'REQUEST_METHOD' => 'GET', |
126 | ]); | 131 | ]); |
diff --git a/tests/api/controllers/links/GetLinksTest.php b/tests/api/controllers/links/GetLinksTest.php index 711a3152..ca1bfc63 100644 --- a/tests/api/controllers/links/GetLinksTest.php +++ b/tests/api/controllers/links/GetLinksTest.php | |||
@@ -1,8 +1,11 @@ | |||
1 | <?php | 1 | <?php |
2 | namespace Shaarli\Api\Controllers; | 2 | namespace Shaarli\Api\Controllers; |
3 | 3 | ||
4 | use Shaarli\Bookmark\Bookmark; | ||
5 | use Shaarli\Bookmark\BookmarkFileService; | ||
4 | use Shaarli\Bookmark\LinkDB; | 6 | use Shaarli\Bookmark\LinkDB; |
5 | use Shaarli\Config\ConfigManager; | 7 | use Shaarli\Config\ConfigManager; |
8 | use Shaarli\History; | ||
6 | use Slim\Container; | 9 | use Slim\Container; |
7 | use Slim\Http\Environment; | 10 | use Slim\Http\Environment; |
8 | use Slim\Http\Request; | 11 | use Slim\Http\Request; |
@@ -17,7 +20,7 @@ use Slim\Http\Response; | |||
17 | * | 20 | * |
18 | * @package Shaarli\Api\Controllers | 21 | * @package Shaarli\Api\Controllers |
19 | */ | 22 | */ |
20 | class GetLinksTest extends \PHPUnit\Framework\TestCase | 23 | class GetLinksTest extends \Shaarli\TestCase |
21 | { | 24 | { |
22 | /** | 25 | /** |
23 | * @var string datastore to test write operations | 26 | * @var string datastore to test write operations |
@@ -50,17 +53,19 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase | |||
50 | const NB_FIELDS_LINK = 9; | 53 | const NB_FIELDS_LINK = 9; |
51 | 54 | ||
52 | /** | 55 | /** |
53 | * Before every test, instantiate a new Api with its config, plugins and links. | 56 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. |
54 | */ | 57 | */ |
55 | public function setUp() | 58 | protected function setUp(): void |
56 | { | 59 | { |
57 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | 60 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
61 | $this->conf->set('resource.datastore', self::$testDatastore); | ||
58 | $this->refDB = new \ReferenceLinkDB(); | 62 | $this->refDB = new \ReferenceLinkDB(); |
59 | $this->refDB->write(self::$testDatastore); | 63 | $this->refDB->write(self::$testDatastore); |
64 | $history = new History('sandbox/history.php'); | ||
60 | 65 | ||
61 | $this->container = new Container(); | 66 | $this->container = new Container(); |
62 | $this->container['conf'] = $this->conf; | 67 | $this->container['conf'] = $this->conf; |
63 | $this->container['db'] = new LinkDB(self::$testDatastore, true, false); | 68 | $this->container['db'] = new BookmarkFileService($this->conf, $history, true); |
64 | $this->container['history'] = null; | 69 | $this->container['history'] = null; |
65 | 70 | ||
66 | $this->controller = new Links($this->container); | 71 | $this->controller = new Links($this->container); |
@@ -69,13 +74,13 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase | |||
69 | /** | 74 | /** |
70 | * After every test, remove the test datastore. | 75 | * After every test, remove the test datastore. |
71 | */ | 76 | */ |
72 | public function tearDown() | 77 | protected function tearDown(): void |
73 | { | 78 | { |
74 | @unlink(self::$testDatastore); | 79 | @unlink(self::$testDatastore); |
75 | } | 80 | } |
76 | 81 | ||
77 | /** | 82 | /** |
78 | * Test basic getLinks service: returns all links. | 83 | * Test basic getLinks service: returns all bookmarks. |
79 | */ | 84 | */ |
80 | public function testGetLinks() | 85 | public function testGetLinks() |
81 | { | 86 | { |
@@ -104,7 +109,7 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase | |||
104 | 109 | ||
105 | // Check first element fields | 110 | // Check first element fields |
106 | $first = $data[2]; | 111 | $first = $data[2]; |
107 | $this->assertEquals('http://domain.tld/?WDWyig', $first['url']); | 112 | $this->assertEquals('http://domain.tld/shaare/WDWyig', $first['url']); |
108 | $this->assertEquals('WDWyig', $first['shorturl']); | 113 | $this->assertEquals('WDWyig', $first['shorturl']); |
109 | $this->assertEquals('Link title: @website', $first['title']); | 114 | $this->assertEquals('Link title: @website', $first['title']); |
110 | $this->assertEquals( | 115 | $this->assertEquals( |
@@ -114,7 +119,7 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase | |||
114 | $this->assertEquals('sTuff', $first['tags'][0]); | 119 | $this->assertEquals('sTuff', $first['tags'][0]); |
115 | $this->assertEquals(false, $first['private']); | 120 | $this->assertEquals(false, $first['private']); |
116 | $this->assertEquals( | 121 | $this->assertEquals( |
117 | \DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), | 122 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), |
118 | $first['created'] | 123 | $first['created'] |
119 | ); | 124 | ); |
120 | $this->assertEmpty($first['updated']); | 125 | $this->assertEmpty($first['updated']); |
@@ -125,7 +130,7 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase | |||
125 | 130 | ||
126 | // Update date | 131 | // Update date |
127 | $this->assertEquals( | 132 | $this->assertEquals( |
128 | \DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM), | 133 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM), |
129 | $link['updated'] | 134 | $link['updated'] |
130 | ); | 135 | ); |
131 | } | 136 | } |
diff --git a/tests/api/controllers/links/PostLinkTest.php b/tests/api/controllers/links/PostLinkTest.php index d683a984..fe3de66f 100644 --- a/tests/api/controllers/links/PostLinkTest.php +++ b/tests/api/controllers/links/PostLinkTest.php | |||
@@ -2,9 +2,11 @@ | |||
2 | 2 | ||
3 | namespace Shaarli\Api\Controllers; | 3 | namespace Shaarli\Api\Controllers; |
4 | 4 | ||
5 | use PHPUnit\Framework\TestCase; | 5 | use Shaarli\Bookmark\Bookmark; |
6 | use Shaarli\Bookmark\BookmarkFileService; | ||
6 | use Shaarli\Config\ConfigManager; | 7 | use Shaarli\Config\ConfigManager; |
7 | use Shaarli\History; | 8 | use Shaarli\History; |
9 | use Shaarli\TestCase; | ||
8 | use Slim\Container; | 10 | use Slim\Container; |
9 | use Slim\Http\Environment; | 11 | use Slim\Http\Environment; |
10 | use Slim\Http\Request; | 12 | use Slim\Http\Request; |
@@ -41,6 +43,11 @@ class PostLinkTest extends TestCase | |||
41 | protected $refDB = null; | 43 | protected $refDB = null; |
42 | 44 | ||
43 | /** | 45 | /** |
46 | * @var BookmarkFileService instance. | ||
47 | */ | ||
48 | protected $bookmarkService; | ||
49 | |||
50 | /** | ||
44 | * @var HistoryController instance. | 51 | * @var HistoryController instance. |
45 | */ | 52 | */ |
46 | protected $history; | 53 | protected $history; |
@@ -61,29 +68,30 @@ class PostLinkTest extends TestCase | |||
61 | const NB_FIELDS_LINK = 9; | 68 | const NB_FIELDS_LINK = 9; |
62 | 69 | ||
63 | /** | 70 | /** |
64 | * Before every test, instantiate a new Api with its config, plugins and links. | 71 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. |
65 | */ | 72 | */ |
66 | public function setUp() | 73 | protected function setUp(): void |
67 | { | 74 | { |
68 | $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); | 75 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
76 | $this->conf->set('resource.datastore', self::$testDatastore); | ||
69 | $this->refDB = new \ReferenceLinkDB(); | 77 | $this->refDB = new \ReferenceLinkDB(); |
70 | $this->refDB->write(self::$testDatastore); | 78 | $this->refDB->write(self::$testDatastore); |
71 | |||
72 | $refHistory = new \ReferenceHistory(); | 79 | $refHistory = new \ReferenceHistory(); |
73 | $refHistory->write(self::$testHistory); | 80 | $refHistory->write(self::$testHistory); |
74 | $this->history = new History(self::$testHistory); | 81 | $this->history = new History(self::$testHistory); |
82 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | ||
75 | 83 | ||
76 | $this->container = new Container(); | 84 | $this->container = new Container(); |
77 | $this->container['conf'] = $this->conf; | 85 | $this->container['conf'] = $this->conf; |
78 | $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); | 86 | $this->container['db'] = $this->bookmarkService; |
79 | $this->container['history'] = new History(self::$testHistory); | 87 | $this->container['history'] = $this->history; |
80 | 88 | ||
81 | $this->controller = new Links($this->container); | 89 | $this->controller = new Links($this->container); |
82 | 90 | ||
83 | $mock = $this->createMock(Router::class); | 91 | $mock = $this->createMock(Router::class); |
84 | $mock->expects($this->any()) | 92 | $mock->expects($this->any()) |
85 | ->method('relativePathFor') | 93 | ->method('relativePathFor') |
86 | ->willReturn('api/v1/links/1'); | 94 | ->willReturn('api/v1/bookmarks/1'); |
87 | 95 | ||
88 | // affect @property-read... seems to work | 96 | // affect @property-read... seems to work |
89 | $this->controller->getCi()->router = $mock; | 97 | $this->controller->getCi()->router = $mock; |
@@ -99,7 +107,7 @@ class PostLinkTest extends TestCase | |||
99 | /** | 107 | /** |
100 | * After every test, remove the test datastore. | 108 | * After every test, remove the test datastore. |
101 | */ | 109 | */ |
102 | public function tearDown() | 110 | protected function tearDown(): void |
103 | { | 111 | { |
104 | @unlink(self::$testDatastore); | 112 | @unlink(self::$testDatastore); |
105 | @unlink(self::$testHistory); | 113 | @unlink(self::$testHistory); |
@@ -118,16 +126,16 @@ class PostLinkTest extends TestCase | |||
118 | 126 | ||
119 | $response = $this->controller->postLink($request, new Response()); | 127 | $response = $this->controller->postLink($request, new Response()); |
120 | $this->assertEquals(201, $response->getStatusCode()); | 128 | $this->assertEquals(201, $response->getStatusCode()); |
121 | $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]); | 129 | $this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]); |
122 | $data = json_decode((string) $response->getBody(), true); | 130 | $data = json_decode((string) $response->getBody(), true); |
123 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | 131 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); |
124 | $this->assertEquals(43, $data['id']); | 132 | $this->assertEquals(43, $data['id']); |
125 | $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']); | 133 | $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']); |
126 | $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']); | 134 | $this->assertEquals('http://domain.tld/shaare/' . $data['shorturl'], $data['url']); |
127 | $this->assertEquals('?' . $data['shorturl'], $data['title']); | 135 | $this->assertEquals('/shaare/' . $data['shorturl'], $data['title']); |
128 | $this->assertEquals('', $data['description']); | 136 | $this->assertEquals('', $data['description']); |
129 | $this->assertEquals([], $data['tags']); | 137 | $this->assertEquals([], $data['tags']); |
130 | $this->assertEquals(false, $data['private']); | 138 | $this->assertEquals(true, $data['private']); |
131 | $this->assertTrue( | 139 | $this->assertTrue( |
132 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | 140 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) |
133 | ); | 141 | ); |
@@ -163,7 +171,7 @@ class PostLinkTest extends TestCase | |||
163 | $response = $this->controller->postLink($request, new Response()); | 171 | $response = $this->controller->postLink($request, new Response()); |
164 | 172 | ||
165 | $this->assertEquals(201, $response->getStatusCode()); | 173 | $this->assertEquals(201, $response->getStatusCode()); |
166 | $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]); | 174 | $this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]); |
167 | $data = json_decode((string) $response->getBody(), true); | 175 | $data = json_decode((string) $response->getBody(), true); |
168 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | 176 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); |
169 | $this->assertEquals(43, $data['id']); | 177 | $this->assertEquals(43, $data['id']); |
@@ -211,11 +219,11 @@ class PostLinkTest extends TestCase | |||
211 | $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); | 219 | $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); |
212 | $this->assertEquals(false, $data['private']); | 220 | $this->assertEquals(false, $data['private']); |
213 | $this->assertEquals( | 221 | $this->assertEquals( |
214 | \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130614_184135'), | 222 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'), |
215 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | 223 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) |
216 | ); | 224 | ); |
217 | $this->assertEquals( | 225 | $this->assertEquals( |
218 | \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130615_184230'), | 226 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'), |
219 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | 227 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) |
220 | ); | 228 | ); |
221 | } | 229 | } |
diff --git a/tests/api/controllers/links/PutLinkTest.php b/tests/api/controllers/links/PutLinkTest.php index cd815b66..a2e87c59 100644 --- a/tests/api/controllers/links/PutLinkTest.php +++ b/tests/api/controllers/links/PutLinkTest.php | |||
@@ -3,6 +3,8 @@ | |||
3 | 3 | ||
4 | namespace Shaarli\Api\Controllers; | 4 | namespace Shaarli\Api\Controllers; |
5 | 5 | ||
6 | use Shaarli\Bookmark\Bookmark; | ||
7 | use Shaarli\Bookmark\BookmarkFileService; | ||
6 | use Shaarli\Config\ConfigManager; | 8 | use Shaarli\Config\ConfigManager; |
7 | use Shaarli\History; | 9 | use Shaarli\History; |
8 | use Slim\Container; | 10 | use Slim\Container; |
@@ -10,7 +12,7 @@ use Slim\Http\Environment; | |||
10 | use Slim\Http\Request; | 12 | use Slim\Http\Request; |
11 | use Slim\Http\Response; | 13 | use Slim\Http\Response; |
12 | 14 | ||
13 | class PutLinkTest extends \PHPUnit\Framework\TestCase | 15 | class PutLinkTest extends \Shaarli\TestCase |
14 | { | 16 | { |
15 | /** | 17 | /** |
16 | * @var string datastore to test write operations | 18 | * @var string datastore to test write operations |
@@ -33,6 +35,11 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase | |||
33 | protected $refDB = null; | 35 | protected $refDB = null; |
34 | 36 | ||
35 | /** | 37 | /** |
38 | * @var BookmarkFileService instance. | ||
39 | */ | ||
40 | protected $bookmarkService; | ||
41 | |||
42 | /** | ||
36 | * @var HistoryController instance. | 43 | * @var HistoryController instance. |
37 | */ | 44 | */ |
38 | protected $history; | 45 | protected $history; |
@@ -53,22 +60,23 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase | |||
53 | const NB_FIELDS_LINK = 9; | 60 | const NB_FIELDS_LINK = 9; |
54 | 61 | ||
55 | /** | 62 | /** |
56 | * Before every test, instantiate a new Api with its config, plugins and links. | 63 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. |
57 | */ | 64 | */ |
58 | public function setUp() | 65 | protected function setUp(): void |
59 | { | 66 | { |
60 | $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); | 67 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
68 | $this->conf->set('resource.datastore', self::$testDatastore); | ||
61 | $this->refDB = new \ReferenceLinkDB(); | 69 | $this->refDB = new \ReferenceLinkDB(); |
62 | $this->refDB->write(self::$testDatastore); | 70 | $this->refDB->write(self::$testDatastore); |
63 | |||
64 | $refHistory = new \ReferenceHistory(); | 71 | $refHistory = new \ReferenceHistory(); |
65 | $refHistory->write(self::$testHistory); | 72 | $refHistory->write(self::$testHistory); |
66 | $this->history = new History(self::$testHistory); | 73 | $this->history = new History(self::$testHistory); |
74 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | ||
67 | 75 | ||
68 | $this->container = new Container(); | 76 | $this->container = new Container(); |
69 | $this->container['conf'] = $this->conf; | 77 | $this->container['conf'] = $this->conf; |
70 | $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); | 78 | $this->container['db'] = $this->bookmarkService; |
71 | $this->container['history'] = new History(self::$testHistory); | 79 | $this->container['history'] = $this->history; |
72 | 80 | ||
73 | $this->controller = new Links($this->container); | 81 | $this->controller = new Links($this->container); |
74 | 82 | ||
@@ -83,7 +91,7 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase | |||
83 | /** | 91 | /** |
84 | * After every test, remove the test datastore. | 92 | * After every test, remove the test datastore. |
85 | */ | 93 | */ |
86 | public function tearDown() | 94 | protected function tearDown(): void |
87 | { | 95 | { |
88 | @unlink(self::$testDatastore); | 96 | @unlink(self::$testDatastore); |
89 | @unlink(self::$testHistory); | 97 | @unlink(self::$testHistory); |
@@ -106,11 +114,11 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase | |||
106 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | 114 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); |
107 | $this->assertEquals($id, $data['id']); | 115 | $this->assertEquals($id, $data['id']); |
108 | $this->assertEquals('WDWyig', $data['shorturl']); | 116 | $this->assertEquals('WDWyig', $data['shorturl']); |
109 | $this->assertEquals('http://domain.tld/?WDWyig', $data['url']); | 117 | $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']); |
110 | $this->assertEquals('?WDWyig', $data['title']); | 118 | $this->assertEquals('/shaare/WDWyig', $data['title']); |
111 | $this->assertEquals('', $data['description']); | 119 | $this->assertEquals('', $data['description']); |
112 | $this->assertEquals([], $data['tags']); | 120 | $this->assertEquals([], $data['tags']); |
113 | $this->assertEquals(false, $data['private']); | 121 | $this->assertEquals(true, $data['private']); |
114 | $this->assertEquals( | 122 | $this->assertEquals( |
115 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), | 123 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), |
116 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | 124 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) |
@@ -199,23 +207,23 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase | |||
199 | $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); | 207 | $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); |
200 | $this->assertEquals(false, $data['private']); | 208 | $this->assertEquals(false, $data['private']); |
201 | $this->assertEquals( | 209 | $this->assertEquals( |
202 | \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130614_184135'), | 210 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'), |
203 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | 211 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) |
204 | ); | 212 | ); |
205 | $this->assertEquals( | 213 | $this->assertEquals( |
206 | \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130615_184230'), | 214 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'), |
207 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | 215 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) |
208 | ); | 216 | ); |
209 | } | 217 | } |
210 | 218 | ||
211 | /** | 219 | /** |
212 | * Test link update on non existent link => ApiLinkNotFoundException. | 220 | * Test link update on non existent link => ApiLinkNotFoundException. |
213 | * | ||
214 | * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException | ||
215 | * @expectedExceptionMessage Link not found | ||
216 | */ | 221 | */ |
217 | public function testGetLink404() | 222 | public function testGetLink404() |
218 | { | 223 | { |
224 | $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class); | ||
225 | $this->expectExceptionMessage('Link not found'); | ||
226 | |||
219 | $env = Environment::mock([ | 227 | $env = Environment::mock([ |
220 | 'REQUEST_METHOD' => 'PUT', | 228 | 'REQUEST_METHOD' => 'PUT', |
221 | ]); | 229 | ]); |