4 namespace Shaarli\Api\Controllers
;
6 use Shaarli\Api\Exceptions\ApiBadParametersException
;
7 use Shaarli\Config\ConfigManager
;
9 use Slim\Http\Environment
;
10 use Slim\Http\Request
;
11 use Slim\Http\Response
;
13 class PutTagTest
extends \PHPUnit_Framework_TestCase
16 * @var string datastore to test write operations
18 protected static $testDatastore = 'sandbox/datastore.php';
21 * @var string datastore to test write operations
23 protected static $testHistory = 'sandbox/history.php';
26 * @var ConfigManager instance
31 * @var \ReferenceLinkDB instance.
33 protected $refDB = null;
36 * @var \Shaarli\History instance.
41 * @var Container instance.
46 * @var \Shaarli\Bookmark\LinkDB instance.
51 * @var Tags controller instance.
53 protected $controller;
56 * Number of JSON field per link.
58 const NB_FIELDS_TAG
= 2;
61 * Before every test, instantiate a new Api with its config, plugins and links.
63 public function setUp()
65 $this->conf
= new ConfigManager('tests/utils/config/configJson.json.php');
66 $this->refDB
= new \
ReferenceLinkDB();
67 $this->refDB
->write(self
::$testDatastore);
69 $refHistory = new \
ReferenceHistory();
70 $refHistory->write(self
::$testHistory);
71 $this->history
= new \Shaarli\
History(self
::$testHistory);
73 $this->container
= new Container();
74 $this->container
['conf'] = $this->conf
;
75 $this->linkDB
= new \Shaarli\Bookmark\
LinkDB(self
::$testDatastore, true, false);
76 $this->container
['db'] = $this->linkDB
;
77 $this->container
['history'] = $this->history
;
79 $this->controller
= new Tags($this->container
);
83 * After every test, remove the test datastore.
85 public function tearDown()
87 @unlink(self
::$testDatastore);
88 @unlink(self
::$testHistory);
94 public function testPutLinkValid()
96 $env = Environment
::mock([
97 'REQUEST_METHOD' => 'PUT',
100 $update = ['name' => $newName = 'newtag'];
101 $request = Request
::createFromEnvironment($env);
102 $request = $request->withParsedBody($update);
104 $response = $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
105 $this->assertEquals(200, $response->getStatusCode());
106 $data = json_decode((string) $response->getBody(), true);
107 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
108 $this->assertEquals($newName, $data['name']);
109 $this->assertEquals(2, $data['occurrences']);
111 $tags = $this->linkDB
->linksCountPerTag();
112 $this->assertNotTrue(isset($tags[$tagName]));
113 $this->assertEquals(2, $tags[$newName]);
115 $historyEntry = $this->history
->getHistory()[0];
116 $this->assertEquals(\Shaarli\History
::UPDATED
, $historyEntry['event']);
118 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
120 $historyEntry = $this->history
->getHistory()[1];
121 $this->assertEquals(\Shaarli\History
::UPDATED
, $historyEntry['event']);
123 (new \
DateTime())->add(\DateInterval
::createFromDateString('-5 seconds')) < $historyEntry['datetime']
128 * Test tag update with an existing tag: they should be merged
130 public function testPutTagMerge()
135 $tags = $this->linkDB
->linksCountPerTag();
136 $this->assertEquals(1, $tags[$newName]);
137 $this->assertEquals(2, $tags[$tagName]);
139 $env = Environment
::mock([
140 'REQUEST_METHOD' => 'PUT',
142 $update = ['name' => $newName];
143 $request = Request
::createFromEnvironment($env);
144 $request = $request->withParsedBody($update);
146 $response = $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
147 $this->assertEquals(200, $response->getStatusCode());
148 $data = json_decode((string) $response->getBody(), true);
149 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
150 $this->assertEquals($newName, $data['name']);
151 $this->assertEquals(3, $data['occurrences']);
153 $tags = $this->linkDB
->linksCountPerTag();
154 $this->assertNotTrue(isset($tags[$tagName]));
155 $this->assertEquals(3, $tags[$newName]);
159 * Test tag update with an empty new tag name => ApiBadParametersException
161 * @expectedException Shaarli\Api\Exceptions\ApiBadParametersException
162 * @expectedExceptionMessage New tag name is required in the request body
164 public function testPutTagEmpty()
169 $tags = $this->linkDB
->linksCountPerTag();
170 $this->assertEquals(2, $tags[$tagName]);
172 $env = Environment
::mock([
173 'REQUEST_METHOD' => 'PUT',
175 $request = Request
::createFromEnvironment($env);
177 $env = Environment
::mock([
178 'REQUEST_METHOD' => 'PUT',
180 $update = ['name' => $newName];
181 $request = Request
::createFromEnvironment($env);
182 $request = $request->withParsedBody($update);
185 $this->controller
->putTag($request, new Response(), ['tagName' => $tagName]);
186 } catch (ApiBadParametersException
$e) {
187 $tags = $this->linkDB
->linksCountPerTag();
188 $this->assertEquals(2, $tags[$tagName]);
194 * Test tag update on non existent tag => ApiTagNotFoundException.
196 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
197 * @expectedExceptionMessage Tag not found
199 public function testPutTag404()
201 $env = Environment
::mock([
202 'REQUEST_METHOD' => 'PUT',
204 $request = Request
::createFromEnvironment($env);
206 $this->controller
->putTag($request, new Response(), ['tagName' => 'nopenope']);