diff options
author | ArthurHoaro <arthur@hoa.ro> | 2018-05-19 15:04:04 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2018-06-04 18:51:22 +0200 |
commit | d3f42ca487287447efb81061609644108044a038 (patch) | |
tree | 6e9d5a33290f857c8f7a04dbdf8cf0ca749db149 /tests/api/controllers/tags/PutTagTest.php | |
parent | 17e45b2e9c33c736751e059276fadb480f98e621 (diff) | |
download | Shaarli-d3f42ca487287447efb81061609644108044a038.tar.gz Shaarli-d3f42ca487287447efb81061609644108044a038.tar.zst Shaarli-d3f42ca487287447efb81061609644108044a038.zip |
Implements Tags endpoints for Shaarli's REST API
Endpoints:
* List All Tags [GET]
* Get a tag [GET]
* Update a tag [PUT]
* Delete a tag [DELETE]
Fixes #904
References shaarli/api-documentation#34
Diffstat (limited to 'tests/api/controllers/tags/PutTagTest.php')
-rw-r--r-- | tests/api/controllers/tags/PutTagTest.php | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/tests/api/controllers/tags/PutTagTest.php b/tests/api/controllers/tags/PutTagTest.php new file mode 100644 index 00000000..6f7dec22 --- /dev/null +++ b/tests/api/controllers/tags/PutTagTest.php | |||
@@ -0,0 +1,209 @@ | |||
1 | <?php | ||
2 | |||
3 | |||
4 | namespace Shaarli\Api\Controllers; | ||
5 | |||
6 | |||
7 | use Shaarli\Api\Exceptions\ApiBadParametersException; | ||
8 | use Shaarli\Config\ConfigManager; | ||
9 | use Slim\Container; | ||
10 | use Slim\Http\Environment; | ||
11 | use Slim\Http\Request; | ||
12 | use Slim\Http\Response; | ||
13 | |||
14 | class PutTagTest extends \PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | /** | ||
17 | * @var string datastore to test write operations | ||
18 | */ | ||
19 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
20 | |||
21 | /** | ||
22 | * @var string datastore to test write operations | ||
23 | */ | ||
24 | protected static $testHistory = 'sandbox/history.php'; | ||
25 | |||
26 | /** | ||
27 | * @var ConfigManager instance | ||
28 | */ | ||
29 | protected $conf; | ||
30 | |||
31 | /** | ||
32 | * @var \ReferenceLinkDB instance. | ||
33 | */ | ||
34 | protected $refDB = null; | ||
35 | |||
36 | /** | ||
37 | * @var \History instance. | ||
38 | */ | ||
39 | protected $history; | ||
40 | |||
41 | /** | ||
42 | * @var Container instance. | ||
43 | */ | ||
44 | protected $container; | ||
45 | |||
46 | /** | ||
47 | * @var \LinkDB instance. | ||
48 | */ | ||
49 | protected $linkDB; | ||
50 | |||
51 | /** | ||
52 | * @var Tags controller instance. | ||
53 | */ | ||
54 | protected $controller; | ||
55 | |||
56 | /** | ||
57 | * Number of JSON field per link. | ||
58 | */ | ||
59 | const NB_FIELDS_TAG = 2; | ||
60 | |||
61 | /** | ||
62 | * Before every test, instantiate a new Api with its config, plugins and links. | ||
63 | */ | ||
64 | public function setUp() | ||
65 | { | ||
66 | $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); | ||
67 | $this->refDB = new \ReferenceLinkDB(); | ||
68 | $this->refDB->write(self::$testDatastore); | ||
69 | |||
70 | $refHistory = new \ReferenceHistory(); | ||
71 | $refHistory->write(self::$testHistory); | ||
72 | $this->history = new \History(self::$testHistory); | ||
73 | |||
74 | $this->container = new Container(); | ||
75 | $this->container['conf'] = $this->conf; | ||
76 | $this->linkDB = new \LinkDB(self::$testDatastore, true, false); | ||
77 | $this->container['db'] = $this->linkDB; | ||
78 | $this->container['history'] = $this->history; | ||
79 | |||
80 | $this->controller = new Tags($this->container); | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * After every test, remove the test datastore. | ||
85 | */ | ||
86 | public function tearDown() | ||
87 | { | ||
88 | @unlink(self::$testDatastore); | ||
89 | @unlink(self::$testHistory); | ||
90 | } | ||
91 | |||
92 | /** | ||
93 | * Test tags update | ||
94 | */ | ||
95 | public function testPutLinkValid() | ||
96 | { | ||
97 | $env = Environment::mock([ | ||
98 | 'REQUEST_METHOD' => 'PUT', | ||
99 | ]); | ||
100 | $tagName = 'gnu'; | ||
101 | $update = ['name' => $newName = 'newtag']; | ||
102 | $request = Request::createFromEnvironment($env); | ||
103 | $request = $request->withParsedBody($update); | ||
104 | |||
105 | $response = $this->controller->putTag($request, new Response(), ['tagName' => $tagName]); | ||
106 | $this->assertEquals(200, $response->getStatusCode()); | ||
107 | $data = json_decode((string) $response->getBody(), true); | ||
108 | $this->assertEquals(self::NB_FIELDS_TAG, count($data)); | ||
109 | $this->assertEquals($newName, $data['name']); | ||
110 | $this->assertEquals(2, $data['occurrences']); | ||
111 | |||
112 | $tags = $this->linkDB->linksCountPerTag(); | ||
113 | $this->assertNotTrue(isset($tags[$tagName])); | ||
114 | $this->assertEquals(2, $tags[$newName]); | ||
115 | |||
116 | $historyEntry = $this->history->getHistory()[0]; | ||
117 | $this->assertEquals(\History::UPDATED, $historyEntry['event']); | ||
118 | $this->assertTrue( | ||
119 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | ||
120 | ); | ||
121 | $historyEntry = $this->history->getHistory()[1]; | ||
122 | $this->assertEquals(\History::UPDATED, $historyEntry['event']); | ||
123 | $this->assertTrue( | ||
124 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | ||
125 | ); | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * Test tag update with an existing tag: they should be merged | ||
130 | */ | ||
131 | public function testPutTagMerge() | ||
132 | { | ||
133 | $tagName = 'gnu'; | ||
134 | $newName = 'w3c'; | ||
135 | |||
136 | $tags = $this->linkDB->linksCountPerTag(); | ||
137 | $this->assertEquals(1, $tags[$newName]); | ||
138 | $this->assertEquals(2, $tags[$tagName]); | ||
139 | |||
140 | $env = Environment::mock([ | ||
141 | 'REQUEST_METHOD' => 'PUT', | ||
142 | ]); | ||
143 | $update = ['name' => $newName]; | ||
144 | $request = Request::createFromEnvironment($env); | ||
145 | $request = $request->withParsedBody($update); | ||
146 | |||
147 | $response = $this->controller->putTag($request, new Response(), ['tagName' => $tagName]); | ||
148 | $this->assertEquals(200, $response->getStatusCode()); | ||
149 | $data = json_decode((string) $response->getBody(), true); | ||
150 | $this->assertEquals(self::NB_FIELDS_TAG, count($data)); | ||
151 | $this->assertEquals($newName, $data['name']); | ||
152 | $this->assertEquals(3, $data['occurrences']); | ||
153 | |||
154 | $tags = $this->linkDB->linksCountPerTag(); | ||
155 | $this->assertNotTrue(isset($tags[$tagName])); | ||
156 | $this->assertEquals(3, $tags[$newName]); | ||
157 | } | ||
158 | |||
159 | /** | ||
160 | * 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 | */ | ||
165 | public function testPutTagEmpty() | ||
166 | { | ||
167 | $tagName = 'gnu'; | ||
168 | $newName = ''; | ||
169 | |||
170 | $tags = $this->linkDB->linksCountPerTag(); | ||
171 | $this->assertEquals(2, $tags[$tagName]); | ||
172 | |||
173 | $env = Environment::mock([ | ||
174 | 'REQUEST_METHOD' => 'PUT', | ||
175 | ]); | ||
176 | $request = Request::createFromEnvironment($env); | ||
177 | |||
178 | $env = Environment::mock([ | ||
179 | 'REQUEST_METHOD' => 'PUT', | ||
180 | ]); | ||
181 | $update = ['name' => $newName]; | ||
182 | $request = Request::createFromEnvironment($env); | ||
183 | $request = $request->withParsedBody($update); | ||
184 | |||
185 | try { | ||
186 | $this->controller->putTag($request, new Response(), ['tagName' => $tagName]); | ||
187 | } catch (ApiBadParametersException $e) { | ||
188 | $tags = $this->linkDB->linksCountPerTag(); | ||
189 | $this->assertEquals(2, $tags[$tagName]); | ||
190 | throw $e; | ||
191 | } | ||
192 | } | ||
193 | |||
194 | /** | ||
195 | * Test tag update on non existent tag => ApiTagNotFoundException. | ||
196 | * | ||
197 | * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException | ||
198 | * @expectedExceptionMessage Tag not found | ||
199 | */ | ||
200 | public function testPutTag404() | ||
201 | { | ||
202 | $env = Environment::mock([ | ||
203 | 'REQUEST_METHOD' => 'PUT', | ||
204 | ]); | ||
205 | $request = Request::createFromEnvironment($env); | ||
206 | |||
207 | $this->controller->putTag($request, new Response(), ['tagName' => 'nopenope']); | ||
208 | } | ||
209 | } | ||