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