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 testConstructFileCreated()
26 new History(self
::$historyFilePath);
27 $this->assertFileExists(self
::$historyFilePath);
31 * Not writable history file: raise an exception.
33 * @expectedException Exception
34 * @expectedExceptionMessage History file isn't readable or writable
36 public function testConstructNotWritable()
38 touch(self
::$historyFilePath);
39 chmod(self
::$historyFilePath, 0440);
40 new History(self
::$historyFilePath);
44 * Not parsable history file: raise an exception.
46 * @expectedException Exception
47 * @expectedExceptionMessageRegExp /Could not parse history file/
49 public function testConstructNotParsable()
51 file_put_contents(self
::$historyFilePath, 'not parsable');
52 // gzinflate generates a warning
53 @new History(self
::$historyFilePath);
59 public function testAddLink()
61 $history = new History(self
::$historyFilePath);
62 $history->addLink(['id' => 0]);
63 $actual = $history->getHistory()[0];
64 $this->assertEquals(History
::CREATED
, $actual['event']);
65 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
66 $this->assertEquals(0, $actual['id']);
68 $history = new History(self
::$historyFilePath);
69 $history->addLink(['id' => 1]);
70 $actual = $history->getHistory()[0];
71 $this->assertEquals(History
::CREATED
, $actual['event']);
72 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
73 $this->assertEquals(1, $actual['id']);
75 $history = new History(self
::$historyFilePath);
76 $history->addLink(['id' => 'str']);
77 $actual = $history->getHistory()[0];
78 $this->assertEquals(History
::CREATED
, $actual['event']);
79 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
80 $this->assertEquals('str', $actual['id']);
84 * Test updated link event
86 public function testUpdateLink()
88 $history = new History(self
::$historyFilePath);
89 $history->updateLink(['id' => 1]);
90 $actual = $history->getHistory()[0];
91 $this->assertEquals(History
::UPDATED
, $actual['event']);
92 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
93 $this->assertEquals(1, $actual['id']);
97 * Test delete link event
99 public function testDeleteLink()
101 $history = new History(self
::$historyFilePath);
102 $history->deleteLink(['id' => 1]);
103 $actual = $history->getHistory()[0];
104 $this->assertEquals(History
::DELETED
, $actual['event']);
105 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
106 $this->assertEquals(1, $actual['id']);
110 * Test updated settings event
112 public function testUpdateSettings()
114 $history = new History(self
::$historyFilePath);
115 $history->updateSettings();
116 $actual = $history->getHistory()[0];
117 $this->assertEquals(History
::SETTINGS
, $actual['event']);
118 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
119 $this->assertEmpty($actual['id']);
123 * Make sure that new items are stored at the beginning
125 public function testHistoryOrder()
127 $history = new History(self
::$historyFilePath);
128 $history->updateLink(['id' => 1]);
129 $actual = $history->getHistory()[0];
130 $this->assertEquals(History
::UPDATED
, $actual['event']);
131 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
132 $this->assertEquals(1, $actual['id']);
134 $history->addLink(['id' => 1]);
135 $actual = $history->getHistory()[0];
136 $this->assertEquals(History
::CREATED
, $actual['event']);
137 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
138 $this->assertEquals(1, $actual['id']);
142 * Re-read history from file after writing an event
144 public function testHistoryRead()
146 $history = new History(self
::$historyFilePath);
147 $history->updateLink(['id' => 1]);
148 $history = new History(self
::$historyFilePath);
149 $actual = $history->getHistory()[0];
150 $this->assertEquals(History
::UPDATED
, $actual['event']);
151 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
152 $this->assertEquals(1, $actual['id']);
156 * Re-read history from file after writing an event and make sure that the order is correct
158 public function testHistoryOrderRead()
160 $history = new History(self
::$historyFilePath);
161 $history->updateLink(['id' => 1]);
162 $history->addLink(['id' => 1]);
164 $history = new History(self
::$historyFilePath);
165 $actual = $history->getHistory()[0];
166 $this->assertEquals(History
::CREATED
, $actual['event']);
167 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
168 $this->assertEquals(1, $actual['id']);
170 $actual = $history->getHistory()[1];
171 $this->assertEquals(History
::UPDATED
, $actual['event']);
172 $this->assertTrue(new DateTime('-2 seconds') < DateTime
::createFromFormat(DateTime
::ATOM
, $actual['datetime']));
173 $this->assertEquals(1, $actual['id']);
177 * Test retention time: delete old entries.
179 public function testHistoryRententionTime()
181 $history = new History(self
::$historyFilePath, 5);
182 $history->updateLink(['id' => 1]);
183 $this->assertEquals(1, count($history->getHistory()));
184 $arr = $history->getHistory();
185 $arr[0]['datetime'] = (new DateTime('-1 hour'))->format(DateTime
::ATOM
);
186 FileUtils
::writeFlatDB(self
::$historyFilePath, $arr);
188 $history = new History(self
::$historyFilePath, 60);
189 $this->assertEquals(1, count($history->getHistory()));
190 $this->assertEquals(1, $history->getHistory()[0]['id']);
191 $history->updateLink(['id' => 2]);
192 $this->assertEquals(1, count($history->getHistory()));
193 $this->assertEquals(2, $history->getHistory()[0]['id']);