Trait luya\testsuite\traits\MigrationFileCheckTrait

Available since version1.0.1
Source Code https://github.com/luyadev/luya-testsuite/blob/master/src/traits/MigrationFileCheckTrait.php

Migration File Check.

This trait allows you to check a given folder or file of migrations whether each create table statement has a drop table statement.

Check a single migration file.

use MigrationFileCheckTrait;

public function testMigrationFiles()
{
    $this->checkMigrationFile('@estoreadmin/migrations/m170515_115236_basetables.php');
}

Check folder with migration files:

$this->checkMigrationFolder('@estoreadmin/migrations');

Public Properties

Hide inherited properties

Property Type Description Defined By
$dbMigrationInstance string The instance class to compare the migration files. luya\testsuite\traits\MigrationFileCheckTrait

Public Methods

Hide inherited methods

Method Description Defined By
checkMigrationFile() Check a given migration file based on its path. luya\testsuite\traits\MigrationFileCheckTrait
checkMigrationFolder() Check a folder with migrations files. luya\testsuite\traits\MigrationFileCheckTrait

Property Details

Hide inherited properties

$dbMigrationInstance public property

The instance class to compare the migration files.

public string $dbMigrationInstance 'yii\db\Migration'

Method Details

Hide inherited methods

checkMigrationFile() public method

Check a given migration file based on its path.

public void checkMigrationFile ( $filePath )
$filePath string

Path to the migration file.

                public function checkMigrationFile($filePath)
{
    $filePath = Yii::getAlias($filePath);
    $className = FileHelper::getFileInfo($filePath)->name;
    
    $this->assertInstanceOf($this->dbMigrationInstance, $this->createMigration($filePath, $className));
    $rawClassContent = FileHelper::getFileContent($filePath);
    
    preg_match_all('/createTable\(([\"\'\0%_\{\}\-a-zA-Z0-9]+)/mi', $rawClassContent, $result);
    
    foreach ($result[1] as $create) {
        $tableName = str_replace(["\"", "'"], '', $create);
        
        preg_match_all("/dropTable\((\"|')(".preg_quote($tableName).")(\"|')/mi", $rawClassContent, $r);
        
        $this->assertSame(1, count($r[0]), "Missing dropTable command for table {$tableName}.");
    }
}

            
checkMigrationFolder() public method

Check a folder with migrations files.

Each migration will be checked by {{checkMigrationFile()}}.

public void checkMigrationFolder ( $folder )
$folder string

Path to the folder @estoreadmin/migrations.

                public function checkMigrationFolder($folder)
{
    $folder = Yii::getAlias($folder);
    
    foreach (FileHelper::findFiles($folder) as $file) {
        $this->checkMigrationFile($file);
    }
}