Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

#115078 add method for generating items with several extra data #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down