diff options
Diffstat (limited to 'tests/api/controllers/tags/PutTagTest.php')
-rw-r--r-- | tests/api/controllers/tags/PutTagTest.php | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/tests/api/controllers/tags/PutTagTest.php b/tests/api/controllers/tags/PutTagTest.php index 86106fc7..74edde78 100644 --- a/tests/api/controllers/tags/PutTagTest.php +++ b/tests/api/controllers/tags/PutTagTest.php | |||
@@ -3,6 +3,7 @@ | |||
3 | namespace Shaarli\Api\Controllers; | 3 | namespace Shaarli\Api\Controllers; |
4 | 4 | ||
5 | use Shaarli\Api\Exceptions\ApiBadParametersException; | 5 | use Shaarli\Api\Exceptions\ApiBadParametersException; |
6 | use Shaarli\Bookmark\BookmarkFileService; | ||
6 | use Shaarli\Bookmark\LinkDB; | 7 | use Shaarli\Bookmark\LinkDB; |
7 | use Shaarli\Config\ConfigManager; | 8 | use Shaarli\Config\ConfigManager; |
8 | use Shaarli\History; | 9 | use Shaarli\History; |
@@ -11,7 +12,7 @@ use Slim\Http\Environment; | |||
11 | use Slim\Http\Request; | 12 | use Slim\Http\Request; |
12 | use Slim\Http\Response; | 13 | use Slim\Http\Response; |
13 | 14 | ||
14 | class PutTagTest extends \PHPUnit\Framework\TestCase | 15 | class PutTagTest extends \Shaarli\TestCase |
15 | { | 16 | { |
16 | /** | 17 | /** |
17 | * @var string datastore to test write operations | 18 | * @var string datastore to test write operations |
@@ -44,9 +45,9 @@ class PutTagTest extends \PHPUnit\Framework\TestCase | |||
44 | protected $container; | 45 | protected $container; |
45 | 46 | ||
46 | /** | 47 | /** |
47 | * @var LinkDB instance. | 48 | * @var BookmarkFileService instance. |
48 | */ | 49 | */ |
49 | protected $linkDB; | 50 | protected $bookmarkService; |
50 | 51 | ||
51 | /** | 52 | /** |
52 | * @var Tags controller instance. | 53 | * @var Tags controller instance. |
@@ -59,22 +60,22 @@ class PutTagTest extends \PHPUnit\Framework\TestCase | |||
59 | const NB_FIELDS_TAG = 2; | 60 | const NB_FIELDS_TAG = 2; |
60 | 61 | ||
61 | /** | 62 | /** |
62 | * 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. |
63 | */ | 64 | */ |
64 | public function setUp() | 65 | protected function setUp(): void |
65 | { | 66 | { |
66 | $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); | ||
67 | $this->refDB = new \ReferenceLinkDB(); | 69 | $this->refDB = new \ReferenceLinkDB(); |
68 | $this->refDB->write(self::$testDatastore); | 70 | $this->refDB->write(self::$testDatastore); |
69 | |||
70 | $refHistory = new \ReferenceHistory(); | 71 | $refHistory = new \ReferenceHistory(); |
71 | $refHistory->write(self::$testHistory); | 72 | $refHistory->write(self::$testHistory); |
72 | $this->history = new History(self::$testHistory); | 73 | $this->history = new History(self::$testHistory); |
74 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | ||
73 | 75 | ||
74 | $this->container = new Container(); | 76 | $this->container = new Container(); |
75 | $this->container['conf'] = $this->conf; | 77 | $this->container['conf'] = $this->conf; |
76 | $this->linkDB = new LinkDB(self::$testDatastore, true, false); | 78 | $this->container['db'] = $this->bookmarkService; |
77 | $this->container['db'] = $this->linkDB; | ||
78 | $this->container['history'] = $this->history; | 79 | $this->container['history'] = $this->history; |
79 | 80 | ||
80 | $this->controller = new Tags($this->container); | 81 | $this->controller = new Tags($this->container); |
@@ -83,7 +84,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase | |||
83 | /** | 84 | /** |
84 | * After every test, remove the test datastore. | 85 | * After every test, remove the test datastore. |
85 | */ | 86 | */ |
86 | public function tearDown() | 87 | protected function tearDown(): void |
87 | { | 88 | { |
88 | @unlink(self::$testDatastore); | 89 | @unlink(self::$testDatastore); |
89 | @unlink(self::$testHistory); | 90 | @unlink(self::$testHistory); |
@@ -109,7 +110,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase | |||
109 | $this->assertEquals($newName, $data['name']); | 110 | $this->assertEquals($newName, $data['name']); |
110 | $this->assertEquals(2, $data['occurrences']); | 111 | $this->assertEquals(2, $data['occurrences']); |
111 | 112 | ||
112 | $tags = $this->linkDB->linksCountPerTag(); | 113 | $tags = $this->bookmarkService->bookmarksCountPerTag(); |
113 | $this->assertNotTrue(isset($tags[$tagName])); | 114 | $this->assertNotTrue(isset($tags[$tagName])); |
114 | $this->assertEquals(2, $tags[$newName]); | 115 | $this->assertEquals(2, $tags[$newName]); |
115 | 116 | ||
@@ -133,7 +134,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase | |||
133 | $tagName = 'gnu'; | 134 | $tagName = 'gnu'; |
134 | $newName = 'w3c'; | 135 | $newName = 'w3c'; |
135 | 136 | ||
136 | $tags = $this->linkDB->linksCountPerTag(); | 137 | $tags = $this->bookmarkService->bookmarksCountPerTag(); |
137 | $this->assertEquals(1, $tags[$newName]); | 138 | $this->assertEquals(1, $tags[$newName]); |
138 | $this->assertEquals(2, $tags[$tagName]); | 139 | $this->assertEquals(2, $tags[$tagName]); |
139 | 140 | ||
@@ -151,23 +152,23 @@ class PutTagTest extends \PHPUnit\Framework\TestCase | |||
151 | $this->assertEquals($newName, $data['name']); | 152 | $this->assertEquals($newName, $data['name']); |
152 | $this->assertEquals(3, $data['occurrences']); | 153 | $this->assertEquals(3, $data['occurrences']); |
153 | 154 | ||
154 | $tags = $this->linkDB->linksCountPerTag(); | 155 | $tags = $this->bookmarkService->bookmarksCountPerTag(); |
155 | $this->assertNotTrue(isset($tags[$tagName])); | 156 | $this->assertNotTrue(isset($tags[$tagName])); |
156 | $this->assertEquals(3, $tags[$newName]); | 157 | $this->assertEquals(3, $tags[$newName]); |
157 | } | 158 | } |
158 | 159 | ||
159 | /** | 160 | /** |
160 | * Test tag update with an empty new tag name => ApiBadParametersException | 161 | * Test tag update with an empty new tag name => ApiBadParametersException |
161 | * | ||
162 | * @expectedException Shaarli\Api\Exceptions\ApiBadParametersException | ||
163 | * @expectedExceptionMessage New tag name is required in the request body | ||
164 | */ | 162 | */ |
165 | public function testPutTagEmpty() | 163 | public function testPutTagEmpty() |
166 | { | 164 | { |
165 | $this->expectException(\Shaarli\Api\Exceptions\ApiBadParametersException::class); | ||
166 | $this->expectExceptionMessage('New tag name is required in the request body'); | ||
167 | |||
167 | $tagName = 'gnu'; | 168 | $tagName = 'gnu'; |
168 | $newName = ''; | 169 | $newName = ''; |
169 | 170 | ||
170 | $tags = $this->linkDB->linksCountPerTag(); | 171 | $tags = $this->bookmarkService->bookmarksCountPerTag(); |
171 | $this->assertEquals(2, $tags[$tagName]); | 172 | $this->assertEquals(2, $tags[$tagName]); |
172 | 173 | ||
173 | $env = Environment::mock([ | 174 | $env = Environment::mock([ |
@@ -185,7 +186,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase | |||
185 | try { | 186 | try { |
186 | $this->controller->putTag($request, new Response(), ['tagName' => $tagName]); | 187 | $this->controller->putTag($request, new Response(), ['tagName' => $tagName]); |
187 | } catch (ApiBadParametersException $e) { | 188 | } catch (ApiBadParametersException $e) { |
188 | $tags = $this->linkDB->linksCountPerTag(); | 189 | $tags = $this->bookmarkService->bookmarksCountPerTag(); |
189 | $this->assertEquals(2, $tags[$tagName]); | 190 | $this->assertEquals(2, $tags[$tagName]); |
190 | throw $e; | 191 | throw $e; |
191 | } | 192 | } |
@@ -193,12 +194,12 @@ class PutTagTest extends \PHPUnit\Framework\TestCase | |||
193 | 194 | ||
194 | /** | 195 | /** |
195 | * Test tag update on non existent tag => ApiTagNotFoundException. | 196 | * Test tag update on non existent tag => ApiTagNotFoundException. |
196 | * | ||
197 | * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException | ||
198 | * @expectedExceptionMessage Tag not found | ||
199 | */ | 197 | */ |
200 | public function testPutTag404() | 198 | public function testPutTag404() |
201 | { | 199 | { |
200 | $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class); | ||
201 | $this->expectExceptionMessage('Tag not found'); | ||
202 | |||
202 | $env = Environment::mock([ | 203 | $env = Environment::mock([ |
203 | 'REQUEST_METHOD' => 'PUT', | 204 | 'REQUEST_METHOD' => 'PUT', |
204 | ]); | 205 | ]); |