D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
psa
/
admin
/
plib
/
modules
/
git
/
vendor
/
plesk
/
db-upgrader
/
tests
/
php
/
Db
/
Filename :
MigrationManagerTest.php
back
Copy
<?php // Copyright 1999-2022. Parallels IP Holdings GmbH. All Rights Reserved. use PleskExt\Upgrader\MigrationManager; use PleskExt\Upgrader\Versions; class Db_MigrationManagerTest extends PHPUnit\Framework\TestCase { private $_dbAdapter = null; public function setUp(): void { $this->_dbAdapter = Zend_Db::factory('Pdo_Sqlite', array('dbname'=>':memory:')); } public function tearDown(): void { $this->_dbAdapter = null; } public function testInitSchema() { $migrationManager = new MigrationManager($this->_dbAdapter, __DIR__ . "/../../data/Migrations/correctMigrations"); $migrationManager->initSchema(); $this->assertEquals(static::_getDbVersionFromInitSchema(), (new Versions(['db' => $this->_dbAdapter]))->getDbVersion()); } public function testApplyMigrations() { $migrationManager = new MigrationManager($this->_dbAdapter, __DIR__ . "/../../data/Migrations/correctMigrations"); $migrationManager->applyMigrations(); $this->assertEquals('201601010101', (new Versions(['db' => $this->_dbAdapter]))->getDbVersion(), 'Wrong db version in init_schema.sql'); } public function testGetDbVersionByMigration() { $migrationManager = new MigrationManager($this->_dbAdapter); $this->assertEquals('201501010101', $migrationManager->getDbVersionByMigration('201501010101_SOmeMigrationStep.sql')); } /** * @dataProvider dataLoadMigrations */ public function testLoadMigrations($currentDbVersion, $expectedMigrationsCount, $expectedMigrations) { $migrationManager = new MigrationManager($this->_dbAdapter, __DIR__ . "/../../data/Migrations/failingMigrations"); $migrations = static::_convertMigrationFilesToDates($migrationManager->loadMigrations($currentDbVersion)); $this->assertEquals($expectedMigrationsCount, count($migrations)); if (!is_null($expectedMigrations)) { $this->assertEquals($expectedMigrations, $migrations); } } public static function dataLoadMigrations() { return [ [ '201501010101', count(static::_getAllMigrationsList()), null ], [ static::_getLastMigration(), 0, [] ], [ static::_getLastMigration()-1, 1, [ static::_getLastMigration() ] ], ]; } private static function _getLastMigration() { $migrations = static::_getAllMigrationsList(); return end($migrations); } private static function _getAllMigrationsList() { static $allMigrationsList; if (is_null($allMigrationsList)) { $migrationFiles = glob(__DIR__ . "/../../data/Migrations/failingMigrations//2016*.sql"); $allMigrationsList = static::_convertMigrationFilesToDates($migrationFiles); } return $allMigrationsList; } private static function _convertMigrationFilesToDates($files) { $dbAdapter = Zend_Db::factory('Pdo_Sqlite', array('dbname'=>':memory:')); $migrationManager = new MigrationManager($dbAdapter); return array_map( [$migrationManager, 'getDbVersionByMigration'], array_map('basename', $files) ); } private static function _getDbVersionFromInitSchema() { $initSchema = file_get_contents(__DIR__ . "/../../data/Migrations/correctMigrations/init_schema.sql"); preg_match('/INSERT INTO `version` \(`dbVersion`\) VALUES \(\'(.*)\'\);/', $initSchema, $matches); return $matches[1]; } }