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
;
11 use Slim\Http\Environment
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
16 * Class GetTagNameTest
18 * Test getTag by tag name API service.
20 * @package Shaarli\Api\Controllers
22 class GetTagNameTest
extends \Shaarli\TestCase
25 * @var string datastore to test write operations
27 protected static $testDatastore = 'sandbox/datastore.php';
30 * @var ConfigManager instance
35 * @var \ReferenceLinkDB instance.
37 protected $refDB = null;
40 * @var Container instance.
45 * @var Tags controller instance.
47 protected $controller;
50 * Number of JSON fields per link.
52 const NB_FIELDS_TAG
= 2;
55 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
57 protected function setUp(): void
59 $mutex = new NoMutex();
60 $this->conf
= new ConfigManager('tests/utils/config/configJson');
61 $this->conf
->set('resource.datastore', self
::$testDatastore);
62 $this->refDB
= new \
ReferenceLinkDB();
63 $this->refDB
->write(self
::$testDatastore);
64 $history = new History('sandbox/history.php');
66 $this->container
= new Container();
67 $this->container
['conf'] = $this->conf
;
68 $this->container
['db'] = new BookmarkFileService($this->conf
, $history, $mutex, true);
69 $this->container
['history'] = null;
71 $this->controller
= new Tags($this->container
);
75 * After each test, remove the test datastore.
77 protected function tearDown(): void
79 @unlink(self
::$testDatastore);
83 * Test basic getTag service: return gnu tag with 2 occurrences.
85 public function testGetTag()
88 $env = Environment
::mock([
89 'REQUEST_METHOD' => 'GET',
91 $request = Request
::createFromEnvironment($env);
93 $response = $this->controller
->getTag($request, new Response(), ['tagName' => $tagName]);
94 $this->assertEquals(200, $response->getStatusCode());
95 $data = json_decode((string) $response->getBody(), true);
96 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
97 $this->assertEquals($tagName, $data['name']);
98 $this->assertEquals(2, $data['occurrences']);
102 * Test getTag service which is not case sensitive: occurrences with both sTuff and stuff
104 public function testGetTagNotCaseSensitive()
107 $env = Environment
::mock([
108 'REQUEST_METHOD' => 'GET',
110 $request = Request
::createFromEnvironment($env);
112 $response = $this->controller
->getTag($request, new Response(), ['tagName' => $tagName]);
113 $this->assertEquals(200, $response->getStatusCode());
114 $data = json_decode((string) $response->getBody(), true);
115 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
116 $this->assertEquals($tagName, $data['name']);
117 $this->assertEquals(2, $data['occurrences']);
121 * Test basic getTag service: get non existent tag => ApiTagNotFoundException.
123 public function testGetTag404()
125 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException
::class);
126 $this->expectExceptionMessage('Tag not found');
128 $env = Environment
::mock([
129 'REQUEST_METHOD' => 'GET',
131 $request = Request
::createFromEnvironment($env);
133 $this->controller
->getTag($request, new Response(), ['tagName' => 'nopenope']);