Add history entries for API endpoint

CHANGED: datetime is now store as an object in history store file
This commit is contained in:
ArthurHoaro 2017-05-07 16:50:20 +02:00
parent a4af59f471
commit 813849e521
16 changed files with 114 additions and 36 deletions

View file

@ -74,21 +74,21 @@ class HistoryTest extends PHPUnit_Framework_TestCase
$history->addLink(['id' => 0]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::CREATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEquals(0, $actual['id']);
$history = new History(self::$historyFilePath);
$history->addLink(['id' => 1]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::CREATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEquals(1, $actual['id']);
$history = new History(self::$historyFilePath);
$history->addLink(['id' => 'str']);
$actual = $history->getHistory()[0];
$this->assertEquals(History::CREATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEquals('str', $actual['id']);
}
@ -101,7 +101,7 @@ class HistoryTest extends PHPUnit_Framework_TestCase
$history->updateLink(['id' => 1]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::UPDATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEquals(1, $actual['id']);
}
@ -114,7 +114,7 @@ class HistoryTest extends PHPUnit_Framework_TestCase
$history->deleteLink(['id' => 1]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::DELETED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEquals(1, $actual['id']);
}
@ -127,7 +127,7 @@ class HistoryTest extends PHPUnit_Framework_TestCase
$history->updateSettings();
$actual = $history->getHistory()[0];
$this->assertEquals(History::SETTINGS, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEmpty($actual['id']);
}
@ -140,13 +140,13 @@ class HistoryTest extends PHPUnit_Framework_TestCase
$history->updateLink(['id' => 1]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::UPDATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEquals(1, $actual['id']);
$history->addLink(['id' => 1]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::CREATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEquals(1, $actual['id']);
}
@ -160,7 +160,7 @@ class HistoryTest extends PHPUnit_Framework_TestCase
$history = new History(self::$historyFilePath);
$actual = $history->getHistory()[0];
$this->assertEquals(History::UPDATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEquals(1, $actual['id']);
}
@ -176,12 +176,12 @@ class HistoryTest extends PHPUnit_Framework_TestCase
$history = new History(self::$historyFilePath);
$actual = $history->getHistory()[0];
$this->assertEquals(History::CREATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEquals(1, $actual['id']);
$actual = $history->getHistory()[1];
$this->assertEquals(History::UPDATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
$this->assertEquals(1, $actual['id']);
}
@ -194,7 +194,7 @@ class HistoryTest extends PHPUnit_Framework_TestCase
$history->updateLink(['id' => 1]);
$this->assertEquals(1, count($history->getHistory()));
$arr = $history->getHistory();
$arr[0]['datetime'] = (new DateTime('-1 hour'))->format(DateTime::ATOM);
$arr[0]['datetime'] = new DateTime('-1 hour');
FileUtils::writeFlatDB(self::$historyFilePath, $arr);
$history = new History(self::$historyFilePath, 60);

View file

@ -628,7 +628,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals($nbLinks, count($history));
foreach ($history as $value) {
$this->assertEquals(History::CREATED, $value['event']);
$this->assertTrue(new DateTime('-5 seconds') < DateTime::createFromFormat(DateTime::ATOM, $value['datetime']));
$this->assertTrue(new DateTime('-5 seconds') < $value['datetime']);
$this->assertTrue(is_int($value['id']));
}
@ -638,7 +638,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals($nbLinks * 2, count($history));
for ($i = 0 ; $i < $nbLinks ; $i++) {
$this->assertEquals(History::UPDATED, $history[$i]['event']);
$this->assertTrue(new DateTime('-5 seconds') < DateTime::createFromFormat(DateTime::ATOM, $history[$i]['datetime']));
$this->assertTrue(new DateTime('-5 seconds') < $history[$i]['datetime']);
$this->assertTrue(is_int($history[$i]['id']));
}
}

View file

@ -16,6 +16,11 @@ class DeleteLinkTest extends \PHPUnit_Framework_TestCase
*/
protected static $testDatastore = 'sandbox/datastore.php';
/**
* @var string datastore to test write operations
*/
protected static $testHistory = 'sandbox/history.php';
/**
* @var ConfigManager instance
*/
@ -31,6 +36,11 @@ class DeleteLinkTest extends \PHPUnit_Framework_TestCase
*/
protected $linkDB;
/**
* @var \History instance.
*/
protected $history;
/**
* @var Container instance.
*/
@ -50,9 +60,13 @@ class DeleteLinkTest extends \PHPUnit_Framework_TestCase
$this->refDB = new \ReferenceLinkDB();
$this->refDB->write(self::$testDatastore);
$this->linkDB = new \LinkDB(self::$testDatastore, true, false);
$refHistory = new \ReferenceHistory();
$refHistory->write(self::$testHistory);
$this->history = new \History(self::$testHistory);
$this->container = new Container();
$this->container['conf'] = $this->conf;
$this->container['db'] = $this->linkDB;
$this->container['history'] = $this->history;
$this->controller = new Links($this->container);
}
@ -63,6 +77,7 @@ class DeleteLinkTest extends \PHPUnit_Framework_TestCase
public function tearDown()
{
@unlink(self::$testDatastore);
@unlink(self::$testHistory);
}
/**
@ -83,6 +98,13 @@ class DeleteLinkTest extends \PHPUnit_Framework_TestCase
$this->linkDB = new \LinkDB(self::$testDatastore, true, false);
$this->assertFalse(isset($this->linkDB[$id]));
$historyEntry = $this->history->getHistory()[0];
$this->assertEquals(\History::DELETED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);
$this->assertEquals($id, $historyEntry['id']);
}
/**

View file

@ -62,6 +62,7 @@ class GetLinkIdTest extends \PHPUnit_Framework_TestCase
$this->container = new Container();
$this->container['conf'] = $this->conf;
$this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
$this->container['history'] = null;
$this->controller = new Links($this->container);
}

View file

@ -61,6 +61,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
$this->container = new Container();
$this->container['conf'] = $this->conf;
$this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
$this->container['history'] = null;
$this->controller = new Links($this->container);
}

View file

@ -29,11 +29,6 @@ class HistoryTest extends \PHPUnit_Framework_TestCase
*/
protected $refHistory = null;
/**
* @var \History instance.
*/
protected $history;
/**
* @var Container instance.
*/
@ -52,10 +47,10 @@ class HistoryTest extends \PHPUnit_Framework_TestCase
$this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
$this->refHistory = new \ReferenceHistory();
$this->refHistory->write(self::$testHistory);
$this->conf->set('resource.history', self::$testHistory);
$this->container = new Container();
$this->container['conf'] = $this->conf;
$this->container['db'] = true;
$this->container['history'] = new \History(self::$testHistory);
$this->controller = new History($this->container);
}

View file

@ -54,6 +54,7 @@ class InfoTest extends \PHPUnit_Framework_TestCase
$this->container = new Container();
$this->container['conf'] = $this->conf;
$this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
$this->container['history'] = null;
$this->controller = new Info($this->container);
}

View file

@ -23,6 +23,11 @@ class PostLinkTest extends \PHPUnit_Framework_TestCase
*/
protected static $testDatastore = 'sandbox/datastore.php';
/**
* @var string datastore to test write operations
*/
protected static $testHistory = 'sandbox/history.php';
/**
* @var ConfigManager instance
*/
@ -33,6 +38,11 @@ class PostLinkTest extends \PHPUnit_Framework_TestCase
*/
protected $refDB = null;
/**
* @var \History instance.
*/
protected $history;
/**
* @var Container instance.
*/
@ -57,9 +67,14 @@ class PostLinkTest extends \PHPUnit_Framework_TestCase
$this->refDB = new \ReferenceLinkDB();
$this->refDB->write(self::$testDatastore);
$refHistory = new \ReferenceHistory();
$refHistory->write(self::$testHistory);
$this->history = new \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->controller = new Links($this->container);
@ -85,6 +100,7 @@ class PostLinkTest extends \PHPUnit_Framework_TestCase
public function tearDown()
{
@unlink(self::$testDatastore);
@unlink(self::$testHistory);
}
/**
@ -112,6 +128,13 @@ class PostLinkTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(false, $data['private']);
$this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']));
$this->assertEquals('', $data['updated']);
$historyEntry = $this->history->getHistory()[0];
$this->assertEquals(\History::CREATED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);
$this->assertEquals(43, $historyEntry['id']);
}
/**

View file

@ -17,6 +17,11 @@ class PutLinkTest extends \PHPUnit_Framework_TestCase
*/
protected static $testDatastore = 'sandbox/datastore.php';
/**
* @var string datastore to test write operations
*/
protected static $testHistory = 'sandbox/history.php';
/**
* @var ConfigManager instance
*/
@ -27,6 +32,11 @@ class PutLinkTest extends \PHPUnit_Framework_TestCase
*/
protected $refDB = null;
/**
* @var \History instance.
*/
protected $history;
/**
* @var Container instance.
*/
@ -51,9 +61,14 @@ class PutLinkTest extends \PHPUnit_Framework_TestCase
$this->refDB = new \ReferenceLinkDB();
$this->refDB->write(self::$testDatastore);
$refHistory = new \ReferenceHistory();
$refHistory->write(self::$testHistory);
$this->history = new \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->controller = new Links($this->container);
@ -71,6 +86,7 @@ class PutLinkTest extends \PHPUnit_Framework_TestCase
public function tearDown()
{
@unlink(self::$testDatastore);
@unlink(self::$testHistory);
}
/**
@ -100,6 +116,13 @@ class PutLinkTest extends \PHPUnit_Framework_TestCase
\DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
);
$this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']));
$historyEntry = $this->history->getHistory()[0];
$this->assertEquals(\History::UPDATED, $historyEntry['event']);
$this->assertTrue(
(new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
);
$this->assertEquals($id, $historyEntry['id']);
}
/**