Skip to content

Commit c48ef25

Browse files
committed
ACP2E-782: Introduce an alternative way to define fixtures using PHP8 Attributes
1 parent 762d075 commit c48ef25

File tree

7 files changed

+64
-15
lines changed

7 files changed

+64
-15
lines changed

dev/tests/integration/framework/Magento/TestFramework/Annotation/Parser/Composite.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class Composite implements ParserInterface
2323
/**
2424
* @var string
2525
*/
26-
private string $strategy;
26+
private int $strategy;
2727

2828
/**
2929
* @param ParserInterface[] $parsers
30-
* @param string $strategy
30+
* @param int $strategy
3131
*/
3232
public function __construct(
3333
array $parsers,
34-
string $strategy = self::STRATEGY_MERGE
34+
int $strategy = self::STRATEGY_MERGE
3535
) {
3636
$this->parsers = $parsers;
3737
$this->strategy = $strategy;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
declare(strict_types=1);
77

88
namespace Magento\TestFramework\Fixture;
9+
910
use Attribute;
1011

1112
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\App\Area;
1010
use Magento\Framework\ObjectManagerInterface;
1111
use Magento\TestFramework\Annotation\TestCaseAnnotation;
12+
use Magento\TestFramework\Fixture\Parser\AppArea;
1213
use Magento\TestFramework\Helper\Bootstrap;
1314
use PHPUnit\Framework\MockObject\MockObject;
1415
use ReflectionProperty;
@@ -46,10 +47,19 @@ protected function setUp(): void
4647
->disableOriginalConstructor()
4748
->getMockForAbstractClass();
4849

50+
$sharedInstances = [
51+
AppArea::class => $this->createConfiguredMock(AppArea::class, ['parse' => []])
52+
];
53+
$objectManager->method('get')
54+
->willReturnCallback(
55+
function (string $type) use ($sharedInstances) {
56+
return $sharedInstances[$type] ?? new $type();
57+
}
58+
);
4959
$objectManager->method('create')
5060
->willReturnCallback(
51-
function (string $type) {
52-
return $this->createMock($type);
61+
function (string $type, array $arguments = []){
62+
return new $type(...array_values($arguments));
5363
}
5464
);
5565

@@ -80,7 +90,7 @@ public function testGetTestAppArea($annotations, $expectedArea)
8090
$property = new ReflectionProperty(TestCaseAnnotation::class, 'instance');
8191
$property->setAccessible(true);
8292
$property->setValue($this->testCaseAnnotationsMock);
83-
$this->testCaseAnnotationsMock->expects($this->once())->method('getAnnotations')->willReturn($annotations);
93+
$this->testCaseAnnotationsMock->method('getAnnotations')->willReturn($annotations);
8494
$this->_applicationMock->expects($this->any())->method('getArea')->willReturn(null);
8595
$this->_applicationMock->expects($this->once())->method('reinitialize');
8696
$this->_applicationMock->expects($this->once())->method('loadArea')->with($expectedArea);

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Magento\Test\Annotation;
1111

1212
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\TestFramework\Fixture\Parser\AppIsolation;
1314
use Magento\TestFramework\Helper\Bootstrap;
1415
use PHPUnit\Framework\MockObject\MockObject;
1516

@@ -33,10 +34,19 @@ protected function setUp(): void
3334
->disableOriginalConstructor()
3435
->getMockForAbstractClass();
3536

37+
$sharedInstances = [
38+
AppIsolation::class => $this->createConfiguredMock(AppIsolation::class, ['parse' => []])
39+
];
40+
$objectManager->method('get')
41+
->willReturnCallback(
42+
function (string $type) use ($sharedInstances) {
43+
return $sharedInstances[$type] ?? new $type();
44+
}
45+
);
3646
$objectManager->method('create')
3747
->willReturnCallback(
38-
function (string $type) {
39-
return $this->createMock($type);
48+
function (string $type, array $arguments = []){
49+
return new $type(...array_values($arguments));
4050
}
4151
);
4252
Bootstrap::setObjectManager($objectManager);

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
namespace Magento\Test\Annotation;
88

99
use Magento\Framework\Component\ComponentRegistrar;
10+
use Magento\Framework\ObjectManagerInterface;
1011
use Magento\TestFramework\Annotation\ComponentRegistrarFixture;
12+
use Magento\TestFramework\Fixture\Parser\ComponentsDir;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\MockObject\MockObject;
1115

1216
class ComponentRegistrarFixtureTest extends \PHPUnit\Framework\TestCase
1317
{
@@ -23,6 +27,28 @@ class ComponentRegistrarFixtureTest extends \PHPUnit\Framework\TestCase
2327

2428
protected function setUp(): void
2529
{
30+
/** @var ObjectManagerInterface|MockObject $objectManager */
31+
$objectManager = $this->getMockBuilder(ObjectManagerInterface::class)
32+
->onlyMethods(['get', 'create'])
33+
->disableOriginalConstructor()
34+
->getMockForAbstractClass();
35+
36+
$sharedInstances = [
37+
ComponentsDir::class => $this->createConfiguredMock(ComponentsDir::class, ['parse' => []])
38+
];
39+
$objectManager->method('get')
40+
->willReturnCallback(
41+
function (string $type) use ($sharedInstances) {
42+
return $sharedInstances[$type] ?? new $type();
43+
}
44+
);
45+
$objectManager->method('create')
46+
->willReturnCallback(
47+
function (string $type, array $arguments = []){
48+
return new $type(...array_values($arguments));
49+
}
50+
);
51+
Bootstrap::setObjectManager($objectManager);
2652
$this->componentRegistrar = new ComponentRegistrar();
2753
}
2854

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Framework\DataObject;
1212
use Magento\Framework\ObjectManagerInterface;
1313
use Magento\Framework\Registry;
14-
use Magento\Framework\Serialize\Serializer\Json;
1514
use Magento\TestFramework\Annotation\DataFixture as DataFixtureAnnotation;
1615
use Magento\TestFramework\Annotation\DataFixtureDataProvider;
1716
use Magento\TestFramework\Annotation\DataFixtureSetup;
@@ -106,7 +105,6 @@ protected function setUp(): void
106105
\Magento\TestFramework\Annotation\Parser\DataFixture::class => $annotationParser,
107106
DataFixtureFactory::class => $dataFixtureFactory,
108107
DataFixtureSetup::class => new DataFixtureSetup(new Registry(), $dataFixtureFactory),
109-
DataFixtureDataProvider::class => new DataFixtureDataProvider(new Json()),
110108
'MockFixture1' => $this->fixture1,
111109
'MockFixture2' => $this->fixture2,
112110
'MockFixture3' => $this->fixture3,

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Test\Annotation;
77

88
use Magento\Framework\ObjectManagerInterface;
9+
use Magento\TestFramework\Fixture\Parser\DbIsolation;
910
use Magento\TestFramework\Helper\Bootstrap;
1011
use PHPUnit\Framework\MockObject\MockObject;
1112

@@ -29,15 +30,18 @@ protected function setUp(): void
2930
->disableOriginalConstructor()
3031
->getMockForAbstractClass();
3132

32-
$objectManager->method('create')
33+
$sharedInstances = [
34+
DbIsolation::class => $this->createConfiguredMock(DbIsolation::class, ['parse' => []])
35+
];
36+
$objectManager->method('get')
3337
->willReturnCallback(
34-
function (string $type) {
35-
return $this->createMock($type);
38+
function (string $type) use ($sharedInstances) {
39+
return $sharedInstances[$type] ?? new $type();
3640
}
3741
);
38-
$objectManager->method('get')
42+
$objectManager->method('create')
3943
->willReturnCallback(
40-
function (string $type, array $arguments = []) {
44+
function (string $type, array $arguments = []){
4145
return new $type(...array_values($arguments));
4246
}
4347
);

0 commit comments

Comments
 (0)