diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-05-06 17:32:16 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2017-05-07 15:58:49 +0200 |
commit | 0843848c1d18e92504c43d181063a2012f8fd5b9 (patch) | |
tree | faf798ea7d1e4a41edce202cebd0cd7d45e22300 /tests/api/controllers/DeleteLinkTest.php | |
parent | cf9181dddf8b6113b1b017e4bcb21fac0a0b1c83 (diff) | |
download | Shaarli-0843848c1d18e92504c43d181063a2012f8fd5b9.tar.gz Shaarli-0843848c1d18e92504c43d181063a2012f8fd5b9.tar.zst Shaarli-0843848c1d18e92504c43d181063a2012f8fd5b9.zip |
API: add DELETE endpoint
Based on #840
See http://shaarli.github.io/api-documentation/\#links-link-delete
Diffstat (limited to 'tests/api/controllers/DeleteLinkTest.php')
-rw-r--r-- | tests/api/controllers/DeleteLinkTest.php | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/tests/api/controllers/DeleteLinkTest.php b/tests/api/controllers/DeleteLinkTest.php new file mode 100644 index 00000000..6894e8a2 --- /dev/null +++ b/tests/api/controllers/DeleteLinkTest.php | |||
@@ -0,0 +1,104 @@ | |||
1 | <?php | ||
2 | |||
3 | |||
4 | namespace Shaarli\Api\Controllers; | ||
5 | |||
6 | use Shaarli\Config\ConfigManager; | ||
7 | use Slim\Container; | ||
8 | use Slim\Http\Environment; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | class DeleteLinkTest extends \PHPUnit_Framework_TestCase | ||
13 | { | ||
14 | /** | ||
15 | * @var string datastore to test write operations | ||
16 | */ | ||
17 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
18 | |||
19 | /** | ||
20 | * @var ConfigManager instance | ||
21 | */ | ||
22 | protected $conf; | ||
23 | |||
24 | /** | ||
25 | * @var \ReferenceLinkDB instance. | ||
26 | */ | ||
27 | protected $refDB = null; | ||
28 | |||
29 | /** | ||
30 | * @var \LinkDB instance. | ||
31 | */ | ||
32 | protected $linkDB; | ||
33 | |||
34 | /** | ||
35 | * @var Container instance. | ||
36 | */ | ||
37 | protected $container; | ||
38 | |||
39 | /** | ||
40 | * @var Links controller instance. | ||
41 | */ | ||
42 | protected $controller; | ||
43 | |||
44 | /** | ||
45 | * Before each test, instantiate a new Api with its config, plugins and links. | ||
46 | */ | ||
47 | public function setUp() | ||
48 | { | ||
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; | ||
56 | |||
57 | $this->controller = new Links($this->container); | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * After each test, remove the test datastore. | ||
62 | */ | ||
63 | public function tearDown() | ||
64 | { | ||
65 | @unlink(self::$testDatastore); | ||
66 | } | ||
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 | |||
80 | $response = $this->controller->deleteLink($request, new Response(), ['id' => $id]); | ||
81 | $this->assertEquals(204, $response->getStatusCode()); | ||
82 | $this->assertEmpty((string) $response->getBody()); | ||
83 | |||
84 | $this->linkDB = new \LinkDB(self::$testDatastore, true, false); | ||
85 | $this->assertFalse(isset($this->linkDB[$id])); | ||
86 | } | ||
87 | |||
88 | /** | ||
89 | * Test DELETE link endpoint: reach not existing ID. | ||
90 | * | ||
91 | * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException | ||
92 | */ | ||
93 | public function testDeleteLink404() | ||
94 | { | ||
95 | $id = -1; | ||
96 | $this->assertFalse(isset($this->linkDB[$id])); | ||
97 | $env = Environment::mock([ | ||
98 | 'REQUEST_METHOD' => 'DELETE', | ||
99 | ]); | ||
100 | $request = Request::createFromEnvironment($env); | ||
101 | |||
102 | $this->controller->deleteLink($request, new Response(), ['id' => $id]); | ||
103 | } | ||
104 | } | ||