3 require_once 'application/History.php';
6 class HistoryTest
extends PHPUnit_Framework_TestCase
9 * @var string History file path
11 protected static $historyFilePath = 'sandbox/history.php';
14 * Delete history file.
16 public function tearDown()
18 @unlink(self
::$historyFilePath);
22 * Test that the history file is created if it doesn't exist.
24 public function testConstructLazyLoading()
26 new History(self
::$historyFilePath);
27 $this->assertFileNotExists(self
::$historyFilePath);
31 * Test that the history file is created if it doesn't exist.
33 public function testAddEventCreateFile()
35 $history = new History(self
::$historyFilePath);
36 $history->updateSettings();
37 $this->assertFileExists(self
::$historyFilePath);
41 * Not writable history file: raise an exception.
43 * @expectedException Exception
44 * @expectedExceptionMessage History file isn't readable or writable
46 public function testConstructNotWritable()
48 touch(self
::$historyFilePath);
49 chmod(self
::$historyFilePath, 0440);
50 $history = new History(self
::$historyFilePath);
51 $history->updateSettings();
55 * Not parsable history file: raise an exception.
57 * @expectedException Exception
58 * @expectedExceptionMessageRegExp /Could not parse history file/
60 public function testConstructNotParsable()
62 file_put_contents(self
::$historyFilePath, 'not parsable');
63 $history = new History(self
::$historyFilePath);
64 // gzinflate generates a warning
65 @$history->updateSettings();
71 public function testAddLink()
73 $history = new History(self
::$historyFilePath);
74 $history->addLink(['id' => 0]);
75 $actual = $history->getHistory()[0];
76 $this->assertEquals(History
::CREATED
, $actual['event']);
77 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
78 $this->assertEquals(0, $actual['id']);
80 $history = new History(self
::$historyFilePath);
81 $history->addLink(['id' => 1]);
82 $actual = $history->getHistory()[0];
83 $this->assertEquals(History
::CREATED
, $actual['event']);
84 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
85 $this->assertEquals(1, $actual['id']);
87 $history = new History(self
::$historyFilePath);
88 $history->addLink(['id' => 'str']);
89 $actual = $history->getHistory()[0];
90 $this->assertEquals(History
::CREATED
, $actual['event']);
91 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
92 $this->assertEquals('str', $actual['id']);
96 * Test updated link event
98 public function testUpdateLink()
100 $history = new History(self
::$historyFilePath);
101 $history->updateLink(['id' => 1]);
102 $actual = $history->getHistory()[0];
103 $this->assertEquals(History
::UPDATED
, $actual['event']);
104 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
105 $this->assertEquals(1, $actual['id']);
109 * Test delete link event
111 public function testDeleteLink()
113 $history = new History(self
::$historyFilePath);
114 $history->deleteLink(['id' => 1]);
115 $actual = $history->getHistory()[0];
116 $this->assertEquals(History
::DELETED
, $actual['event']);
117 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
118 $this->assertEquals(1, $actual['id']);
122 * Test updated settings event
124 public function testUpdateSettings()
126 $history = new History(self
::$historyFilePath);
127 $history->updateSettings();
128 $actual = $history->getHistory()[0];
129 $this->assertEquals(History
::SETTINGS
, $actual['event']);
130 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
131 $this->assertEmpty($actual['id']);
135 * Make sure that new items are stored at the beginning
137 public function testHistoryOrder()
139 $history = new History(self
::$historyFilePath);
140 $history->updateLink(['id' => 1]);
141 $actual = $history->getHistory()[0];
142 $this->assertEquals(History
::UPDATED
, $actual['event']);
143 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
144 $this->assertEquals(1, $actual['id']);
146 $history->addLink(['id' => 1]);
147 $actual = $history->getHistory()[0];
148 $this->assertEquals(History
::CREATED
, $actual['event']);
149 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
150 $this->assertEquals(1, $actual['id']);
154 * Re-read history from file after writing an event
156 public function testHistoryRead()
158 $history = new History(self
::$historyFilePath);
159 $history->updateLink(['id' => 1]);
160 $history = new History(self
::$historyFilePath);
161 $actual = $history->getHistory()[0];
162 $this->assertEquals(History
::UPDATED
, $actual['event']);
163 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
164 $this->assertEquals(1, $actual['id']);
168 * Re-read history from file after writing an event and make sure that the order is correct
170 public function testHistoryOrderRead()
172 $history = new History(self
::$historyFilePath);
173 $history->updateLink(['id' => 1]);
174 $history->addLink(['id' => 1]);
176 $history = new History(self
::$historyFilePath);
177 $actual = $history->getHistory()[0];
178 $this->assertEquals(History
::CREATED
, $actual['event']);
179 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
180 $this->assertEquals(1, $actual['id']);
182 $actual = $history->getHistory()[1];
183 $this->assertEquals(History
::UPDATED
, $actual['event']);
184 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
185 $this->assertEquals(1, $actual['id']);
189 * Test retention time: delete old entries.
191 public function testHistoryRententionTime()
193 $history = new History(self
::$historyFilePath, 5);
194 $history->updateLink(['id' => 1]);
195 $this->assertEquals(1, count($history->getHistory()));
196 $arr = $history->getHistory();
197 $arr[0]['datetime'] = new DateTime('-1 hour');
198 FileUtils
::writeFlatDB(self
::$historyFilePath, $arr);
200 $history = new History(self
::$historyFilePath, 60);
201 $this->assertEquals(1, count($history->getHistory()));
202 $this->assertEquals(1, $history->getHistory()[0]['id']);
203 $history->updateLink(['id' => 2]);
204 $this->assertEquals(1, count($history->getHistory()));
205 $this->assertEquals(2, $history->getHistory()[0]['id']);