Created
September 6, 2017 05:04
-
-
Save sters/788f84a20936b49f5834f845e01995fd to your computer and use it in GitHub Desktop.
Confirm the validity of migration and definition in Laravel x PHPUnit.
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
<?php | |
namespace Tests; | |
use Tests\TestCase; | |
class MigrationsTest extends TestCase | |
{ | |
/** | |
* Check table columns. MySQL only. | |
* | |
* @param string $tableName | |
* @param array $columnInfoList | |
*/ | |
private function __checkColumns($tableName, $columnInfoList) | |
{ | |
$descResult = \DB::select('desc ' . $tableName . ';'); | |
foreach ($columnInfoList as $field => $type) { | |
$success = false; | |
foreach ($descResult as $columnInfo) { | |
if ($field !== $columnInfo->Field) { | |
continue; | |
} | |
$message = "[{$tableName}] {$field} / {$type} - {$columnInfo->Field} / {$columnInfo->Type}"; | |
$this->assertEquals($field, $columnInfo->Field, $message); | |
$this->assertContains(str_replace(' ', '', $type), str_replace(' ', '', $columnInfo->Type), $message); | |
$success = true; | |
break; | |
} | |
if ($success) { | |
continue; | |
} | |
$this->fail("[{$tableName}] {$field} / {$type} column not found"); | |
} | |
if (count($columnInfoList) != count($descResult)) { | |
fputs(STDERR, "[MIGRATION WARNING][{$tableName}] Invalid column count. " . count($columnInfoList) . ' / ' . count($descResult) . "\n"); | |
} | |
} | |
public function testAccountTable() | |
{ | |
$this->__checkColumns('account', [ | |
'email' => 'varchar(191)', | |
'token' => 'varchar(191)', | |
'created_at' => 'timestamp', | |
'updated_at' => 'timestamp', | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment