Skip to content

Commit cada0cf

Browse files
authored
[feature] pass an index to FactoryCollection attributes (#318)
1 parent 619987f commit cada0cf

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ fixcs: docker-start bin/tools/cs-fixer/vendor ### Run PHP CS-Fixer
7171
@${DOCKER_PHP} bin/tools/cs-fixer/vendor/friendsofphp/php-cs-fixer/php-cs-fixer --no-interaction --diff -v fix
7272

7373
bin/tools/cs-fixer/vendor: vendor bin/tools/cs-fixer/composer.json bin/tools/cs-fixer/composer.lock
74-
@${DOCKER_PHP} composer bin cs-fixer update
74+
@${DOCKER_PHP} composer bin cs-fixer install
7575

7676
sca: docker-start bin/tools/psalm/vendor ### Run Psalm
7777
@${DOCKER_PHP} bin/tools/psalm/vendor/vimeo/psalm/psalm --config=./psalm.xml
7878

7979
bin/tools/psalm/vendor: vendor bin/tools/psalm/composer.json bin/tools/psalm/composer.lock
80-
@${DOCKER_PHP} composer bin psalm update
80+
@${DOCKER_PHP} composer bin psalm install
8181

8282
database-generate-migration: docker-start vendor ### Generate new migration based on mapping in Zenstruck\Foundry\Tests\Fixtures\Entity
8383
@${DC_EXEC} -e DATABASE_URL=${MYSQL_URL} php vendor/bin/doctrine-migrations migrations:migrate --no-interaction --allow-no-migration # first, let's load into db existing migrations

docs/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ Using your Factory
278278
PostFactory::createMany(5); // returns Post[]|Proxy[]
279279
PostFactory::createMany(5, ['title' => 'My Title']);
280280
281+
// Create 5 posts with incremental title
282+
PostFactory::createMany(
283+
5,
284+
static function(int $i) {
285+
return ['title' => "Title $i"]; // "Title 1", "Title 2", ... "Title 5"
286+
}
287+
);
288+
281289
// find a persisted object for the given attributes, if not found, create with the attributes
282290
PostFactory::findOrCreate(['title' => 'My Title']); // returns Post|Proxy
283291

src/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ final protected function class(): string
307307
*/
308308
private static function normalizeAttributes($attributes): array
309309
{
310-
return \is_callable($attributes) ? $attributes(self::faker()) : $attributes;
310+
return \is_callable($attributes) ? $attributes() : $attributes;
311311
}
312312

313313
/**

src/FactoryCollection.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ public static function sequence(Factory $factory, iterable $sequence): self
7373
*/
7474
public function create($attributes = []): array
7575
{
76-
return \array_map(
77-
static function(Factory $factory) use ($attributes) {
78-
return $factory->create($attributes);
79-
},
80-
$this->all()
81-
);
76+
$objects = [];
77+
foreach ($this->all() as $i => $factory) {
78+
$objects[] = $factory->create(
79+
\is_callable($attributes) ? $attributes($i + 1) : $attributes
80+
);
81+
}
82+
83+
return $objects;
8284
}
8385

8486
/**

tests/Functional/ModelFactoryTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,22 @@ static function(): \Generator {
330330
];
331331
}
332332

333+
/**
334+
* @test
335+
*/
336+
public function can_create_many_objects_with_index(): void
337+
{
338+
$categoryFactoryClass = $this->categoryFactoryClass();
339+
$categoryFactoryClass::createMany(2, static function(int $i) {
340+
return [
341+
'name' => "foo {$i}",
342+
];
343+
});
344+
345+
$categoryFactoryClass::assert()->exists(['name' => 'foo 1']);
346+
$categoryFactoryClass::assert()->exists(['name' => 'foo 2']);
347+
}
348+
333349
/**
334350
* @test
335351
* @dataProvider factoryCollectionAsDataProvider

tests/Unit/FactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class FactoryTest extends TestCase
2727
public function can_instantiate_object(): void
2828
{
2929
$attributeArray = ['title' => 'title', 'body' => 'body'];
30-
$attributeCallback = static function(Faker\Generator $faker) {
30+
$attributeCallback = static function() {
3131
return ['title' => 'title', 'body' => 'body'];
3232
};
3333

@@ -46,7 +46,7 @@ public function can_instantiate_object(): void
4646
public function can_instantiate_many_objects_legacy(): void
4747
{
4848
$attributeArray = ['title' => 'title', 'body' => 'body'];
49-
$attributeCallback = static function(Faker\Generator $faker) {
49+
$attributeCallback = static function() {
5050
return ['title' => 'title', 'body' => 'body'];
5151
};
5252

0 commit comments

Comments
 (0)