1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Helper methods for testing Database class

May not actually work yet
This commit is contained in:
J. King 2017-03-27 23:19:05 -04:00
parent b8f9b6070e
commit 9d5dab249c
3 changed files with 44 additions and 0 deletions

10
tests/Db/TestDatabase.php Normal file
View file

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync;
class TestDatabase extends \PHPUnit\Framework\TestCase {
use Test\Tools, Test\Db\Tools;
}

30
tests/lib/Db/Tools.php Normal file
View file

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync\Test\Db;
trait Tools {
function prime(\JKingWeb\NewsSync\Db\Driver $drv, array $data): bool {
$drv->begin();
foreach($data as $table => $info) {
$cols = implode(",", array_keys($info['columns']));
$bindings = array_values($info['columns']);
$params = implode(",", array_fill(0, sizeof($info['columns']), "?"));
$s = $drv->prepareArray("INSERT INTO $table($cols) values($params)", $bindings);
foreach($info['rows'] as $row) {
$this->assertEquals(1, $s->runArray($row)->changes());
}
}
$drv->commit();
return true;
}
function compare(\JKingWeb\NewsSync\Db\Driver $drv, array $expected): bool {
foreach($expected as $table => $info) {
$cols = implode(",", array_keys($info['columns']));
foreach($drv->prepare("SELECT $cols from $table")->run() as $num => $row) {
$row = array_values($row);
$assertSame($expected[$table]['rows']['$num'], $row, "Row $num of table $table does not match expectation.");
}
}
}
}

View file

@ -35,6 +35,10 @@
<file>Db/SQLite3/TestDbUpdateSQLite3.php</file>
</testsuite>
<testsuite name="Database functions">
<file>Db/TestDatabase.php</file>
</testsuite>
<testsuite name="NextCloud News API">
<file>REST/NextCloudNews/TestNCNVersionDiscovery.php</file>
</testsuite>