Skip to content

Commit

Permalink
feat: add assertDatabaseTable test assertion method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Jul 12, 2023
1 parent 69eeff7 commit 3880aa2
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Apiato\Core\Abstracts\Models\Model;
use Illuminate\Auth\Access\Gate;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;

Expand Down Expand Up @@ -57,4 +58,21 @@ protected function inIds($id, Collection $collection): bool
{
return in_array($id, $collection->map(fn ($item) => $item->getHashedKey())->toArray());
}

/**
* Assert if the given database table has the expected columns with the expected types.
*
* @param string $table The table name.
* @param array<string, string> $expectedColumns The key is the column name and the value is the column type.
*
* Example: $this->assertDatabaseTable('users', ['id' => 'bigint']);
*/
protected function assertDatabaseTable(string $table, array $expectedColumns): void
{
$this->assertSameSize($expectedColumns, Schema::getColumnListing($table), "Column count mismatch for '$table' table.");
foreach ($expectedColumns as $column => $type) {
$this->assertTrue(Schema::hasColumn($table, $column), "Column '$column' not found in '$table' table.");
$this->assertEquals($type, Schema::getColumnType($table, $column), "Column '$column' in '$table' table does not match expected $type type.");
}
}
}

0 comments on commit 3880aa2

Please sign in to comment.