aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/HistoryTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/HistoryTest.php')
-rw-r--r--tests/HistoryTest.php255
1 files changed, 130 insertions, 125 deletions
diff --git a/tests/HistoryTest.php b/tests/HistoryTest.php
index 8303e53a..6dc0e5b7 100644
--- a/tests/HistoryTest.php
+++ b/tests/HistoryTest.php
@@ -3,9 +3,9 @@
3namespace Shaarli; 3namespace Shaarli;
4 4
5use DateTime; 5use DateTime;
6use Exception; 6use Shaarli\Bookmark\Bookmark;
7 7
8class HistoryTest extends \PHPUnit\Framework\TestCase 8class HistoryTest extends \Shaarli\TestCase
9{ 9{
10 /** 10 /**
11 * @var string History file path 11 * @var string History file path
@@ -15,9 +15,11 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
15 /** 15 /**
16 * Delete history file. 16 * Delete history file.
17 */ 17 */
18 public function tearDown() 18 protected function setUp(): void
19 { 19 {
20 @unlink(self::$historyFilePath); 20 if (file_exists(self::$historyFilePath)) {
21 unlink(self::$historyFilePath);
22 }
21 } 23 }
22 24
23 /** 25 /**
@@ -41,12 +43,12 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
41 43
42 /** 44 /**
43 * Not writable history file: raise an exception. 45 * Not writable history file: raise an exception.
44 *
45 * @expectedException Exception
46 * @expectedExceptionMessage History file isn't readable or writable
47 */ 46 */
48 public function testConstructNotWritable() 47 public function testConstructNotWritable()
49 { 48 {
49 $this->expectException(\Exception::class);
50 $this->expectExceptionMessage('History file isn\'t readable or writable');
51
50 touch(self::$historyFilePath); 52 touch(self::$historyFilePath);
51 chmod(self::$historyFilePath, 0440); 53 chmod(self::$historyFilePath, 0440);
52 $history = new History(self::$historyFilePath); 54 $history = new History(self::$historyFilePath);
@@ -55,12 +57,12 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
55 57
56 /** 58 /**
57 * Not parsable history file: raise an exception. 59 * Not parsable history file: raise an exception.
58 *
59 * @expectedException Exception
60 * @expectedExceptionMessageRegExp /Could not parse history file/
61 */ 60 */
62 public function testConstructNotParsable() 61 public function testConstructNotParsable()
63 { 62 {
63 $this->expectException(\Exception::class);
64 $this->expectExceptionMessageRegExp('/Could not parse history file/');
65
64 file_put_contents(self::$historyFilePath, 'not parsable'); 66 file_put_contents(self::$historyFilePath, 'not parsable');
65 $history = new History(self::$historyFilePath); 67 $history = new History(self::$historyFilePath);
66 // gzinflate generates a warning 68 // gzinflate generates a warning
@@ -73,137 +75,140 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
73 public function testAddLink() 75 public function testAddLink()
74 { 76 {
75 $history = new History(self::$historyFilePath); 77 $history = new History(self::$historyFilePath);
76 $history->addLink(['id' => 0]); 78 $bookmark = (new Bookmark())->setId(0);
79 $history->addLink($bookmark);
77 $actual = $history->getHistory()[0]; 80 $actual = $history->getHistory()[0];
78 $this->assertEquals(History::CREATED, $actual['event']); 81 $this->assertEquals(History::CREATED, $actual['event']);
79 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 82 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
80 $this->assertEquals(0, $actual['id']); 83 $this->assertEquals(0, $actual['id']);
81 84
82 $history = new History(self::$historyFilePath); 85 $history = new History(self::$historyFilePath);
83 $history->addLink(['id' => 1]); 86 $bookmark = (new Bookmark())->setId(1);
87 $history->addLink($bookmark);
84 $actual = $history->getHistory()[0]; 88 $actual = $history->getHistory()[0];
85 $this->assertEquals(History::CREATED, $actual['event']); 89 $this->assertEquals(History::CREATED, $actual['event']);
86 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 90 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
87 $this->assertEquals(1, $actual['id']); 91 $this->assertEquals(1, $actual['id']);
88 92
89 $history = new History(self::$historyFilePath); 93 $history = new History(self::$historyFilePath);
90 $history->addLink(['id' => 'str']); 94 $bookmark = (new Bookmark())->setId('str');
95 $history->addLink($bookmark);
91 $actual = $history->getHistory()[0]; 96 $actual = $history->getHistory()[0];
92 $this->assertEquals(History::CREATED, $actual['event']); 97 $this->assertEquals(History::CREATED, $actual['event']);
93 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 98 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
94 $this->assertEquals('str', $actual['id']); 99 $this->assertEquals('str', $actual['id']);
95 } 100 }
96 101
97 /** 102// /**
98 * Test updated link event 103// * Test updated link event
99 */ 104// */
100 public function testUpdateLink() 105// public function testUpdateLink()
101 { 106// {
102 $history = new History(self::$historyFilePath); 107// $history = new History(self::$historyFilePath);
103 $history->updateLink(['id' => 1]); 108// $history->updateLink(['id' => 1]);
104 $actual = $history->getHistory()[0]; 109// $actual = $history->getHistory()[0];
105 $this->assertEquals(History::UPDATED, $actual['event']); 110// $this->assertEquals(History::UPDATED, $actual['event']);
106 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 111// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
107 $this->assertEquals(1, $actual['id']); 112// $this->assertEquals(1, $actual['id']);
108 } 113// }
109 114//
110 /** 115// /**
111 * Test delete link event 116// * Test delete link event
112 */ 117// */
113 public function testDeleteLink() 118// public function testDeleteLink()
114 { 119// {
115 $history = new History(self::$historyFilePath); 120// $history = new History(self::$historyFilePath);
116 $history->deleteLink(['id' => 1]); 121// $history->deleteLink(['id' => 1]);
117 $actual = $history->getHistory()[0]; 122// $actual = $history->getHistory()[0];
118 $this->assertEquals(History::DELETED, $actual['event']); 123// $this->assertEquals(History::DELETED, $actual['event']);
119 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 124// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
120 $this->assertEquals(1, $actual['id']); 125// $this->assertEquals(1, $actual['id']);
121 } 126// }
122 127//
123 /** 128// /**
124 * Test updated settings event 129// * Test updated settings event
125 */ 130// */
126 public function testUpdateSettings() 131// public function testUpdateSettings()
127 { 132// {
128 $history = new History(self::$historyFilePath); 133// $history = new History(self::$historyFilePath);
129 $history->updateSettings(); 134// $history->updateSettings();
130 $actual = $history->getHistory()[0]; 135// $actual = $history->getHistory()[0];
131 $this->assertEquals(History::SETTINGS, $actual['event']); 136// $this->assertEquals(History::SETTINGS, $actual['event']);
132 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 137// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
133 $this->assertEmpty($actual['id']); 138// $this->assertEmpty($actual['id']);
134 } 139// }
135 140//
136 /** 141// /**
137 * Make sure that new items are stored at the beginning 142// * Make sure that new items are stored at the beginning
138 */ 143// */
139 public function testHistoryOrder() 144// public function testHistoryOrder()
140 { 145// {
141 $history = new History(self::$historyFilePath); 146// $history = new History(self::$historyFilePath);
142 $history->updateLink(['id' => 1]); 147// $history->updateLink(['id' => 1]);
143 $actual = $history->getHistory()[0]; 148// $actual = $history->getHistory()[0];
144 $this->assertEquals(History::UPDATED, $actual['event']); 149// $this->assertEquals(History::UPDATED, $actual['event']);
145 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 150// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
146 $this->assertEquals(1, $actual['id']); 151// $this->assertEquals(1, $actual['id']);
147 152//
148 $history->addLink(['id' => 1]); 153// $history->addLink(['id' => 1]);
149 $actual = $history->getHistory()[0]; 154// $actual = $history->getHistory()[0];
150 $this->assertEquals(History::CREATED, $actual['event']); 155// $this->assertEquals(History::CREATED, $actual['event']);
151 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 156// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
152 $this->assertEquals(1, $actual['id']); 157// $this->assertEquals(1, $actual['id']);
153 } 158// }
154 159//
155 /** 160// /**
156 * Re-read history from file after writing an event 161// * Re-read history from file after writing an event
157 */ 162// */
158 public function testHistoryRead() 163// public function testHistoryRead()
159 { 164// {
160 $history = new History(self::$historyFilePath); 165// $history = new History(self::$historyFilePath);
161 $history->updateLink(['id' => 1]); 166// $history->updateLink(['id' => 1]);
162 $history = new History(self::$historyFilePath); 167// $history = new History(self::$historyFilePath);
163 $actual = $history->getHistory()[0]; 168// $actual = $history->getHistory()[0];
164 $this->assertEquals(History::UPDATED, $actual['event']); 169// $this->assertEquals(History::UPDATED, $actual['event']);
165 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 170// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
166 $this->assertEquals(1, $actual['id']); 171// $this->assertEquals(1, $actual['id']);
167 } 172// }
168 173//
169 /** 174// /**
170 * Re-read history from file after writing an event and make sure that the order is correct 175// * Re-read history from file after writing an event and make sure that the order is correct
171 */ 176// */
172 public function testHistoryOrderRead() 177// public function testHistoryOrderRead()
173 { 178// {
174 $history = new History(self::$historyFilePath); 179// $history = new History(self::$historyFilePath);
175 $history->updateLink(['id' => 1]); 180// $history->updateLink(['id' => 1]);
176 $history->addLink(['id' => 1]); 181// $history->addLink(['id' => 1]);
177 182//
178 $history = new History(self::$historyFilePath); 183// $history = new History(self::$historyFilePath);
179 $actual = $history->getHistory()[0]; 184// $actual = $history->getHistory()[0];
180 $this->assertEquals(History::CREATED, $actual['event']); 185// $this->assertEquals(History::CREATED, $actual['event']);
181 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 186// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
182 $this->assertEquals(1, $actual['id']); 187// $this->assertEquals(1, $actual['id']);
183 188//
184 $actual = $history->getHistory()[1]; 189// $actual = $history->getHistory()[1];
185 $this->assertEquals(History::UPDATED, $actual['event']); 190// $this->assertEquals(History::UPDATED, $actual['event']);
186 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 191// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
187 $this->assertEquals(1, $actual['id']); 192// $this->assertEquals(1, $actual['id']);
188 } 193// }
189 194//
190 /** 195// /**
191 * Test retention time: delete old entries. 196// * Test retention time: delete old entries.
192 */ 197// */
193 public function testHistoryRententionTime() 198// public function testHistoryRententionTime()
194 { 199// {
195 $history = new History(self::$historyFilePath, 5); 200// $history = new History(self::$historyFilePath, 5);
196 $history->updateLink(['id' => 1]); 201// $history->updateLink(['id' => 1]);
197 $this->assertEquals(1, count($history->getHistory())); 202// $this->assertEquals(1, count($history->getHistory()));
198 $arr = $history->getHistory(); 203// $arr = $history->getHistory();
199 $arr[0]['datetime'] = new DateTime('-1 hour'); 204// $arr[0]['datetime'] = new DateTime('-1 hour');
200 FileUtils::writeFlatDB(self::$historyFilePath, $arr); 205// FileUtils::writeFlatDB(self::$historyFilePath, $arr);
201 206//
202 $history = new History(self::$historyFilePath, 60); 207// $history = new History(self::$historyFilePath, 60);
203 $this->assertEquals(1, count($history->getHistory())); 208// $this->assertEquals(1, count($history->getHistory()));
204 $this->assertEquals(1, $history->getHistory()[0]['id']); 209// $this->assertEquals(1, $history->getHistory()[0]['id']);
205 $history->updateLink(['id' => 2]); 210// $history->updateLink(['id' => 2]);
206 $this->assertEquals(1, count($history->getHistory())); 211// $this->assertEquals(1, count($history->getHistory()));
207 $this->assertEquals(2, $history->getHistory()[0]['id']); 212// $this->assertEquals(2, $history->getHistory()[0]['id']);
208 } 213// }
209} 214}