From 813849e5216cb87121e0f778a734575be6a36052 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 7 May 2017 16:50:20 +0200 Subject: Add history entries for API endpoint CHANGED: datetime is now store as an object in history store file --- tests/HistoryTest.php | 24 +++++++++++----------- tests/NetscapeBookmarkUtils/BookmarkImportTest.php | 4 ++-- tests/api/controllers/DeleteLinkTest.php | 22 ++++++++++++++++++++ tests/api/controllers/GetLinkIdTest.php | 1 + tests/api/controllers/GetLinksTest.php | 1 + tests/api/controllers/HistoryTest.php | 7 +------ tests/api/controllers/InfoTest.php | 1 + tests/api/controllers/PostLinkTest.php | 23 +++++++++++++++++++++ tests/api/controllers/PutLinkTest.php | 23 +++++++++++++++++++++ 9 files changed, 86 insertions(+), 20 deletions(-) (limited to 'tests') diff --git a/tests/HistoryTest.php b/tests/HistoryTest.php index 91525845..d3bef5a3 100644 --- a/tests/HistoryTest.php +++ b/tests/HistoryTest.php @@ -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); diff --git a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php index f838f259..5fc1d1e8 100644 --- a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php +++ b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php @@ -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'])); } } diff --git a/tests/api/controllers/DeleteLinkTest.php b/tests/api/controllers/DeleteLinkTest.php index 6894e8a2..7d797137 100644 --- a/tests/api/controllers/DeleteLinkTest.php +++ b/tests/api/controllers/DeleteLinkTest.php @@ -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']); } /** diff --git a/tests/api/controllers/GetLinkIdTest.php b/tests/api/controllers/GetLinkIdTest.php index 45b18e6a..57528d5a 100644 --- a/tests/api/controllers/GetLinkIdTest.php +++ b/tests/api/controllers/GetLinkIdTest.php @@ -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); } diff --git a/tests/api/controllers/GetLinksTest.php b/tests/api/controllers/GetLinksTest.php index 10330cd9..84ae7f7a 100644 --- a/tests/api/controllers/GetLinksTest.php +++ b/tests/api/controllers/GetLinksTest.php @@ -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); } diff --git a/tests/api/controllers/HistoryTest.php b/tests/api/controllers/HistoryTest.php index 21e9c0ba..61046d97 100644 --- a/tests/api/controllers/HistoryTest.php +++ b/tests/api/controllers/HistoryTest.php @@ -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); } diff --git a/tests/api/controllers/InfoTest.php b/tests/api/controllers/InfoTest.php index 4beef3f7..e85eb281 100644 --- a/tests/api/controllers/InfoTest.php +++ b/tests/api/controllers/InfoTest.php @@ -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); } diff --git a/tests/api/controllers/PostLinkTest.php b/tests/api/controllers/PostLinkTest.php index 3ed7bcb0..31954e39 100644 --- a/tests/api/controllers/PostLinkTest.php +++ b/tests/api/controllers/PostLinkTest.php @@ -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']); } /** diff --git a/tests/api/controllers/PutLinkTest.php b/tests/api/controllers/PutLinkTest.php index 4096c1a7..8a562571 100644 --- a/tests/api/controllers/PutLinkTest.php +++ b/tests/api/controllers/PutLinkTest.php @@ -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']); } /** -- cgit v1.2.3 From 6bc90f50af611a1674ee1118587feddd4f625d44 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 7 May 2017 16:56:20 +0200 Subject: History: fix entries order --- tests/utils/ReferenceHistory.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/utils/ReferenceHistory.php b/tests/utils/ReferenceHistory.php index 20284770..75cbb326 100644 --- a/tests/utils/ReferenceHistory.php +++ b/tests/utils/ReferenceHistory.php @@ -15,15 +15,14 @@ class ReferenceHistory public function __construct() { $this->addEntry( - History::CREATED, - DateTime::createFromFormat('Ymd_His', '20170101_121212'), - 123 + History::DELETED, + DateTime::createFromFormat('Ymd_His', '20170303_121216'), + 124 ); $this->addEntry( - History::CREATED, - DateTime::createFromFormat('Ymd_His', '20170201_121214'), - 124 + History::SETTINGS, + DateTime::createFromFormat('Ymd_His', '20170302_121215') ); $this->addEntry( @@ -33,14 +32,15 @@ class ReferenceHistory ); $this->addEntry( - History::SETTINGS, - DateTime::createFromFormat('Ymd_His', '20170302_121215') + History::CREATED, + DateTime::createFromFormat('Ymd_His', '20170201_121214'), + 124 ); $this->addEntry( - History::DELETED, - DateTime::createFromFormat('Ymd_His', '20170303_121216'), - 124 + History::CREATED, + DateTime::createFromFormat('Ymd_His', '20170101_121212'), + 123 ); } -- cgit v1.2.3