DeleteLinkTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. class DeleteLinkTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var string datastore to test write operations
  12. */
  13. protected static $testDatastore = 'sandbox/datastore.php';
  14. /**
  15. * @var string datastore to test write operations
  16. */
  17. protected static $testHistory = 'sandbox/history.php';
  18. /**
  19. * @var ConfigManager instance
  20. */
  21. protected $conf;
  22. /**
  23. * @var \ReferenceLinkDB instance.
  24. */
  25. protected $refDB = null;
  26. /**
  27. * @var \LinkDB instance.
  28. */
  29. protected $linkDB;
  30. /**
  31. * @var \History instance.
  32. */
  33. protected $history;
  34. /**
  35. * @var Container instance.
  36. */
  37. protected $container;
  38. /**
  39. * @var Links controller instance.
  40. */
  41. protected $controller;
  42. /**
  43. * Before each test, instantiate a new Api with its config, plugins and links.
  44. */
  45. public function setUp()
  46. {
  47. $this->conf = new ConfigManager('tests/utils/config/configJson');
  48. $this->refDB = new \ReferenceLinkDB();
  49. $this->refDB->write(self::$testDatastore);
  50. $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
  51. $refHistory = new \ReferenceHistory();
  52. $refHistory->write(self::$testHistory);
  53. $this->history = new \History(self::$testHistory);
  54. $this->container = new Container();
  55. $this->container['conf'] = $this->conf;
  56. $this->container['db'] = $this->linkDB;
  57. $this->container['history'] = $this->history;
  58. $this->controller = new Links($this->container);
  59. }
  60. /**
  61. * After each test, remove the test datastore.
  62. */
  63. public function tearDown()
  64. {
  65. @unlink(self::$testDatastore);
  66. @unlink(self::$testHistory);
  67. }
  68. /**
  69. * Test DELETE link endpoint: the link should be removed.
  70. */
  71. public function testDeleteLinkValid()
  72. {
  73. $id = '41';
  74. $this->assertTrue(isset($this->linkDB[$id]));
  75. $env = Environment::mock([
  76. 'REQUEST_METHOD' => 'DELETE',
  77. ]);
  78. $request = Request::createFromEnvironment($env);
  79. $response = $this->controller->deleteLink($request, new Response(), ['id' => $id]);
  80. $this->assertEquals(204, $response->getStatusCode());
  81. $this->assertEmpty((string) $response->getBody());
  82. $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
  83. $this->assertFalse(isset($this->linkDB[$id]));
  84. $historyEntry = $this->history->getHistory()[0];
  85. $this->assertEquals(\History::DELETED, $historyEntry['event']);
  86. $this->assertTrue(
  87. (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
  88. );
  89. $this->assertEquals($id, $historyEntry['id']);
  90. }
  91. /**
  92. * Test DELETE link endpoint: reach not existing ID.
  93. *
  94. * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
  95. */
  96. public function testDeleteLink404()
  97. {
  98. $id = -1;
  99. $this->assertFalse(isset($this->linkDB[$id]));
  100. $env = Environment::mock([
  101. 'REQUEST_METHOD' => 'DELETE',
  102. ]);
  103. $request = Request::createFromEnvironment($env);
  104. $this->controller->deleteLink($request, new Response(), ['id' => $id]);
  105. }
  106. }