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

[5.6] Pass table name to the post migration create hooks #24621

Merged
merged 1 commit into from
Jun 18, 2018
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
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