]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/tags/GetTagNameTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / api / controllers / tags / GetTagNameTest.php
CommitLineData
16e3d006
A
1<?php
2
3namespace Shaarli\Api\Controllers;
4
e26e2060 5use Shaarli\Bookmark\BookmarkFileService;
dea72c71 6use Shaarli\Bookmark\LinkDB;
3c66e564 7use Shaarli\Config\ConfigManager;
e26e2060 8use Shaarli\History;
16e3d006
A
9use Slim\Container;
10use Slim\Http\Environment;
11use Slim\Http\Request;
12use Slim\Http\Response;
13
14/**
d3f42ca4 15 * Class GetTagNameTest
16e3d006 16 *
d3f42ca4 17 * Test getTag by tag name API service.
16e3d006
A
18 *
19 * @package Shaarli\Api\Controllers
20 */
a5a9cf23 21class GetTagNameTest extends \Shaarli\TestCase
16e3d006
A
22{
23 /**
24 * @var string datastore to test write operations
25 */
26 protected static $testDatastore = 'sandbox/datastore.php';
27
28 /**
3c66e564 29 * @var ConfigManager instance
16e3d006
A
30 */
31 protected $conf;
32
33 /**
34 * @var \ReferenceLinkDB instance.
35 */
36 protected $refDB = null;
37
38 /**
39 * @var Container instance.
40 */
41 protected $container;
42
43 /**
d3f42ca4 44 * @var Tags controller instance.
16e3d006
A
45 */
46 protected $controller;
47
48 /**
49 * Number of JSON fields per link.
50 */
d3f42ca4 51 const NB_FIELDS_TAG = 2;
16e3d006
A
52
53 /**
e26e2060 54 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
16e3d006 55 */
8f60e120 56 protected function setUp(): void
16e3d006 57 {
3c66e564 58 $this->conf = new ConfigManager('tests/utils/config/configJson');
e26e2060 59 $this->conf->set('resource.datastore', self::$testDatastore);
16e3d006
A
60 $this->refDB = new \ReferenceLinkDB();
61 $this->refDB->write(self::$testDatastore);
e26e2060 62 $history = new History('sandbox/history.php');
16e3d006
A
63
64 $this->container = new Container();
65 $this->container['conf'] = $this->conf;
e26e2060 66 $this->container['db'] = new BookmarkFileService($this->conf, $history, true);
813849e5 67 $this->container['history'] = null;
16e3d006 68
d3f42ca4 69 $this->controller = new Tags($this->container);
16e3d006
A
70 }
71
72 /**
73 * After each test, remove the test datastore.
74 */
8f60e120 75 protected function tearDown(): void
16e3d006
A
76 {
77 @unlink(self::$testDatastore);
78 }
79
80 /**
d3f42ca4 81 * Test basic getTag service: return gnu tag with 2 occurrences.
16e3d006 82 */
d3f42ca4 83 public function testGetTag()
16e3d006 84 {
d3f42ca4
A
85 $tagName = 'gnu';
86 $env = Environment::mock([
87 'REQUEST_METHOD' => 'GET',
88 ]);
89 $request = Request::createFromEnvironment($env);
90
91 $response = $this->controller->getTag($request, new Response(), ['tagName' => $tagName]);
92 $this->assertEquals(200, $response->getStatusCode());
93 $data = json_decode((string) $response->getBody(), true);
94 $this->assertEquals(self::NB_FIELDS_TAG, count($data));
95 $this->assertEquals($tagName, $data['name']);
96 $this->assertEquals(2, $data['occurrences']);
97 }
16e3d006 98
d3f42ca4
A
99 /**
100 * Test getTag service which is not case sensitive: occurrences with both sTuff and stuff
101 */
102 public function testGetTagNotCaseSensitive()
103 {
104 $tagName = 'sTuff';
16e3d006
A
105 $env = Environment::mock([
106 'REQUEST_METHOD' => 'GET',
107 ]);
108 $request = Request::createFromEnvironment($env);
109
d3f42ca4 110 $response = $this->controller->getTag($request, new Response(), ['tagName' => $tagName]);
16e3d006
A
111 $this->assertEquals(200, $response->getStatusCode());
112 $data = json_decode((string) $response->getBody(), true);
d3f42ca4
A
113 $this->assertEquals(self::NB_FIELDS_TAG, count($data));
114 $this->assertEquals($tagName, $data['name']);
115 $this->assertEquals(2, $data['occurrences']);
16e3d006
A
116 }
117
118 /**
7c57bd95 119 * Test basic getTag service: get non existent tag => ApiTagNotFoundException.
16e3d006 120 */
d3f42ca4 121 public function testGetTag404()
16e3d006 122 {
b1baca99
A
123 $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
124 $this->expectExceptionMessage('Tag not found');
125
16e3d006
A
126 $env = Environment::mock([
127 'REQUEST_METHOD' => 'GET',
128 ]);
129 $request = Request::createFromEnvironment($env);
130
d3f42ca4 131 $this->controller->getTag($request, new Response(), ['tagName' => 'nopenope']);
16e3d006
A
132 }
133}