if ($retentionTime !== null) {
$this->retentionTime = $retentionTime;
}
+ }
+
+ /**
+ * Initialize: read history file.
+ *
+ * Allow lazy loading (don't read the file if it isn't necessary).
+ */
+ protected function initialize()
+ {
$this->check();
$this->read();
}
*/
protected function addEvent($status, $id = null)
{
+ if ($this->history === null) {
+ $this->initialize();
+ }
+
$item = [
'event' => $status,
'datetime' => (new DateTime())->format(DateTime::ATOM),
*/
public function getHistory()
{
+ if ($this->history === null) {
+ $this->initialize();
+ }
+
return $this->history;
}
}
/**
* Test that the history file is created if it doesn't exist.
*/
- public function testConstructFileCreated()
+ public function testConstructLazyLoading()
{
new History(self::$historyFilePath);
+ $this->assertFileNotExists(self::$historyFilePath);
+ }
+
+ /**
+ * Test that the history file is created if it doesn't exist.
+ */
+ public function testAddEventCreateFile()
+ {
+ $history = new History(self::$historyFilePath);
+ $history->updateSettings();
$this->assertFileExists(self::$historyFilePath);
}
{
touch(self::$historyFilePath);
chmod(self::$historyFilePath, 0440);
- new History(self::$historyFilePath);
+ $history = new History(self::$historyFilePath);
+ $history->updateSettings();
}
/**
public function testConstructNotParsable()
{
file_put_contents(self::$historyFilePath, 'not parsable');
+ $history = new History(self::$historyFilePath);
// gzinflate generates a warning
- @new History(self::$historyFilePath);
+ @$history->updateSettings();
}
/**