diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-05-07 16:02:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-07 16:02:14 +0200 |
commit | b8fcb7d4403a344158ab5d2c8979bdd002e6001d (patch) | |
tree | 2ac6304cfdef1322c19bd47ba972e8894d98ab68 /tests | |
parent | 77de24876ff542e3770aa2845e993c58f87e37df (diff) | |
parent | 0843848c1d18e92504c43d181063a2012f8fd5b9 (diff) | |
download | Shaarli-b8fcb7d4403a344158ab5d2c8979bdd002e6001d.tar.gz Shaarli-b8fcb7d4403a344158ab5d2c8979bdd002e6001d.tar.zst Shaarli-b8fcb7d4403a344158ab5d2c8979bdd002e6001d.zip |
Merge pull request #856 from ArthurHoaro/api/delete-link
API: add DELETE endpoint
Diffstat (limited to 'tests')
-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 | } | ||