Skip to content

Commit a9df14e

Browse files
committed
ACP2E-782: Create PHP Attribute that must be used instead of magentoDataFixture for executing parameterized fixtures in the test
1 parent c0e199b commit a9df14e

File tree

29 files changed

+555
-96
lines changed

29 files changed

+555
-96
lines changed
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/integration/framework/Magento/TestFramework/Annotation/AbstractDataFixture.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ protected function getParsers(): array
265265
*/
266266
protected function getDbIsolationState(TestCase $test)
267267
{
268-
$annotations = $this->getAnnotations($test);
269-
return $annotations[DbIsolation::MAGENTO_DB_ISOLATION] ?? null;
268+
return Bootstrap::getObjectManager()->get(DbIsolationState::class)->getState($test) ?: null;
270269
}
271270

272271
/**

dev/tests/integration/framework/Magento/TestFramework/Annotation/AppArea.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
*/
66
namespace Magento\TestFramework\Annotation;
77

8-
use Magento\TestFramework\Annotation\TestCaseAnnotation;
8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\TestFramework\Application;
10+
use Magento\TestFramework\Fixture\ParserInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use PHPUnit\Framework\TestCase;
913

1014
class AppArea
1115
{
12-
const ANNOTATION_NAME = 'magentoAppArea';
16+
public const ANNOTATION_NAME = 'magentoAppArea';
1317

1418
/**
15-
* @var \Magento\TestFramework\Application
19+
* @var Application
1620
*/
1721
private $_application;
1822

@@ -32,9 +36,9 @@ class AppArea
3236
];
3337

3438
/**
35-
* @param \Magento\TestFramework\Application $application
39+
* @param Application $application
3640
*/
37-
public function __construct(\Magento\TestFramework\Application $application)
41+
public function __construct(Application $application)
3842
{
3943
$this->_application = $application;
4044
}
@@ -44,7 +48,7 @@ public function __construct(\Magento\TestFramework\Application $application)
4448
*
4549
* @param array $annotations
4650
* @return string
47-
* @throws \Magento\Framework\Exception\LocalizedException
51+
* @throws LocalizedException
4852
*/
4953
protected function _getTestAppArea($annotations)
5054
{
@@ -73,12 +77,28 @@ protected function _getTestAppArea($annotations)
7377
/**
7478
* Start test case event observer
7579
*
76-
* @param \PHPUnit\Framework\TestCase $test
80+
* @param TestCase $test
7781
*/
78-
public function startTest(\PHPUnit\Framework\TestCase $test)
82+
public function startTest(TestCase $test)
7983
{
8084
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
81-
$area = $this->_getTestAppArea($annotations);
85+
$parser = Bootstrap::getObjectManager()->create(\Magento\TestFramework\Fixture\Parser\AppArea::class);
86+
$converter = static fn ($info) => $info['area'];
87+
$classAppIsolationState = array_map($converter, $parser->parse($test, ParserInterface::SCOPE_CLASS))
88+
?: ($annotations['class'][self::ANNOTATION_NAME] ?? []);
89+
$methodAppIsolationState = array_map($converter, $parser->parse($test, ParserInterface::SCOPE_METHOD))
90+
?: ($annotations['method'][self::ANNOTATION_NAME] ?? []);
91+
$area = current($methodAppIsolationState ?: $classAppIsolationState) ?: Application::DEFAULT_APP_AREA;
92+
93+
if (!in_array($area, $this->_allowedAreas, true)) {
94+
throw new LocalizedException(
95+
__(
96+
'Invalid "@magentoAppArea" annotation, can be "%1" only.',
97+
implode('", "', $this->_allowedAreas)
98+
)
99+
);
100+
}
101+
82102
if ($this->_application->getArea() !== $area) {
83103
$this->_application->reinitialize();
84104

dev/tests/integration/framework/Magento/TestFramework/Annotation/AppIsolation.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
namespace Magento\TestFramework\Annotation;
1111

1212
use Magento\Framework\Exception\LocalizedException;
13-
use Magento\TestFramework\Annotation\TestCaseAnnotation;
1413
use Magento\TestFramework\Application;
14+
use Magento\TestFramework\Fixture\ParserInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
1516
use Magento\TestFramework\TestCase\AbstractController;
1617
use PHPUnit\Framework\TestCase;
1718

1819
class AppIsolation
1920
{
21+
private const ANNOTATION = 'magentoAppIsolation';
22+
2023
/**
2124
* Flag to prevent an excessive test case isolation if the last test has been just isolated
2225
*
@@ -85,10 +88,15 @@ public function endTest(TestCase $test)
8588
{
8689
$this->hasNonIsolatedTests = true;
8790

88-
/* Determine an isolation from doc comment */
89-
$annotations = $this->getAnnotations($test);
90-
if (isset($annotations['magentoAppIsolation'])) {
91-
$isolation = $annotations['magentoAppIsolation'];
91+
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
92+
$parser = Bootstrap::getObjectManager()->create(\Magento\TestFramework\Fixture\Parser\AppIsolation::class);
93+
$converter = static fn ($stateInfo) => $stateInfo['enabled'] ? 'enabled' : 'disabled';
94+
$classAppIsolationState = array_map($converter, $parser->parse($test, ParserInterface::SCOPE_CLASS))
95+
?: ($annotations['class'][self::ANNOTATION] ?? []);
96+
$methodAppIsolationState = array_map($converter, $parser->parse($test, ParserInterface::SCOPE_METHOD))
97+
?: ($annotations['method'][self::ANNOTATION] ?? []);
98+
$isolation = $methodAppIsolationState ?: $classAppIsolationState;
99+
if ($isolation) {
92100
if ($isolation !== ['enabled'] && $isolation !== ['disabled']) {
93101
throw new LocalizedException(
94102
__('Invalid "@magentoAppIsolation" annotation, can be "enabled" or "disabled" only.')

dev/tests/integration/framework/Magento/TestFramework/Annotation/ConfigFixture.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ private function getConfigFixturesFromAttribute(TestCase $test): array
318318
'%s%s %s',
319319
$data['scopeType'] === ScopeConfigInterface::SCOPE_TYPE_DEFAULT
320320
? 'default/'
321-
: ($data['scopeValue'] ?? 'current') . '_' . $data['scopeType'],
321+
: ($data['scopeValue'] ?? 'current') . '_' . $data['scopeType'] . ' ',
322322
$data['path'],
323323
$data['value'],
324324
);

dev/tests/integration/framework/Magento/TestFramework/Annotation/DbIsolation.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
namespace Magento\TestFramework\Annotation;
77

88
use Magento\Framework\Exception\LocalizedException;
9-
use Magento\TestFramework\Annotation\TestCaseAnnotation;
109
use Magento\TestFramework\Event\Param\Transaction;
10+
use Magento\TestFramework\Helper\Bootstrap;
1111
use PHPUnit\Framework\TestCase;
1212

1313
/**
@@ -86,9 +86,8 @@ public function rollbackTransaction()
8686
*/
8787
protected function _getIsolation(TestCase $test)
8888
{
89-
$annotations = $this->getAnnotations($test);
90-
if (isset($annotations[self::MAGENTO_DB_ISOLATION])) {
91-
$isolation = $annotations[self::MAGENTO_DB_ISOLATION];
89+
$isolation = Bootstrap::getObjectManager()->get(DbIsolationState::class)->getState($test);
90+
if ($isolation) {
9291
if ($isolation !== ['enabled'] && $isolation !== ['disabled']) {
9392
throw new LocalizedException(
9493
__('Invalid "@magentoDbIsolation" annotation, can be "enabled" or "disabled" only.')
@@ -98,19 +97,4 @@ protected function _getIsolation(TestCase $test)
9897
}
9998
return null;
10099
}
101-
102-
/**
103-
* Get method annotations.
104-
*
105-
* Overwrites class-defined annotations.
106-
*
107-
* @param TestCase $test
108-
* @return array
109-
*/
110-
private function getAnnotations(TestCase $test)
111-
{
112-
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
113-
114-
return array_replace((array)$annotations['class'], (array)$annotations['method']);
115-
}
116100
}

0 commit comments

Comments
 (0)