]>
Commit | Line | Data |
---|---|---|
16e3d006 A |
1 | <?php |
2 | ||
3 | namespace Shaarli\Api\Controllers; | |
4 | ||
fd1ddad9 | 5 | use malkusch\lock\mutex\NoMutex; |
e26e2060 | 6 | use Shaarli\Bookmark\BookmarkFileService; |
dea72c71 | 7 | use Shaarli\Bookmark\LinkDB; |
3c66e564 | 8 | use Shaarli\Config\ConfigManager; |
e26e2060 | 9 | use Shaarli\History; |
16e3d006 A |
10 | use Slim\Container; |
11 | use Slim\Http\Environment; | |
12 | use Slim\Http\Request; | |
13 | use Slim\Http\Response; | |
14 | ||
15 | /** | |
d3f42ca4 | 16 | * Class GetTagNameTest |
16e3d006 | 17 | * |
d3f42ca4 | 18 | * Test getTag by tag name API service. |
16e3d006 A |
19 | * |
20 | * @package Shaarli\Api\Controllers | |
21 | */ | |
a5a9cf23 | 22 | class GetTagNameTest extends \Shaarli\TestCase |
16e3d006 A |
23 | { |
24 | /** | |
25 | * @var string datastore to test write operations | |
26 | */ | |
27 | protected static $testDatastore = 'sandbox/datastore.php'; | |
28 | ||
29 | /** | |
3c66e564 | 30 | * @var ConfigManager instance |
16e3d006 A |
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 | /** | |
d3f42ca4 | 45 | * @var Tags controller instance. |
16e3d006 A |
46 | */ |
47 | protected $controller; | |
48 | ||
49 | /** | |
50 | * Number of JSON fields per link. | |
51 | */ | |
d3f42ca4 | 52 | const NB_FIELDS_TAG = 2; |
16e3d006 A |
53 | |
54 | /** | |
e26e2060 | 55 | * Before each test, instantiate a new Api with its config, plugins and bookmarks. |
16e3d006 | 56 | */ |
8f60e120 | 57 | protected function setUp(): void |
16e3d006 | 58 | { |
fd1ddad9 | 59 | $mutex = new NoMutex(); |
3c66e564 | 60 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
e26e2060 | 61 | $this->conf->set('resource.datastore', self::$testDatastore); |
16e3d006 A |
62 | $this->refDB = new \ReferenceLinkDB(); |
63 | $this->refDB->write(self::$testDatastore); | |
e26e2060 | 64 | $history = new History('sandbox/history.php'); |
16e3d006 A |
65 | |
66 | $this->container = new Container(); | |
67 | $this->container['conf'] = $this->conf; | |
fd1ddad9 | 68 | $this->container['db'] = new BookmarkFileService($this->conf, $history, $mutex, true); |
813849e5 | 69 | $this->container['history'] = null; |
16e3d006 | 70 | |
d3f42ca4 | 71 | $this->controller = new Tags($this->container); |
16e3d006 A |
72 | } |
73 | ||
74 | /** | |
75 | * After each test, remove the test datastore. | |
76 | */ | |
8f60e120 | 77 | protected function tearDown(): void |
16e3d006 A |
78 | { |
79 | @unlink(self::$testDatastore); | |
80 | } | |
81 | ||
82 | /** | |
d3f42ca4 | 83 | * Test basic getTag service: return gnu tag with 2 occurrences. |
16e3d006 | 84 | */ |
d3f42ca4 | 85 | public function testGetTag() |
16e3d006 | 86 | { |
d3f42ca4 A |
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 | } | |
16e3d006 | 100 | |
d3f42ca4 A |
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'; | |
16e3d006 A |
107 | $env = Environment::mock([ |
108 | 'REQUEST_METHOD' => 'GET', | |
109 | ]); | |
110 | $request = Request::createFromEnvironment($env); | |
111 | ||
d3f42ca4 | 112 | $response = $this->controller->getTag($request, new Response(), ['tagName' => $tagName]); |
16e3d006 A |
113 | $this->assertEquals(200, $response->getStatusCode()); |
114 | $data = json_decode((string) $response->getBody(), true); | |
d3f42ca4 A |
115 | $this->assertEquals(self::NB_FIELDS_TAG, count($data)); |
116 | $this->assertEquals($tagName, $data['name']); | |
117 | $this->assertEquals(2, $data['occurrences']); | |
16e3d006 A |
118 | } |
119 | ||
120 | /** | |
7c57bd95 | 121 | * Test basic getTag service: get non existent tag => ApiTagNotFoundException. |
16e3d006 | 122 | */ |
d3f42ca4 | 123 | public function testGetTag404() |
16e3d006 | 124 | { |
b1baca99 A |
125 | $this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class); |
126 | $this->expectExceptionMessage('Tag not found'); | |
127 | ||
16e3d006 A |
128 | $env = Environment::mock([ |
129 | 'REQUEST_METHOD' => 'GET', | |
130 | ]); | |
131 | $request = Request::createFromEnvironment($env); | |
132 | ||
d3f42ca4 | 133 | $this->controller->getTag($request, new Response(), ['tagName' => 'nopenope']); |
16e3d006 A |
134 | } |
135 | } |