aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/HistoryTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-17 21:34:12 +0100
committerArthurHoaro <arthur@hoa.ro>2020-01-18 09:56:32 +0100
commite26e2060f5470ce8bf4c5973284bae07b8af170a (patch)
treeadf8512f93f5559ba87d0c9931969ae4ebea7133 /tests/HistoryTest.php
parentcf92b4dd1521241eefc58eaf6dcd202cd83969d8 (diff)
downloadShaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.tar.gz
Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.tar.zst
Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.zip
Add and update unit test for the new system (Bookmark + Service)
See #1307
Diffstat (limited to 'tests/HistoryTest.php')
-rw-r--r--tests/HistoryTest.php240
1 files changed, 123 insertions, 117 deletions
diff --git a/tests/HistoryTest.php b/tests/HistoryTest.php
index 8303e53a..7189c3a9 100644
--- a/tests/HistoryTest.php
+++ b/tests/HistoryTest.php
@@ -4,6 +4,7 @@ namespace Shaarli;
4 4
5use DateTime; 5use DateTime;
6use Exception; 6use Exception;
7use Shaarli\Bookmark\Bookmark;
7 8
8class HistoryTest extends \PHPUnit\Framework\TestCase 9class HistoryTest extends \PHPUnit\Framework\TestCase
9{ 10{
@@ -15,9 +16,11 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
15 /** 16 /**
16 * Delete history file. 17 * Delete history file.
17 */ 18 */
18 public function tearDown() 19 public function setUp()
19 { 20 {
20 @unlink(self::$historyFilePath); 21 if (file_exists(self::$historyFilePath)) {
22 unlink(self::$historyFilePath);
23 }
21 } 24 }
22 25
23 /** 26 /**
@@ -73,137 +76,140 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
73 public function testAddLink() 76 public function testAddLink()
74 { 77 {
75 $history = new History(self::$historyFilePath); 78 $history = new History(self::$historyFilePath);
76 $history->addLink(['id' => 0]); 79 $bookmark = (new Bookmark())->setId(0);
80 $history->addLink($bookmark);
77 $actual = $history->getHistory()[0]; 81 $actual = $history->getHistory()[0];
78 $this->assertEquals(History::CREATED, $actual['event']); 82 $this->assertEquals(History::CREATED, $actual['event']);
79 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 83 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
80 $this->assertEquals(0, $actual['id']); 84 $this->assertEquals(0, $actual['id']);
81 85
82 $history = new History(self::$historyFilePath); 86 $history = new History(self::$historyFilePath);
83 $history->addLink(['id' => 1]); 87 $bookmark = (new Bookmark())->setId(1);
88 $history->addLink($bookmark);
84 $actual = $history->getHistory()[0]; 89 $actual = $history->getHistory()[0];
85 $this->assertEquals(History::CREATED, $actual['event']); 90 $this->assertEquals(History::CREATED, $actual['event']);
86 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 91 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
87 $this->assertEquals(1, $actual['id']); 92 $this->assertEquals(1, $actual['id']);
88 93
89 $history = new History(self::$historyFilePath); 94 $history = new History(self::$historyFilePath);
90 $history->addLink(['id' => 'str']); 95 $bookmark = (new Bookmark())->setId('str');
96 $history->addLink($bookmark);
91 $actual = $history->getHistory()[0]; 97 $actual = $history->getHistory()[0];
92 $this->assertEquals(History::CREATED, $actual['event']); 98 $this->assertEquals(History::CREATED, $actual['event']);
93 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 99 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
94 $this->assertEquals('str', $actual['id']); 100 $this->assertEquals('str', $actual['id']);
95 } 101 }
96 102
97 /** 103// /**
98 * Test updated link event 104// * Test updated link event
99 */ 105// */
100 public function testUpdateLink() 106// public function testUpdateLink()
101 { 107// {
102 $history = new History(self::$historyFilePath); 108// $history = new History(self::$historyFilePath);
103 $history->updateLink(['id' => 1]); 109// $history->updateLink(['id' => 1]);
104 $actual = $history->getHistory()[0]; 110// $actual = $history->getHistory()[0];
105 $this->assertEquals(History::UPDATED, $actual['event']); 111// $this->assertEquals(History::UPDATED, $actual['event']);
106 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 112// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
107 $this->assertEquals(1, $actual['id']); 113// $this->assertEquals(1, $actual['id']);
108 } 114// }
109 115//
110 /** 116// /**
111 * Test delete link event 117// * Test delete link event
112 */ 118// */
113 public function testDeleteLink() 119// public function testDeleteLink()
114 { 120// {
115 $history = new History(self::$historyFilePath); 121// $history = new History(self::$historyFilePath);
116 $history->deleteLink(['id' => 1]); 122// $history->deleteLink(['id' => 1]);
117 $actual = $history->getHistory()[0]; 123// $actual = $history->getHistory()[0];
118 $this->assertEquals(History::DELETED, $actual['event']); 124// $this->assertEquals(History::DELETED, $actual['event']);
119 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 125// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
120 $this->assertEquals(1, $actual['id']); 126// $this->assertEquals(1, $actual['id']);
121 } 127// }
122 128//
123 /** 129// /**
124 * Test updated settings event 130// * Test updated settings event
125 */ 131// */
126 public function testUpdateSettings() 132// public function testUpdateSettings()
127 { 133// {
128 $history = new History(self::$historyFilePath); 134// $history = new History(self::$historyFilePath);
129 $history->updateSettings(); 135// $history->updateSettings();
130 $actual = $history->getHistory()[0]; 136// $actual = $history->getHistory()[0];
131 $this->assertEquals(History::SETTINGS, $actual['event']); 137// $this->assertEquals(History::SETTINGS, $actual['event']);
132 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 138// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
133 $this->assertEmpty($actual['id']); 139// $this->assertEmpty($actual['id']);
134 } 140// }
135 141//
136 /** 142// /**
137 * Make sure that new items are stored at the beginning 143// * Make sure that new items are stored at the beginning
138 */ 144// */
139 public function testHistoryOrder() 145// public function testHistoryOrder()
140 { 146// {
141 $history = new History(self::$historyFilePath); 147// $history = new History(self::$historyFilePath);
142 $history->updateLink(['id' => 1]); 148// $history->updateLink(['id' => 1]);
143 $actual = $history->getHistory()[0]; 149// $actual = $history->getHistory()[0];
144 $this->assertEquals(History::UPDATED, $actual['event']); 150// $this->assertEquals(History::UPDATED, $actual['event']);
145 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 151// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
146 $this->assertEquals(1, $actual['id']); 152// $this->assertEquals(1, $actual['id']);
147 153//
148 $history->addLink(['id' => 1]); 154// $history->addLink(['id' => 1]);
149 $actual = $history->getHistory()[0]; 155// $actual = $history->getHistory()[0];
150 $this->assertEquals(History::CREATED, $actual['event']); 156// $this->assertEquals(History::CREATED, $actual['event']);
151 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 157// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
152 $this->assertEquals(1, $actual['id']); 158// $this->assertEquals(1, $actual['id']);
153 } 159// }
154 160//
155 /** 161// /**
156 * Re-read history from file after writing an event 162// * Re-read history from file after writing an event
157 */ 163// */
158 public function testHistoryRead() 164// public function testHistoryRead()
159 { 165// {
160 $history = new History(self::$historyFilePath); 166// $history = new History(self::$historyFilePath);
161 $history->updateLink(['id' => 1]); 167// $history->updateLink(['id' => 1]);
162 $history = new History(self::$historyFilePath); 168// $history = new History(self::$historyFilePath);
163 $actual = $history->getHistory()[0]; 169// $actual = $history->getHistory()[0];
164 $this->assertEquals(History::UPDATED, $actual['event']); 170// $this->assertEquals(History::UPDATED, $actual['event']);
165 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 171// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
166 $this->assertEquals(1, $actual['id']); 172// $this->assertEquals(1, $actual['id']);
167 } 173// }
168 174//
169 /** 175// /**
170 * Re-read history from file after writing an event and make sure that the order is correct 176// * Re-read history from file after writing an event and make sure that the order is correct
171 */ 177// */
172 public function testHistoryOrderRead() 178// public function testHistoryOrderRead()
173 { 179// {
174 $history = new History(self::$historyFilePath); 180// $history = new History(self::$historyFilePath);
175 $history->updateLink(['id' => 1]); 181// $history->updateLink(['id' => 1]);
176 $history->addLink(['id' => 1]); 182// $history->addLink(['id' => 1]);
177 183//
178 $history = new History(self::$historyFilePath); 184// $history = new History(self::$historyFilePath);
179 $actual = $history->getHistory()[0]; 185// $actual = $history->getHistory()[0];
180 $this->assertEquals(History::CREATED, $actual['event']); 186// $this->assertEquals(History::CREATED, $actual['event']);
181 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 187// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
182 $this->assertEquals(1, $actual['id']); 188// $this->assertEquals(1, $actual['id']);
183 189//
184 $actual = $history->getHistory()[1]; 190// $actual = $history->getHistory()[1];
185 $this->assertEquals(History::UPDATED, $actual['event']); 191// $this->assertEquals(History::UPDATED, $actual['event']);
186 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 192// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
187 $this->assertEquals(1, $actual['id']); 193// $this->assertEquals(1, $actual['id']);
188 } 194// }
189 195//
190 /** 196// /**
191 * Test retention time: delete old entries. 197// * Test retention time: delete old entries.
192 */ 198// */
193 public function testHistoryRententionTime() 199// public function testHistoryRententionTime()
194 { 200// {
195 $history = new History(self::$historyFilePath, 5); 201// $history = new History(self::$historyFilePath, 5);
196 $history->updateLink(['id' => 1]); 202// $history->updateLink(['id' => 1]);
197 $this->assertEquals(1, count($history->getHistory())); 203// $this->assertEquals(1, count($history->getHistory()));
198 $arr = $history->getHistory(); 204// $arr = $history->getHistory();
199 $arr[0]['datetime'] = new DateTime('-1 hour'); 205// $arr[0]['datetime'] = new DateTime('-1 hour');
200 FileUtils::writeFlatDB(self::$historyFilePath, $arr); 206// FileUtils::writeFlatDB(self::$historyFilePath, $arr);
201 207//
202 $history = new History(self::$historyFilePath, 60); 208// $history = new History(self::$historyFilePath, 60);
203 $this->assertEquals(1, count($history->getHistory())); 209// $this->assertEquals(1, count($history->getHistory()));
204 $this->assertEquals(1, $history->getHistory()[0]['id']); 210// $this->assertEquals(1, $history->getHistory()[0]['id']);
205 $history->updateLink(['id' => 2]); 211// $history->updateLink(['id' => 2]);
206 $this->assertEquals(1, count($history->getHistory())); 212// $this->assertEquals(1, count($history->getHistory()));
207 $this->assertEquals(2, $history->getHistory()[0]['id']); 213// $this->assertEquals(2, $history->getHistory()[0]['id']);
208 } 214// }
209} 215}