From 0ce723a3f9c5eb84f9ff9d40cc3faa6c1f60141f Mon Sep 17 00:00:00 2001 From: Georgy Karkavin Date: Fri, 27 Oct 2023 10:54:35 +0500 Subject: [PATCH] #115078 add method for generating items with several extra data --- composer.json | 1 + src/Factory.php | 5 +++++ tests/FactoryTest.php | 12 ++++++++++++ 3 files changed, 18 insertions(+) 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();