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