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

[8.x] Support times() with raw() #34667

Merged
merged 1 commit into from
Oct 5, 2020
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
8 changes: 7 additions & 1 deletion src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ public function configure()
*/
public function raw($attributes = [], ?Model $parent = null)
{
return $this->state($attributes)->getExpandedAttributes($parent);
if ($this->count === null) {
return $this->state($attributes)->getExpandedAttributes($parent);
}

return array_map(function () use ($attributes, $parent) {
return $this->state($attributes)->getExpandedAttributes($parent);
}, range(1, $this->count));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Null Coalescing Operator instead of checking for null

range(1, $this->count ?? 1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because that would return an array containing an array of raw attributes. If the count is null you just want the raw attributes themselves. This code uses the same pattern from the make method.

}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ public function test_expanded_model_attributes_can_be_created()
$this->assertSame('Test Title', $post['title']);
}

public function test_multiple_model_attributes_can_be_created()
{
$posts = FactoryTestPostFactory::new()->times(10)->raw();
$this->assertIsArray($posts);

$this->assertCount(10, $posts);
}

public function test_after_creating_and_making_callbacks_are_called()
{
$user = FactoryTestUserFactory::new()
Expand Down