DeleteLinkTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 ConfigManager instance
  16. */
  17. protected $conf;
  18. /**
  19. * @var \ReferenceLinkDB instance.
  20. */
  21. protected $refDB = null;
  22. /**
  23. * @var \LinkDB instance.
  24. */
  25. protected $linkDB;
  26. /**
  27. * @var Container instance.
  28. */
  29. protected $container;
  30. /**
  31. * @var Links controller instance.
  32. */
  33. protected $controller;
  34. /**
  35. * Before each test, instantiate a new Api with its config, plugins and links.
  36. */
  37. public function setUp()
  38. {
  39. $this->conf = new ConfigManager('tests/utils/config/configJson');
  40. $this->refDB = new \ReferenceLinkDB();
  41. $this->refDB->write(self::$testDatastore);
  42. $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
  43. $this->container = new Container();
  44. $this->container['conf'] = $this->conf;
  45. $this->container['db'] = $this->linkDB;
  46. $this->controller = new Links($this->container);
  47. }
  48. /**
  49. * After each test, remove the test datastore.
  50. */
  51. public function tearDown()
  52. {
  53. @unlink(self::$testDatastore);
  54. }
  55. /**
  56. * Test DELETE link endpoint: the link should be removed.
  57. */
  58. public function testDeleteLinkValid()
  59. {
  60. $id = '41';
  61. $this->assertTrue(isset($this->linkDB[$id]));
  62. $env = Environment::mock([
  63. 'REQUEST_METHOD' => 'DELETE',
  64. ]);
  65. $request = Request::createFromEnvironment($env);
  66. $response = $this->controller->deleteLink($request, new Response(), ['id' => $id]);
  67. $this->assertEquals(204, $response->getStatusCode());
  68. $this->assertEmpty((string) $response->getBody());
  69. $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
  70. $this->assertFalse(isset($this->linkDB[$id]));
  71. }
  72. /**
  73. * Test DELETE link endpoint: reach not existing ID.
  74. *
  75. * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
  76. */
  77. public function testDeleteLink404()
  78. {
  79. $id = -1;
  80. $this->assertFalse(isset($this->linkDB[$id]));
  81. $env = Environment::mock([
  82. 'REQUEST_METHOD' => 'DELETE',
  83. ]);
  84. $request = Request::createFromEnvironment($env);
  85. $this->controller->deleteLink($request, new Response(), ['id' => $id]);
  86. }
  87. }