Skip to content

Commit

Permalink
Merge branch 'migrator-events' of https://github.com/wouter2203/frame…
Browse files Browse the repository at this point in the history
…work into wouter2203-migrator-events
  • Loading branch information
taylorotwell committed Feb 3, 2020
2 parents f795af8 + fc3a5a9 commit 7b84d4a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 28 deletions.
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';
});
}
}

0 comments on commit 7b84d4a

Please sign in to comment.