]>
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 | ||
813849e5 A |
19 | /** |
20 | * @var string datastore to test write operations | |
21 | */ | |
22 | protected static $testHistory = 'sandbox/history.php'; | |
23 | ||
0843848c A |
24 | /** |
25 | * @var ConfigManager instance | |
26 | */ | |
27 | protected $conf; | |
28 | ||
29 | /** | |
30 | * @var \ReferenceLinkDB instance. | |
31 | */ | |
32 | protected $refDB = null; | |
33 | ||
34 | /** | |
35 | * @var \LinkDB instance. | |
36 | */ | |
37 | protected $linkDB; | |
38 | ||
813849e5 A |
39 | /** |
40 | * @var \History instance. | |
41 | */ | |
42 | protected $history; | |
43 | ||
0843848c A |
44 | /** |
45 | * @var Container instance. | |
46 | */ | |
47 | protected $container; | |
48 | ||
49 | /** | |
50 | * @var Links controller instance. | |
51 | */ | |
52 | protected $controller; | |
53 | ||
54 | /** | |
55 | * Before each test, instantiate a new Api with its config, plugins and links. | |
56 | */ | |
57 | public function setUp() | |
58 | { | |
59 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | |
60 | $this->refDB = new \ReferenceLinkDB(); | |
61 | $this->refDB->write(self::$testDatastore); | |
62 | $this->linkDB = new \LinkDB(self::$testDatastore, true, false); | |
813849e5 A |
63 | $refHistory = new \ReferenceHistory(); |
64 | $refHistory->write(self::$testHistory); | |
65 | $this->history = new \History(self::$testHistory); | |
0843848c A |
66 | $this->container = new Container(); |
67 | $this->container['conf'] = $this->conf; | |
68 | $this->container['db'] = $this->linkDB; | |
813849e5 | 69 | $this->container['history'] = $this->history; |
0843848c A |
70 | |
71 | $this->controller = new Links($this->container); | |
72 | } | |
73 | ||
74 | /** | |
75 | * After each test, remove the test datastore. | |
76 | */ | |
77 | public function tearDown() | |
78 | { | |
79 | @unlink(self::$testDatastore); | |
813849e5 | 80 | @unlink(self::$testHistory); |
0843848c A |
81 | } |
82 | ||
83 | /** | |
84 | * Test DELETE link endpoint: the link should be removed. | |
85 | */ | |
86 | public function testDeleteLinkValid() | |
87 | { | |
88 | $id = '41'; | |
89 | $this->assertTrue(isset($this->linkDB[$id])); | |
90 | $env = Environment::mock([ | |
91 | 'REQUEST_METHOD' => 'DELETE', | |
92 | ]); | |
93 | $request = Request::createFromEnvironment($env); | |
94 | ||
95 | $response = $this->controller->deleteLink($request, new Response(), ['id' => $id]); | |
96 | $this->assertEquals(204, $response->getStatusCode()); | |
97 | $this->assertEmpty((string) $response->getBody()); | |
98 | ||
99 | $this->linkDB = new \LinkDB(self::$testDatastore, true, false); | |
100 | $this->assertFalse(isset($this->linkDB[$id])); | |
813849e5 A |
101 | |
102 | $historyEntry = $this->history->getHistory()[0]; | |
103 | $this->assertEquals(\History::DELETED, $historyEntry['event']); | |
104 | $this->assertTrue( | |
105 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | |
106 | ); | |
107 | $this->assertEquals($id, $historyEntry['id']); | |
0843848c A |
108 | } |
109 | ||
110 | /** | |
111 | * Test DELETE link endpoint: reach not existing ID. | |
112 | * | |
113 | * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException | |
114 | */ | |
115 | public function testDeleteLink404() | |
116 | { | |
117 | $id = -1; | |
118 | $this->assertFalse(isset($this->linkDB[$id])); | |
119 | $env = Environment::mock([ | |
120 | 'REQUEST_METHOD' => 'DELETE', | |
121 | ]); | |
122 | $request = Request::createFromEnvironment($env); | |
123 | ||
124 | $this->controller->deleteLink($request, new Response(), ['id' => $id]); | |
125 | } | |
126 | } |