]>
Commit | Line | Data |
---|---|---|
0843848c A |
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 | } |