diff --git a/src/Illuminate/Database/Events/NoMigrations.php b/src/Illuminate/Database/Events/NoMigrations.php new file mode 100644 index 000000000000..f6d3831fbacf --- /dev/null +++ b/src/Illuminate/Database/Events/NoMigrations.php @@ -0,0 +1,24 @@ +method = $method; + } +} diff --git a/src/Illuminate/Database/Migrations/Migrator.php b/src/Illuminate/Database/Migrations/Migrator.php index 00106e511111..9e6f0c314bc2 100755 --- a/src/Illuminate/Database/Migrations/Migrator.php +++ b/src/Illuminate/Database/Migrations/Migrator.php @@ -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; @@ -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('Nothing to migrate.'); return; @@ -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('Nothing to rollback.'); return []; diff --git a/tests/Integration/Database/MigrateWithRealpathTest.php b/tests/Integration/Database/MigrateWithRealpathTest.php index 0a976ad19d79..edc87f7fdec5 100644 --- a/tests/Integration/Database/MigrateWithRealpathTest.php +++ b/tests/Integration/Database/MigrateWithRealpathTest.php @@ -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 @@ -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'); - } } diff --git a/tests/Integration/Database/MigratorEventsTest.php b/tests/Integration/Database/MigratorEventsTest.php new file mode 100644 index 000000000000..7a20f9d8af92 --- /dev/null +++ b/tests/Integration/Database/MigratorEventsTest.php @@ -0,0 +1,71 @@ + 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'; + }); + } +}