3 namespace Shaarli\Api\Controllers
;
5 use Shaarli\Bookmark\BookmarkFileService
;
6 use Shaarli\Bookmark\LinkDB
;
7 use Shaarli\Config\ConfigManager
;
10 use Slim\Http\Environment
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
15 * Class GetTagNameTest
17 * Test getTag by tag name API service.
19 * @package Shaarli\Api\Controllers
21 class GetTagNameTest
extends \PHPUnit\Framework\TestCase
24 * @var string datastore to test write operations
26 protected static $testDatastore = 'sandbox/datastore.php';
29 * @var ConfigManager instance
34 * @var \ReferenceLinkDB instance.
36 protected $refDB = null;
39 * @var Container instance.
44 * @var Tags controller instance.
46 protected $controller;
49 * Number of JSON fields per link.
51 const NB_FIELDS_TAG
= 2;
54 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
56 public function setUp()
58 $this->conf
= new ConfigManager('tests/utils/config/configJson');
59 $this->conf
->set('resource.datastore', self
::$testDatastore);
60 $this->refDB
= new \
ReferenceLinkDB();
61 $this->refDB
->write(self
::$testDatastore);
62 $history = new History('sandbox/history.php');
64 $this->container
= new Container();
65 $this->container
['conf'] = $this->conf
;
66 $this->container
['db'] = new BookmarkFileService($this->conf
, $history, true);
67 $this->container
['history'] = null;
69 $this->controller
= new Tags($this->container
);
73 * After each test, remove the test datastore.
75 public function tearDown()
77 @unlink(self
::$testDatastore);
81 * Test basic getTag service: return gnu tag with 2 occurrences.
83 public function testGetTag()
86 $env = Environment
::mock([
87 'REQUEST_METHOD' => 'GET',
89 $request = Request
::createFromEnvironment($env);
91 $response = $this->controller
->getTag($request, new Response(), ['tagName' => $tagName]);
92 $this->assertEquals(200, $response->getStatusCode());
93 $data = json_decode((string) $response->getBody(), true);
94 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
95 $this->assertEquals($tagName, $data['name']);
96 $this->assertEquals(2, $data['occurrences']);
100 * Test getTag service which is not case sensitive: occurrences with both sTuff and stuff
102 public function testGetTagNotCaseSensitive()
105 $env = Environment
::mock([
106 'REQUEST_METHOD' => 'GET',
108 $request = Request
::createFromEnvironment($env);
110 $response = $this->controller
->getTag($request, new Response(), ['tagName' => $tagName]);
111 $this->assertEquals(200, $response->getStatusCode());
112 $data = json_decode((string) $response->getBody(), true);
113 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
114 $this->assertEquals($tagName, $data['name']);
115 $this->assertEquals(2, $data['occurrences']);
119 * Test basic getTag service: get non existent tag => ApiTagNotFoundException.
121 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
122 * @expectedExceptionMessage Tag not found
124 public function testGetTag404()
126 $env = Environment
::mock([
127 'REQUEST_METHOD' => 'GET',
129 $request = Request
::createFromEnvironment($env);
131 $this->controller
->getTag($request, new Response(), ['tagName' => 'nopenope']);