GetLinkIdTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 GetLinkIdTest
  10. *
  11. * Test getLink by ID API service.
  12. *
  13. * @see http://shaarli.github.io/api-documentation/#links-link-get
  14. *
  15. * @package Shaarli\Api\Controllers
  16. */
  17. class GetLinkIdTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var string datastore to test write operations
  21. */
  22. protected static $testDatastore = 'sandbox/datastore.php';
  23. /**
  24. * @var ConfigManager instance
  25. */
  26. protected $conf;
  27. /**
  28. * @var \ReferenceLinkDB instance.
  29. */
  30. protected $refDB = null;
  31. /**
  32. * @var Container instance.
  33. */
  34. protected $container;
  35. /**
  36. * @var Links controller instance.
  37. */
  38. protected $controller;
  39. /**
  40. * Number of JSON fields per link.
  41. */
  42. const NB_FIELDS_LINK = 9;
  43. /**
  44. * Before each test, instantiate a new Api with its config, plugins and links.
  45. */
  46. public function setUp()
  47. {
  48. $this->conf = new ConfigManager('tests/utils/config/configJson');
  49. $this->refDB = new \ReferenceLinkDB();
  50. $this->refDB->write(self::$testDatastore);
  51. $this->container = new Container();
  52. $this->container['conf'] = $this->conf;
  53. $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
  54. $this->container['history'] = null;
  55. $this->controller = new Links($this->container);
  56. }
  57. /**
  58. * After each test, remove the test datastore.
  59. */
  60. public function tearDown()
  61. {
  62. @unlink(self::$testDatastore);
  63. }
  64. /**
  65. * Test basic getLink service: return link ID=41.
  66. */
  67. public function testGetLinkId()
  68. {
  69. // Used by index_url().
  70. $_SERVER['SERVER_NAME'] = 'domain.tld';
  71. $_SERVER['SERVER_PORT'] = 80;
  72. $_SERVER['SCRIPT_NAME'] = '/';
  73. $id = 41;
  74. $env = Environment::mock([
  75. 'REQUEST_METHOD' => 'GET',
  76. ]);
  77. $request = Request::createFromEnvironment($env);
  78. $response = $this->controller->getLink($request, new Response(), ['id' => $id]);
  79. $this->assertEquals(200, $response->getStatusCode());
  80. $data = json_decode((string) $response->getBody(), true);
  81. $this->assertEquals(self::NB_FIELDS_LINK, count($data));
  82. $this->assertEquals($id, $data['id']);
  83. // Check link elements
  84. $this->assertEquals('http://domain.tld/?WDWyig', $data['url']);
  85. $this->assertEquals('WDWyig', $data['shorturl']);
  86. $this->assertEquals('Link title: @website', $data['title']);
  87. $this->assertEquals(
  88. 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
  89. $data['description']
  90. );
  91. $this->assertEquals('sTuff', $data['tags'][0]);
  92. $this->assertEquals(false, $data['private']);
  93. $this->assertEquals(
  94. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
  95. $data['created']
  96. );
  97. $this->assertEmpty($data['updated']);
  98. }
  99. /**
  100. * Test basic getLink service: get non existent link => ApiLinkNotFoundException.
  101. *
  102. * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
  103. * @expectedExceptionMessage Link not found
  104. */
  105. public function testGetLink404()
  106. {
  107. $env = Environment::mock([
  108. 'REQUEST_METHOD' => 'GET',
  109. ]);
  110. $request = Request::createFromEnvironment($env);
  111. $this->controller->getLink($request, new Response(), ['id' => -1]);
  112. }
  113. }