Skip to content

Commit

Permalink
Pass table name to the post migration create hooks (#24621)
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravmak authored and taylorotwell committed Jun 18, 2018
1 parent 14beafa commit 5999d6e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/Illuminate/Database/Migrations/MigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function create($name, $path, $table = null, $create = false)
// Next, we will fire any hooks that are supposed to fire after a migration is
// created. Once that is done we'll be ready to return the full path to the
// migration file so it can be used however it's needed by the developer.
$this->firePostCreateHooks();
$this->firePostCreateHooks($table);

return $path;
}
Expand Down Expand Up @@ -150,12 +150,13 @@ protected function getPath($name, $path)
/**
* Fire the registered post create hooks.
*
* @param string $table
* @return void
*/
protected function firePostCreateHooks()
protected function firePostCreateHooks($table)
{
foreach ($this->postCreate as $callback) {
call_user_func($callback);
call_user_func($callback, $table);
}
}

Expand Down
24 changes: 19 additions & 5 deletions tests/Database/DatabaseMigrationCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,31 @@ public function tearDown()
public function testBasicCreateMethodStoresMigrationFile()
{
$creator = $this->getCreator();
unset($_SERVER['__migration.creator']);
$creator->afterCreate(function () {
$_SERVER['__migration.creator'] = true;
});

$creator->expects($this->any())->method('getDatePrefix')->will($this->returnValue('foo'));
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/blank.stub')->andReturn('DummyClass');
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar');

$creator->create('create_bar', 'foo');
}

public function testBasicCreateMethodCallsPostCreateHooks()
{
$table = 'baz';

$creator = $this->getCreator();
unset($_SERVER['__migration.creator']);
$creator->afterCreate(function ($table) {
$_SERVER['__migration.creator'] = $table;
});

$creator->expects($this->any())->method('getDatePrefix')->will($this->returnValue('foo'));
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/update.stub')->andReturn('DummyClass DummyTable');
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');

$creator->create('create_bar', 'foo', $table);

$this->assertTrue($_SERVER['__migration.creator']);
$this->assertEquals($_SERVER['__migration.creator'], $table);

unset($_SERVER['__migration.creator']);
}
Expand Down

0 comments on commit 5999d6e

Please sign in to comment.