Skip to content

Commit

Permalink
fix(2.x): allow sequence with associative arrays (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored May 8, 2024
1 parent d5d83dd commit 0479ddf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/FactoryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public function all(): array
{
$factories = [];

foreach (($this->items)() as $i => $attributes) {
$factories[] = $this->factory->with($attributes)->with(['__index' => $i + 1]);
$i = 1;
foreach (($this->items)() as $attributes) {
$factories[] = $this->factory->with($attributes)->with(['__index' => $i++]);
}

return $factories;
Expand Down
25 changes: 24 additions & 1 deletion tests/Unit/ObjectFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public static function sequenceDataProvider(): iterable
],
];

yield 'sequence as iterable which returns array' => [
yield 'sequence as callable which returns array' => [
static fn() => array_map(
static fn(int $i) => ['prop1' => "foo{$i}", 'prop2' => "bar{$i}"],
range(1, 2)
Expand All @@ -389,6 +389,29 @@ static function () {
];
}

/**
* @test
*/
public function can_use_sequence_with_associative_array(): void
{
self::assertEquals(
[
new Object1('foo1', 'bar1'),
new Object1('foo2', 'bar2'),
],
Object1Factory::createSequence([
'object 1' => [
'prop1' => 'foo1',
'prop2' => 'bar1',
],
'object 2' => [
'prop1' => 'foo2',
'prop2' => 'bar2',
],
]),
);
}

/**
* @test
*/
Expand Down

0 comments on commit 0479ddf

Please sign in to comment.