9 class HistoryTest
extends \PHPUnit\Framework\TestCase
12 * @var string History file path
14 protected static $historyFilePath = 'sandbox/history.php';
17 * Delete history file.
19 public function tearDown()
21 @unlink(self
::$historyFilePath);
25 * Test that the history file is created if it doesn't exist.
27 public function testConstructLazyLoading()
29 new History(self
::$historyFilePath);
30 $this->assertFileNotExists(self
::$historyFilePath);
34 * Test that the history file is created if it doesn't exist.
36 public function testAddEventCreateFile()
38 $history = new History(self
::$historyFilePath);
39 $history->updateSettings();
40 $this->assertFileExists(self
::$historyFilePath);
44 * Not writable history file: raise an exception.
46 * @expectedException Exception
47 * @expectedExceptionMessage History file isn't readable or writable
49 public function testConstructNotWritable()
51 touch(self
::$historyFilePath);
52 chmod(self
::$historyFilePath, 0440);
53 $history = new History(self
::$historyFilePath);
54 $history->updateSettings();
58 * Not parsable history file: raise an exception.
60 * @expectedException Exception
61 * @expectedExceptionMessageRegExp /Could not parse history file/
63 public function testConstructNotParsable()
65 file_put_contents(self
::$historyFilePath, 'not parsable');
66 $history = new History(self
::$historyFilePath);
67 // gzinflate generates a warning
68 @$history->updateSettings();
74 public function testAddLink()
76 $history = new History(self
::$historyFilePath);
77 $history->addLink(['id' => 0]);
78 $actual = $history->getHistory()[0];
79 $this->assertEquals(History
::CREATED
, $actual['event']);
80 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
81 $this->assertEquals(0, $actual['id']);
83 $history = new History(self
::$historyFilePath);
84 $history->addLink(['id' => 1]);
85 $actual = $history->getHistory()[0];
86 $this->assertEquals(History
::CREATED
, $actual['event']);
87 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
88 $this->assertEquals(1, $actual['id']);
90 $history = new History(self
::$historyFilePath);
91 $history->addLink(['id' => 'str']);
92 $actual = $history->getHistory()[0];
93 $this->assertEquals(History
::CREATED
, $actual['event']);
94 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
95 $this->assertEquals('str', $actual['id']);
99 * Test updated link event
101 public function testUpdateLink()
103 $history = new History(self
::$historyFilePath);
104 $history->updateLink(['id' => 1]);
105 $actual = $history->getHistory()[0];
106 $this->assertEquals(History
::UPDATED
, $actual['event']);
107 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
108 $this->assertEquals(1, $actual['id']);
112 * Test delete link event
114 public function testDeleteLink()
116 $history = new History(self
::$historyFilePath);
117 $history->deleteLink(['id' => 1]);
118 $actual = $history->getHistory()[0];
119 $this->assertEquals(History
::DELETED
, $actual['event']);
120 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
121 $this->assertEquals(1, $actual['id']);
125 * Test updated settings event
127 public function testUpdateSettings()
129 $history = new History(self
::$historyFilePath);
130 $history->updateSettings();
131 $actual = $history->getHistory()[0];
132 $this->assertEquals(History
::SETTINGS
, $actual['event']);
133 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
134 $this->assertEmpty($actual['id']);
138 * Make sure that new items are stored at the beginning
140 public function testHistoryOrder()
142 $history = new History(self
::$historyFilePath);
143 $history->updateLink(['id' => 1]);
144 $actual = $history->getHistory()[0];
145 $this->assertEquals(History
::UPDATED
, $actual['event']);
146 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
147 $this->assertEquals(1, $actual['id']);
149 $history->addLink(['id' => 1]);
150 $actual = $history->getHistory()[0];
151 $this->assertEquals(History
::CREATED
, $actual['event']);
152 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
153 $this->assertEquals(1, $actual['id']);
157 * Re-read history from file after writing an event
159 public function testHistoryRead()
161 $history = new History(self
::$historyFilePath);
162 $history->updateLink(['id' => 1]);
163 $history = new History(self
::$historyFilePath);
164 $actual = $history->getHistory()[0];
165 $this->assertEquals(History
::UPDATED
, $actual['event']);
166 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
167 $this->assertEquals(1, $actual['id']);
171 * Re-read history from file after writing an event and make sure that the order is correct
173 public function testHistoryOrderRead()
175 $history = new History(self
::$historyFilePath);
176 $history->updateLink(['id' => 1]);
177 $history->addLink(['id' => 1]);
179 $history = new History(self
::$historyFilePath);
180 $actual = $history->getHistory()[0];
181 $this->assertEquals(History
::CREATED
, $actual['event']);
182 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
183 $this->assertEquals(1, $actual['id']);
185 $actual = $history->getHistory()[1];
186 $this->assertEquals(History
::UPDATED
, $actual['event']);
187 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
188 $this->assertEquals(1, $actual['id']);
192 * Test retention time: delete old entries.
194 public function testHistoryRententionTime()
196 $history = new History(self
::$historyFilePath, 5);
197 $history->updateLink(['id' => 1]);
198 $this->assertEquals(1, count($history->getHistory()));
199 $arr = $history->getHistory();
200 $arr[0]['datetime'] = new DateTime('-1 hour');
201 FileUtils
::writeFlatDB(self
::$historyFilePath, $arr);
203 $history = new History(self
::$historyFilePath, 60);
204 $this->assertEquals(1, count($history->getHistory()));
205 $this->assertEquals(1, $history->getHistory()[0]['id']);
206 $history->updateLink(['id' => 2]);
207 $this->assertEquals(1, count($history->getHistory()));
208 $this->assertEquals(2, $history->getHistory()[0]['id']);