]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/tags/GetTagNameTest.php
Merge pull request #1698 from ArthurHoaro/feature/plugins-search-filter
[github/shaarli/Shaarli.git] / tests / api / controllers / tags / GetTagNameTest.php
CommitLineData
16e3d006
A
1<?php
2
3namespace Shaarli\Api\Controllers;
4
fd1ddad9 5use malkusch\lock\mutex\NoMutex;
e26e2060 6use Shaarli\Bookmark\BookmarkFileService;
dea72c71 7use Shaarli\Bookmark\LinkDB;
3c66e564 8use Shaarli\Config\ConfigManager;
e26e2060 9use Shaarli\History;
bcba6bd3 10use Shaarli\Plugin\PluginManager;
16e3d006
A
11use Slim\Container;
12use Slim\Http\Environment;
13use Slim\Http\Request;
14use Slim\Http\Response;
15
16/**
d3f42ca4 17 * Class GetTagNameTest
16e3d006 18 *
d3f42ca4 19 * Test getTag by tag name API service.
16e3d006
A
20 *
21 * @package Shaarli\Api\Controllers
22 */
a5a9cf23 23class GetTagNameTest extends \Shaarli\TestCase
16e3d006
A
24{
25 /**
26 * @var string datastore to test write operations
27 */
28 protected static $testDatastore = 'sandbox/datastore.php';
29
30 /**
3c66e564 31 * @var ConfigManager instance
16e3d006
A
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 /**
d3f42ca4 46 * @var Tags controller instance.
16e3d006
A
47 */
48 protected $controller;
49
bcba6bd3
A
50 /** @var PluginManager */
51 protected $pluginManager;
52
16e3d006
A
53 /**
54 * Number of JSON fields per link.
55 */
d3f42ca4 56 const NB_FIELDS_TAG = 2;
16e3d006
A
57
58 /**
e26e2060 59 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
16e3d006 60 */
8f60e120 61 protected function setUp(): void
16e3d006 62 {
fd1ddad9 63 $mutex = new NoMutex();
3c66e564 64 $this->conf = new ConfigManager('tests/utils/config/configJson');
e26e2060 65 $this->conf->set('resource.datastore', self::$testDatastore);
16e3d006
A
66 $this->refDB = new \ReferenceLinkDB();
67 $this->refDB->write(self::$testDatastore);
e26e2060 68 $history = new History('sandbox/history.php');
16e3d006
A
69
70 $this->container = new Container();
71 $this->container['conf'] = $this->conf;
bcba6bd3
A
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 );
813849e5 80 $this->container['history'] = null;
16e3d006 81
d3f42ca4 82 $this->controller = new Tags($this->container);
16e3d006
A
83 }
84
85 /**
86 * After each test, remove the test datastore.
87 */
8f60e120 88 protected function tearDown(): void
16e3d006
A
89 {
90 @unlink(self::$testDatastore);
91 }
92
93 /**
d3f42ca4 94 * Test basic getTag service: return gnu tag with 2 occurrences.
16e3d006 95 */
d3f42ca4 96 public function testGetTag()
16e3d006 97 {
d3f42ca4
A
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 }
16e3d006 111
d3f42ca4
A
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';
16e3d006
A
118 $env = Environment::mock([
119 'REQUEST_METHOD' => 'GET',
120 ]);
121 $request = Request::createFromEnvironment($env);
122
d3f42ca4 123 $response = $this->controller->getTag($request, new Response(), ['tagName' => $tagName]);
16e3d006
A
124 $this->assertEquals(200, $response->getStatusCode());
125 $data = json_decode((string) $response->getBody(), true);
d3f42ca4
A
126 $this->assertEquals(self::NB_FIELDS_TAG, count($data));
127 $this->assertEquals($tagName, $data['name']);
128 $this->assertEquals(2, $data['occurrences']);
16e3d006
A
129 }
130
131 /**
7c57bd95 132 * Test basic getTag service: get non existent tag => ApiTagNotFoundException.
16e3d006 133 */
d3f42ca4 134 public function testGetTag404()
16e3d006 135 {
b1baca99
A
136 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
137 $this->expectExceptionMessage('Tag not found');
138
16e3d006
A
139 $env = Environment::mock([
140 'REQUEST_METHOD' => 'GET',
141 ]);
142 $request = Request::createFromEnvironment($env);
143
d3f42ca4 144 $this->controller->getTag($request, new Response(), ['tagName' => 'nopenope']);
16e3d006
A
145 }
146}