|
17 | 17 | use Zenstruck\Foundry\Factory;
|
18 | 18 | use Zenstruck\Foundry\Object\Instantiator;
|
19 | 19 | 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; |
20 | 22 | use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory;
|
21 | 23 | use Zenstruck\Foundry\Tests\Fixture\Factories\Object2Factory;
|
22 | 24 | use Zenstruck\Foundry\Tests\Fixture\Factories\SimpleObjectFactory;
|
@@ -472,6 +474,65 @@ public function providing_invalid_values_number_to_distribute_throws(): void
|
472 | 474 | SimpleObjectFactory::new()->many(2)->distribute('prop1', ['foo']);
|
473 | 475 | }
|
474 | 476 |
|
| 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 | + |
475 | 536 | /**
|
476 | 537 | * @test
|
477 | 538 | */
|
|
0 commit comments