Skip to content

Commit

Permalink
[1.x] Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Mar 15, 2024
1 parent 0f8c03a commit 87509b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
6 changes: 4 additions & 2 deletions src/CustomizableMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/CustomizableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
50 changes: 34 additions & 16 deletions tests/CustomizableMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public function creates_columns(): void
return true;
});



TestModel::migration()->up();
}

Expand Down Expand 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]
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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]
Expand All @@ -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);

Expand All @@ -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();
}
}

0 comments on commit 87509b4

Please sign in to comment.