namespacing: \Shaarli\History

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
VirtualTam 2018-12-02 23:24:58 +01:00
parent 1826e383ec
commit bdc5152d48
13 changed files with 50 additions and 37 deletions

View file

@ -1,4 +1,9 @@
<?php
namespace Shaarli;
use DateTime;
use Exception;
use FileUtils;
/**
* Class History
@ -141,7 +146,7 @@ class History
* Save a new event and write it in the history file.
*
* @param string $status Event key, should be defined as constant.
* @param mixed $id Event item identifier (e.g. link ID).
* @param mixed $id Event item identifier (e.g. link ID).
*/
protected function addEvent($status, $id = null)
{
@ -166,11 +171,11 @@ class History
*/
protected function check()
{
if (! is_file($this->historyFilePath)) {
if (!is_file($this->historyFilePath)) {
FileUtils::writeFlatDB($this->historyFilePath, []);
}
if (! is_writable($this->historyFilePath)) {
if (!is_writable($this->historyFilePath)) {
throw new Exception(t('History file isn\'t readable or writable'));
}
}
@ -191,7 +196,7 @@ class History
*/
protected function write()
{
$comparaison = new DateTime('-'. $this->retentionTime . ' seconds');
$comparaison = new DateTime('-' . $this->retentionTime . ' seconds');
foreach ($this->history as $key => $value) {
if ($value['datetime'] < $comparaison) {
unset($this->history[$key]);

View file

@ -2,6 +2,7 @@
use Psr\Log\LogLevel;
use Shaarli\Config\ConfigManager;
use Shaarli\History;
use Shaarli\NetscapeBookmarkParser\NetscapeBookmarkParser;
use Katzgrau\KLogger\Logger;

View file

@ -30,7 +30,7 @@ abstract class ApiController
protected $linkDb;
/**
* @var \History
* @var \Shaarli\History
*/
protected $history;

View file

@ -76,6 +76,7 @@ require_once 'application/PluginManager.php';
require_once 'application/Router.php';
require_once 'application/Updater.php';
use \Shaarli\Config\ConfigManager;
use Shaarli\History;
use \Shaarli\Languages;
use \Shaarli\Security\LoginManager;
use \Shaarli\Security\SessionManager;

View file

@ -1,9 +1,12 @@
<?php
require_once 'application/History.php';
namespace Shaarli;
use DateTime;
use Exception;
use FileUtils;
class HistoryTest extends PHPUnit_Framework_TestCase
class HistoryTest extends \PHPUnit\Framework\TestCase
{
/**
* @var string History file path

View file

@ -3,6 +3,7 @@
require_once 'application/NetscapeBookmarkUtils.php';
use Shaarli\Config\ConfigManager;
use Shaarli\History;
/**
* Utility function to load a file's metadata in a $_FILES-like array

View file

@ -49,7 +49,7 @@ class HistoryTest extends \PHPUnit_Framework_TestCase
$this->container = new Container();
$this->container['conf'] = $this->conf;
$this->container['db'] = true;
$this->container['history'] = new \History(self::$testHistory);
$this->container['history'] = new \Shaarli\History(self::$testHistory);
$this->controller = new History($this->container);
}
@ -78,35 +78,35 @@ class HistoryTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($this->refHistory->count(), count($data));
$this->assertEquals(\History::DELETED, $data[0]['event']);
$this->assertEquals(\Shaarli\History::DELETED, $data[0]['event']);
$this->assertEquals(
\DateTime::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime::ATOM),
$data[0]['datetime']
);
$this->assertEquals(124, $data[0]['id']);
$this->assertEquals(\History::SETTINGS, $data[1]['event']);
$this->assertEquals(\Shaarli\History::SETTINGS, $data[1]['event']);
$this->assertEquals(
\DateTime::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime::ATOM),
$data[1]['datetime']
);
$this->assertNull($data[1]['id']);
$this->assertEquals(\History::UPDATED, $data[2]['event']);
$this->assertEquals(\Shaarli\History::UPDATED, $data[2]['event']);
$this->assertEquals(
\DateTime::createFromFormat('Ymd_His', '20170301_121214')->format(\DateTime::ATOM),
$data[2]['datetime']
);
$this->assertEquals(123, $data[2]['id']);
$this->assertEquals(\History::CREATED, $data[3]['event']);
$this->assertEquals(\Shaarli\History::CREATED, $data[3]['event']);
$this->assertEquals(
\DateTime::createFromFormat('Ymd_His', '20170201_121214')->format(\DateTime::ATOM),
$data[3]['datetime']
);
$this->assertEquals(124, $data[3]['id']);
$this->assertEquals(\History::CREATED, $data[4]['event']);
$this->assertEquals(\Shaarli\History::CREATED, $data[4]['event']);
$this->assertEquals(
\DateTime::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime::ATOM),
$data[4]['datetime']
@ -131,7 +131,7 @@ class HistoryTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, count($data));
$this->assertEquals(\History::DELETED, $data[0]['event']);
$this->assertEquals(\Shaarli\History::DELETED, $data[0]['event']);
$this->assertEquals(
\DateTime::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime::ATOM),
$data[0]['datetime']
@ -156,7 +156,7 @@ class HistoryTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, count($data));
$this->assertEquals(\History::CREATED, $data[0]['event']);
$this->assertEquals(\Shaarli\History::CREATED, $data[0]['event']);
$this->assertEquals(
\DateTime::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime::ATOM),
$data[0]['datetime']
@ -181,7 +181,7 @@ class HistoryTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, count($data));
$this->assertEquals(\History::DELETED, $data[0]['event']);
$this->assertEquals(\Shaarli\History::DELETED, $data[0]['event']);
$this->assertEquals(
\DateTime::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime::ATOM),
$data[0]['datetime']
@ -206,7 +206,7 @@ class HistoryTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, count($data));
$this->assertEquals(\History::SETTINGS, $data[0]['event']);
$this->assertEquals(\Shaarli\History::SETTINGS, $data[0]['event']);
$this->assertEquals(
\DateTime::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime::ATOM),
$data[0]['datetime']

View file

@ -37,7 +37,7 @@ class DeleteLinkTest extends \PHPUnit_Framework_TestCase
protected $linkDB;
/**
* @var \History instance.
* @var \Shaarli\History instance.
*/
protected $history;
@ -62,7 +62,7 @@ class DeleteLinkTest extends \PHPUnit_Framework_TestCase
$this->linkDB = new \LinkDB(self::$testDatastore, true, false);
$refHistory = new \ReferenceHistory();
$refHistory->write(self::$testHistory);
$this->history = new \History(self::$testHistory);
$this->history = new \Shaarli\History(self::$testHistory);
$this->container = new Container();
$this->container['conf'] = $this->conf;
$this->container['db'] = $this->linkDB;
@ -100,7 +100,7 @@ class DeleteLinkTest extends \PHPUnit_Framework_TestCase
$this->assertFalse(isset($this->linkDB[$id]));
$historyEntry = $this->history->getHistory()[0];
$this->assertEquals(\History::DELETED, $historyEntry['event']);
$this->assertEquals(\Shaarli\History::DELETED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);

View file

@ -40,7 +40,7 @@ class PostLinkTest extends TestCase
protected $refDB = null;
/**
* @var \History instance.
* @var \Shaarli\History instance.
*/
protected $history;
@ -70,12 +70,12 @@ class PostLinkTest extends TestCase
$refHistory = new \ReferenceHistory();
$refHistory->write(self::$testHistory);
$this->history = new \History(self::$testHistory);
$this->history = new \Shaarli\History(self::$testHistory);
$this->container = new Container();
$this->container['conf'] = $this->conf;
$this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
$this->container['history'] = new \History(self::$testHistory);
$this->container['history'] = new \Shaarli\History(self::$testHistory);
$this->controller = new Links($this->container);
@ -133,7 +133,7 @@ class PostLinkTest extends TestCase
$this->assertEquals('', $data['updated']);
$historyEntry = $this->history->getHistory()[0];
$this->assertEquals(\History::CREATED, $historyEntry['event']);
$this->assertEquals(\Shaarli\History::CREATED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);

View file

@ -32,7 +32,7 @@ class PutLinkTest extends \PHPUnit_Framework_TestCase
protected $refDB = null;
/**
* @var \History instance.
* @var \Shaarli\History instance.
*/
protected $history;
@ -62,12 +62,12 @@ class PutLinkTest extends \PHPUnit_Framework_TestCase
$refHistory = new \ReferenceHistory();
$refHistory->write(self::$testHistory);
$this->history = new \History(self::$testHistory);
$this->history = new \Shaarli\History(self::$testHistory);
$this->container = new Container();
$this->container['conf'] = $this->conf;
$this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
$this->container['history'] = new \History(self::$testHistory);
$this->container['history'] = new \Shaarli\History(self::$testHistory);
$this->controller = new Links($this->container);
@ -119,7 +119,7 @@ class PutLinkTest extends \PHPUnit_Framework_TestCase
);
$historyEntry = $this->history->getHistory()[0];
$this->assertEquals(\History::UPDATED, $historyEntry['event']);
$this->assertEquals(\Shaarli\History::UPDATED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);

View file

@ -37,7 +37,7 @@ class DeleteTagTest extends \PHPUnit_Framework_TestCase
protected $linkDB;
/**
* @var \History instance.
* @var \Shaarli\History instance.
*/
protected $history;
@ -62,7 +62,7 @@ class DeleteTagTest extends \PHPUnit_Framework_TestCase
$this->linkDB = new \LinkDB(self::$testDatastore, true, false);
$refHistory = new \ReferenceHistory();
$refHistory->write(self::$testHistory);
$this->history = new \History(self::$testHistory);
$this->history = new \Shaarli\History(self::$testHistory);
$this->container = new Container();
$this->container['conf'] = $this->conf;
$this->container['db'] = $this->linkDB;
@ -103,12 +103,12 @@ class DeleteTagTest extends \PHPUnit_Framework_TestCase
// 2 links affected
$historyEntry = $this->history->getHistory()[0];
$this->assertEquals(\History::UPDATED, $historyEntry['event']);
$this->assertEquals(\Shaarli\History::UPDATED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);
$historyEntry = $this->history->getHistory()[1];
$this->assertEquals(\History::UPDATED, $historyEntry['event']);
$this->assertEquals(\Shaarli\History::UPDATED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);
@ -137,7 +137,7 @@ class DeleteTagTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($tags[strtolower($tagName)] > 0);
$historyEntry = $this->history->getHistory()[0];
$this->assertEquals(\History::UPDATED, $historyEntry['event']);
$this->assertEquals(\Shaarli\History::UPDATED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);

View file

@ -33,7 +33,7 @@ class PutTagTest extends \PHPUnit_Framework_TestCase
protected $refDB = null;
/**
* @var \History instance.
* @var \Shaarli\History instance.
*/
protected $history;
@ -68,7 +68,7 @@ class PutTagTest extends \PHPUnit_Framework_TestCase
$refHistory = new \ReferenceHistory();
$refHistory->write(self::$testHistory);
$this->history = new \History(self::$testHistory);
$this->history = new \Shaarli\History(self::$testHistory);
$this->container = new Container();
$this->container['conf'] = $this->conf;
@ -113,12 +113,12 @@ class PutTagTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(2, $tags[$newName]);
$historyEntry = $this->history->getHistory()[0];
$this->assertEquals(\History::UPDATED, $historyEntry['event']);
$this->assertEquals(\Shaarli\History::UPDATED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);
$historyEntry = $this->history->getHistory()[1];
$this->assertEquals(\History::UPDATED, $historyEntry['event']);
$this->assertEquals(\Shaarli\History::UPDATED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);

View file

@ -1,5 +1,7 @@
<?php
use Shaarli\History;
/**
* Populates a reference history
*/