GetTagNameTest.php 3.5 KB

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