]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/api/controllers/tags/GetTagNameTest.php
878de5a4202d2dbb5a2e54ec2f29a719cc8a5412
[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 Slim\Container;
11 use Slim\Http\Environment;
12 use Slim\Http\Request;
13 use Slim\Http\Response;
14
15 /**
16 * Class GetTagNameTest
17 *
18 * Test getTag by tag name API service.
19 *
20 * @package Shaarli\Api\Controllers
21 */
22 class GetTagNameTest extends \Shaarli\TestCase
23 {
24 /**
25 * @var string datastore to test write operations
26 */
27 protected static $testDatastore = 'sandbox/datastore.php';
28
29 /**
30 * @var ConfigManager instance
31 */
32 protected $conf;
33
34 /**
35 * @var \ReferenceLinkDB instance.
36 */
37 protected $refDB = null;
38
39 /**
40 * @var Container instance.
41 */
42 protected $container;
43
44 /**
45 * @var Tags controller instance.
46 */
47 protected $controller;
48
49 /**
50 * Number of JSON fields per link.
51 */
52 const NB_FIELDS_TAG = 2;
53
54 /**
55 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
56 */
57 protected function setUp(): void
58 {
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');
65
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;
70
71 $this->controller = new Tags($this->container);
72 }
73
74 /**
75 * After each test, remove the test datastore.
76 */
77 protected function tearDown(): void
78 {
79 @unlink(self::$testDatastore);
80 }
81
82 /**
83 * Test basic getTag service: return gnu tag with 2 occurrences.
84 */
85 public function testGetTag()
86 {
87 $tagName = 'gnu';
88 $env = Environment::mock([
89 'REQUEST_METHOD' => 'GET',
90 ]);
91 $request = Request::createFromEnvironment($env);
92
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']);
99 }
100
101 /**
102 * Test getTag service which is not case sensitive: occurrences with both sTuff and stuff
103 */
104 public function testGetTagNotCaseSensitive()
105 {
106 $tagName = 'sTuff';
107 $env = Environment::mock([
108 'REQUEST_METHOD' => 'GET',
109 ]);
110 $request = Request::createFromEnvironment($env);
111
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']);
118 }
119
120 /**
121 * Test basic getTag service: get non existent tag => ApiTagNotFoundException.
122 */
123 public function testGetTag404()
124 {
125 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
126 $this->expectExceptionMessage('Tag not found');
127
128 $env = Environment::mock([
129 'REQUEST_METHOD' => 'GET',
130 ]);
131 $request = Request::createFromEnvironment($env);
132
133 $this->controller->getTag($request, new Response(), ['tagName' => 'nopenope']);
134 }
135 }