diff --git a/src/SchemaServiceProvider.php b/src/SchemaServiceProvider.php index 360b76a..9702f07 100644 --- a/src/SchemaServiceProvider.php +++ b/src/SchemaServiceProvider.php @@ -5,6 +5,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Builder; use Illuminate\Support\ServiceProvider; +use InvalidArgumentException; class SchemaServiceProvider extends ServiceProvider { @@ -13,7 +14,7 @@ class SchemaServiceProvider extends ServiceProvider */ protected function registerBuilderMacros() { - Builder::macro('getColumnComment', function (string $table, string $column) { + Builder::macro('getColumnComment', function (string $table, string $column, bool $fail_on_missing = false) { /** @var $this Builder */ $column_info = $this->getColumns($table); @@ -23,10 +24,14 @@ protected function registerBuilderMacros() } } + if ($fail_on_missing) { + throw new InvalidArgumentException(sprintf("No such column [%s] in the table [%s].", $column, $table)); + } + return null; }); - Builder::macro('getTableComment', function (string $table) { + Builder::macro('getTableComment', function (string $table, bool $fail_on_missing = false) { /** @var $this Builder */ $table_info = $this->getTables(); @@ -36,6 +41,10 @@ protected function registerBuilderMacros() } } + if ($fail_on_missing) { + throw new InvalidArgumentException(sprintf("No such table [%s] in the database [%s].", $table, $this->getConnection()->getDatabaseName())); + } + return null; }); } diff --git a/src/Testing/Constraints/HasDatabaseColumnComment.php b/src/Testing/Constraints/HasDatabaseColumnComment.php index ad7b806..af9d07c 100644 --- a/src/Testing/Constraints/HasDatabaseColumnComment.php +++ b/src/Testing/Constraints/HasDatabaseColumnComment.php @@ -49,7 +49,7 @@ public function __construct(Connection $database, string $table, string $column) */ public function getColumnComment(): ?string { - return $this->database->getSchemaBuilder()->getColumnComment($this->table, $this->column); + return $this->database->getSchemaBuilder()->getColumnComment($this->table, $this->column, true); } /** diff --git a/src/Testing/Constraints/HasDatabaseTableComment.php b/src/Testing/Constraints/HasDatabaseTableComment.php index 125e35a..fc03558 100644 --- a/src/Testing/Constraints/HasDatabaseTableComment.php +++ b/src/Testing/Constraints/HasDatabaseTableComment.php @@ -41,7 +41,7 @@ public function __construct(Connection $database, string $table) */ public function getTableComment(): ?string { - return $this->database->getSchemaBuilder()->getTableComment($this->table); + return $this->database->getSchemaBuilder()->getTableComment($this->table, true); } /** diff --git a/tests/Feature/SchemaTest.php b/tests/Feature/SchemaTest.php index dc03eca..1950eb1 100644 --- a/tests/Feature/SchemaTest.php +++ b/tests/Feature/SchemaTest.php @@ -2,11 +2,14 @@ namespace Javaabu\Schema\Tests\Feature; +use Illuminate\Foundation\Testing\RefreshDatabase; use Javaabu\Schema\Tests\TestCase; use Javaabu\Schema\Tests\TestSupport\Enums\CityStatus; class SchemaTest extends TestCase { + use RefreshDatabase; + /** @test */ public function it_can_create_an_enum_column(): void { diff --git a/tests/Unit/SchemaTest.php b/tests/Unit/BuilderTest.php similarity index 94% rename from tests/Unit/SchemaTest.php rename to tests/Unit/BuilderTest.php index 471aeb3..86f11ff 100644 --- a/tests/Unit/SchemaTest.php +++ b/tests/Unit/BuilderTest.php @@ -7,7 +7,7 @@ use Javaabu\Schema\Tests\TestCase; use Javaabu\Schema\Tests\TestSupport\Enums\CityStatus; -class SchemaTest extends TestCase +class BuilderTest extends TestCase { use RefreshDatabase; diff --git a/tests/Unit/HasDatabaseTableCommentTest.php b/tests/Unit/HasDatabaseTableCommentTest.php deleted file mode 100644 index 48c1058..0000000 --- a/tests/Unit/HasDatabaseTableCommentTest.php +++ /dev/null @@ -1,22 +0,0 @@ -getConnection(null, 'cities'), 'cities'); - - $this->assertEquals('files', $constraint->getTableComment()); - } -}