Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Add a no migrations event #31289

Merged
merged 2 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Events/NoMigrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Database\Events;

class NoMigrations
{
/**
* The migration method that was called.
*
* @var string
*/
public $method;

/**
* Create a new event instance.
*
* @param string $method
* @return void
*/
public function __construct($method)
{
$this->method = $method;
}
}
5 changes: 5 additions & 0 deletions src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Database\Events\MigrationsStarted;
use Illuminate\Database\Events\MigrationStarted;
use Illuminate\Database\Events\NoMigrations;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -139,6 +140,8 @@ public function runPending(array $migrations, array $options = [])
// aren't, we will just make a note of it to the developer so they're aware
// that all of the migrations have been run against this database system.
if (count($migrations) === 0) {
$this->fireMigrationEvent(new NoMigrations('up'));

$this->note('<info>Nothing to migrate.</info>');

return;
Expand Down Expand Up @@ -221,6 +224,8 @@ public function rollback($paths = [], array $options = [])
$migrations = $this->getMigrationsForRollback($options);

if (count($migrations) === 0) {
$this->fireMigrationEvent(new NoMigrations('down'));

$this->note('<info>Nothing to rollback.</info>');

return [];
Expand Down
28 changes: 0 additions & 28 deletions tests/Integration/Database/MigrateWithRealpathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Database\Events\MigrationEnded;
use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Database\Events\MigrationsStarted;
use Illuminate\Database\Events\MigrationStarted;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;

class MigrateWithRealpathTest extends DatabaseTestCase
Expand Down Expand Up @@ -40,27 +35,4 @@ public function testMigrationsHasTheMigratedTable()
'batch' => 1,
]);
}

public function testMigrationEventsAreFired()
{
Event::fake();

Event::listen(MigrationsStarted::class, function ($event) {
return $this->assertInstanceOf(MigrationsStarted::class, $event);
});

Event::listen(MigrationsEnded::class, function ($event) {
return $this->assertInstanceOf(MigrationsEnded::class, $event);
});

Event::listen(MigrationStarted::class, function ($event) {
return $this->assertInstanceOf(MigrationStarted::class, $event);
});

Event::listen(MigrationEnded::class, function ($event) {
return $this->assertInstanceOf(MigrationEnded::class, $event);
});

$this->artisan('migrate');
}
}
71 changes: 71 additions & 0 deletions tests/Integration/Database/MigratorEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Database\Events\MigrationEnded;
use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Database\Events\MigrationsStarted;
use Illuminate\Database\Events\MigrationStarted;
use Illuminate\Database\Events\NoMigrations;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Event;

class MigratorEventsTest extends DatabaseTestCase
{
protected function migrateOptions()
{
return [
'--path' => realpath(__DIR__.'/stubs/'),
'--realpath' => true,
];
}

public function testMigrationEventsAreFired()
{
Event::fake();

$this->artisan('migrate', $this->migrateOptions());
$this->artisan('migrate:rollback', $this->migrateOptions());

Event::assertDispatched(MigrationsStarted::class, 2);
Event::assertDispatched(MigrationsEnded::class, 2);
Event::assertDispatched(MigrationStarted::class, 2);
Event::assertDispatched(MigrationEnded::class, 2);
}

public function testMigrationEventsContainTheMigrationAndMethod()
{
Event::fake();

$this->artisan('migrate', $this->migrateOptions());
$this->artisan('migrate:rollback', $this->migrateOptions());

Event::assertDispatched(MigrationStarted::class, function ($event) {
return $event->method == 'up' && $event->migration instanceof Migration;
});
Event::assertDispatched(MigrationStarted::class, function ($event) {
return $event->method == 'down' && $event->migration instanceof Migration;
});
Event::assertDispatched(MigrationEnded::class, function ($event) {
return $event->method == 'up' && $event->migration instanceof Migration;
});
Event::assertDispatched(MigrationEnded::class, function ($event) {
return $event->method == 'down' && $event->migration instanceof Migration;
});
}

public function testTheNoMigrationEventIsFiredWhenNothingToMigrate()
{
Event::fake();

$this->artisan('migrate');
$this->artisan('migrate:rollback');

Event::assertDispatched(NoMigrations::class, function ($event) {
return $event->method == 'up';
});
Event::assertDispatched(NoMigrations::class, function ($event) {
return $event->method == 'down';
});
}
}