-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialization of Db from INIT-currentschema script (Fixes #395)
- Loading branch information
1 parent
16c6832
commit d0121fc
Showing
7 changed files
with
296 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,17 @@ final class DatabaseAccounts | |
"sqlite3" => [ | ||
"sqlite:///" . __DIR__ . "/../../testreports/test.db?mode=0640", | ||
"sqlite:///" . __DIR__ . "/../../testreports/migtest.db?mode=0640", | ||
"sqlite:///" . __DIR__ . "/../../testreports/inittest.db?mode=0640", | ||
], | ||
"postgres" => [ | ||
"pgsql://postgres:[email protected]/rcmcarddavtest", | ||
"pgsql://postgres:[email protected]/rcmcarddavmigtest", | ||
"pgsql://postgres:[email protected]/rcmcarddavinittest", | ||
], | ||
"mysql" => [ | ||
"mysql://root:[email protected]/rcmcarddavtest", | ||
"mysql://root:[email protected]/rcmcarddavmigtest", | ||
"mysql://root:[email protected]/rcmcarddavinittest", | ||
], | ||
]; | ||
} | ||
|
110 changes: 110 additions & 0 deletions
110
tests/DBInteroperability/DatabaseInitializationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
/* | ||
* RCMCardDAV - CardDAV plugin for Roundcube webmail | ||
* | ||
* Copyright (C) 2011-2022 Benjamin Schieder <[email protected]>, | ||
* Michael Stilkerich <[email protected]> | ||
* | ||
* This file is part of RCMCardDAV. | ||
* | ||
* RCMCardDAV is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* RCMCardDAV is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with RCMCardDAV. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MStilkerich\Tests\RCMCardDAV\DBInteroperability; | ||
|
||
use MStilkerich\Tests\RCMCardDAV\TestInfrastructure; | ||
use PHPUnit\Framework\TestCase; | ||
use MStilkerich\RCMCardDAV\Db\AbstractDatabase; | ||
|
||
final class DatabaseInitializationTest extends TestCase | ||
{ | ||
private const SCRIPTDIR = __DIR__ . "/../../dbmigrations"; | ||
|
||
/** @var AbstractDatabase Database used for the schema initialization test */ | ||
private static $db; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
self::resetRcubeDb(); | ||
} | ||
|
||
public function setUp(): void | ||
{ | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
TestInfrastructure::logger()->reset(); | ||
|
||
// We need to create a new rcube_db instance after each test (technically, each test that creates or drops | ||
// tables) so that the rcube_db::list_tables() cache is cleared. This is particularly important after the first | ||
// migration which creates the carddav tables. | ||
self::resetRcubeDb(); | ||
} | ||
|
||
private static function resetRcubeDb(): void | ||
{ | ||
$dbsettings = TestInfrastructureDB::dbSettings(); | ||
$initdb_dsnw = $dbsettings[2]; | ||
self::$db = TestInfrastructureDB::initDatabase($initdb_dsnw); | ||
TestInfrastructure::init(self::$db); | ||
} | ||
|
||
/** | ||
* Tests the schema initialization. | ||
* | ||
* This test is invoked once on an empty database (init database, only used for this test). The makefiles dump the | ||
* schema and compare it to a schema created from script-based execution of the init script. | ||
*/ | ||
public function testSchemaInitializationWorks(): void | ||
{ | ||
$db = self::$db; | ||
|
||
// check preconditions | ||
$this->assertDirectoryExists(self::SCRIPTDIR . "/INIT-currentschema"); | ||
|
||
// Perform the initialization | ||
$db->checkMigrations("", self::SCRIPTDIR); | ||
|
||
// After the initialization, the migrations table must contain all the available migrations | ||
$migsavail = array_map( | ||
function (string $s): string { | ||
return basename($s); | ||
}, | ||
glob(self::SCRIPTDIR . "/0???-*") | ||
); | ||
$migsDone = $this->getDoneMigrations(); | ||
$this->assertSame($migsavail, $migsDone); | ||
} | ||
|
||
/** | ||
* Gets a sorted list of migrations that have been recorded as done in the carddav_migrations table. | ||
* | ||
* @return list<string> | ||
*/ | ||
private function getDoneMigrations(): array | ||
{ | ||
$db = self::$db; | ||
/** @var list<array{filename: string}> $rows */ | ||
$rows = $db->get([], ['filename'], 'migrations'); | ||
$migsDone = array_column($rows, 'filename'); | ||
sort($migsDone); | ||
return $migsDone; | ||
} | ||
} | ||
|
||
// vim: ts=4:sw=4:expandtab:fenc=utf8:ff=unix:tw=120 |
Oops, something went wrong.