Skip to content

Instantly share code, notes, and snippets.

@sters
Created September 6, 2017 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sters/788f84a20936b49f5834f845e01995fd to your computer and use it in GitHub Desktop.
Save sters/788f84a20936b49f5834f845e01995fd to your computer and use it in GitHub Desktop.
Confirm the validity of migration and definition in Laravel x PHPUnit.
<?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