Skip to content

Commit 20b7e0d

Browse files
authored
Merge pull request #6036 from magento-performance/develop-storefront-test-isolation
Tests Broken isolation
2 parents 1c9ba3e + c08faa7 commit 20b7e0d

File tree

36 files changed

+680
-53
lines changed

36 files changed

+680
-53
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,20 @@ public function startTest(TestCase $test)
3232
{
3333
Bootstrap::getInstance()->reinitialize();
3434
/** Apply method level fixtures if thy are available, apply class level fixtures otherwise */
35-
$this->_applyFixtures($this->_getFixtures($test, 'method') ?: $this->_getFixtures($test, 'class'));
35+
$this->_applyFixtures(
36+
$this->_getFixtures($test, 'method') ?: $this->_getFixtures($test, 'class'),
37+
$test
38+
);
3639
}
3740

3841
/**
3942
* Handler for 'endTest' event
43+
*
44+
* @param TestCase $test
4045
*/
41-
public function endTest()
46+
public function endTest(TestCase $test)
4247
{
43-
$this->_revertFixtures();
48+
$this->_revertFixtures($test);
4449
$objectManager = Bootstrap::getObjectManager();
4550
$objectManager->get(AttributeMetadataCache::class)->clean();
4651
}

dev/tests/api-functional/testsuite/Magento/Catalog/Api/CategoryRepositoryTest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class CategoryRepositoryTest extends WebapiAbstract
4343
*/
4444
private $adminTokens;
4545

46+
/**
47+
* @var string[]
48+
*/
49+
private $createdCategories;
50+
4651
/**
4752
* @inheritDoc
4853
*/
@@ -132,8 +137,7 @@ public function testCreate()
132137
sprintf('"%s" field value is invalid', $fieldName)
133138
);
134139
}
135-
// delete category to clean up auto-generated url rewrites
136-
$this->deleteCategory($result['id']);
140+
$this->createdCategories = [$result['id']];
137141
}
138142

139143
/**
@@ -214,8 +218,7 @@ public function testUpdate()
214218
$this->assertFalse((bool)$category->getIsActive(), 'Category "is_active" must equal to false');
215219
$this->assertEquals("Update Category Test", $category->getName());
216220
$this->assertEquals("Update Category Description Test", $category->getDescription());
217-
// delete category to clean up auto-generated url rewrites
218-
$this->deleteCategory($categoryId);
221+
$this->createdCategories = [$categoryId];
219222
}
220223

221224
/**
@@ -243,8 +246,7 @@ public function testUpdateWithDefaultSortByAttribute()
243246
$this->assertTrue((bool)$category->getIsActive(), 'Category "is_active" must equal to true');
244247
$this->assertEquals("Update Category Test With default_sort_by Attribute", $category->getName());
245248
$this->assertEquals("name", $category->getDefaultSortBy());
246-
// delete category to clean up auto-generated url rewrites
247-
$this->deleteCategory($categoryId);
249+
$this->createdCategories = [$categoryId];
248250
}
249251

250252
protected function getSimpleCategoryData($categoryData = [])
@@ -476,5 +478,23 @@ public function testSaveDesign(): void
476478
}
477479
//We don't have permissions to do that.
478480
$this->assertEquals('Not allowed to edit the category\'s design attributes', $exceptionMessage);
481+
$this->createdCategories = [$result['id']];
482+
}
483+
484+
/**
485+
* @inheritDoc
486+
*
487+
* @return void
488+
*/
489+
protected function tearDown(): void
490+
{
491+
if (!empty($this->createdCategories)) {
492+
// delete category to clean up auto-generated url rewrites
493+
foreach ($this->createdCategories as $categoryId) {
494+
$this->deleteCategory($categoryId);
495+
}
496+
}
497+
498+
parent::tearDown();
479499
}
480500
}

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace Magento\TestFramework\Annotation;
99

10-
use Magento\Framework\Component\ComponentRegistrarInterface;
11-
use Magento\Framework\Exception\LocalizedException;
1210
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
1311
use PHPUnit\Framework\Exception;
1412
use PHPUnit\Framework\TestCase;
@@ -106,10 +104,18 @@ protected function _applyOneFixture($fixture)
106104
* Execute fixture scripts if any
107105
*
108106
* @param array $fixtures
107+
* @param TestCase $test
109108
* @return void
110109
*/
111-
protected function _applyFixtures(array $fixtures)
110+
protected function _applyFixtures(array $fixtures, TestCase $test)
112111
{
112+
/** @var \Magento\TestFramework\Annotation\TestsIsolation $testsIsolation */
113+
$testsIsolation = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
114+
\Magento\TestFramework\Annotation\TestsIsolation::class
115+
);
116+
$dbIsolationState = $this->getDbIsolationState($test);
117+
$testsIsolation->createDbSnapshot($test, $dbIsolationState);
118+
113119
/* Execute fixture scripts */
114120
foreach ($fixtures as $oneFixture) {
115121
$this->_applyOneFixture($oneFixture);
@@ -122,9 +128,10 @@ protected function _applyFixtures(array $fixtures)
122128
/**
123129
* Revert changes done by fixtures
124130
*
131+
* @param TestCase|null $test
125132
* @return void
126133
*/
127-
protected function _revertFixtures()
134+
protected function _revertFixtures(?TestCase $test = null)
128135
{
129136
$resolver = Resolver::getInstance();
130137
$resolver->setCurrentFixtureType($this->getAnnotation());
@@ -149,13 +156,22 @@ protected function _revertFixtures()
149156
}
150157
$this->_appliedFixtures = [];
151158
$resolver->setCurrentFixtureType(null);
159+
160+
if (null !== $test) {
161+
/** @var \Magento\TestFramework\Annotation\TestsIsolation $testsIsolation */
162+
$testsIsolation = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
163+
\Magento\TestFramework\Annotation\TestsIsolation::class
164+
);
165+
$dbIsolationState = $this->getDbIsolationState($test);
166+
$testsIsolation->checkTestIsolation($test, $dbIsolationState);
167+
}
152168
}
153169

154170
/**
155171
* Return is explicit set isolation state
156172
*
157173
* @param TestCase $test
158-
* @return bool|null
174+
* @return array|null
159175
*/
160176
protected function getDbIsolationState(TestCase $test)
161177
{

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function startTestTransactionRequest(TestCase $test, Transaction $param):
3232
if ($this->getDbIsolationState($test) !== ['disabled']) {
3333
$param->requestTransactionStart();
3434
} else {
35-
$this->_applyFixtures($fixtures);
35+
$this->_applyFixtures($fixtures, $test);
3636
}
3737
}
3838
}
@@ -51,7 +51,7 @@ public function endTestTransactionRequest(TestCase $test, Transaction $param): v
5151
if ($this->getDbIsolationState($test) !== ['disabled']) {
5252
$param->requestTransactionRollback();
5353
} else {
54-
$this->_revertFixtures();
54+
$this->_revertFixtures($test);
5555
}
5656
}
5757
}
@@ -64,12 +64,13 @@ public function endTestTransactionRequest(TestCase $test, Transaction $param): v
6464
*/
6565
public function startTransaction(TestCase $test): void
6666
{
67-
$this->_applyFixtures($this->_getFixtures($test));
67+
$this->_applyFixtures($this->_getFixtures($test), $test);
6868
}
6969

7070
/**
7171
* Handler for 'rollbackTransaction' event
7272
*
73+
* @param TestCase $test
7374
* @return void
7475
*/
7576
public function rollbackTransaction(): void

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function startTest(TestCase $test)
2424
{
2525
$fixtures = $this->_getFixtures($test);
2626
if ($fixtures) {
27-
$this->_applyFixtures($fixtures);
27+
$this->_applyFixtures($fixtures, $test);
2828
}
2929
}
3030

@@ -37,7 +37,7 @@ public function endTest(TestCase $test)
3737
{
3838
/* Isolate other tests from test-specific fixtures */
3939
if ($this->_appliedFixtures && $this->_getFixtures($test)) {
40-
$this->_revertFixtures();
40+
$this->_revertFixtures($test);
4141
}
4242
}
4343

0 commit comments

Comments
 (0)