Skip to content

Commit 5e011a5

Browse files
committed
feat: introduce mapEach() method
1 parent d15de0e commit 5e011a5

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/FactoryCollection.php

+23
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,29 @@ public function distribute(string $field, array $values): self
187187
);
188188
}
189189

190+
/**
191+
* @param list<mixed>|list<list<mixed>> $values
192+
*
193+
* @return self<T, TFactory>
194+
*/
195+
public function mapEach(callable $callback, array $values): self // @phpstan-ignore missingType.callable (cannot properly type the callable)
196+
{
197+
$factories = $this->all();
198+
199+
if (count($factories) !== count($values)) {
200+
throw new \InvalidArgumentException('Number of values must match number of factories.');
201+
}
202+
203+
return new self(
204+
$this->factory,
205+
static fn() => \array_map(
206+
static fn(Factory $f, mixed $value) => is_array($value) ? $callback($f, ...$value) : $callback($f, $value),
207+
$factories,
208+
$values
209+
)
210+
);
211+
}
212+
190213
public function getIterator(): \Traversable
191214
{
192215
return new \ArrayIterator($this->all());

tests/Unit/ObjectFactoryTest.php

+61
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Zenstruck\Foundry\Factory;
1818
use Zenstruck\Foundry\Object\Instantiator;
1919
use Zenstruck\Foundry\Test\Factories;
20+
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Category\CategoryFactory;
21+
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory;
2022
use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory;
2123
use Zenstruck\Foundry\Tests\Fixture\Factories\Object2Factory;
2224
use Zenstruck\Foundry\Tests\Fixture\Factories\SimpleObjectFactory;
@@ -472,6 +474,65 @@ public function providing_invalid_values_number_to_distribute_throws(): void
472474
SimpleObjectFactory::new()->many(2)->distribute('prop1', ['foo']);
473475
}
474476

477+
/**
478+
* @test
479+
*/
480+
#[Test]
481+
public function map_each_with_single_value(): void
482+
{
483+
$objects = SimpleObjectFactory::new()
484+
->many(2)
485+
->mapEach(
486+
static fn(SimpleObjectFactory $f, $prop1) => $f->withProps($prop1),
487+
['foo', 'bar']
488+
)
489+
->create();
490+
491+
self::assertCount(2, $objects);
492+
493+
self::assertSame('foo', $objects[0]->prop1);
494+
self::assertSame('bar', $objects[1]->prop1);
495+
}
496+
497+
/**
498+
* @test
499+
*/
500+
#[Test]
501+
public function map_each(): void
502+
{
503+
$objects = SimpleObjectFactory::new()
504+
->many(2)
505+
->mapEach(
506+
static fn(SimpleObjectFactory $f, $prop1, $prop2) => $f->withProps($prop1, $prop2),
507+
[['foo', 'bar'], ['prop1', 'prop2']]
508+
)
509+
->create();
510+
511+
self::assertCount(2, $objects);
512+
513+
self::assertSame('foo', $objects[0]->prop1);
514+
self::assertSame('bar', $objects[0]->prop2);
515+
516+
self::assertSame('prop1', $objects[1]->prop1);
517+
self::assertSame('prop2', $objects[1]->prop2);
518+
}
519+
520+
/**
521+
* @test
522+
*/
523+
#[Test]
524+
public function providing_invalid_values_number_to_map_each_throws(): void
525+
{
526+
$this->expectException(\InvalidArgumentException::class);
527+
528+
SimpleObjectFactory::new()
529+
->many(2)
530+
->mapEach(
531+
static fn(SimpleObjectFactory $f, $prop1) => $f->withProps($prop1),
532+
['foo']
533+
);
534+
}
535+
475536
/**
476537
* @test
477538
*/

0 commit comments

Comments
 (0)