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

Make firePostCreateHook() backwards compatability with all Laravel 5.6 #24916

Closed
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
},
"autoload-dev": {
"files": [
"tests/Database/stubs/MigrationCreatorFakeMigration.php"
"tests/Database/stubs/MigrationCreatorFakeMigration.php",
"tests/Database/stubs/MigrationCreatorSubclass.php"
],
"psr-4": {
"Illuminate\\Tests\\": "tests/"
Expand Down
18 changes: 14 additions & 4 deletions src/Illuminate/Database/Migrations/MigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class MigrationCreator
*/
protected $postCreate = [];

/**
* The table.
*
* @var string
*/
protected $table;

/**
* Create a new migration creator instance.
*
Expand All @@ -48,6 +55,10 @@ public function create($name, $path, $table = null, $create = false)
{
$this->ensureMigrationDoesntAlreadyExist($name);

// We are assigning the variable to the instance for backwards compatibility with prior versions
// so that it can be used in the firePostCreateHooks
$this->table = $table;

// First we will get the stub file for the migration, which serves as a type
// of template for the migration. Once we have those we will populate the
// various place-holders, save the file, and run the post create event.
Expand All @@ -61,7 +72,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($table);
$this->firePostCreateHooks();

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

Expand Down
33 changes: 33 additions & 0 deletions tests/Database/DatabaseMigrationCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Foo\Bar\MigrationCreatorSubclass;

class DatabaseMigrationCreatorTest extends TestCase
{
Expand Down Expand Up @@ -44,6 +45,38 @@ public function testBasicCreateMethodCallsPostCreateHooks()
unset($_SERVER['__migration.creator']);
}

/**
* The original MigrationCreator::firePostCreateHooks() of 5.6 was parameter-less.
* Since that method is protected, developers can extend the class, like in MigrationCreatorSubclass.
* With recent changes, we saw an added parameter which breaks the MigrationCreatorSubclass in the
* original state of Larvel 5.6. The testBasicCreateMethodCallsPostCreateHooks above verifies the
* new functionality still works, while this test ensures the new changes are still compatible with
* the original 5.6 implementation.
*/
public function testPostCreateHookMaintainsSameMethodSignature()
{
$table = 'baz';

$creator = $this->getMockBuilder(MigrationCreatorSubclass::class)
->setMethods(['getDatePrefix'])
->setConstructorArgs([m::mock('Illuminate\Filesystem\Filesystem')])
->getMock();

$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->assertEquals($_SERVER['__migration.creator'], $table);

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

public function testTableUpdateMigrationStoresMigrationFile()
{
$creator = $this->getCreator();
Expand Down
20 changes: 20 additions & 0 deletions tests/Database/stubs/MigrationCreatorSubclass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Foo\Bar;

use Illuminate\Database\Migrations\MigrationCreator;

/**
* Class MigrationCreatorSubclass.
*/
class MigrationCreatorSubclass extends MigrationCreator
{
/**
* Here we override the original method of Laravel 5.6's original firePostCreateHook, with no parameters.
*/
protected function firePostCreateHooks()
{
// Run an arbitrary command
parent::firePostCreateHooks();
}
}