Skip to content

Commit

Permalink
Added alternative assertions for Type in ColumnAssertions. (#25)
Browse files Browse the repository at this point in the history
* Added columnType assertion via postgres (assertPostgresTypeColumn)
* Renamed assertion (assertTypeColumn => assertLaravelTypeColumn)
  • Loading branch information
pvsaintpe committed Aug 23, 2019
1 parent 36197aa commit 40a48a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/Helpers/ColumnAssertions.php
Expand Up @@ -35,11 +35,16 @@ protected function assertDefaultOnColumn(string $table, string $column, ?string
$this->assertSame($expected, $defaultValue);
}

protected function assertTypeColumn(string $table, string $column, string $expected): void
protected function assertLaravelTypeColumn(string $table, string $column, string $expected): void
{
$this->assertSame($expected, Schema::getColumnType($table, $column));
}

protected function assertPostgresTypeColumn(string $table, string $column, string $expected): void
{
$this->assertSame($expected, $this->getTypeListing($table, $column));
}

private function getCommentListing(string $table, string $column)
{
$definition = DB::selectOne(
Expand All @@ -57,6 +62,20 @@ private function getCommentListing(string $table, string $column)
return $definition ? $definition->description : null;
}

private function getTypeListing(string $table, string $column): ?string
{
$definition = DB::selectOne(
'
SELECT data_type
FROM information_schema.columns
WHERE table_name = ? AND column_name = ?
',
[$table, $column]
);

return $definition ? $definition->data_type : null;
}

private function getDefaultListing(string $table, string $column)
{
$definition = DB::selectOne(
Expand Down
5 changes: 3 additions & 2 deletions tests/Functional/Schema/CreateTableTest.php
Expand Up @@ -40,8 +40,9 @@ public function columnAssertions(): void

$this->assertSameTable(['id', 'name', 'field_comment', 'field_default'], 'test_table');

$this->assertTypeColumn('test_table', 'id', 'integer');
$this->assertTypeColumn('test_table', 'name', 'string');
$this->assertPostgresTypeColumn('test_table', 'id', 'integer');
$this->assertLaravelTypeColumn('test_table', 'name', 'string');
$this->assertPostgresTypeColumn('test_table', 'name', 'character varying');

$this->assertDefaultOnColumn('test_table', 'field_default', '123');
$this->assertCommentOnColumn('test_table', 'field_comment', 'test');
Expand Down

0 comments on commit 40a48a2

Please sign in to comment.