Skip to content

Instantly share code, notes, and snippets.

@sters
Created September 6, 2017 05:04

Revisions

  1. sters created this gist Sep 6, 2017.
    55 changes: 55 additions & 0 deletions MigrationsTest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    <?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',
    ]);
    }
    }