3 namespace Shaarli\Api\Controllers
;
5 use malkusch\lock\mutex\NoMutex
;
6 use Shaarli\Bookmark\BookmarkFileService
;
7 use Shaarli\Bookmark\LinkDB
;
8 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Plugin\PluginManager
;
12 use Slim\Http\Environment
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
17 * Class GetTagNameTest
19 * Test getTag by tag name API service.
21 * @package Shaarli\Api\Controllers
23 class GetTagNameTest
extends \Shaarli\TestCase
26 * @var string datastore to test write operations
28 protected static $testDatastore = 'sandbox/datastore.php';
31 * @var ConfigManager instance
36 * @var \ReferenceLinkDB instance.
38 protected $refDB = null;
41 * @var Container instance.
46 * @var Tags controller instance.
48 protected $controller;
50 /** @var PluginManager */
51 protected $pluginManager;
54 * Number of JSON fields per link.
56 const NB_FIELDS_TAG
= 2;
59 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
61 protected function setUp(): void
63 $mutex = new NoMutex();
64 $this->conf
= new ConfigManager('tests/utils/config/configJson');
65 $this->conf
->set('resource.datastore', self
::$testDatastore);
66 $this->refDB
= new \
ReferenceLinkDB();
67 $this->refDB
->write(self
::$testDatastore);
68 $history = new History('sandbox/history.php');
70 $this->container
= new Container();
71 $this->container
['conf'] = $this->conf
;
72 $this->pluginManager
= new PluginManager($this->conf
);
73 $this->container
['db'] = new BookmarkFileService(
80 $this->container
['history'] = null;
82 $this->controller
= new Tags($this->container
);
86 * After each test, remove the test datastore.
88 protected function tearDown(): void
90 @unlink(self
::$testDatastore);
94 * Test basic getTag service: return gnu tag with 2 occurrences.
96 public function testGetTag()
99 $env = Environment
::mock([
100 'REQUEST_METHOD' => 'GET',
102 $request = Request
::createFromEnvironment($env);
104 $response = $this->controller
->getTag($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($tagName, $data['name']);
109 $this->assertEquals(2, $data['occurrences']);
113 * Test getTag service which is not case sensitive: occurrences with both sTuff and stuff
115 public function testGetTagNotCaseSensitive()
118 $env = Environment
::mock([
119 'REQUEST_METHOD' => 'GET',
121 $request = Request
::createFromEnvironment($env);
123 $response = $this->controller
->getTag($request, new Response(), ['tagName' => $tagName]);
124 $this->assertEquals(200, $response->getStatusCode());
125 $data = json_decode((string) $response->getBody(), true);
126 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
127 $this->assertEquals($tagName, $data['name']);
128 $this->assertEquals(2, $data['occurrences']);
132 * Test basic getTag service: get non existent tag => ApiTagNotFoundException.
134 public function testGetTag404()
136 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException
::class);
137 $this->expectExceptionMessage('Tag not found');
139 $env = Environment
::mock([
140 'REQUEST_METHOD' => 'GET',
142 $request = Request
::createFromEnvironment($env);
144 $this->controller
->getTag($request, new Response(), ['tagName' => 'nopenope']);