Skip to content

Commit 95b0396

Browse files
authored
Merge pull request #7721 from magento-l3/ACP2E-782-2.4-develop
ACP2E-782: Introduce an alternative way to define fixtures using PHP8 Attributes
2 parents db4f897 + 735c9d9 commit 95b0396

File tree

106 files changed

+3333
-1253
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+3333
-1253
lines changed

app/code/Magento/CatalogInventory/Test/Fixture/SourceItem.php

Lines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Indexer\Test\Fixture;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\DataObjectFactory;
12+
use Magento\Framework\Indexer\IndexerRegistry;
13+
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
14+
15+
class ScheduleMode implements RevertibleDataFixtureInterface
16+
{
17+
/**
18+
* @var IndexerRegistry
19+
*/
20+
private IndexerRegistry $indexerRegistry;
21+
22+
/**
23+
* @var DataObjectFactory
24+
*/
25+
private DataObjectFactory $dataObjectFactory;
26+
27+
/**
28+
* @param IndexerRegistry $indexerRegistry
29+
* @param DataObjectFactory $dataObjectFactory
30+
*/
31+
public function __construct(
32+
IndexerRegistry $indexerRegistry,
33+
DataObjectFactory $dataObjectFactory
34+
) {
35+
$this->indexerRegistry = $indexerRegistry;
36+
$this->dataObjectFactory = $dataObjectFactory;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
* @param array $data Parameters
42+
* <pre>
43+
* $data = [
44+
* 'indexer' => (string) Indexer code. Required.
45+
* ]
46+
* </pre>
47+
*/
48+
public function apply(array $data = []): ?DataObject
49+
{
50+
$this->indexerRegistry->get($data['indexer'])->setScheduled(true);
51+
52+
return $this->dataObjectFactory->create(['data' => $data]);
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
public function revert(DataObject $data): void
59+
{
60+
$this->indexerRegistry->get($data['indexer'])->setScheduled(false);
61+
}
62+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Tax\Test\Fixture;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Tax\Api\TaxClassManagementInterface;
12+
13+
class CustomerTaxClass extends TaxClass
14+
{
15+
private const DEFAULT_DATA = [
16+
'class_type' => TaxClassManagementInterface::TYPE_CUSTOMER,
17+
];
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function apply(array $data = []): ?DataObject
23+
{
24+
return parent::apply(array_merge(self::DEFAULT_DATA, $data));
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Tax\Test\Fixture;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Tax\Api\TaxClassManagementInterface;
12+
13+
class ProductTaxClass extends TaxClass
14+
{
15+
private const DEFAULT_DATA = [
16+
'class_type' => TaxClassManagementInterface::TYPE_PRODUCT,
17+
];
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function apply(array $data = []): ?DataObject
23+
{
24+
return parent::apply(array_merge(self::DEFAULT_DATA, $data));
25+
}
26+
}

app/code/Magento/Tax/Test/Fixture/TaxClass.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Magento\Tax\Test\Fixture;
99

1010
use Magento\Framework\DataObject;
11-
use Magento\Framework\DataObjectFactory;
1211
use Magento\Tax\Api\TaxClassRepositoryInterface;
1312
use Magento\TestFramework\Fixture\Api\ServiceFactory;
1413
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
@@ -17,7 +16,7 @@ class TaxClass implements RevertibleDataFixtureInterface
1716
{
1817
private const DEFAULT_DATA = [
1918
'class_name' => '%uniqid%',
20-
'class_type' => '%uniqid%',
19+
'class_type' => null,
2120
];
2221

2322
/**
@@ -26,18 +25,20 @@ class TaxClass implements RevertibleDataFixtureInterface
2625
private ServiceFactory $serviceFactory;
2726

2827
/**
29-
* @var DataObjectFactory
28+
* @var TaxClassRepositoryInterface
3029
*/
31-
private DataObjectFactory $dataObjectFactory;
30+
private TaxClassRepositoryInterface $taxClassRepository;
3231

3332
/**
3433
* @param ServiceFactory $serviceFactory
35-
* @param DataObjectFactory $dataObjectFactory
34+
* @param TaxClassRepositoryInterface $taxClassRepository
3635
*/
37-
public function __construct(ServiceFactory $serviceFactory, DataObjectFactory $dataObjectFactory)
38-
{
36+
public function __construct(
37+
ServiceFactory $serviceFactory,
38+
TaxClassRepositoryInterface $taxClassRepository
39+
) {
3940
$this->serviceFactory = $serviceFactory;
40-
$this->dataObjectFactory = $dataObjectFactory;
41+
$this->taxClassRepository = $taxClassRepository;
4142
}
4243

4344
/**
@@ -46,10 +47,9 @@ public function __construct(ServiceFactory $serviceFactory, DataObjectFactory $d
4647
public function apply(array $data = []): ?DataObject
4748
{
4849
$service = $this->serviceFactory->create(TaxClassRepositoryInterface::class, 'save');
50+
$taxClassId = $service->execute(['taxClass' => array_merge(self::DEFAULT_DATA, $data)]);
4951

50-
return $this->dataObjectFactory->create()->addData([
51-
'id' => $service->execute(['taxClass' => array_merge(self::DEFAULT_DATA, $data)]),
52-
]);
52+
return $this->taxClassRepository->get($taxClassId);
5353
}
5454

5555
/**

app/code/Magento/Tax/Test/Fixture/TaxRate.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
class TaxRate implements RevertibleDataFixtureInterface
1616
{
1717
private const DEFAULT_DATA = [
18-
'code' => '%uniqid%',
19-
'rate' => '%uniqid%',
20-
'tax_country_id' => '%uniqid%',
18+
'code' => 'taxrate%uniqid%',
19+
'rate' => 10,
20+
'tax_country_id' => 'US',
21+
'tax_region_id' => 0,
22+
'region_name' => null,
23+
'tax_postcode' => '*',
24+
'zip_is_range' => null,
25+
'zip_from' => null,
26+
'zip_to' => null,
27+
'titles' => [],
2128
];
2229

2330
/**

app/code/Magento/Tax/Test/Fixture/TaxRule.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
class TaxRule implements RevertibleDataFixtureInterface
1616
{
1717
private const DEFAULT_DATA = [
18-
'code' => '%uniqid%',
19-
'customer_tax_class_ids' => '%uniqid%',
18+
'code' => 'taxrule%uniqid%',
2019
'position' => '0',
2120
'priority' => '0',
22-
'product_tax_class_ids' => ['%uniqid%'],
23-
'tax_rate_ids' => ['%uniqid%'],
21+
'calculate_subtotal' => false,
22+
'customer_tax_class_ids' => [],
23+
'product_tax_class_ids' => [],
24+
'tax_rate_ids' => [],
2425
];
2526

2627
/**

dev/tests/api-functional/framework/Magento/TestFramework/Annotation/ApiConfigFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected function setStoreConfigValue(array $matches, $configPathAndValue): voi
5252
protected function setGlobalConfigValue($configPathAndValue): void
5353
{
5454
[$configPath, $requiredValue] = preg_split('/\s+/', $configPathAndValue, 2);
55+
$configPath = str_starts_with($configPath, 'default/') ? substr($configPath, 8) : $configPath;
5556
/** @var ConfigStorage $configStorage */
5657
$configStorage = Bootstrap::getObjectManager()->get(ConfigStorage::class);
5758
if (!$configStorage->checkIsRecordExist($configPath)) {

dev/tests/api-functional/framework/Magento/TestFramework/Annotation/ApiDataFixture.php

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
<?php
22
/**
3-
* Implementation of the magentoApiDataFixture DocBlock annotation.
4-
*
5-
* The difference of magentoApiDataFixture from magentoDataFixture is
6-
* that no transactions should be used for API data fixtures.
7-
* Otherwise fixture data will not be accessible to Web API functional tests.
8-
*
93
* Copyright © Magento, Inc. All rights reserved.
104
* See COPYING.txt for license details.
115
*/
6+
declare(strict_types=1);
127

138
namespace Magento\TestFramework\Annotation;
149

1510
use Magento\Customer\Model\Metadata\AttributeMetadataCache;
11+
use Magento\TestFramework\Event\Param\Transaction;
1612
use Magento\TestFramework\Helper\Bootstrap;
1713
use PHPUnit\Framework\TestCase;
1814

@@ -31,11 +27,7 @@ class ApiDataFixture extends DataFixture
3127
public function startTest(TestCase $test)
3228
{
3329
Bootstrap::getInstance()->reinitialize();
34-
/** Apply method level fixtures if thy are available, apply class level fixtures otherwise */
35-
$this->_applyFixtures(
36-
$this->_getFixtures($test, 'method') ?: $this->_getFixtures($test, 'class'),
37-
$test
38-
);
30+
$this->_applyFixtures($this->_getFixtures($test), $test);
3931
}
4032

4133
/**
@@ -57,4 +49,48 @@ protected function getAnnotation(): string
5749
{
5850
return self::ANNOTATION;
5951
}
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
protected function getDbIsolationState(TestCase $test)
57+
{
58+
return parent::getDbIsolationState($test) ?: ['disabled'];
59+
}
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
public function startTestTransactionRequest(TestCase $test, Transaction $param): void
65+
{
66+
Bootstrap::getInstance()->reinitialize();
67+
parent::startTestTransactionRequest($test, $param);
68+
}
69+
70+
/**
71+
* @inheritdoc
72+
*/
73+
public function endTestTransactionRequest(TestCase $test, Transaction $param): void
74+
{
75+
parent::endTestTransactionRequest($test, $param);
76+
$objectManager = Bootstrap::getObjectManager();
77+
$objectManager->get(AttributeMetadataCache::class)->clean();
78+
}
79+
80+
/**
81+
* @inheritdoc
82+
*/
83+
protected function getParsers(): array
84+
{
85+
$parsers = [];
86+
// Add magentoDataFixture annotations
87+
$parsers[] = Bootstrap::getObjectManager()->create(
88+
\Magento\TestFramework\Annotation\Parser\DataFixture::class,
89+
['annotation' => DataFixture::ANNOTATION]
90+
);
91+
return array_merge(
92+
parent::getParsers(),
93+
$parsers
94+
);
95+
}
6096
}

0 commit comments

Comments
 (0)