Skip to content

Commit

Permalink
Merge branch '2.4-develop' into asi-1792
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaschenko authored Sep 9, 2020
2 parents 86cc39c + 20b7e0d commit b21a245
Show file tree
Hide file tree
Showing 36 changed files with 680 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ public function startTest(TestCase $test)
{
Bootstrap::getInstance()->reinitialize();
/** Apply method level fixtures if thy are available, apply class level fixtures otherwise */
$this->_applyFixtures($this->_getFixtures($test, 'method') ?: $this->_getFixtures($test, 'class'));
$this->_applyFixtures(
$this->_getFixtures($test, 'method') ?: $this->_getFixtures($test, 'class'),
$test
);
}

/**
* Handler for 'endTest' event
*
* @param TestCase $test
*/
public function endTest()
public function endTest(TestCase $test)
{
$this->_revertFixtures();
$this->_revertFixtures($test);
$objectManager = Bootstrap::getObjectManager();
$objectManager->get(AttributeMetadataCache::class)->clean();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class CategoryRepositoryTest extends WebapiAbstract
*/
private $adminTokens;

/**
* @var string[]
*/
private $createdCategories;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -132,8 +137,7 @@ public function testCreate()
sprintf('"%s" field value is invalid', $fieldName)
);
}
// delete category to clean up auto-generated url rewrites
$this->deleteCategory($result['id']);
$this->createdCategories = [$result['id']];
}

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

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

protected function getSimpleCategoryData($categoryData = [])
Expand Down Expand Up @@ -476,5 +478,23 @@ public function testSaveDesign(): void
}
//We don't have permissions to do that.
$this->assertEquals('Not allowed to edit the category\'s design attributes', $exceptionMessage);
$this->createdCategories = [$result['id']];
}

/**
* @inheritDoc
*
* @return void
*/
protected function tearDown(): void
{
if (!empty($this->createdCategories)) {
// delete category to clean up auto-generated url rewrites
foreach ($this->createdCategories as $categoryId) {
$this->deleteCategory($categoryId);
}
}

parent::tearDown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Magento\TestFramework\Annotation;

use Magento\Framework\Component\ComponentRegistrarInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -106,10 +104,18 @@ protected function _applyOneFixture($fixture)
* Execute fixture scripts if any
*
* @param array $fixtures
* @param TestCase $test
* @return void
*/
protected function _applyFixtures(array $fixtures)
protected function _applyFixtures(array $fixtures, TestCase $test)
{
/** @var \Magento\TestFramework\Annotation\TestsIsolation $testsIsolation */
$testsIsolation = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\TestFramework\Annotation\TestsIsolation::class
);
$dbIsolationState = $this->getDbIsolationState($test);
$testsIsolation->createDbSnapshot($test, $dbIsolationState);

/* Execute fixture scripts */
foreach ($fixtures as $oneFixture) {
$this->_applyOneFixture($oneFixture);
Expand All @@ -122,9 +128,10 @@ protected function _applyFixtures(array $fixtures)
/**
* Revert changes done by fixtures
*
* @param TestCase|null $test
* @return void
*/
protected function _revertFixtures()
protected function _revertFixtures(?TestCase $test = null)
{
$resolver = Resolver::getInstance();
$resolver->setCurrentFixtureType($this->getAnnotation());
Expand All @@ -149,13 +156,22 @@ protected function _revertFixtures()
}
$this->_appliedFixtures = [];
$resolver->setCurrentFixtureType(null);

if (null !== $test) {
/** @var \Magento\TestFramework\Annotation\TestsIsolation $testsIsolation */
$testsIsolation = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\TestFramework\Annotation\TestsIsolation::class
);
$dbIsolationState = $this->getDbIsolationState($test);
$testsIsolation->checkTestIsolation($test, $dbIsolationState);
}
}

/**
* Return is explicit set isolation state
*
* @param TestCase $test
* @return bool|null
* @return array|null
*/
protected function getDbIsolationState(TestCase $test)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function startTestTransactionRequest(TestCase $test, Transaction $param):
if ($this->getDbIsolationState($test) !== ['disabled']) {
$param->requestTransactionStart();
} else {
$this->_applyFixtures($fixtures);
$this->_applyFixtures($fixtures, $test);
}
}
}
Expand All @@ -51,7 +51,7 @@ public function endTestTransactionRequest(TestCase $test, Transaction $param): v
if ($this->getDbIsolationState($test) !== ['disabled']) {
$param->requestTransactionRollback();
} else {
$this->_revertFixtures();
$this->_revertFixtures($test);
}
}
}
Expand All @@ -64,12 +64,13 @@ public function endTestTransactionRequest(TestCase $test, Transaction $param): v
*/
public function startTransaction(TestCase $test): void
{
$this->_applyFixtures($this->_getFixtures($test));
$this->_applyFixtures($this->_getFixtures($test), $test);
}

/**
* Handler for 'rollbackTransaction' event
*
* @param TestCase $test
* @return void
*/
public function rollbackTransaction(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function startTest(TestCase $test)
{
$fixtures = $this->_getFixtures($test);
if ($fixtures) {
$this->_applyFixtures($fixtures);
$this->_applyFixtures($fixtures, $test);
}
}

Expand All @@ -37,7 +37,7 @@ public function endTest(TestCase $test)
{
/* Isolate other tests from test-specific fixtures */
if ($this->_appliedFixtures && $this->_getFixtures($test)) {
$this->_revertFixtures();
$this->_revertFixtures($test);
}
}

Expand Down
Loading

0 comments on commit b21a245

Please sign in to comment.