From 30d6f6db377a4c54ede76b7595ce5deea5974f61 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Thu, 8 Nov 2018 14:50:58 -0500 Subject: [PATCH] Consolidate Db result test series into single file --- tests/cases/Db/SQLite3/TestResult.php | 108 ------------------ tests/cases/Db/SQLite3PDO/TestResult.php | 108 ------------------ tests/cases/Db/TestResult.php | 134 +++++++++++++++++++++++ tests/lib/AbstractTest.php | 14 +++ tests/phpunit.xml | 4 +- 5 files changed, 150 insertions(+), 218 deletions(-) delete mode 100644 tests/cases/Db/SQLite3/TestResult.php delete mode 100644 tests/cases/Db/SQLite3PDO/TestResult.php create mode 100644 tests/cases/Db/TestResult.php diff --git a/tests/cases/Db/SQLite3/TestResult.php b/tests/cases/Db/SQLite3/TestResult.php deleted file mode 100644 index 7c302a6..0000000 --- a/tests/cases/Db/SQLite3/TestResult.php +++ /dev/null @@ -1,108 +0,0 @@ - */ -class TestResult extends \JKingWeb\Arsse\Test\AbstractTest { - protected $c; - - public function setUp() { - if (!\JKingWeb\Arsse\Db\SQLite3\Driver::requirementsMet()) { - $this->markTestSkipped("SQLite extension not loaded"); - } - $this->clearData(); - $c = new \SQLite3(":memory:"); - $c->enableExceptions(true); - $this->c = $c; - } - - public function tearDown() { - $this->c->close(); - unset($this->c); - $this->clearData(); - } - - public function testConstructResult() { - $set = $this->c->query("SELECT 1"); - $this->assertInstanceOf(Result::class, new Result($set)); - } - - public function testGetChangeCountAndLastInsertId() { - $this->c->query("CREATE TABLE test(col)"); - $set = $this->c->query("INSERT INTO test(col) values(1)"); - $rows = $this->c->changes(); - $id = $this->c->lastInsertRowID(); - $r = new Result($set, [$rows,$id]); - $this->assertEquals($rows, $r->changes()); - $this->assertEquals($id, $r->lastId()); - } - - public function testIterateOverResults() { - $set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col"); - $rows = []; - foreach (new Result($set) as $index => $row) { - $rows[$index] = $row['col']; - } - $this->assertEquals([0 => 1, 1 => 2, 2 => 3], $rows); - } - - public function testIterateOverResultsTwice() { - $set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col"); - $rows = []; - $test = new Result($set); - foreach ($test as $row) { - $rows[] = $row['col']; - } - $this->assertEquals([1,2,3], $rows); - $this->assertException("resultReused", "Db"); - foreach ($test as $row) { - $rows[] = $row['col']; - } - } - - public function testGetSingleValues() { - $set = $this->c->query("SELECT 1867 as year union select 1970 as year union select 2112 as year"); - $test = new Result($set); - $this->assertEquals(1867, $test->getValue()); - $this->assertEquals(1970, $test->getValue()); - $this->assertEquals(2112, $test->getValue()); - $this->assertSame(null, $test->getValue()); - } - - public function testGetFirstValuesOnly() { - $set = $this->c->query("SELECT 1867 as year, 19 as century union select 1970 as year, 20 as century union select 2112 as year, 22 as century"); - $test = new Result($set); - $this->assertEquals(1867, $test->getValue()); - $this->assertEquals(1970, $test->getValue()); - $this->assertEquals(2112, $test->getValue()); - $this->assertSame(null, $test->getValue()); - } - - public function testGetRows() { - $set = $this->c->query("SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track"); - $rows = [ - ['album' => '2112', 'track' => '2112'], - ['album' => 'Clockwork Angels', 'track' => 'The Wreckers'], - ]; - $test = new Result($set); - $this->assertEquals($rows[0], $test->getRow()); - $this->assertEquals($rows[1], $test->getRow()); - $this->assertSame(null, $test->getRow()); - } - - public function testGetAllRows() { - $set = $this->c->query("SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track"); - $rows = [ - ['album' => '2112', 'track' => '2112'], - ['album' => 'Clockwork Angels', 'track' => 'The Wreckers'], - ]; - $test = new Result($set); - $this->assertEquals($rows, $test->getAll()); - } -} diff --git a/tests/cases/Db/SQLite3PDO/TestResult.php b/tests/cases/Db/SQLite3PDO/TestResult.php deleted file mode 100644 index 7537b87..0000000 --- a/tests/cases/Db/SQLite3PDO/TestResult.php +++ /dev/null @@ -1,108 +0,0 @@ - */ -class TestResult extends \JKingWeb\Arsse\Test\AbstractTest { - protected $c; - - public function setUp() { - if (!PDODriver::requirementsMet()) { - $this->markTestSkipped("PDO-SQLite extension not loaded"); - } - $this->clearData(); - $c = new \PDO("sqlite::memory:", "", "", [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]); - $this->c = $c; - } - - public function tearDown() { - unset($this->c); - $this->clearData(); - } - - public function testConstructResult() { - $set = $this->c->query("SELECT 1"); - $this->assertInstanceOf(Result::class, new PDOResult($set)); - } - - public function testGetChangeCountAndLastInsertId() { - $this->c->query("CREATE TABLE test(col)"); - $set = $this->c->query("INSERT INTO test(col) values(1)"); - $rows = $set->rowCount(); - $id = $this->c->lastInsertID(); - $r = new PDOResult($set, [$rows,$id]); - $this->assertSame((int) $rows, $r->changes()); - $this->assertSame((int) $id, $r->lastId()); - } - - public function testIterateOverResults() { - $set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col"); - $rows = []; - foreach (new PDOResult($set) as $index => $row) { - $rows[$index] = $row['col']; - } - $this->assertSame([0 => "1", 1 => "2", 2 => "3"], $rows); - } - - public function testIterateOverResultsTwice() { - $set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col"); - $rows = []; - $test = new PDOResult($set); - foreach ($test as $row) { - $rows[] = $row['col']; - } - $this->assertSame(["1","2","3"], $rows); - $this->assertException("resultReused", "Db"); - foreach ($test as $row) { - $rows[] = $row['col']; - } - } - - public function testGetSingleValues() { - $set = $this->c->query("SELECT 1867 as year union select 1970 as year union select 2112 as year"); - $test = new PDOResult($set); - $this->assertEquals(1867, $test->getValue()); - $this->assertEquals(1970, $test->getValue()); - $this->assertEquals(2112, $test->getValue()); - $this->assertSame(null, $test->getValue()); - } - - public function testGetFirstValuesOnly() { - $set = $this->c->query("SELECT 1867 as year, 19 as century union select 1970 as year, 20 as century union select 2112 as year, 22 as century"); - $test = new PDOResult($set); - $this->assertEquals(1867, $test->getValue()); - $this->assertEquals(1970, $test->getValue()); - $this->assertEquals(2112, $test->getValue()); - $this->assertSame(null, $test->getValue()); - } - - public function testGetRows() { - $set = $this->c->query("SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track"); - $rows = [ - ['album' => '2112', 'track' => '2112'], - ['album' => 'Clockwork Angels', 'track' => 'The Wreckers'], - ]; - $test = new PDOResult($set); - $this->assertEquals($rows[0], $test->getRow()); - $this->assertEquals($rows[1], $test->getRow()); - $this->assertSame(null, $test->getRow()); - } - - public function testGetAllRows() { - $set = $this->c->query("SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track"); - $rows = [ - ['album' => '2112', 'track' => '2112'], - ['album' => 'Clockwork Angels', 'track' => 'The Wreckers'], - ]; - $test = new PDOResult($set); - $this->assertEquals($rows, $test->getAll()); - } -} diff --git a/tests/cases/Db/TestResult.php b/tests/cases/Db/TestResult.php new file mode 100644 index 0000000..7a024a9 --- /dev/null +++ b/tests/cases/Db/TestResult.php @@ -0,0 +1,134 @@ + + * @covers \JKingWeb\Arsse\Db\SQLite3\Result + */ +class TestResult extends \JKingWeb\Arsse\Test\AbstractTest { + public function provideDrivers() { + $drvSqlite3 = (function() { + if (\JKingWeb\Arsse\Db\SQLite3\Driver::requirementsMet()) { + $d = new \SQLite3(":memory:"); + $d->enableExceptions(true); + return $d; + } + })(); + $drvPdo = (function() { + if (\JKingWeb\Arsse\Db\SQLite3\PDODriver::requirementsMet()) { + return new \PDO("sqlite::memory:", "", "", [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]); + } + })(); + return [ + 'SQLite 3' => [isset($drvSqlite3), false, \JKingWeb\Arsse\Db\SQLite3\Result::class, function(string $query) use($drvSqlite3) { + $set = $drvSqlite3->query($query); + $rows = $drvSqlite3->changes(); + $id = $drvSqlite3->lastInsertRowID(); + return [$set, [$rows, $id]]; + }], + 'PDO' => [isset($drvPdo), true, \JKingWeb\Arsse\Db\PDOResult::class, function(string $query) use($drvPdo) { + $set = $drvPdo->query($query); + $rows = $set->rowCount(); + $id = $drvPdo->lastInsertID(); + return [$set, [$rows, $id]]; + }], + ]; + } + + /** @dataProvider provideDrivers */ + public function testConstructResult($driver, bool $stringCoersion, string $class, \Closure $func) { + if (is_null($driver)) { + $this->markTestSkipped(); + } + $this->assertInstanceOf(Result::class, new $class(...$func("SELECT 1"))); + } + + /** @dataProvider provideDrivers */ + public function testGetChangeCountAndLastInsertId($driver, bool $stringCoersion, string $class, \Closure $func) { + $func("CREATE TABLE if not exists arsse_meta(key varchar(255) primary key not null, value text)"); + $out = $func("INSERT INTO arsse_meta(key,value) values('test', 1)"); + $rows = $out[1][0]; + $id = $out[1][1]; + $r = new $class(...$out); + $this->assertSame((int) $rows, $r->changes()); + $this->assertSame((int) $id, $r->lastId()); + } + + /** @dataProvider provideDrivers */ + public function testIterateOverResults($driver, bool $stringCoersion, string $class, \Closure $func) { + $exp = [0 => 1, 1 => 2, 2 => 3]; + $exp = $stringCoersion ? $this->stringify($exp) : $exp; + foreach (new $class(...$func("SELECT 1 as col union select 2 as col union select 3 as col")) as $index => $row) { + $rows[$index] = $row['col']; + } + $this->assertSame($exp, $rows); + } + + /** @dataProvider provideDrivers */ + public function testIterateOverResultsTwice($driver, bool $stringCoersion, string $class, \Closure $func) { + $exp = [0 => 1, 1 => 2, 2 => 3]; + $exp = $stringCoersion ? $this->stringify($exp) : $exp; + $result = new $class(...$func("SELECT 1 as col union select 2 as col union select 3 as col")); + foreach ($result as $index => $row) { + $rows[$index] = $row['col']; + } + $this->assertSame($exp, $rows); + $this->assertException("resultReused", "Db"); + foreach ($result as $row) { + $rows[] = $row['col']; + } + } + + /** @dataProvider provideDrivers */ + public function testGetSingleValues($driver, bool $stringCoersion, string $class, \Closure $func) { + $exp = [1867, 1970, 2112]; + $exp = $stringCoersion ? $this->stringify($exp) : $exp; + $test = new $class(...$func("SELECT 1867 as year union select 1970 as year union select 2112 as year")); + $this->assertSame($exp[0], $test->getValue()); + $this->assertSame($exp[1], $test->getValue()); + $this->assertSame($exp[2], $test->getValue()); + $this->assertSame(null, $test->getValue()); + } + + /** @dataProvider provideDrivers */ + public function testGetFirstValuesOnly($driver, bool $stringCoersion, string $class, \Closure $func) { + $exp = [1867, 1970, 2112]; + $exp = $stringCoersion ? $this->stringify($exp) : $exp; + $test = new $class(...$func("SELECT 1867 as year, 19 as century union select 1970 as year, 20 as century union select 2112 as year, 22 as century")); + $this->assertSame($exp[0], $test->getValue()); + $this->assertSame($exp[1], $test->getValue()); + $this->assertSame($exp[2], $test->getValue()); + $this->assertSame(null, $test->getValue()); + } + + /** @dataProvider provideDrivers */ + public function testGetRows($driver, bool $stringCoersion, string $class, \Closure $func) { + $exp = [ + ['album' => '2112', 'track' => '2112'], + ['album' => 'Clockwork Angels', 'track' => 'The Wreckers'], + ]; + $test = new $class(...$func("SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track")); + $this->assertSame($exp[0], $test->getRow()); + $this->assertSame($exp[1], $test->getRow()); + $this->assertSame(null, $test->getRow()); + } + + /** @dataProvider provideDrivers */ + public function testGetAllRows($driver, bool $stringCoersion, string $class, \Closure $func) { + $exp = [ + ['album' => '2112', 'track' => '2112'], + ['album' => 'Clockwork Angels', 'track' => 'The Wreckers'], + ]; + $test = new $class(...$func("SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track")); + $this->assertEquals($exp, $test->getAll()); + } +} diff --git a/tests/lib/AbstractTest.php b/tests/lib/AbstractTest.php index effe34e..a75c339 100644 --- a/tests/lib/AbstractTest.php +++ b/tests/lib/AbstractTest.php @@ -105,4 +105,18 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase { return $act; } } + + public function stringify($value) { + if (!is_array($value)) { + return $value; + } + foreach ($value as $k => $v) { + if (is_array($v)) { + $value[$k] = $this->v($v); + } elseif (is_int($v) || is_float($v)) { + $value[$k] = (string) $v; + } + } + return $value; + } } diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 59b7833..fb2e5ab 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -45,13 +45,13 @@ cases/Db/TestTransaction.php cases/Db/TestResultAggregate.php cases/Db/TestResultEmpty.php - cases/Db/SQLite3/TestResult.php + cases/Db/TestResult.php + cases/Db/SQLite3/TestStatement.php cases/Db/SQLite3/TestCreation.php cases/Db/SQLite3/TestDriver.php cases/Db/SQLite3/TestUpdate.php - cases/Db/SQLite3PDO/TestResult.php cases/Db/SQLite3PDO/TestStatement.php cases/Db/SQLite3PDO/TestCreation.php cases/Db/SQLite3PDO/TestDriver.php