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