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] Prevent considering an array attribute as callable in model factories #23372

Merged
merged 1 commit into from
Mar 2, 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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ protected function stateAttributes($state, array $attributes)
protected function expandAttributes(array $attributes)
{
foreach ($attributes as &$attribute) {
if (is_callable($attribute) && ! is_string($attribute)) {
if (is_callable($attribute) && ! is_string($attribute) && ! is_array($attribute)) {
$attribute = $attribute($attributes);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Integration/Database/EloquentFactoryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function getEnvironmentSetUp($app)
return [
'name' => $faker->name,
'status' => 'active',
'tags' => ['Storage', 'Data'],
'user_id' => function () {
return factory(FactoryBuildableUser::class)->create()->id;
},
Expand Down Expand Up @@ -80,6 +81,7 @@ public function setUp()
Schema::create('servers', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('tags');
$table->integer('user_id');
$table->string('status');
});
Expand Down Expand Up @@ -134,6 +136,7 @@ public function creating_models_with_callable_states()
$callableServer = factory(FactoryBuildableServer::class)->states('callable')->create();

$this->assertEquals('active', $server->status);
$this->assertEquals(['Storage', 'Data'], $server->tags);
$this->assertEquals('callable', $callableServer->status);
}

Expand Down Expand Up @@ -208,6 +211,7 @@ class FactoryBuildableServer extends Model
public $table = 'servers';
public $timestamps = false;
protected $guarded = ['id'];
public $casts = ['tags' => 'array'];

public function user()
{
Expand Down