]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/api/controllers/tags/GetTagNameTest.php
New plugin hook: ability to add custom filters to Shaarli search engine
[github/shaarli/Shaarli.git] / tests / api / controllers / tags / GetTagNameTest.php
1 <?php
2
3 namespace Shaarli\Api\Controllers;
4
5 use malkusch\lock\mutex\NoMutex;
6 use Shaarli\Bookmark\BookmarkFileService;
7 use Shaarli\Bookmark\LinkDB;
8 use Shaarli\Config\ConfigManager;
9 use Shaarli\History;
10 use Shaarli\Plugin\PluginManager;
11 use Slim\Container;
12 use Slim\Http\Environment;
13 use Slim\Http\Request;
14 use Slim\Http\Response;
15
16 /**
17 * Class GetTagNameTest
18 *
19 * Test getTag by tag name API service.
20 *
21 * @package Shaarli\Api\Controllers
22 */
23 class GetTagNameTest extends \Shaarli\TestCase
24 {
25 /**
26 * @var string datastore to test write operations
27 */
28 protected static $testDatastore = 'sandbox/datastore.php';
29
30 /**
31 * @var ConfigManager instance
32 */
33 protected $conf;
34
35 /**
36 * @var \ReferenceLinkDB instance.
37 */
38 protected $refDB = null;
39
40 /**
41 * @var Container instance.
42 */
43 protected $container;
44
45 /**
46 * @var Tags controller instance.
47 */
48 protected $controller;
49
50 /** @var PluginManager */
51 protected $pluginManager;
52
53 /**
54 * Number of JSON fields per link.
55 */
56 const NB_FIELDS_TAG = 2;
57
58 /**
59 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
60 */
61 protected function setUp(): void
62 {
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');
69
70 $this->container = new Container();
71 $this->container['conf'] = $this->conf;
72 $this->pluginManager = new PluginManager($this->conf);
73 $this->container['db'] = new BookmarkFileService(
74 $this->conf,
75 $this->pluginManager,
76 $history,
77 $mutex,
78 true
79 );
80 $this->container['history'] = null;
81
82 $this->controller = new Tags($this->container);
83 }
84
85 /**
86 * After each test, remove the test datastore.
87 */
88 protected function tearDown(): void
89 {
90 @unlink(self::$testDatastore);
91 }
92
93 /**
94 * Test basic getTag service: return gnu tag with 2 occurrences.
95 */
96 public function testGetTag()
97 {
98 $tagName = 'gnu';
99 $env = Environment::mock([
100 'REQUEST_METHOD' => 'GET',
101 ]);
102 $request = Request::createFromEnvironment($env);
103
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']);
110 }
111
112 /**
113 * Test getTag service which is not case sensitive: occurrences with both sTuff and stuff
114 */
115 public function testGetTagNotCaseSensitive()
116 {
117 $tagName = 'sTuff';
118 $env = Environment::mock([
119 'REQUEST_METHOD' => 'GET',
120 ]);
121 $request = Request::createFromEnvironment($env);
122
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']);
129 }
130
131 /**
132 * Test basic getTag service: get non existent tag => ApiTagNotFoundException.
133 */
134 public function testGetTag404()
135 {
136 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
137 $this->expectExceptionMessage('Tag not found');
138
139 $env = Environment::mock([
140 'REQUEST_METHOD' => 'GET',
141 ]);
142 $request = Request::createFromEnvironment($env);
143
144 $this->controller->getTag($request, new Response(), ['tagName' => 'nopenope']);
145 }
146 }