X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2FLinkDBTest.php;h=504c81901a8fc402278e66eb50689d51e5fe0b92;hb=96db105e4c0833324f7168edb5673278de8ccd54;hp=f67d4d9bdde93d80a582d80644a2e6e493c0876d;hpb=eaefcba724e93c5f6b426ad8855ab1af8ac8212a;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index f67d4d9b..504c8190 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -7,9 +7,6 @@ require_once 'application/LinkDB.php'; require_once 'application/Utils.php'; require_once 'tests/utils/ReferenceLinkDB.php'; -define('PHPPREFIX', ''); - /** * Unitary tests for LinkDB @@ -18,7 +15,6 @@ class LinkDBTest extends PHPUnit_Framework_TestCase { // datastore to test write operations protected static $testDatastore = 'tests/datastore.php'; - protected static $dummyDatastoreSHA1 = 'e3edea8ea7bb50be4bcb404df53fbb4546a7156e'; protected static $refDB = null; protected static $publicLinkDB = null; protected static $privateLinkDB = null; @@ -38,11 +34,10 @@ class LinkDBTest extends PHPUnit_Framework_TestCase public static function setUpBeforeClass() { self::$refDB = new ReferenceLinkDB(); - self::$refDB->write(self::$testDatastore, PHPPREFIX, PHPSUFFIX); + self::$refDB->write(self::$testDatastore); - $GLOBALS['config']['DATASTORE'] = self::$testDatastore; - self::$publicLinkDB = new LinkDB(false, false); - self::$privateLinkDB = new LinkDB(true, false); + self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false); + self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false); } /** @@ -50,7 +45,6 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ protected function setUp() { - $GLOBALS['config']['DATASTORE'] = self::$testDatastore; if (file_exists(self::$testDatastore)) { unlink(self::$testDatastore); } @@ -76,7 +70,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ public function testConstructLoggedIn() { - new LinkDB(true, false); + new LinkDB(self::$testDatastore, true, false); $this->assertFileExists(self::$testDatastore); } @@ -85,7 +79,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ public function testConstructLoggedOut() { - new LinkDB(false, false); + new LinkDB(self::$testDatastore, false, false); $this->assertFileExists(self::$testDatastore); } @@ -97,8 +91,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ public function testConstructDatastoreNotWriteable() { - $GLOBALS['config']['DATASTORE'] = 'null/store.db'; - new LinkDB(false, false); + new LinkDB('null/store.db', false, false); } /** @@ -106,19 +99,16 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ public function testCheckDBNew() { - $linkDB = new LinkDB(false, false); + $linkDB = new LinkDB(self::$testDatastore, false, false); unlink(self::$testDatastore); $this->assertFileNotExists(self::$testDatastore); - $checkDB = self::getMethod('checkDB'); + $checkDB = self::getMethod('_checkDB'); $checkDB->invokeArgs($linkDB, array()); $this->assertFileExists(self::$testDatastore); // ensure the correct data has been written - $this->assertEquals( - self::$dummyDatastoreSHA1, - sha1_file(self::$testDatastore) - ); + $this->assertGreaterThan(0, filesize(self::$testDatastore)); } /** @@ -126,19 +116,17 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ public function testCheckDBLoad() { - $linkDB = new LinkDB(false, false); - $this->assertEquals( - self::$dummyDatastoreSHA1, - sha1_file(self::$testDatastore) - ); + $linkDB = new LinkDB(self::$testDatastore, false, false); + $datastoreSize = filesize(self::$testDatastore); + $this->assertGreaterThan(0, $datastoreSize); - $checkDB = self::getMethod('checkDB'); + $checkDB = self::getMethod('_checkDB'); $checkDB->invokeArgs($linkDB, array()); // ensure the datastore is left unmodified $this->assertEquals( - self::$dummyDatastoreSHA1, - sha1_file(self::$testDatastore) + $datastoreSize, + filesize(self::$testDatastore) ); } @@ -147,8 +135,8 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ public function testReadEmptyDB() { - file_put_contents(self::$testDatastore, PHPPREFIX.'S7QysKquBQA='.PHPSUFFIX); - $emptyDB = new LinkDB(false, false); + file_put_contents(self::$testDatastore, ''); + $emptyDB = new LinkDB(self::$testDatastore, false, false); $this->assertEquals(0, sizeof($emptyDB)); $this->assertEquals(0, count($emptyDB)); } @@ -180,7 +168,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ public function testSaveDB() { - $testDB = new LinkDB(true, false); + $testDB = new LinkDB(self::$testDatastore, true, false); $dbSize = sizeof($testDB); $link = array( @@ -198,7 +186,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase $testDB->savedb(); - $testDB = new LinkDB(true, false); + $testDB = new LinkDB(self::$testDatastore, true, false); $this->assertEquals($dbSize + 1, sizeof($testDB)); } @@ -222,7 +210,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ public function testCountHiddenPublic() { - $linkDB = new LinkDB(false, true); + $linkDB = new LinkDB(self::$testDatastore, false, true); $this->assertEquals( 0, @@ -240,12 +228,12 @@ class LinkDBTest extends PHPUnit_Framework_TestCase public function testDays() { $this->assertEquals( - ['20121206', '20130614', '20150310'], + array('20121206', '20130614', '20150310'), self::$publicLinkDB->days() ); $this->assertEquals( - ['20121206', '20130614', '20141125', '20150310'], + array('20121206', '20130614', '20141125', '20150310'), self::$privateLinkDB->days() ); } @@ -281,7 +269,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase public function testAllTags() { $this->assertEquals( - [ + array( 'web' => 3, 'cartoon' => 2, 'gnu' => 2, @@ -291,12 +279,12 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'software' => 1, 'stallman' => 1, 'free' => 1 - ], + ), self::$publicLinkDB->allTags() ); $this->assertEquals( - [ + array( 'web' => 4, 'cartoon' => 3, 'gnu' => 2, @@ -310,7 +298,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'w3c' => 1, 'css' => 1, 'Mercurial' => 1 - ], + ), self::$privateLinkDB->allTags() ); } @@ -408,19 +396,22 @@ class LinkDBTest extends PHPUnit_Framework_TestCase /** * Use an invalid date format + * @expectedException Exception + * @expectedExceptionMessageRegExp /Invalid date format/ */ - public function testFilterInvalidDay() + public function testFilterInvalidDayWithChars() { - $this->assertEquals( - 0, - sizeof(self::$privateLinkDB->filterDay('Rainy day, dream away')) - ); + self::$privateLinkDB->filterDay('Rainy day, dream away'); + } - // TODO: check input format - $this->assertEquals( - 6, - sizeof(self::$privateLinkDB->filterDay('20')) - ); + /** + * Use an invalid date format + * @expectedException Exception + * @expectedExceptionMessageRegExp /Invalid date format/ + */ + public function testFilterInvalidDayDigits() + { + self::$privateLinkDB->filterDay('20'); } /**