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
@ -166,11 +171,11 @@ protected function addEvent($status, $id = null)
*/
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 @@ protected function read()
*/
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/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 @@ public function setUp()
$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 @@ public function testGetHistory()
$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 @@ public function testGetHistoryLimit()
$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 @@ public function testGetHistoryOffset()
$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 @@ public function testGetHistorySince()
$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 @@ public function testGetHistorySinceOffsetLimit()
$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 @@ public function setUp()
$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 @@ public function testDeleteLinkValid()
$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 @@ public function setUp()
$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 @@ public function testPostLinkMinimal()
$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 @@ public function setUp()
$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 @@ public function testPutLinkMinimal()
);
$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 @@ public function setUp()
$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 @@ public function testDeleteTagValid()
// 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 @@ public function testDeleteTagCaseSensitivity()
$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 @@ public function setUp()
$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 @@ public function testPutLinkValid()
$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
*/