From 87509b4cec1ba05446faa2de74d36bd54ff6f1bc Mon Sep 17 00:00:00 2001 From: Italo Date: Fri, 15 Mar 2024 13:16:24 -0300 Subject: [PATCH] [1.x] Adds tests --- src/CustomizableMigration.php | 6 ++-- src/CustomizableModel.php | 6 ++-- tests/CustomizableMigrationTest.php | 50 ++++++++++++++++++++--------- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/CustomizableMigration.php b/src/CustomizableMigration.php index 946d1e2..83757dc 100644 --- a/src/CustomizableMigration.php +++ b/src/CustomizableMigration.php @@ -53,14 +53,16 @@ public function __construct( abstract public function create(Blueprint $table): void; /** - * Execute a callback from the developer to add more columns in the table, if any. + * Execute stored callbacks using the table Blueprint instance. * * @param \Illuminate\Database\Schema\Blueprint $table * @return void */ protected function addColumns(Blueprint $table): void { - with($table, $this->with); + foreach ($this->with as $callback) { + $callback($table); + } } /** diff --git a/src/CustomizableModel.php b/src/CustomizableModel.php index a6fb35c..00d4a21 100644 --- a/src/CustomizableModel.php +++ b/src/CustomizableModel.php @@ -112,10 +112,10 @@ abstract protected static function migrationClass(): string; /** * Return a new customizable migration instance. * - * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void)|null $with + * @param (\Closure(\Illuminate\Database\Schema\Blueprint $table):void) ...$with */ - public static function migration(Closure $with = null): CustomizableMigration + public static function migration(Closure ...$with): CustomizableMigration { - return new (static::migrationClass())(static::class, $with); + return (new (static::migrationClass())(static::class, $with)); } } diff --git a/tests/CustomizableMigrationTest.php b/tests/CustomizableMigrationTest.php index 4b10281..8e70b51 100644 --- a/tests/CustomizableMigrationTest.php +++ b/tests/CustomizableMigrationTest.php @@ -57,8 +57,6 @@ public function creates_columns(): void return true; }); - - TestModel::migration()->up(); } @@ -100,19 +98,24 @@ public function creates_column_with_callback(): void TestMigration::$callMethod = true; $blueprint = m::mock(Blueprint::class); - $blueprint->expects('createCall')->twice(); - $blueprint->expects('addCall')->twice(); + $blueprint->expects('createCall')->once(); + $blueprint->expects('firstCall')->once(); + $blueprint->expects('secondCall')->once(); + $blueprint->expects('thirdCall')->once(); + $blueprint->expects('fourthCall')->once(); $this->container->instance('db.schema', $schema = m::mock(SchemaBuilder::class)); - $schema->expects('create')->twice()->withArgs(function (string $table, Closure $closure) use ($blueprint): bool { + $schema->expects('create')->once()->withArgs(function (string $table, Closure $closure) use ($blueprint): bool { static::assertSame('test_models', $table); $closure($blueprint); return true; }); - TestModel::migration()->with(fn($table) => $table->addCall())->up(); - TestModel::migration(fn($table) => $table->addCall())->up(); + TestModel::migration(fn($table) => $table->firstCall()) + ->with(fn($table) => $table->secondCall()) + ->with(fn($table) => $table->thirdCall(), fn($table) => $table->fourthCall()) + ->up(); } #[Test] @@ -148,7 +151,8 @@ public function morphs_to_numeric(): void $blueprint->expects('nullableNumericMorphs')->with('bar', null)->twice(); $this->container->instance('db.schema', $schema = m::mock(SchemaBuilder::class)); - $schema->expects('create')->twice()->withArgs(function (string $table, Closure $closure) use ($blueprint): bool { + $schema->expects('create')->twice()->withArgs(function (string $table, Closure $closure) use ($blueprint + ): bool { static::assertSame('test_models', $table); $closure($blueprint); @@ -171,7 +175,8 @@ public function morphs_to_uuid(): void $blueprint->expects('nullableUuidMorphs')->with('bar', null)->twice(); $this->container->instance('db.schema', $schema = m::mock(SchemaBuilder::class)); - $schema->expects('create')->twice()->withArgs(function (string $table, Closure $closure) use ($blueprint): bool { + $schema->expects('create')->twice()->withArgs(function (string $table, Closure $closure) use ($blueprint + ): bool { static::assertSame('test_models', $table); $closure($blueprint); @@ -193,7 +198,8 @@ public function morphs_to_ulid(): void $blueprint->expects('nullableUlidMorphs')->with('bar', null)->twice(); $this->container->instance('db.schema', $schema = m::mock(SchemaBuilder::class)); - $schema->expects('create')->twice()->withArgs(function (string $table, Closure $closure) use ($blueprint): bool { + $schema->expects('create')->twice()->withArgs(function (string $table, Closure $closure) use ($blueprint + ): bool { static::assertSame('test_models', $table); $closure($blueprint); @@ -208,18 +214,24 @@ public function morphs_to_ulid(): void public function calls_after_up(): void { $blueprint = m::mock(Blueprint::class); - $blueprint->expects('createCall')->once(); + $blueprint->expects('firstCall')->once(); + $blueprint->expects('secondCall')->once(); + $blueprint->expects('thirdCall')->once(); $this->container->instance('db.schema', $schema = m::mock(SchemaBuilder::class)); $schema->expects('create')->once(); - $schema->expects('table')->once()->withArgs(function (string $table, Closure $closure) use ($blueprint): bool { + $schema->expects('table')->times(3)->withArgs(function (string $table, Closure $closure) use ($blueprint + ): bool { static::assertSame('test_models', $table); $closure($blueprint); return true; }); - TestModel::migration()->afterUp(fn($table) => $table->createCall())->up(); + TestModel::migration() + ->afterUp(fn($table) => $table->firstCall()) + ->afterUp(fn($table) => $table->secondCall(), fn($table) => $table->thirdCall()) + ->up(); } #[Test] @@ -237,10 +249,13 @@ public function drops_table(): void public function calls_before_down(): void { $blueprint = m::mock(Blueprint::class); - $blueprint->expects('afterDownCall')->once(); + $blueprint->expects('firstCall')->once(); + $blueprint->expects('secondCall')->once(); + $blueprint->expects('thirdCall')->once(); $this->container->instance('db.schema', $schema = m::mock(SchemaBuilder::class)); - $schema->expects('table')->once()->withArgs(function (string $table, Closure $closure) use ($blueprint): bool { + $schema->expects('table')->times(3)->withArgs(function (string $table, Closure $closure) use ($blueprint + ): bool { static::assertSame('test_models', $table); $closure($blueprint); @@ -249,6 +264,9 @@ public function calls_before_down(): void $schema->expects('dropIfExists')->with('test_models')->once(); - TestModel::migration()->beforeDown(fn($table) => $table->afterDownCall())->down(); + TestModel::migration() + ->beforeDown(fn($table) => $table->firstCall()) + ->beforeDown(fn($table) => $table->secondCall(), fn($table) => $table->thirdCall()) + ->down(); } }