Skip to content

Commit b16b20a

Browse files
committed
ACP2E-1133: Add ability to generate multiple instances of a data fixture using an optional "count" parameter
1 parent f395dff commit b16b20a

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

dev/tests/integration/framework/Magento/TestFramework/Fixture/DataFixture.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
class DataFixture
1414
{
1515
/**
16-
* @param string $type
17-
* @param array $data
18-
* @param string|null $as
16+
* @param string $type Fixture class name
17+
* @param array $data Data passed on to the fixture.
18+
* @param string|null $as Fixture identifier used to retrieve the data returned by the fixture
19+
* @param int $count Number of instances to generate
1920
*/
2021
public function __construct(
2122
public string $type,
2223
public array $data = [],
23-
public ?string $as = null
24+
public ?string $as = null,
25+
public int $count = 1
2426
) {
2527
}
2628
}

dev/tests/integration/framework/Magento/TestFramework/Fixture/Parser/DataFixture.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public function __construct(
3535
*/
3636
public function parse(TestCase $test, string $scope): array
3737
{
38-
$fixtures = [];
3938
try {
4039
$reflection = $scope === ParserInterface::SCOPE_CLASS
4140
? new \ReflectionClass($test)
@@ -50,14 +49,21 @@ public function parse(TestCase $test, string $scope): array
5049
);
5150
}
5251

52+
$fixtures = [];
5353
$attributes = $reflection->getAttributes($this->attributeClass);
5454
foreach ($attributes as $attribute) {
5555
$args = $attribute->getArguments();
56-
$fixtures[] = [
57-
'name' => $args['as'] ?? $args[2] ?? null,
58-
'factory' => $args[0],
59-
'data' => $args[1] ?? [],
60-
];
56+
$alias = $args['as'] ?? $args[2] ?? null;
57+
$count = $args['count'] ?? $args[3] ?? 1;
58+
$id = $count > 1 ? 1 : '';
59+
do {
60+
$fixtures[] = [
61+
'name' => $alias !== null ? ($alias.($id++)) : null,
62+
'factory' => $args[0],
63+
'data' => $args[1] ?? []
64+
];
65+
} while (--$count > 0);
66+
6167
}
6268
return $fixtures;
6369
}

dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/DataFixtureTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,42 @@ public function testVariables(): void
484484
$this->revertFixtures();
485485
}
486486

487+
#[
488+
DbIsolation(false),
489+
DataFixture('MockFixture1', ['p1' => 'param-value1'], 'fixture1', count: 2),
490+
DataFixture('MockFixture2', ['p2' => '$fixture12.attr_1$'], 'fixture2'),
491+
DataFixture('MockFixture3', ['p3' => '$fixture2.attr_3$', 'p4' => ['p5' => '$fixture11$']], 'fixture3'),
492+
]
493+
public function testCount(): void
494+
{
495+
$fixture11 = new DataObject(['attr_1' => 'attr-value11', 'attr_2' => 'attr-value21']);
496+
$fixture12 = new DataObject(['attr_1' => 'attr-value12', 'attr_2' => 'attr-value22']);
497+
$fixture2 = new DataObject(['attr_3' => 1]);
498+
$this->fixture1->expects($this->exactly(2))
499+
->method('apply')
500+
->with(['p1' => 'param-value1'])
501+
->willReturnOnConsecutiveCalls($fixture11, $fixture12);
502+
$this->fixture2->expects($this->once())
503+
->method('apply')
504+
->with(['p2' => 'attr-value12'])
505+
->willReturn($fixture2);
506+
$this->fixture3->expects($this->once())
507+
->method('apply')
508+
->with(['p3' => 1, 'p4' => ['p5' => $fixture11]]);
509+
$this->applyFixtures();
510+
$this->assertSame($fixture11, $this->fixtureStorage->get('fixture11'));
511+
$this->assertSame($fixture12, $this->fixtureStorage->get('fixture12'));
512+
$this->assertSame($fixture2, $this->fixtureStorage->get('fixture2'));
513+
$this->assertNull($this->fixtureStorage->get('fixture3'));
514+
$this->fixture1->expects($this->exactly(2))
515+
->method('revert')
516+
->withConsecutive([$fixture12], [$fixture11]);
517+
$this->fixture2->expects($this->once())
518+
->method('revert')
519+
->with($fixture2);
520+
$this->revertFixtures();
521+
}
522+
487523
/**
488524
* @throws ReflectionException
489525
*/

0 commit comments

Comments
 (0)