2 namespace Shaarli\Api\Controllers
;
4 use Shaarli\Config\ConfigManager
;
7 use Slim\Http\Environment
;
9 use Slim\Http\Response
;
14 * Test get tag list REST API service.
16 * @package Shaarli\Api\Controllers
18 class GetTagsTest
extends \PHPUnit_Framework_TestCase
21 * @var string datastore to test write operations
23 protected static $testDatastore = 'sandbox/datastore.php';
26 * @var ConfigManager instance
31 * @var \ReferenceLinkDB instance.
33 protected $refDB = null;
36 * @var Container instance.
41 * @var \LinkDB instance.
46 * @var Tags controller instance.
48 protected $controller;
51 * Number of JSON field per link.
53 const NB_FIELDS_TAG
= 2;
56 * Before every test, instantiate a new Api with its config, plugins and links.
58 public function setUp()
60 $this->conf
= new ConfigManager('tests/utils/config/configJson');
61 $this->refDB
= new \
ReferenceLinkDB();
62 $this->refDB
->write(self
::$testDatastore);
64 $this->container
= new Container();
65 $this->container
['conf'] = $this->conf
;
66 $this->linkDB
= new \
LinkDB(self
::$testDatastore, true, false);
67 $this->container
['db'] = $this->linkDB
;
68 $this->container
['history'] = null;
70 $this->controller
= new Tags($this->container
);
74 * After every test, remove the test datastore.
76 public function tearDown()
78 @unlink(self
::$testDatastore);
82 * Test basic getTags service: returns all tags.
84 public function testGetTagsAll()
86 $tags = $this->linkDB
->linksCountPerTag();
87 $env = Environment
::mock([
88 'REQUEST_METHOD' => 'GET',
90 $request = Request
::createFromEnvironment($env);
92 $response = $this->controller
->getTags($request, new Response());
93 $this->assertEquals(200, $response->getStatusCode());
94 $data = json_decode((string) $response->getBody(), true);
95 $this->assertEquals(count($tags), count($data));
98 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
99 $this->assertEquals('web', $data[0]['name']);
100 $this->assertEquals(4, $data[0]['occurrences']);
101 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[1]));
102 $this->assertEquals('cartoon', $data[1]['name']);
103 $this->assertEquals(3, $data[1]['occurrences']);
105 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[5]));
106 $this->assertEquals('sTuff', $data[5]['name']);
107 $this->assertEquals(2, $data[5]['occurrences']);
109 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[count($data) - 1]));
110 $this->assertEquals('w3c', $data[count($data) - 1]['name']);
111 $this->assertEquals(1, $data[count($data) - 1]['occurrences']);
115 * Test getTags service with offset and limit parameter:
116 * limit=1 and offset=1 should return only the second tag, cartoon with 3 occurrences
118 public function testGetTagsOffsetLimit()
120 $env = Environment
::mock([
121 'REQUEST_METHOD' => 'GET',
122 'QUERY_STRING' => 'offset=1&limit=1'
124 $request = Request
::createFromEnvironment($env);
125 $response = $this->controller
->getTags($request, new Response());
126 $this->assertEquals(200, $response->getStatusCode());
127 $data = json_decode((string) $response->getBody(), true);
128 $this->assertEquals(1, count($data));
129 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
130 $this->assertEquals('cartoon', $data[0]['name']);
131 $this->assertEquals(3, $data[0]['occurrences']);
135 * Test getTags with limit=all (return all tags).
137 public function testGetTagsLimitAll()
139 $tags = $this->linkDB
->linksCountPerTag();
140 $env = Environment
::mock([
141 'REQUEST_METHOD' => 'GET',
142 'QUERY_STRING' => 'limit=all'
144 $request = Request
::createFromEnvironment($env);
145 $response = $this->controller
->getTags($request, new Response());
146 $this->assertEquals(200, $response->getStatusCode());
147 $data = json_decode((string) $response->getBody(), true);
148 $this->assertEquals(count($tags), count($data));
152 * Test getTags service with offset and limit parameter:
153 * limit=1 and offset=1 should not return any tag
155 public function testGetTagsOffsetTooHigh()
157 $env = Environment
::mock([
158 'REQUEST_METHOD' => 'GET',
159 'QUERY_STRING' => 'offset=100'
161 $request = Request
::createFromEnvironment($env);
162 $response = $this->controller
->getTags($request, new Response());
163 $this->assertEquals(200, $response->getStatusCode());
164 $data = json_decode((string) $response->getBody(), true);
165 $this->assertEmpty(count($data));
169 * Test getTags with visibility parameter set to private
171 public function testGetTagsVisibilityPrivate()
173 $tags = $this->linkDB
->linksCountPerTag([], 'private');
174 $env = Environment
::mock([
175 'REQUEST_METHOD' => 'GET',
176 'QUERY_STRING' => 'visibility=private'
178 $request = Request
::createFromEnvironment($env);
179 $response = $this->controller
->getTags($request, new Response());
180 $this->assertEquals(200, $response->getStatusCode());
181 $data = json_decode((string) $response->getBody(), true);
182 $this->assertEquals(count($tags), count($data));
183 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
184 $this->assertEquals('Mercurial', $data[0]['name']);
185 $this->assertEquals(1, $data[0]['occurrences']);
189 * Test getTags with visibility parameter set to public
191 public function testGetTagsVisibilityPublic()
193 $tags = $this->linkDB
->linksCountPerTag([], 'public');
194 $env = Environment
::mock(
196 'REQUEST_METHOD' => 'GET',
197 'QUERY_STRING' => 'visibility=public'
200 $request = Request
::createFromEnvironment($env);
201 $response = $this->controller
->getTags($request, new Response());
202 $this->assertEquals(200, $response->getStatusCode());
203 $data = json_decode((string)$response->getBody(), true);
204 $this->assertEquals(count($tags), count($data));
205 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data[0]));
206 $this->assertEquals('web', $data[0]['name']);
207 $this->assertEquals(3, $data[0]['occurrences']);