8 class HistoryTest
extends \PHPUnit\Framework\TestCase
11 * @var string History file path
13 protected static $historyFilePath = 'sandbox/history.php';
16 * Delete history file.
18 public function tearDown()
20 @unlink(self
::$historyFilePath);
24 * Test that the history file is created if it doesn't exist.
26 public function testConstructLazyLoading()
28 new History(self
::$historyFilePath);
29 $this->assertFileNotExists(self
::$historyFilePath);
33 * Test that the history file is created if it doesn't exist.
35 public function testAddEventCreateFile()
37 $history = new History(self
::$historyFilePath);
38 $history->updateSettings();
39 $this->assertFileExists(self
::$historyFilePath);
43 * Not writable history file: raise an exception.
45 * @expectedException Exception
46 * @expectedExceptionMessage History file isn't readable or writable
48 public function testConstructNotWritable()
50 touch(self
::$historyFilePath);
51 chmod(self
::$historyFilePath, 0440);
52 $history = new History(self
::$historyFilePath);
53 $history->updateSettings();
57 * Not parsable history file: raise an exception.
59 * @expectedException Exception
60 * @expectedExceptionMessageRegExp /Could not parse history file/
62 public function testConstructNotParsable()
64 file_put_contents(self
::$historyFilePath, 'not parsable');
65 $history = new History(self
::$historyFilePath);
66 // gzinflate generates a warning
67 @$history->updateSettings();
73 public function testAddLink()
75 $history = new History(self
::$historyFilePath);
76 $history->addLink(['id' => 0]);
77 $actual = $history->getHistory()[0];
78 $this->assertEquals(History
::CREATED
, $actual['event']);
79 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
80 $this->assertEquals(0, $actual['id']);
82 $history = new History(self
::$historyFilePath);
83 $history->addLink(['id' => 1]);
84 $actual = $history->getHistory()[0];
85 $this->assertEquals(History
::CREATED
, $actual['event']);
86 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
87 $this->assertEquals(1, $actual['id']);
89 $history = new History(self
::$historyFilePath);
90 $history->addLink(['id' => 'str']);
91 $actual = $history->getHistory()[0];
92 $this->assertEquals(History
::CREATED
, $actual['event']);
93 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
94 $this->assertEquals('str', $actual['id']);
98 * Test updated link event
100 public function testUpdateLink()
102 $history = new History(self
::$historyFilePath);
103 $history->updateLink(['id' => 1]);
104 $actual = $history->getHistory()[0];
105 $this->assertEquals(History
::UPDATED
, $actual['event']);
106 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
107 $this->assertEquals(1, $actual['id']);
111 * Test delete link event
113 public function testDeleteLink()
115 $history = new History(self
::$historyFilePath);
116 $history->deleteLink(['id' => 1]);
117 $actual = $history->getHistory()[0];
118 $this->assertEquals(History
::DELETED
, $actual['event']);
119 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
120 $this->assertEquals(1, $actual['id']);
124 * Test updated settings event
126 public function testUpdateSettings()
128 $history = new History(self
::$historyFilePath);
129 $history->updateSettings();
130 $actual = $history->getHistory()[0];
131 $this->assertEquals(History
::SETTINGS
, $actual['event']);
132 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
133 $this->assertEmpty($actual['id']);
137 * Make sure that new items are stored at the beginning
139 public function testHistoryOrder()
141 $history = new History(self
::$historyFilePath);
142 $history->updateLink(['id' => 1]);
143 $actual = $history->getHistory()[0];
144 $this->assertEquals(History
::UPDATED
, $actual['event']);
145 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
146 $this->assertEquals(1, $actual['id']);
148 $history->addLink(['id' => 1]);
149 $actual = $history->getHistory()[0];
150 $this->assertEquals(History
::CREATED
, $actual['event']);
151 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
152 $this->assertEquals(1, $actual['id']);
156 * Re-read history from file after writing an event
158 public function testHistoryRead()
160 $history = new History(self
::$historyFilePath);
161 $history->updateLink(['id' => 1]);
162 $history = new History(self
::$historyFilePath);
163 $actual = $history->getHistory()[0];
164 $this->assertEquals(History
::UPDATED
, $actual['event']);
165 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
166 $this->assertEquals(1, $actual['id']);
170 * Re-read history from file after writing an event and make sure that the order is correct
172 public function testHistoryOrderRead()
174 $history = new History(self
::$historyFilePath);
175 $history->updateLink(['id' => 1]);
176 $history->addLink(['id' => 1]);
178 $history = new History(self
::$historyFilePath);
179 $actual = $history->getHistory()[0];
180 $this->assertEquals(History
::CREATED
, $actual['event']);
181 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
182 $this->assertEquals(1, $actual['id']);
184 $actual = $history->getHistory()[1];
185 $this->assertEquals(History
::UPDATED
, $actual['event']);
186 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
187 $this->assertEquals(1, $actual['id']);
191 * Test retention time: delete old entries.
193 public function testHistoryRententionTime()
195 $history = new History(self
::$historyFilePath, 5);
196 $history->updateLink(['id' => 1]);
197 $this->assertEquals(1, count($history->getHistory()));
198 $arr = $history->getHistory();
199 $arr[0]['datetime'] = new DateTime('-1 hour');
200 FileUtils
::writeFlatDB(self
::$historyFilePath, $arr);
202 $history = new History(self
::$historyFilePath, 60);
203 $this->assertEquals(1, count($history->getHistory()));
204 $this->assertEquals(1, $history->getHistory()[0]['id']);
205 $history->updateLink(['id' => 2]);
206 $this->assertEquals(1, count($history->getHistory()));
207 $this->assertEquals(2, $history->getHistory()[0]['id']);