Skip to content

Commit

Permalink
[10.x] createMany & createManyQuietly make argument optional (#48070)
Browse files Browse the repository at this point in the history
* Add null as possible argument to createMany

* Update Factory.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
JHWelch and taylorotwell committed Aug 15, 2023
1 parent c1e6281 commit 036f0a0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Expand Up @@ -235,11 +235,15 @@ public function createOneQuietly($attributes = [])
/**
* Create a collection of models and persist them to the database.
*
* @param int|iterable<int, array<string, mixed>> $records
* @param int|null|iterable<int, array<string, mixed>> $records
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
*/
public function createMany(int|iterable $records)
public function createMany(int|iterable|null $records = null)
{
if (is_null($records)) {
$records = $this->count ?? 1;
}

if (is_numeric($records)) {
$records = array_fill(0, $records, []);
}
Expand All @@ -254,10 +258,10 @@ public function createMany(int|iterable $records)
/**
* Create a collection of models and persist them to the database without dispatching any model events.
*
* @param int|iterable<int, array<string, mixed>> $records
* @param int|null|iterable<int, array<string, mixed>> $records
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
*/
public function createManyQuietly(int|iterable $records)
public function createManyQuietly(int|iterable|null $records = null)
{
return Model::withoutEvents(function () use ($records) {
return $this->createMany($records);
Expand Down
8 changes: 8 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Expand Up @@ -130,6 +130,14 @@ public function test_basic_model_can_be_created()
$this->assertInstanceOf(Collection::class, $users);
$this->assertCount(2, $users);

$users = FactoryTestUserFactory::times(2)->createMany();
$this->assertInstanceOf(Collection::class, $users);
$this->assertCount(2, $users);

$users = FactoryTestUserFactory::new()->createMany();
$this->assertInstanceOf(Collection::class, $users);
$this->assertCount(1, $users);

$users = FactoryTestUserFactory::times(10)->create();
$this->assertCount(10, $users);
}
Expand Down
2 changes: 2 additions & 0 deletions types/Database/Eloquent/Factories/Factory.php
Expand Up @@ -76,12 +76,14 @@ public function definition()
[['string' => 'string']]
));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createMany(3));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createMany());

// assertType('Illuminate\Database\Eloquent\Collection<int, User>', $factory->createManyQuietly([['string' => 'string']]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createManyQuietly(
[['string' => 'string']]
));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createManyQuietly(3));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createManyQuietly());

// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->create());
// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->create([
Expand Down

0 comments on commit 036f0a0

Please sign in to comment.