diff --git a/composer.json b/composer.json index 88d9fb9..3c5e548 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.2", + "orchestra/testbench": "^8.14", "pestphp/pest": "^1.16 || ^2.0", "php-parallel-lint/php-var-dump-check": "^0.5.0", "phpunit/phpunit": "^9.0 || ^10.0" diff --git a/src/Factory.php b/src/Factory.php index 24dc9a1..28cb23e 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -43,6 +43,11 @@ public function makeSeveral(int $count, array $extra = []): Collection ->map(fn () => $this->make($extra)); } + public function makeSeveralExtras(array ...$extras): Collection + { + return collect($extras)->map($this->make(...)); + } + public function only(array $fields): static { return $this->immutableSet('only', $fields); diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 4b4200e..9119442 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -31,6 +31,18 @@ $result->each(fn ($x) => expect($x->fields['id'])->toEqual($id)); }); +test('makeSeveralExtras works', function () { + $extras = [['id' => 10], ['id' => 11, 'user_id' => 20]]; + $result = TestObjectFactory::new()->makeSeveralExtras(...$extras); + + expect($result)->toBeInstanceOf(Collection::class); + expect($result)->toHaveCount(count($extras)); + $result->each(function ($obj) use ($extras) { + static $i = 0; + expect($obj->fields)->toMatchArray($extras[$i++]); + }); +}); + test('whenNotNull excludes null field', function () { $result = TestArrayFactory::new()->make();