D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
usr
/
local
/
psa
/
admin
/
plib
/
modules
/
git
/
vendor
/
plesk
/
db-upgrader
/
tests
/
php
/
Db
/
Filename :
MigrationTest.php
back
Copy
<?php // Copyright 1999-2022. Parallels IP Holdings GmbH. All Rights Reserved. use PleskExt\Upgrader\Migration; use PleskExt\Upgrader\Versions; use PleskExt\Upgrader\Exception; class Db_MigrationTest extends PHPUnit\Framework\TestCase { const START_DB_VERSION = '201512310101'; private $_dbAdapter = null; public function setUp(): void { $this->_dbAdapter = Zend_Db::factory('Pdo_Sqlite', array('dbname'=>':memory:')); $this->_dbAdapter->query(<<<QUERY CREATE TABLE IF NOT EXISTS `version` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `dbVersion` TEXT NOT NULL ) QUERY ); $this->_dbAdapter->query("INSERT INTO `version` (`dbVersion`) VALUES ('". static::START_DB_VERSION . "');"); } public function tearDown(): void { $this->_dbAdapter = null; } private function _getDatabaseDump(Zend_Db_Adapter_Abstract $dbAdapter, array $skipTables = ['sqlite_sequence', 'version']) { $dump = []; foreach ($dbAdapter->listTables() as $table) { if (in_array($table, $skipTables)) { continue; } $dump[$table] = [ 'structure' => $dbAdapter->describeTable($table), 'rows' => $dbAdapter->fetchAssoc("SELECT * FROM `{$table}`"), ]; } return $dump; } /** * @param $expectations * @dataProvider failedMigrations */ public function testFailedMigration($expectations) { $migration = new Migration($expectations['migrationFile']); $versionsBroker = new Versions(['db' => $this->_dbAdapter]); $this->assertEquals(static::START_DB_VERSION, $versionsBroker->getDbVersion()); $exceptionFlag = false; try { $migration->run($this->_dbAdapter); } catch (Exception $e) { $exceptionFlag = true; } $this->assertEquals($expectations['resultDbVersion'], $versionsBroker->getDbVersion()); $dump = $this->_getDatabaseDump($this->_dbAdapter); $this->assertEquals($expectations['resultSchema'], $dump); $this->assertEquals($expectations['wasException'], $exceptionFlag); } private static function _getMigrationsDir() { return __DIR__ . "/../../data/Migrations/failingMigrations"; } public static function failedMigrations() { return [ [[ 'migrationFile' => static::_getMigrationsDir() . '/201602020202_FailingMigration1.sql', 'resultDbVersion' => static::START_DB_VERSION, 'resultSchema' => [], 'wasException' => true, ]], [[ 'migrationFile' => static::_getMigrationsDir() . '/201602020203_FailingMigration2.sql', 'resultDbVersion' => static::START_DB_VERSION, 'resultSchema' => [], 'wasException' => true, ]], [[ 'migrationFile' => static::_getMigrationsDir() . '/201601010101_Migration.sql', 'resultDbVersion' => '201601010101', 'resultSchema' => [ 'table1' => [ 'structure' => [ 'id' => [ 'SCHEMA_NAME' => '', 'TABLE_NAME' => 'table1', 'COLUMN_NAME' => 'id', 'COLUMN_POSITION' => 1, 'DATA_TYPE' => 'INTEGER', 'DEFAULT' => null, 'NULLABLE' => false, 'LENGTH' => null, 'SCALE' => null, 'PRECISION' => null, 'UNSIGNED' => null, 'PRIMARY' => true, 'PRIMARY_POSITION' => 1, 'IDENTITY' => true, ], 'col1' => [ 'SCHEMA_NAME' => '', 'TABLE_NAME' => 'table1', 'COLUMN_NAME' => 'col1', 'COLUMN_POSITION' => 2, 'DATA_TYPE' => 'TEXT', 'DEFAULT' => null, 'NULLABLE' => false, 'LENGTH' => null, 'SCALE' => null, 'PRECISION' => null, 'UNSIGNED' => null, 'PRIMARY' => false, 'PRIMARY_POSITION' => null, 'IDENTITY' => false, ], ], 'rows' => [], ] ], 'wasException' => false, ]], ]; } }