4 namespace Shaarli\Api\Controllers
;
7 use Shaarli\Api\Exceptions\ApiBadParametersException
;
8 use Shaarli\Config\ConfigManager
;
10 use Slim\Http\Environment
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
14 class PutTagTest
extends \PHPUnit_Framework_TestCase
17 * @var string datastore to test write operations
19 protected static $testDatastore = 'sandbox/datastore.php';
22 * @var string datastore to test write operations
24 protected static $testHistory = 'sandbox/history.php';
27 * @var ConfigManager instance
32 * @var \ReferenceLinkDB instance.
34 protected $refDB = null;
37 * @var \History instance.
42 * @var Container instance.
47 * @var \LinkDB instance.
52 * @var Tags controller instance.
54 protected $controller;
57 * Number of JSON field per link.
59 const NB_FIELDS_TAG
= 2;
62 * Before every test, instantiate a new Api with its config, plugins and links.
64 public function setUp()
66 $this->conf
= new ConfigManager('tests/utils/config/configJson.json.php');
67 $this->refDB
= new \
ReferenceLinkDB();
68 $this->refDB
->write(self
::$testDatastore);
70 $refHistory = new \
ReferenceHistory();
71 $refHistory->write(self
::$testHistory);
72 $this->history
= new \
History(self
::$testHistory);
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
;
80 $this->controller
= new Tags($this->container
);
84 * After every test, remove the test datastore.
86 public function tearDown()
88 @unlink(self
::$testDatastore);
89 @unlink(self
::$testHistory);
95 public function testPutLinkValid()
97 $env = Environment
::mock([
98 'REQUEST_METHOD' => 'PUT',
101 $update = ['name' => $newName = 'newtag'];
102 $request = Request
::createFromEnvironment($env);
103 $request = $request->withParsedBody($update);
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']);
112 $tags = $this->linkDB
->linksCountPerTag();
113 $this->assertNotTrue(isset($tags[$tagName]));
114 $this->assertEquals(2, $tags[$newName]);
116 $historyEntry = $this->history
->getHistory()[0];
117 $this->assertEquals(\History
::UPDATED
, $historyEntry['event']);
119 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
121 $historyEntry = $this->history
->getHistory()[1];
122 $this->assertEquals(\History
::UPDATED
, $historyEntry['event']);
124 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
129 * Test tag update with an existing tag: they should be merged
131 public function testPutTagMerge()
136 $tags = $this->linkDB
->linksCountPerTag();
137 $this->assertEquals(1, $tags[$newName]);
138 $this->assertEquals(2, $tags[$tagName]);
140 $env = Environment
::mock([
141 'REQUEST_METHOD' => 'PUT',
143 $update = ['name' => $newName];
144 $request = Request
::createFromEnvironment($env);
145 $request = $request->withParsedBody($update);
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']);
154 $tags = $this->linkDB
->linksCountPerTag();
155 $this->assertNotTrue(isset($tags[$tagName]));
156 $this->assertEquals(3, $tags[$newName]);
160 * Test tag update with an empty new tag name => ApiBadParametersException
162 * @expectedException Shaarli\Api\Exceptions\ApiBadParametersException
163 * @expectedExceptionMessage New tag name is required in the request body
165 public function testPutTagEmpty()
170 $tags = $this->linkDB
->linksCountPerTag();
171 $this->assertEquals(2, $tags[$tagName]);
173 $env = Environment
::mock([
174 'REQUEST_METHOD' => 'PUT',
176 $request = Request
::createFromEnvironment($env);
178 $env = Environment
::mock([
179 'REQUEST_METHOD' => 'PUT',
181 $update = ['name' => $newName];
182 $request = Request
::createFromEnvironment($env);
183 $request = $request->withParsedBody($update);
186 $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
187 } catch (ApiBadParametersException
$e) {
188 $tags = $this->linkDB
->linksCountPerTag();
189 $this->assertEquals(2, $tags[$tagName]);
195 * Test tag update on non existent tag => ApiTagNotFoundException.
197 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
198 * @expectedExceptionMessage Tag not found
200 public function testPutTag404()
202 $env = Environment
::mock([
203 'REQUEST_METHOD' => 'PUT',
205 $request = Request
::createFromEnvironment($env);
207 $this->controller
->putTag($request, new Response(), ['tagName' => 'nopenope']);