From 4bada691e957342342c7217cac4840789c19d402 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Tue, 19 Dec 2017 19:08:08 -0500 Subject: [PATCH] PDO tests and fixes; improves #72 --- RoboFile.php | 2 +- lib/Database.php | 8 +++---- .../TestDatabaseArticleSQLite3PDO.php | 16 +++++++++++++ .../TestDatabaseCleanupSQLite3PDO.php | 16 +++++++++++++ .../Database/TestDatabaseFeedSQLite3PDO.php | 16 +++++++++++++ .../Database/TestDatabaseFolderSQLite3PDO.php | 16 +++++++++++++ .../Database/TestDatabaseLabelSQLite3PDO.php | 12 ++++++++++ .../Database/TestDatabaseMetaSQLite3PDO.php | 16 +++++++++++++ .../TestDatabaseMiscellanySQLite3PDO.php | 16 +++++++++++++ .../TestDatabaseSessionSQLite3PDO.php | 12 ++++++++++ .../TestDatabaseSubscriptionSQLite3PDO.php | 16 +++++++++++++ .../Database/TestDatabaseUserSQLite3PDO.php | 16 +++++++++++++ tests/lib/AbstractTest.php | 2 ++ tests/lib/Database/DriverSQLite3PDO.php | 24 +++++++++++++++++++ tests/lib/Database/SeriesArticle.php | 4 ++-- tests/lib/Database/SeriesFeed.php | 4 ++-- tests/lib/Database/Setup.php | 16 +++++++++++-- tests/phpunit.xml | 12 +++++++++- 18 files changed, 212 insertions(+), 12 deletions(-) create mode 100644 tests/cases/Db/SQLite3PDO/Database/TestDatabaseArticleSQLite3PDO.php create mode 100644 tests/cases/Db/SQLite3PDO/Database/TestDatabaseCleanupSQLite3PDO.php create mode 100644 tests/cases/Db/SQLite3PDO/Database/TestDatabaseFeedSQLite3PDO.php create mode 100644 tests/cases/Db/SQLite3PDO/Database/TestDatabaseFolderSQLite3PDO.php create mode 100644 tests/cases/Db/SQLite3PDO/Database/TestDatabaseLabelSQLite3PDO.php create mode 100644 tests/cases/Db/SQLite3PDO/Database/TestDatabaseMetaSQLite3PDO.php create mode 100644 tests/cases/Db/SQLite3PDO/Database/TestDatabaseMiscellanySQLite3PDO.php create mode 100644 tests/cases/Db/SQLite3PDO/Database/TestDatabaseSessionSQLite3PDO.php create mode 100644 tests/cases/Db/SQLite3PDO/Database/TestDatabaseSubscriptionSQLite3PDO.php create mode 100644 tests/cases/Db/SQLite3PDO/Database/TestDatabaseUserSQLite3PDO.php create mode 100644 tests/lib/Database/DriverSQLite3PDO.php diff --git a/RoboFile.php b/RoboFile.php index 4653e4c8..02ae1c42 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -45,7 +45,7 @@ class RoboFile extends \Robo\Tasks { * See help for the "test" task for more details. */ public function testQuick(array $args): Result { - return $this->test(array_merge(["--exclude-group","slow"], $args)); + return $this->test(array_merge(["--exclude-group", "slow,optional"], $args)); } /** Produces a code coverage report diff --git a/lib/Database.php b/lib/Database.php index 0072cd86..6489e303 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -415,7 +415,7 @@ class Database { return $f; } - protected function folderValidateMove(string $user, int $id = null, $parent = null, string $name = null) { + protected function folderValidateMove(string $user, $id = null, $parent = null, string $name = null) { $errData = ["action" => $this->caller(), "field" => "parent", 'id' => $parent]; if (!$id) { // the root cannot be moved @@ -467,7 +467,7 @@ class Database { return $parent; } - protected function folderValidateName($name, bool $checkDuplicates = false, int $parent = null): bool { + protected function folderValidateName($name, bool $checkDuplicates = false, $parent = null): bool { $info = ValueInfo::str($name); if ($info & (ValueInfo::NULL | ValueInfo::EMPTY)) { throw new Db\ExceptionInput("missing", ["action" => $this->caller(), "field" => "name"]); @@ -572,7 +572,7 @@ class Database { // add a suitable WHERE condition $q->setWhere("folder in (select folder from folders)"); } - return $this->db->prepare($q->getQuery(), $q->getTypes())->run($q->getValues())->getValue(); + return (int) $this->db->prepare($q->getQuery(), $q->getTypes())->run($q->getValues())->getValue(); } public function subscriptionRemove(string $user, $id): bool { @@ -1102,7 +1102,7 @@ class Database { $q = $this->articleQuery($user, $context); $q->pushCTE("selected_articles"); $q->setBody("SELECT count(*) from selected_articles"); - return $this->db->prepare($q->getQuery(), $q->getTypes())->run($q->getValues())->getValue(); + return (int) $this->db->prepare($q->getQuery(), $q->getTypes())->run($q->getValues())->getValue(); } } diff --git a/tests/cases/Db/SQLite3PDO/Database/TestDatabaseArticleSQLite3PDO.php b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseArticleSQLite3PDO.php new file mode 100644 index 00000000..a904781e --- /dev/null +++ b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseArticleSQLite3PDO.php @@ -0,0 +1,16 @@ + + * @group optional */ +class TestDatabaseArticleSQLite3PDO extends Test\AbstractTest { + use Test\Database\Setup; + use Test\Database\DriverSQLite3PDO; + use Test\Database\SeriesArticle; +} diff --git a/tests/cases/Db/SQLite3PDO/Database/TestDatabaseCleanupSQLite3PDO.php b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseCleanupSQLite3PDO.php new file mode 100644 index 00000000..8eb5ef7c --- /dev/null +++ b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseCleanupSQLite3PDO.php @@ -0,0 +1,16 @@ + + * @group optional */ +class TestDatabaseCleanupSQLite3PDO extends Test\AbstractTest { + use Test\Database\Setup; + use Test\Database\DriverSQLite3PDO; + use Test\Database\SeriesCleanup; +} diff --git a/tests/cases/Db/SQLite3PDO/Database/TestDatabaseFeedSQLite3PDO.php b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseFeedSQLite3PDO.php new file mode 100644 index 00000000..07ed2ab1 --- /dev/null +++ b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseFeedSQLite3PDO.php @@ -0,0 +1,16 @@ + + * @group optional */ +class TestDatabaseFeedSQLite3PDO extends Test\AbstractTest { + use Test\Database\Setup; + use Test\Database\DriverSQLite3PDO; + use Test\Database\SeriesFeed; +} diff --git a/tests/cases/Db/SQLite3PDO/Database/TestDatabaseFolderSQLite3PDO.php b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseFolderSQLite3PDO.php new file mode 100644 index 00000000..d348de95 --- /dev/null +++ b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseFolderSQLite3PDO.php @@ -0,0 +1,16 @@ + + * @group optional */ +class TestDatabaseFolderSQLite3PDO extends Test\AbstractTest { + use Test\Database\Setup; + use Test\Database\DriverSQLite3PDO; + use Test\Database\SeriesFolder; +} diff --git a/tests/cases/Db/SQLite3PDO/Database/TestDatabaseLabelSQLite3PDO.php b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseLabelSQLite3PDO.php new file mode 100644 index 00000000..3d367af3 --- /dev/null +++ b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseLabelSQLite3PDO.php @@ -0,0 +1,12 @@ + + * @group optional */ +class TestDatabaseLabelSQLite3PDO extends Test\AbstractTest { + use Test\Database\Setup; + use Test\Database\DriverSQLite3PDO; + use Test\Database\SeriesLabel; +} diff --git a/tests/cases/Db/SQLite3PDO/Database/TestDatabaseMetaSQLite3PDO.php b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseMetaSQLite3PDO.php new file mode 100644 index 00000000..8f8ad5eb --- /dev/null +++ b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseMetaSQLite3PDO.php @@ -0,0 +1,16 @@ + + * @group optional */ +class TestDatabaseMetaSQLite3PDO extends Test\AbstractTest { + use Test\Database\Setup; + use Test\Database\DriverSQLite3PDO; + use Test\Database\SeriesMeta; +} diff --git a/tests/cases/Db/SQLite3PDO/Database/TestDatabaseMiscellanySQLite3PDO.php b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseMiscellanySQLite3PDO.php new file mode 100644 index 00000000..2e1d01e6 --- /dev/null +++ b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseMiscellanySQLite3PDO.php @@ -0,0 +1,16 @@ + + * @group optional */ +class TestDatabaseMiscellanySQLite3PDO extends Test\AbstractTest { + use Test\Database\Setup; + use Test\Database\DriverSQLite3PDO; + use Test\Database\SeriesMiscellany; +} diff --git a/tests/cases/Db/SQLite3PDO/Database/TestDatabaseSessionSQLite3PDO.php b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseSessionSQLite3PDO.php new file mode 100644 index 00000000..bd0a857d --- /dev/null +++ b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseSessionSQLite3PDO.php @@ -0,0 +1,12 @@ + + * @group optional */ +class TestDatabaseSessionSQLite3PDO extends Test\AbstractTest { + use Test\Database\Setup; + use Test\Database\DriverSQLite3PDO; + use Test\Database\SeriesSession; +} diff --git a/tests/cases/Db/SQLite3PDO/Database/TestDatabaseSubscriptionSQLite3PDO.php b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseSubscriptionSQLite3PDO.php new file mode 100644 index 00000000..99ec86c5 --- /dev/null +++ b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseSubscriptionSQLite3PDO.php @@ -0,0 +1,16 @@ + + * @group optional */ +class TestDatabaseSubscriptionSQLite3PDO extends Test\AbstractTest { + use Test\Database\Setup; + use Test\Database\DriverSQLite3PDO; + use Test\Database\SeriesSubscription; +} diff --git a/tests/cases/Db/SQLite3PDO/Database/TestDatabaseUserSQLite3PDO.php b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseUserSQLite3PDO.php new file mode 100644 index 00000000..ef5ec44c --- /dev/null +++ b/tests/cases/Db/SQLite3PDO/Database/TestDatabaseUserSQLite3PDO.php @@ -0,0 +1,16 @@ + + * @group optional */ +class TestDatabaseUserSQLite3PDO extends Test\AbstractTest { + use Test\Database\Setup; + use Test\Database\DriverSQLite3PDO; + use Test\Database\SeriesUser; +} diff --git a/tests/lib/AbstractTest.php b/tests/lib/AbstractTest.php index 85bb0eb9..dd63b4df 100644 --- a/tests/lib/AbstractTest.php +++ b/tests/lib/AbstractTest.php @@ -32,6 +32,8 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase { public function approximateTime($exp, $act) { if (is_null($act)) { return null; + } elseif (is_null($exp)) { + return $act; } $target = Date::normalize($exp)->getTimeStamp(); $value = Date::normalize($act)->getTimeStamp(); diff --git a/tests/lib/Database/DriverSQLite3PDO.php b/tests/lib/Database/DriverSQLite3PDO.php new file mode 100644 index 00000000..9c52bd87 --- /dev/null +++ b/tests/lib/Database/DriverSQLite3PDO.php @@ -0,0 +1,24 @@ +markTestSkipped("PDO-SQLite extension not loaded"); + } + Arsse::$conf->dbSQLite3File = ":memory:"; + $this->drv = new PDODriver(); + } + + public function nextID(string $table): int { + return (int) $this->drv->query("SELECT (case when max(id) then max(id) else 0 end)+1 from $table")->getValue(); + } +} diff --git a/tests/lib/Database/SeriesArticle.php b/tests/lib/Database/SeriesArticle.php index 82267115..7ffae2d1 100644 --- a/tests/lib/Database/SeriesArticle.php +++ b/tests/lib/Database/SeriesArticle.php @@ -888,8 +888,8 @@ trait SeriesArticle { public function testFetchStarredCounts() { $exp1 = ['total' => 2, 'unread' => 1, 'read' => 1]; $exp2 = ['total' => 0, 'unread' => 0, 'read' => 0]; - $this->assertSame($exp1, Arsse::$db->articleStarred("john.doe@example.com")); - $this->assertSame($exp2, Arsse::$db->articleStarred("jane.doe@example.com")); + $this->assertEquals($exp1, Arsse::$db->articleStarred("john.doe@example.com")); + $this->assertEquals($exp2, Arsse::$db->articleStarred("jane.doe@example.com")); } public function testFetchStarredCountsWithoutAuthority() { diff --git a/tests/lib/Database/SeriesFeed.php b/tests/lib/Database/SeriesFeed.php index e605bd49..fcfaf6b6 100644 --- a/tests/lib/Database/SeriesFeed.php +++ b/tests/lib/Database/SeriesFeed.php @@ -256,9 +256,9 @@ trait SeriesFeed { } public function testListStaleFeeds() { - $this->assertSame([1,3,4], Arsse::$db->feedListStale()); + $this->assertEquals([1,3,4], Arsse::$db->feedListStale()); Arsse::$db->feedUpdate(3); Arsse::$db->feedUpdate(4); - $this->assertSame([1], Arsse::$db->feedListStale()); + $this->assertEquals([1], Arsse::$db->feedListStale()); } } diff --git a/tests/lib/Database/Setup.php b/tests/lib/Database/Setup.php index 0adb9d3a..41428187 100644 --- a/tests/lib/Database/Setup.php +++ b/tests/lib/Database/Setup.php @@ -10,6 +10,7 @@ use JKingWeb\Arsse\User\Driver as UserDriver; use JKingWeb\Arsse\Arsse; use JKingWeb\Arsse\Conf; use JKingWeb\Arsse\User; +use JKingWeb\Arsse\Misc\ValueInfo; use JKingWeb\Arsse\Test\Database; use JKingWeb\Arsse\Db\Result; use Phake; @@ -90,8 +91,19 @@ trait Setup { $row = array_combine($cols, $row); foreach($data as $index => $test) { foreach ($test as $col => $value) { - if ($types[$col]=="datetime") { - $test[$col] = $this->approximateTime($row[$col], $value); + switch ($types[$col]) { + case "datetime": + $test[$col] = $this->approximateTime($row[$col], $value); + break; + case "int": + $test[$col] = ValueInfo::normalize($value, ValueInfo::T_INT | ValueInfo::M_DROP | valueInfo::M_NULL); + break; + case "float": + $test[$col] = ValueInfo::normalize($value, ValueInfo::T_FLOAT | ValueInfo::M_DROP | valueInfo::M_NULL); + break; + case "bool": + $test[$col] = (int) ValueInfo::normalize($value, ValueInfo::T_BOOL | ValueInfo::M_DROP | valueInfo::M_NULL); + break; } } if($row===$test) { diff --git a/tests/phpunit.xml b/tests/phpunit.xml index c15db129..37e5bbef 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -58,7 +58,7 @@ cases/Db/SQLite3PDO/TestDbDriverSQLite3PDO.php cases/Db/SQLite3PDO/TestDbUpdateSQLite3PDO.php - + cases/Db/SQLite3/Database/TestDatabaseMiscellanySQLite3.php cases/Db/SQLite3/Database/TestDatabaseMetaSQLite3.php cases/Db/SQLite3/Database/TestDatabaseUserSQLite3.php @@ -69,6 +69,16 @@ cases/Db/SQLite3/Database/TestDatabaseArticleSQLite3.php cases/Db/SQLite3/Database/TestDatabaseLabelSQLite3.php cases/Db/SQLite3/Database/TestDatabaseCleanupSQLite3.php + + cases/Db/SQLite3PDO/Database/TestDatabaseMiscellanySQLite3PDO.php + cases/Db/SQLite3PDO/Database/TestDatabaseUserSQLite3PDO.php + cases/Db/SQLite3PDO/Database/TestDatabaseSessionSQLite3PDO.php + cases/Db/SQLite3PDO/Database/TestDatabaseFolderSQLite3PDO.php + cases/Db/SQLite3PDO/Database/TestDatabaseFeedSQLite3PDO.php + cases/Db/SQLite3PDO/Database/TestDatabaseSubscriptionSQLite3PDO.php + cases/Db/SQLite3PDO/Database/TestDatabaseArticleSQLite3PDO.php + cases/Db/SQLite3PDO/Database/TestDatabaseLabelSQLite3PDO.php + cases/Db/SQLite3PDO/Database/TestDatabaseCleanupSQLite3PDO.php