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