From 5999d6e41870c78a05cb84a76eb9eaa7c4bb773d Mon Sep 17 00:00:00 2001 From: Gaurav Makhecha Date: Mon, 18 Jun 2018 18:06:57 +0530 Subject: [PATCH] Pass table name to the post migration create hooks (#24621) --- .../Database/Migrations/MigrationCreator.php | 7 +++--- .../Database/DatabaseMigrationCreatorTest.php | 24 +++++++++++++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Database/Migrations/MigrationCreator.php b/src/Illuminate/Database/Migrations/MigrationCreator.php index 719fbf106c3d..685e03c64a23 100755 --- a/src/Illuminate/Database/Migrations/MigrationCreator.php +++ b/src/Illuminate/Database/Migrations/MigrationCreator.php @@ -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; } @@ -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); } } diff --git a/tests/Database/DatabaseMigrationCreatorTest.php b/tests/Database/DatabaseMigrationCreatorTest.php index 08e5d97d35b9..894c733879cd 100755 --- a/tests/Database/DatabaseMigrationCreatorTest.php +++ b/tests/Database/DatabaseMigrationCreatorTest.php @@ -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']); }