4 namespace Shaarli\Api\Controllers
;
6 use Shaarli\Config\ConfigManager
;
8 use Slim\Http\Environment
;
10 use Slim\Http\Response
;
12 class DeleteLinkTest
extends \PHPUnit_Framework_TestCase
15 * @var string datastore to test write operations
17 protected static $testDatastore = 'sandbox/datastore.php';
20 * @var ConfigManager instance
25 * @var \ReferenceLinkDB instance.
27 protected $refDB = null;
30 * @var \LinkDB instance.
35 * @var Container instance.
40 * @var Links controller instance.
42 protected $controller;
45 * Before each test, instantiate a new Api with its config, plugins and links.
47 public function setUp()
49 $this->conf
= new ConfigManager('tests/utils/config/configJson');
50 $this->refDB
= new \
ReferenceLinkDB();
51 $this->refDB
->write(self
::$testDatastore);
52 $this->linkDB
= new \
LinkDB(self
::$testDatastore, true, false);
53 $this->container
= new Container();
54 $this->container
['conf'] = $this->conf
;
55 $this->container
['db'] = $this->linkDB
;
57 $this->controller
= new Links($this->container
);
61 * After each test, remove the test datastore.
63 public function tearDown()
65 @unlink(self
::$testDatastore);
69 * Test DELETE link endpoint: the link should be removed.
71 public function testDeleteLinkValid()
74 $this->assertTrue(isset($this->linkDB
[$id]));
75 $env = Environment
::mock([
76 'REQUEST_METHOD' => 'DELETE',
78 $request = Request
::createFromEnvironment($env);
80 $response = $this->controller
->deleteLink($request, new Response(), ['id' => $id]);
81 $this->assertEquals(204, $response->getStatusCode());
82 $this->assertEmpty((string) $response->getBody());
84 $this->linkDB
= new \
LinkDB(self
::$testDatastore, true, false);
85 $this->assertFalse(isset($this->linkDB
[$id]));
89 * Test DELETE link endpoint: reach not existing ID.
91 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
93 public function testDeleteLink404()
96 $this->assertFalse(isset($this->linkDB
[$id]));
97 $env = Environment
::mock([
98 'REQUEST_METHOD' => 'DELETE',
100 $request = Request
::createFromEnvironment($env);
102 $this->controller
->deleteLink($request, new Response(), ['id' => $id]);