3 namespace Shaarli\Api\Controllers
;
5 use Shaarli\Bookmark\LinkDB
;
6 use Shaarli\Config\ConfigManager
;
8 use Slim\Http\Environment
;
10 use Slim\Http\Response
;
13 * Class GetTagNameTest
15 * Test getTag by tag name API service.
17 * @package Shaarli\Api\Controllers
19 class GetTagNameTest
extends \PHPUnit\Framework\TestCase
22 * @var string datastore to test write operations
24 protected static $testDatastore = 'sandbox/datastore.php';
27 * @var ConfigManager instance
32 * @var \ReferenceLinkDB instance.
34 protected $refDB = null;
37 * @var Container instance.
42 * @var Tags controller instance.
44 protected $controller;
47 * Number of JSON fields per link.
49 const NB_FIELDS_TAG
= 2;
52 * Before each test, instantiate a new Api with its config, plugins and links.
54 public function setUp()
56 $this->conf
= new ConfigManager('tests/utils/config/configJson');
57 $this->refDB
= new \
ReferenceLinkDB();
58 $this->refDB
->write(self
::$testDatastore);
60 $this->container
= new Container();
61 $this->container
['conf'] = $this->conf
;
62 $this->container
['db'] = new LinkDB(self
::$testDatastore, true, false);
63 $this->container
['history'] = null;
65 $this->controller
= new Tags($this->container
);
69 * After each test, remove the test datastore.
71 public function tearDown()
73 @unlink(self
::$testDatastore);
77 * Test basic getTag service: return gnu tag with 2 occurrences.
79 public function testGetTag()
82 $env = Environment
::mock([
83 'REQUEST_METHOD' => 'GET',
85 $request = Request
::createFromEnvironment($env);
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']);
96 * Test getTag service which is not case sensitive: occurrences with both sTuff and stuff
98 public function testGetTagNotCaseSensitive()
101 $env = Environment
::mock([
102 'REQUEST_METHOD' => 'GET',
104 $request = Request
::createFromEnvironment($env);
106 $response = $this->controller
->getTag($request, new Response(), ['tagName' => $tagName]);
107 $this->assertEquals(200, $response->getStatusCode());
108 $data = json_decode((string) $response->getBody(), true);
109 $this->assertEquals(self
::NB_FIELDS_TAG
, count($data));
110 $this->assertEquals($tagName, $data['name']);
111 $this->assertEquals(2, $data['occurrences']);
115 * Test basic getTag service: get non existent tag => ApiTagNotFoundException.
117 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
118 * @expectedExceptionMessage Tag not found
120 public function testGetTag404()
122 $env = Environment
::mock([
123 'REQUEST_METHOD' => 'GET',
125 $request = Request
::createFromEnvironment($env);
127 $this->controller
->getTag($request, new Response(), ['tagName' => 'nopenope']);