ltsecurityinc安防产品在线商城代码
This commit is contained in:
+234
@@ -0,0 +1,234 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
|
||||
/**
|
||||
* Class consist of dataFixtures base logic
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
abstract class AbstractDataFixture
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $_appliedFixtures = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $fixtures = [];
|
||||
|
||||
/**
|
||||
* Retrieve fixtures from annotation
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @param string|null $scope
|
||||
* @return array
|
||||
*/
|
||||
protected function _getFixtures(TestCase $test, $scope = null)
|
||||
{
|
||||
$annotationKey = $this->getAnnotation();
|
||||
|
||||
if (!empty($this->fixtures[$annotationKey][$this->getTestKey($test)])) {
|
||||
return $this->fixtures[$annotationKey][$this->getTestKey($test)];
|
||||
}
|
||||
|
||||
$resolver = Resolver::getInstance();
|
||||
$resolver->setCurrentFixtureType($annotationKey);
|
||||
$parsers = Bootstrap::getObjectManager()
|
||||
->create(
|
||||
\Magento\TestFramework\Annotation\Parser\Composite::class,
|
||||
[
|
||||
'parsers' => $this->getParsers()
|
||||
]
|
||||
);
|
||||
|
||||
$fixtures = [];
|
||||
try {
|
||||
$fixtures = $parsers->parse($test, $scope ?: ParserInterface::SCOPE_METHOD);
|
||||
if (!$fixtures && !$scope) {
|
||||
$fixtures = $parsers->parse($test, ParserInterface::SCOPE_CLASS);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
ExceptionHandler::handle(
|
||||
'Unable to parse fixtures',
|
||||
get_class($test),
|
||||
$test->getName(false),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
|
||||
/* Need to be applied even test does not have added fixtures because fixture can be added via config */
|
||||
$this->fixtures[$annotationKey][$this->getTestKey($test)] = $resolver->applyDataFixtures(
|
||||
$test,
|
||||
$fixtures,
|
||||
$annotationKey
|
||||
);
|
||||
|
||||
return $this->fixtures[$annotationKey][$this->getTestKey($test)] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get method annotations.
|
||||
*
|
||||
* Overwrites class-defined annotations.
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return array
|
||||
*/
|
||||
protected function getAnnotations(TestCase $test): array
|
||||
{
|
||||
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
|
||||
|
||||
return array_replace((array)$annotations['class'], (array)$annotations['method']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute fixture scripts if any
|
||||
*
|
||||
* @param array $fixtures
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
protected function _applyFixtures(array $fixtures, TestCase $test)
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$testsIsolation = $objectManager->get(TestsIsolation::class);
|
||||
$dbIsolationState = $this->getDbIsolationState($test);
|
||||
$testsIsolation->createDbSnapshot($test, $dbIsolationState);
|
||||
$dataFixtureSetup = $objectManager->get(DataFixtureSetup::class);
|
||||
/* Execute fixture scripts */
|
||||
foreach ($fixtures as $fixture) {
|
||||
if (is_callable([get_class($test), $fixture['factory']])) {
|
||||
$fixture['factory'] = get_class($test) . '::' . $fixture['factory'];
|
||||
}
|
||||
$fixture['test'] = [
|
||||
'class' => get_class($test),
|
||||
'method' => $test->getName(false),
|
||||
'dataSet' => $test->dataName(),
|
||||
];
|
||||
try {
|
||||
$fixture['result'] = $dataFixtureSetup->apply($fixture);
|
||||
} catch (\Throwable $exception) {
|
||||
ExceptionHandler::handle(
|
||||
'Unable to apply fixture: ' . $this->getFixtureReference($fixture),
|
||||
$fixture['test']['class'],
|
||||
$fixture['test']['method'],
|
||||
$exception
|
||||
);
|
||||
}
|
||||
$this->_appliedFixtures[] = $fixture;
|
||||
}
|
||||
$resolver = Resolver::getInstance();
|
||||
$resolver->setCurrentFixtureType(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert changes done by fixtures
|
||||
*
|
||||
* @param TestCase|null $test
|
||||
* @return void
|
||||
*/
|
||||
protected function _revertFixtures(?TestCase $test = null)
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$dataFixtureSetup = $objectManager->get(DataFixtureSetup::class);
|
||||
$resolver = Resolver::getInstance();
|
||||
$resolver->setCurrentFixtureType($this->getAnnotation());
|
||||
$appliedFixtures = array_reverse($this->_appliedFixtures);
|
||||
foreach ($appliedFixtures as $fixture) {
|
||||
try {
|
||||
$dataFixtureSetup->revert($fixture);
|
||||
} catch (\Throwable $exception) {
|
||||
ExceptionHandler::handle(
|
||||
'Unable to revert fixture: ' . $this->getFixtureReference($fixture),
|
||||
$fixture['test']['class'],
|
||||
$fixture['test']['method'],
|
||||
$exception
|
||||
);
|
||||
}
|
||||
}
|
||||
$this->_appliedFixtures = [];
|
||||
$resolver->setCurrentFixtureType(null);
|
||||
|
||||
if (null !== $test) {
|
||||
/** @var TestsIsolation $testsIsolation */
|
||||
$testsIsolation = $objectManager->get(TestsIsolation::class);
|
||||
$dbIsolationState = $this->getDbIsolationState($test);
|
||||
$testsIsolation->checkTestIsolation($test, $dbIsolationState);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get reference to the fixture definition
|
||||
*
|
||||
* @param array $fixture
|
||||
* @return string
|
||||
*/
|
||||
private function getFixtureReference(array $fixture): string
|
||||
{
|
||||
return sprintf(
|
||||
'%s%s',
|
||||
$fixture['factory'],
|
||||
$fixture['name'] ? ' (' . $fixture['name'] . ')' : '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return fixtures parser
|
||||
*
|
||||
* @return ParserInterface[]
|
||||
*/
|
||||
protected function getParsers(): array
|
||||
{
|
||||
$parsers = [];
|
||||
$parsers[] = Bootstrap::getObjectManager()->create(
|
||||
\Magento\TestFramework\Annotation\Parser\DataFixture::class,
|
||||
['annotation' => $this->getAnnotation()]
|
||||
);
|
||||
return $parsers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return is explicit set isolation state
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getDbIsolationState(TestCase $test)
|
||||
{
|
||||
$isEnabled = Bootstrap::getObjectManager()->get(DbIsolationState::class)->isEnabled($test);
|
||||
return $isEnabled === null ? null : [$isEnabled ? 'enabled' : 'disabled'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get uniq test cache key
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return string
|
||||
*/
|
||||
private function getTestKey(TestCase $test): string
|
||||
{
|
||||
return sprintf('%s::%s', get_class($test), $test->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get annotation name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function getAnnotation(): string;
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implementation of the @magentoAdminConfigFixture DocBlock annotation
|
||||
*/
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\App\Config\MutableScopeConfigInterface;
|
||||
use Magento\TestFramework\Annotation\TestCaseAnnotation;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Handler for applying magentoAdminConfigFixture annotation
|
||||
*/
|
||||
class AdminConfigFixture
|
||||
{
|
||||
public const ANNOTATION = 'magentoAdminConfigFixture';
|
||||
|
||||
/**
|
||||
* The test instance that is available between 'startTest' and 'stopTest' events.
|
||||
*
|
||||
* @var TestCase
|
||||
*/
|
||||
protected $_currentTest;
|
||||
|
||||
/**
|
||||
* Original values for global configuration options that need to be restored
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_configValues = [];
|
||||
|
||||
/**
|
||||
* Retrieve configuration node value
|
||||
*
|
||||
* @param string $configPath
|
||||
* @return string
|
||||
*/
|
||||
protected function _getConfigValue($configPath)
|
||||
{
|
||||
return Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class)->getValue($configPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign configuration node value
|
||||
*
|
||||
* @param string $configPath
|
||||
* @param string $value
|
||||
* @return void
|
||||
*/
|
||||
protected function _setConfigValue($configPath, $value)
|
||||
{
|
||||
Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class)->setValue($configPath, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign required config values and save original ones
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
protected function _assignConfigData(TestCase $test)
|
||||
{
|
||||
$resolver = Resolver::getInstance();
|
||||
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
|
||||
$existingFixtures = $annotations['method'][self::ANNOTATION] ?? [];
|
||||
/* Need to be applied even test does not have added fixtures because fixture can be added via config */
|
||||
$testAnnotations = $resolver->applyConfigFixtures($test, $existingFixtures, self::ANNOTATION);
|
||||
foreach ($testAnnotations as $configPathAndValue) {
|
||||
list($configPath, $requiredValue) = preg_split('/\s+/', $configPathAndValue, 2);
|
||||
|
||||
$originalValue = $this->_getConfigValue($configPath);
|
||||
$this->_configValues[$configPath] = $originalValue;
|
||||
|
||||
$this->_setConfigValue($configPath, $requiredValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore original values for changed config options
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _restoreConfigData()
|
||||
{
|
||||
foreach ($this->_configValues as $configPath => $originalValue) {
|
||||
$this->_setConfigValue($configPath, $originalValue);
|
||||
}
|
||||
$this->_configValues = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'startTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
public function startTest(TestCase $test)
|
||||
{
|
||||
$this->_currentTest = $test;
|
||||
$this->_assignConfigData($test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function endTest(TestCase $test)
|
||||
{
|
||||
$this->_currentTest = null;
|
||||
$this->_restoreConfigData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reassign configuration data whenever application is reset
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initStoreAfter()
|
||||
{
|
||||
/* process events triggered from within a test only */
|
||||
if ($this->_currentTest) {
|
||||
$this->_assignConfigData($this->_currentTest);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Application;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AppArea
|
||||
{
|
||||
public const ANNOTATION_NAME = 'magentoAppArea';
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
private $_application;
|
||||
|
||||
/**
|
||||
* List of allowed areas.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_allowedAreas = [
|
||||
\Magento\Framework\App\Area::AREA_GLOBAL,
|
||||
\Magento\Framework\App\Area::AREA_ADMINHTML,
|
||||
\Magento\Framework\App\Area::AREA_FRONTEND,
|
||||
\Magento\Framework\App\Area::AREA_WEBAPI_REST,
|
||||
\Magento\Framework\App\Area::AREA_WEBAPI_SOAP,
|
||||
\Magento\Framework\App\Area::AREA_CRONTAB,
|
||||
\Magento\Framework\App\Area::AREA_GRAPHQL
|
||||
];
|
||||
|
||||
/**
|
||||
* @param Application $application
|
||||
*/
|
||||
public function __construct(Application $application)
|
||||
{
|
||||
$this->_application = $application;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current application area
|
||||
*
|
||||
* @param array $annotations
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function _getTestAppArea($annotations)
|
||||
{
|
||||
$area = isset(
|
||||
$annotations['method'][self::ANNOTATION_NAME]
|
||||
) ? current(
|
||||
$annotations['method'][self::ANNOTATION_NAME]
|
||||
) : (isset(
|
||||
$annotations['class'][self::ANNOTATION_NAME]
|
||||
) ? current(
|
||||
$annotations['class'][self::ANNOTATION_NAME]
|
||||
) : \Magento\TestFramework\Application::DEFAULT_APP_AREA);
|
||||
|
||||
if (false == in_array($area, $this->_allowedAreas)) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__(
|
||||
'Invalid "@magentoAppArea" annotation, can be "%1" only.',
|
||||
implode('", "', $this->_allowedAreas)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $area;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start test case event observer
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function startTest(TestCase $test)
|
||||
{
|
||||
$values = [];
|
||||
try {
|
||||
$values = $this->parse($test);
|
||||
} catch (\Throwable $exception) {
|
||||
ExceptionHandler::handle(
|
||||
'Unable to parse fixtures',
|
||||
get_class($test),
|
||||
$test->getName(false),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
|
||||
$area = $values[0]['area'] ?? Application::DEFAULT_APP_AREA;
|
||||
|
||||
if ($this->_application->getArea() !== $area) {
|
||||
$this->_application->reinitialize();
|
||||
|
||||
if ($this->_application->getArea() !== $area) {
|
||||
$this->_application->loadArea($area);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns AppArea fixtures configuration
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return array
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
private function parse(TestCase $test): array
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$parsers = $objectManager
|
||||
->create(
|
||||
\Magento\TestFramework\Annotation\Parser\Composite::class,
|
||||
[
|
||||
'parsers' => [
|
||||
$objectManager->get(\Magento\TestFramework\Annotation\Parser\AppArea::class),
|
||||
$objectManager->get(\Magento\TestFramework\Fixture\Parser\AppArea::class)
|
||||
]
|
||||
]
|
||||
);
|
||||
$values = $parsers->parse($test, ParserInterface::SCOPE_METHOD)
|
||||
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
|
||||
|
||||
if (count($values) > 1) {
|
||||
throw new LocalizedException(
|
||||
__('Only one "@magentoAppArea" annotation is allowed per test')
|
||||
);
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implementation of the @magentoAppIsolation DocBlock annotation - isolation of global application objects in memory
|
||||
*/
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Application;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AppIsolation
|
||||
{
|
||||
/**
|
||||
* Flag to prevent an excessive test case isolation if the last test has been just isolated
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $hasNonIsolatedTests = true;
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
private $application;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $serverGlobalBackup;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Application $application
|
||||
*/
|
||||
public function __construct(Application $application)
|
||||
{
|
||||
$this->application = $application;
|
||||
}
|
||||
|
||||
/**
|
||||
* Isolate global application objects
|
||||
*/
|
||||
protected function _isolateApp()
|
||||
{
|
||||
if ($this->hasNonIsolatedTests) {
|
||||
$this->application->reinitialize();
|
||||
$_SESSION = [];
|
||||
$_COOKIE = [];
|
||||
session_write_close();
|
||||
$this->hasNonIsolatedTests = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Isolate application before running test case
|
||||
*/
|
||||
public function startTestSuite()
|
||||
{
|
||||
$this->serverGlobalBackup = $_SERVER;
|
||||
$this->_isolateApp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Isolate application after running test case
|
||||
*/
|
||||
public function endTestSuite()
|
||||
{
|
||||
$_SERVER = $this->serverGlobalBackup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function endTest(TestCase $test)
|
||||
{
|
||||
$this->hasNonIsolatedTests = true;
|
||||
$values = [];
|
||||
try {
|
||||
$values = $this->parse($test);
|
||||
} catch (\Throwable $exception) {
|
||||
ExceptionHandler::handle(
|
||||
'Unable to parse fixtures',
|
||||
get_class($test),
|
||||
$test->getName(false),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
if ($values) {
|
||||
$isIsolationEnabled = $values[0]['enabled'];
|
||||
} else {
|
||||
/* Controller tests should be isolated by default */
|
||||
$isIsolationEnabled = $test instanceof AbstractController;
|
||||
}
|
||||
|
||||
if ($isIsolationEnabled) {
|
||||
$this->_isolateApp();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns AppIsolation fixtures configuration
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return array
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
private function parse(TestCase $test): array
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$parsers = $objectManager
|
||||
->create(
|
||||
\Magento\TestFramework\Annotation\Parser\Composite::class,
|
||||
[
|
||||
'parsers' => [
|
||||
$objectManager->get(\Magento\TestFramework\Annotation\Parser\AppIsolation::class),
|
||||
$objectManager->get(\Magento\TestFramework\Fixture\Parser\AppIsolation::class)
|
||||
]
|
||||
]
|
||||
);
|
||||
$values = $parsers->parse($test, ParserInterface::SCOPE_METHOD)
|
||||
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
|
||||
|
||||
if (count($values) > 1) {
|
||||
throw new LocalizedException(
|
||||
__('Only one "@magentoAppIsolation" annotation is allowed per test')
|
||||
);
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Implementation of the @magentoCache DocBlock annotation
|
||||
*/
|
||||
class Cache
|
||||
{
|
||||
/**
|
||||
* Original values for cache type states
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $origValues = [];
|
||||
|
||||
/**
|
||||
* Handler for 'startTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
public function startTest(TestCase $test)
|
||||
{
|
||||
$statusList = [];
|
||||
try {
|
||||
$statusList = $this->parse($test);
|
||||
} catch (\Throwable $exception) {
|
||||
ExceptionHandler::handle(
|
||||
'Unable to parse fixtures',
|
||||
get_class($test),
|
||||
$test->getName(false),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
|
||||
if ($statusList) {
|
||||
$values = [];
|
||||
$typeList = self::getTypeList();
|
||||
foreach ($statusList as $cache) {
|
||||
if ('all' === $cache['type']) {
|
||||
foreach ($typeList->getTypes() as $type) {
|
||||
$values[$type['id']] = $cache['status'];
|
||||
}
|
||||
} else {
|
||||
$values[$cache['type']] = $cache['status'];
|
||||
}
|
||||
}
|
||||
$this->setValues($values, $test);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
public function endTest(TestCase $test)
|
||||
{
|
||||
if ($this->origValues) {
|
||||
$this->setValues($this->origValues, $test);
|
||||
$this->origValues = [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the values of cache types
|
||||
*
|
||||
* @param array $values
|
||||
* @param TestCase $test
|
||||
*/
|
||||
private function setValues($values, TestCase $test)
|
||||
{
|
||||
$typeList = self::getTypeList();
|
||||
if (!$this->origValues) {
|
||||
$this->origValues = [];
|
||||
foreach ($typeList->getTypes() as $type => $row) {
|
||||
$this->origValues[$type] = $row['status'];
|
||||
}
|
||||
}
|
||||
/** @var \Magento\Framework\App\Cache\StateInterface $states */
|
||||
$states = Bootstrap::getInstance()->getObjectManager()->get(\Magento\Framework\App\Cache\StateInterface::class);
|
||||
foreach ($values as $type => $isEnabled) {
|
||||
if (!isset($this->origValues[$type])) {
|
||||
ExceptionHandler::handle(
|
||||
"Unknown cache type specified: '{$type}' in @magentoCache",
|
||||
get_class($test),
|
||||
$test->getName(false)
|
||||
);
|
||||
}
|
||||
$states->setEnabled($type, $isEnabled);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for cache types list
|
||||
*
|
||||
* @return \Magento\Framework\App\Cache\TypeListInterface
|
||||
*/
|
||||
private static function getTypeList()
|
||||
{
|
||||
return Bootstrap::getInstance()->getObjectManager()->get(\Magento\Framework\App\Cache\TypeListInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Cache fixtures configuration
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return array
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
private function parse(TestCase $test): array
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$parsers = $objectManager
|
||||
->create(
|
||||
\Magento\TestFramework\Annotation\Parser\Composite::class,
|
||||
[
|
||||
'parsers' => [
|
||||
$objectManager->get(\Magento\TestFramework\Annotation\Parser\Cache::class),
|
||||
$objectManager->get(\Magento\TestFramework\Fixture\Parser\Cache::class)
|
||||
]
|
||||
]
|
||||
);
|
||||
return $parsers->parse($test, ParserInterface::SCOPE_METHOD)
|
||||
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
|
||||
}
|
||||
}
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use FilesystemIterator;
|
||||
use InvalidArgumentException;
|
||||
use Magento\Framework\Component\ComponentRegistrar;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Annotation\Parser\Composite;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
use ReflectionClass;
|
||||
use RegexIterator;
|
||||
use SplFileInfo;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Implementation of the @magentoComponentsDir DocBlock annotation
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class ComponentRegistrarFixture
|
||||
{
|
||||
public const ANNOTATION_NAME = 'magentoComponentsDir';
|
||||
|
||||
/**#@+
|
||||
* Properties of components registrar
|
||||
*/
|
||||
public const REGISTRAR_CLASS = ComponentRegistrar::class;
|
||||
public const PATHS_FIELD = 'paths';
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* Fixtures base dir
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $fixtureBaseDir;
|
||||
|
||||
/**
|
||||
* Original values of registered components
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $origComponents = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $fixtureBaseDir
|
||||
*/
|
||||
public function __construct($fixtureBaseDir)
|
||||
{
|
||||
$this->fixtureBaseDir = $fixtureBaseDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'startTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
public function startTest(TestCase $test)
|
||||
{
|
||||
$this->registerComponents($test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function endTest(TestCase $test)
|
||||
{
|
||||
$this->restoreComponents();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register fixture components
|
||||
*
|
||||
* @param TestCase $test
|
||||
*/
|
||||
private function registerComponents(TestCase $test)
|
||||
{
|
||||
$values = [];
|
||||
try {
|
||||
$values = $this->parse($test);
|
||||
} catch (Throwable $exception) {
|
||||
ExceptionHandler::handle(
|
||||
'Unable to parse fixtures',
|
||||
get_class($test),
|
||||
$test->getName(false),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
if (!$values) {
|
||||
return;
|
||||
}
|
||||
|
||||
$componentAnnotations = array_unique(array_column($values, 'path'));
|
||||
$reflection = new ReflectionClass(self::REGISTRAR_CLASS);
|
||||
$paths = $reflection->getProperty(self::PATHS_FIELD);
|
||||
$paths->setAccessible(true);
|
||||
$this->origComponents = $paths->getValue();
|
||||
$paths->setAccessible(false);
|
||||
foreach ($componentAnnotations as $fixturePath) {
|
||||
$fixturesDir = $this->fixtureBaseDir . '/' . $fixturePath;
|
||||
if (!file_exists($fixturesDir)) {
|
||||
throw new InvalidArgumentException(
|
||||
self::ANNOTATION_NAME . " fixture '$fixturePath' does not exist"
|
||||
);
|
||||
}
|
||||
$iterator = new RegexIterator(
|
||||
new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($fixturesDir, FilesystemIterator::SKIP_DOTS)
|
||||
),
|
||||
'/^.+\/registration\.php$/'
|
||||
);
|
||||
/**
|
||||
* @var SplFileInfo $registrationFile
|
||||
*/
|
||||
foreach ($iterator as $registrationFile) {
|
||||
require $registrationFile->getRealPath();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore registered components list to the original
|
||||
*/
|
||||
private function restoreComponents()
|
||||
{
|
||||
if (null !== $this->origComponents) {
|
||||
$reflection = new ReflectionClass(self::REGISTRAR_CLASS);
|
||||
$paths = $reflection->getProperty(self::PATHS_FIELD);
|
||||
$paths->setAccessible(true);
|
||||
$paths->setValue($this->origComponents);
|
||||
$paths->setAccessible(false);
|
||||
$this->origComponents = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns ComponentsDir fixtures configuration
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return array
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
private function parse(TestCase $test): array
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$parsers = $objectManager
|
||||
->create(
|
||||
Composite::class,
|
||||
[
|
||||
'parsers' => [
|
||||
$objectManager->get(\Magento\TestFramework\Annotation\Parser\ComponentsDir::class),
|
||||
$objectManager->get(\Magento\TestFramework\Fixture\Parser\ComponentsDir::class)
|
||||
]
|
||||
]
|
||||
);
|
||||
return array_merge(
|
||||
$parsers->parse($test, ParserInterface::SCOPE_CLASS),
|
||||
$parsers->parse($test, ParserInterface::SCOPE_METHOD)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implementation of the @magentoConfigFixture DocBlock annotation
|
||||
*/
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\App\Config\MutableScopeConfigInterface;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\TestFramework\Annotation\TestCaseAnnotation;
|
||||
use Magento\TestFramework\Fixture\Parser\Config as ConfigFixtureParser;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Handler which works with magentoConfigFixture annotations
|
||||
*/
|
||||
class ConfigFixture
|
||||
{
|
||||
public const ANNOTATION = 'magentoConfigFixture';
|
||||
|
||||
/**
|
||||
* Test instance that is available between 'startTest' and 'stopTest' events
|
||||
*
|
||||
* @var TestCase
|
||||
*/
|
||||
protected $_currentTest;
|
||||
|
||||
/**
|
||||
* Original values for global configuration options that need to be restored
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $globalConfigValues = [];
|
||||
|
||||
/**
|
||||
* Original values for website-scoped configuration options that need to be restored
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $websiteConfigValues = [];
|
||||
|
||||
/**
|
||||
* Original values for store-scoped configuration options that need to be restored
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $storeConfigValues = [];
|
||||
|
||||
/**
|
||||
* Retrieve configuration node value
|
||||
*
|
||||
* @param string $configPath
|
||||
* @param string|bool|null $scopeCode
|
||||
* @return string
|
||||
*/
|
||||
protected function _getConfigValue($configPath, $scopeCode = null)
|
||||
{
|
||||
return $this->getScopeConfigValue($configPath, ScopeInterface::SCOPE_STORE, $scopeCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve scope configuration node value
|
||||
*
|
||||
* @param string $configPath
|
||||
* @param string $scopeType
|
||||
* @param string|null $scopeCode
|
||||
* @return mixed|null
|
||||
*/
|
||||
protected function getScopeConfigValue(string $configPath, string $scopeType, string $scopeCode = null)
|
||||
{
|
||||
$result = null;
|
||||
if ($scopeCode !== false) {
|
||||
$scopeConfig = $this->getScopeConfig();
|
||||
$result = $scopeConfig->getValue($configPath, $scopeType, $scopeCode);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign configuration node value
|
||||
*
|
||||
* @param string $configPath
|
||||
* @param string $value
|
||||
* @param string|bool|null $storeCode
|
||||
* @return void
|
||||
*/
|
||||
protected function _setConfigValue($configPath, $value, $storeCode = false)
|
||||
{
|
||||
$scopeType = $storeCode === false ? ScopeConfigInterface::SCOPE_TYPE_DEFAULT : ScopeInterface::SCOPE_STORE;
|
||||
$this->setScopeConfigValue($configPath, $value, $scopeType, $storeCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set config scope value
|
||||
*
|
||||
* @param string $configPath
|
||||
* @param string|null $value
|
||||
* @param string $scopeType
|
||||
* @param string|null $scopeCode
|
||||
* @return void
|
||||
*/
|
||||
protected function setScopeConfigValue(
|
||||
string $configPath,
|
||||
?string $value,
|
||||
string $scopeType,
|
||||
?string $scopeCode
|
||||
): void {
|
||||
$config = $this->getMutableScopeConfig();
|
||||
if (strpos($configPath, 'default/') === 0) {
|
||||
$configPath = substr($configPath, 8);
|
||||
$config->setValue($configPath, $value, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
|
||||
} else {
|
||||
$config->setValue($configPath, $value, $scopeType, $scopeCode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mutable config object
|
||||
*
|
||||
* @return MutableScopeConfigInterface
|
||||
*/
|
||||
protected function getMutableScopeConfig(): MutableScopeConfigInterface
|
||||
{
|
||||
return Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get config object
|
||||
*
|
||||
* @return ScopeConfigInterface
|
||||
*/
|
||||
protected function getScopeConfig(): ScopeConfigInterface
|
||||
{
|
||||
return Bootstrap::getObjectManager()->get(ScopeConfigInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign required config values and save original ones
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
|
||||
*/
|
||||
protected function _assignConfigData(TestCase $test)
|
||||
{
|
||||
$resolver = Resolver::getInstance();
|
||||
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
|
||||
$existingFixtures = array_merge(
|
||||
$annotations['method'][self::ANNOTATION] ?? [],
|
||||
$this->getConfigFixturesFromAttribute($test)
|
||||
);
|
||||
/* Need to be applied even test does not have added fixtures because fixture can be added via config */
|
||||
$testAnnotations = $resolver->applyConfigFixtures(
|
||||
$test,
|
||||
$existingFixtures,
|
||||
self::ANNOTATION
|
||||
);
|
||||
foreach ($testAnnotations as $configPathAndValue) {
|
||||
if (preg_match('/^[^\/]+?(?=_store\s)/', $configPathAndValue, $matches)) {
|
||||
$this->setStoreConfigValue($matches ?? [], $configPathAndValue);
|
||||
} elseif (preg_match('/^[^\/]+?(?=_website\s)/', $configPathAndValue, $matches)) {
|
||||
$this->setWebsiteConfigValue($matches ?? [], $configPathAndValue);
|
||||
} else {
|
||||
$this->setGlobalConfigValue($configPathAndValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets store-scoped config value
|
||||
*
|
||||
* @param array $matches
|
||||
* @param string $configPathAndValue
|
||||
* @return void
|
||||
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
|
||||
*/
|
||||
protected function setStoreConfigValue(array $matches, $configPathAndValue): void
|
||||
{
|
||||
$storeCode = $matches[0] != 'current' ? $matches[0] : null;
|
||||
$parts = preg_split('/\s+/', $configPathAndValue, 3);
|
||||
list($configScope, $configPath, $requiredValue) = $parts + ['', '', ''];
|
||||
$originalValue = $this->_getConfigValue($configPath, $storeCode);
|
||||
$this->storeConfigValues[$storeCode][$configPath] = $originalValue;
|
||||
$this->_setConfigValue($configPath, $requiredValue, $storeCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets website-scoped config value
|
||||
*
|
||||
* @param array $matches
|
||||
* @param string $configPathAndValue
|
||||
* @return void
|
||||
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
|
||||
*/
|
||||
protected function setWebsiteConfigValue(array $matches, $configPathAndValue): void
|
||||
{
|
||||
$websiteCode = $matches[0] != 'current' ? $matches[0] : null;
|
||||
$parts = preg_split('/\s+/', $configPathAndValue, 3);
|
||||
list($configScope, $configPath, $requiredValue) = $parts + ['', '', ''];
|
||||
$originalValue = $this->getScopeConfigValue($configPath, ScopeInterface::SCOPE_WEBSITES, $websiteCode);
|
||||
$this->websiteConfigValues[$websiteCode][$configPath] = $originalValue;
|
||||
$this->setScopeConfigValue($configPath, $requiredValue, ScopeInterface::SCOPE_WEBSITES, $websiteCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets global config value
|
||||
*
|
||||
* @param string $configPathAndValue
|
||||
* @return void
|
||||
*/
|
||||
protected function setGlobalConfigValue($configPathAndValue): void
|
||||
{
|
||||
/* Global config value */
|
||||
list($configPath, $requiredValue) = preg_split('/\s+/', $configPathAndValue, 2);
|
||||
$configPath = str_starts_with($configPath, 'default/') ? substr($configPath, 8) : $configPath;
|
||||
$originalValue = $this->_getConfigValue($configPath);
|
||||
$this->globalConfigValues[$configPath] = $originalValue;
|
||||
$this->_setConfigValue($configPath, $requiredValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore original values for changed config options
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _restoreConfigData()
|
||||
{
|
||||
/* Restore global values */
|
||||
foreach ($this->globalConfigValues as $configPath => $originalValue) {
|
||||
$this->_setConfigValue($configPath, $originalValue);
|
||||
}
|
||||
$this->globalConfigValues = [];
|
||||
|
||||
/* Restore store-scoped values */
|
||||
foreach ($this->storeConfigValues as $storeCode => $originalData) {
|
||||
foreach ($originalData as $configPath => $originalValue) {
|
||||
if (empty($storeCode)) {
|
||||
$storeCode = null;
|
||||
}
|
||||
$this->setScopeConfigValue($configPath, $originalValue, ScopeInterface::SCOPE_STORES, $storeCode);
|
||||
}
|
||||
}
|
||||
$this->storeConfigValues = [];
|
||||
|
||||
/* Restore website-scoped values */
|
||||
foreach ($this->websiteConfigValues as $websiteCode => $originalData) {
|
||||
foreach ($originalData as $configPath => $originalValue) {
|
||||
if (empty($websiteCode)) {
|
||||
$websiteCode = null;
|
||||
}
|
||||
$this->setScopeConfigValue($configPath, $originalValue, ScopeInterface::SCOPE_WEBSITES, $websiteCode);
|
||||
}
|
||||
}
|
||||
$this->websiteConfigValues = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'startTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
public function startTest(TestCase $test)
|
||||
{
|
||||
$this->_currentTest = $test;
|
||||
$this->_assignConfigData($test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function endTest(TestCase $test)
|
||||
{
|
||||
$this->_currentTest = null;
|
||||
$this->_restoreConfigData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reassign configuration data whenever application is reset
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initStoreAfter()
|
||||
{
|
||||
/* process events triggered from within a test only */
|
||||
if ($this->_currentTest) {
|
||||
$this->_assignConfigData($this->_currentTest);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns config fixtures defined using Config attribute
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return array
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
private function getConfigFixturesFromAttribute(TestCase $test): array
|
||||
{
|
||||
$parser = Bootstrap::getObjectManager()->get(ConfigFixtureParser::class);
|
||||
$configs = [];
|
||||
|
||||
foreach ($parser->parse($test, ParserInterface::SCOPE_METHOD) as $data) {
|
||||
$configs[] = sprintf(
|
||||
'%s%s %s',
|
||||
$data['scopeType'] === ScopeConfigInterface::SCOPE_TYPE_DEFAULT
|
||||
? 'default/'
|
||||
: ($data['scopeValue'] ?? 'current') . '_' . $data['scopeType'] . ' ',
|
||||
$data['path'],
|
||||
$data['value'],
|
||||
);
|
||||
}
|
||||
|
||||
return $configs;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\TestFramework\Event\Param\Transaction;
|
||||
use Magento\TestFramework\Fixture\Parser\DataFixtureAttributesParser;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Implementation of the @magentoDataFixture DocBlock annotation.
|
||||
*/
|
||||
class DataFixture extends AbstractDataFixture
|
||||
{
|
||||
public const ANNOTATION = 'magentoDataFixture';
|
||||
|
||||
/**
|
||||
* Handler for 'startTestTransactionRequest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @param Transaction $param
|
||||
* @return void
|
||||
*/
|
||||
public function startTestTransactionRequest(TestCase $test, Transaction $param): void
|
||||
{
|
||||
$fixtures = $this->_getFixtures($test);
|
||||
/* Start transaction before applying first fixture to be able to revert them all further */
|
||||
if ($fixtures) {
|
||||
if ($this->getDbIsolationState($test) !== ['disabled']) {
|
||||
$param->requestTransactionStart();
|
||||
} else {
|
||||
$this->_applyFixtures($fixtures, $test);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTestNeedTransactionRollback' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @param Transaction $param
|
||||
* @return void
|
||||
*/
|
||||
public function endTestTransactionRequest(TestCase $test, Transaction $param): void
|
||||
{
|
||||
/* Isolate other tests from test-specific fixtures */
|
||||
if ($this->_appliedFixtures && $this->_getFixtures($test)) {
|
||||
if ($this->getDbIsolationState($test) !== ['disabled']) {
|
||||
$param->requestTransactionRollback();
|
||||
} else {
|
||||
$this->_revertFixtures($test);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'startTransaction' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
public function startTransaction(TestCase $test): void
|
||||
{
|
||||
$this->_applyFixtures($this->_getFixtures($test), $test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'rollbackTransaction' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
public function rollbackTransaction(): void
|
||||
{
|
||||
$this->_revertFixtures();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAnnotation(): string
|
||||
{
|
||||
return self::ANNOTATION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getParsers(): array
|
||||
{
|
||||
$parsers = [];
|
||||
$parsers[] = Bootstrap::getObjectManager()->get(
|
||||
\Magento\TestFramework\Fixture\Parser\DataFixture::class
|
||||
);
|
||||
return array_merge(
|
||||
parent::getParsers(),
|
||||
$parsers
|
||||
);
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Implementation of the @magentoDataFixtureBeforeTransaction DocBlock annotation
|
||||
*/
|
||||
class DataFixtureBeforeTransaction extends AbstractDataFixture
|
||||
{
|
||||
public const ANNOTATION = 'magentoDataFixtureBeforeTransaction';
|
||||
|
||||
/**
|
||||
* Handler for 'startTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
*/
|
||||
public function startTest(TestCase $test)
|
||||
{
|
||||
$fixtures = $this->_getFixtures($test);
|
||||
if ($fixtures) {
|
||||
$this->_applyFixtures($fixtures, $test);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
*/
|
||||
public function endTest(TestCase $test)
|
||||
{
|
||||
/* Isolate other tests from test-specific fixtures */
|
||||
if ($this->_appliedFixtures && $this->_getFixtures($test)) {
|
||||
$this->_revertFixtures($test);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAnnotation(): string
|
||||
{
|
||||
return self::ANNOTATION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getParsers(): array
|
||||
{
|
||||
$parsers = [];
|
||||
$parsers[] = Bootstrap::getObjectManager()->create(
|
||||
\Magento\TestFramework\Fixture\Parser\DataFixture::class,
|
||||
[
|
||||
'attributeClass' => \Magento\TestFramework\Fixture\DataFixtureBeforeTransaction::class
|
||||
]
|
||||
);
|
||||
return array_merge(
|
||||
parent::getParsers(),
|
||||
$parsers
|
||||
);
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\TestFramework\Fixture\DataFixtureFactory;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
|
||||
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
|
||||
use Magento\TestFramework\ScopeSwitcherInterface;
|
||||
|
||||
/**
|
||||
* Apply and revert data fixtures
|
||||
*/
|
||||
class DataFixtureSetup
|
||||
{
|
||||
/**
|
||||
* @param Registry $registry
|
||||
* @param DataFixtureFactory $dataFixtureFactory
|
||||
* @param ScopeSwitcherInterface $scopeSwitcher
|
||||
*/
|
||||
public function __construct(
|
||||
private Registry $registry,
|
||||
private DataFixtureFactory $dataFixtureFactory,
|
||||
private ScopeSwitcherInterface $scopeSwitcher
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies data fixture and returns the result.
|
||||
*
|
||||
* @param array $fixture
|
||||
* @return DataObject|null
|
||||
*/
|
||||
public function apply(array $fixture): ?DataObject
|
||||
{
|
||||
$data = $this->resolveVariables($fixture['data'] ?? []);
|
||||
$factory = $this->dataFixtureFactory->create($fixture['factory']);
|
||||
if (isset($fixture['scope'])) {
|
||||
$scope = DataFixtureStorageManager::getStorage()->get($fixture['scope']);
|
||||
$fromScope = $this->scopeSwitcher->switch($scope);
|
||||
try {
|
||||
$result = $factory->apply($data);
|
||||
} finally {
|
||||
$this->scopeSwitcher->switch($fromScope);
|
||||
}
|
||||
} else {
|
||||
$result = $factory->apply($data);
|
||||
}
|
||||
|
||||
if ($result !== null && !empty($fixture['name'])) {
|
||||
DataFixtureStorageManager::getStorage()->persist(
|
||||
$fixture['name'],
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert data fixture.
|
||||
*
|
||||
* @param array $fixture
|
||||
*/
|
||||
public function revert(array $fixture): void
|
||||
{
|
||||
$isSecureArea = $this->registry->registry('isSecureArea');
|
||||
$this->registry->unregister('isSecureArea');
|
||||
$this->registry->register('isSecureArea', true);
|
||||
try {
|
||||
$factory = $this->dataFixtureFactory->create($fixture['factory']);
|
||||
if ($factory instanceof RevertibleDataFixtureInterface) {
|
||||
$factory->revert($fixture['result'] ?? new DataObject());
|
||||
}
|
||||
} catch (NoSuchEntityException $exception) {
|
||||
//ignore
|
||||
} finally {
|
||||
$this->registry->unregister('isSecureArea');
|
||||
$this->registry->register('isSecureArea', $isSecureArea);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace fixtures references in the data by their value
|
||||
*
|
||||
* Supported formats:
|
||||
* - $fixture$
|
||||
* - $fixture.attribute$
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
private function resolveVariables(array $data): array
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$data[$key] = $this->resolveVariables($value);
|
||||
} else {
|
||||
if (is_string($value) && preg_match('/^\$\w+(\.\w+)?\$$/', $value)) {
|
||||
list($fixtureName, $attribute) = array_pad(explode('.', trim($value, '$')), 2, null);
|
||||
$fixtureData = DataFixtureStorageManager::getStorage()->get($fixtureName);
|
||||
if (!$fixtureData) {
|
||||
throw new \InvalidArgumentException("Unable to resolve fixture reference '$value'");
|
||||
}
|
||||
$data[$key] = $attribute ? $fixtureData->getDataUsingMethod($attribute) : $fixtureData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Event\Param\Transaction;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Implementation of the @magentoDbIsolation DocBlock annotation
|
||||
*/
|
||||
class DbIsolation
|
||||
{
|
||||
public const MAGENTO_DB_ISOLATION = 'magentoDbIsolation';
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isIsolationActive = false;
|
||||
|
||||
/**
|
||||
* Handler for 'startTestTransactionRequest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @param Transaction $param
|
||||
*/
|
||||
public function startTestTransactionRequest(TestCase $test, Transaction $param)
|
||||
{
|
||||
$methodIsolation = $this->_getIsolation($test);
|
||||
if ($this->_isIsolationActive) {
|
||||
if ($methodIsolation === false) {
|
||||
$param->requestTransactionRollback();
|
||||
}
|
||||
} elseif ($methodIsolation || ($methodIsolation === null && $this->_getIsolation($test))) {
|
||||
$param->requestTransactionStart();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTestTransactionRequest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @param Transaction $param
|
||||
*/
|
||||
public function endTestTransactionRequest(TestCase $test, Transaction $param)
|
||||
{
|
||||
if ($this->_isIsolationActive && $this->_getIsolation($test)) {
|
||||
$param->requestTransactionRollback();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'startTransaction' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function startTransaction(TestCase $test)
|
||||
{
|
||||
$this->_isIsolationActive = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'rollbackTransaction' event
|
||||
*/
|
||||
public function rollbackTransaction()
|
||||
{
|
||||
$this->_isIsolationActive = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve database isolation annotation value for the current scope.
|
||||
* Possible results:
|
||||
* NULL - annotation is not defined
|
||||
* TRUE - annotation is defined as 'enabled'
|
||||
* FALSE - annotation is defined as 'disabled'
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return bool|null Returns NULL, if isolation is not defined for the current scope
|
||||
*/
|
||||
protected function _getIsolation(TestCase $test)
|
||||
{
|
||||
$state = null;
|
||||
try {
|
||||
$state = Bootstrap::getObjectManager()->get(DbIsolationState::class)->isEnabled($test);
|
||||
} catch (\Throwable $exception) {
|
||||
ExceptionHandler::handle(
|
||||
'Unable to parse fixtures',
|
||||
get_class($test),
|
||||
$test->getName(false),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
return $state;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DbIsolationState
|
||||
{
|
||||
/**
|
||||
* Returns the db isolation state
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return bool|null
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function isEnabled(TestCase $test): ?bool
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$parsers = $objectManager
|
||||
->create(
|
||||
\Magento\TestFramework\Annotation\Parser\Composite::class,
|
||||
[
|
||||
'parsers' => [
|
||||
$objectManager->get(\Magento\TestFramework\Annotation\Parser\DbIsolation::class),
|
||||
$objectManager->get(\Magento\TestFramework\Fixture\Parser\DbIsolation::class)
|
||||
]
|
||||
]
|
||||
);
|
||||
$values = $parsers->parse($test, ParserInterface::SCOPE_METHOD)
|
||||
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
|
||||
|
||||
if (count($values) > 1) {
|
||||
throw new LocalizedException(
|
||||
__('Only one "@%1" annotation is allowed per test', DbIsolation::MAGENTO_DB_ISOLATION)
|
||||
);
|
||||
}
|
||||
return $values[0]['enabled'] ?? null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use PHPUnit\Framework\Exception;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
|
||||
class ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* Format exception message and throws PHPUnit\Framework\Exception
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $testClass
|
||||
* @param string|null $testMethod
|
||||
* @param \Throwable|null $previous
|
||||
* @return void
|
||||
*/
|
||||
public static function handle(
|
||||
string $message,
|
||||
string $testClass,
|
||||
string $testMethod = null,
|
||||
\Throwable $previous = null
|
||||
): void {
|
||||
try {
|
||||
$reflected = new ReflectionClass($testClass);
|
||||
} catch (ReflectionException $e) {
|
||||
throw new Exception(
|
||||
$e->getMessage(),
|
||||
(int) $e->getCode(),
|
||||
$e
|
||||
);
|
||||
}
|
||||
|
||||
$name = $testMethod;
|
||||
|
||||
if ($name && $reflected->hasMethod($name)) {
|
||||
try {
|
||||
$reflected = $reflected->getMethod($name);
|
||||
} catch (ReflectionException $e) {
|
||||
throw new Exception(
|
||||
$e->getMessage(),
|
||||
(int) $e->getCode(),
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$location = sprintf(
|
||||
"%s(%d): %s->%s()",
|
||||
$reflected->getFileName(),
|
||||
$reflected->getStartLine(),
|
||||
$testClass,
|
||||
$testMethod
|
||||
);
|
||||
|
||||
$summary = '';
|
||||
if ($previous) {
|
||||
$exception = $previous;
|
||||
do {
|
||||
$summary .= PHP_EOL
|
||||
. PHP_EOL
|
||||
. 'Caused By: '
|
||||
. $exception->getMessage()
|
||||
. PHP_EOL
|
||||
. $exception->getTraceAsString();
|
||||
} while ($exception = $exception->getPrevious());
|
||||
}
|
||||
throw new Exception(
|
||||
sprintf(
|
||||
"%s\n#0 %s%s",
|
||||
$message,
|
||||
$location,
|
||||
$summary
|
||||
),
|
||||
0,
|
||||
$previous
|
||||
);
|
||||
}
|
||||
}
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Catalog\Model\Indexer\Product\Price\DimensionModeConfiguration;
|
||||
use Magento\Catalog\Model\Indexer\Product\Price\ModeSwitcher;
|
||||
use Magento\Catalog\Model\Indexer\Product\Price\ModeSwitcherConfiguration;
|
||||
use Magento\Catalog\Model\Indexer\Product\Price\Processor;
|
||||
use Magento\Framework\App\Cache\TypeListInterface;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\TestFramework\Annotation\TestCaseAnnotation;
|
||||
use Magento\TestFramework\App\Config;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Implementation of the @magentoIndexerDimensionMode DocBlock annotation
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class IndexerDimensionMode
|
||||
{
|
||||
/** @var TypeListInterface */
|
||||
private $cacheTypeList;
|
||||
|
||||
/** @var ScopeConfigInterface */
|
||||
private $configReader;
|
||||
|
||||
/** @var ModeSwitcher */
|
||||
private $modeSwitcher;
|
||||
|
||||
/** @var ObjectManagerInterface */
|
||||
private $objectManager;
|
||||
|
||||
/** @var \Magento\TestFramework\Db\Mysql */
|
||||
private $db;
|
||||
|
||||
/** @var bool */
|
||||
private $isDimensionMode = false;
|
||||
|
||||
/**
|
||||
* Restore db
|
||||
*/
|
||||
private function restoreDb()
|
||||
{
|
||||
$this->db = Bootstrap::getInstance()->getBootstrap()->getApplication()->getDbInstance();
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->db->restoreFromDbDump();
|
||||
$this->cacheTypeList = $this->objectManager->get(TypeListInterface::class);
|
||||
$this->cacheTypeList->cleanType('config');
|
||||
$this->objectManager->get(Config::class)->clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to set a Dimension mode if it wasn't set.
|
||||
*
|
||||
* @param string $mode
|
||||
* @param TestCase $test
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function setDimensionMode(string $mode, TestCase $test)
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->modeSwitcher = $this->objectManager->get(ModeSwitcher::class);
|
||||
$this->configReader = $this->objectManager->get(ScopeConfigInterface::class);
|
||||
$this->cacheTypeList = $this->objectManager->get(TypeListInterface::class);
|
||||
|
||||
$this->configReader->clean();
|
||||
$previousMode = $this->configReader->getValue(ModeSwitcherConfiguration::XML_PATH_PRICE_DIMENSIONS_MODE) ?:
|
||||
DimensionModeConfiguration::DIMENSION_NONE;
|
||||
|
||||
if ($previousMode !== $mode) {
|
||||
//Create new tables and move data
|
||||
$this->modeSwitcher->switchMode($mode, $previousMode);
|
||||
$this->objectManager->get(Config::class)->clean();
|
||||
} else {
|
||||
ExceptionHandler::handle(
|
||||
'Dimensions mode for indexer has not been changed',
|
||||
get_class($test),
|
||||
$test->getName(false)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'startTest' event
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function startTest(TestCase $test)
|
||||
{
|
||||
$values = [];
|
||||
try {
|
||||
$values = $this->parse($test);
|
||||
} catch (\Throwable $exception) {
|
||||
ExceptionHandler::handle(
|
||||
'Unable to parse fixtures',
|
||||
get_class($test),
|
||||
$test->getName(false),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
|
||||
if ($values) {
|
||||
$dbIsolation = Bootstrap::getObjectManager()->get(DbIsolationState::class)->isEnabled($test);
|
||||
if ($dbIsolation) {
|
||||
ExceptionHandler::handle(
|
||||
'@magentoDbIsolation must be disabled when using @magentoIndexerDimensionMode',
|
||||
get_class($test),
|
||||
$test->getName(false)
|
||||
);
|
||||
}
|
||||
|
||||
if ($values[0]['indexer'] === Processor::INDEXER_ID) {
|
||||
$this->isDimensionMode = true;
|
||||
$this->setDimensionMode($values[0]['dimension'], $test);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTest' event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function endTest()
|
||||
{
|
||||
if ($this->isDimensionMode) {
|
||||
$this->restoreDb();
|
||||
$this->isDimensionMode = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns IndexerDimensionMode fixtures configuration
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return array
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
private function parse(TestCase $test): array
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$parsers = $objectManager
|
||||
->create(
|
||||
\Magento\TestFramework\Annotation\Parser\Composite::class,
|
||||
[
|
||||
'parsers' => [
|
||||
$objectManager->get(\Magento\TestFramework\Annotation\Parser\IndexerDimensionMode::class),
|
||||
$objectManager->get(\Magento\TestFramework\Fixture\Parser\IndexerDimensionMode::class)
|
||||
]
|
||||
]
|
||||
);
|
||||
return $parsers->parse($test, ParserInterface::SCOPE_METHOD)
|
||||
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation\Parser;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Annotation\TestCaseAnnotation;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AppArea implements ParserInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const ANNOTATION = 'magentoAppArea';
|
||||
|
||||
/**
|
||||
* List of allowed areas.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private const ALLOWED_AREAS = [
|
||||
\Magento\Framework\App\Area::AREA_GLOBAL,
|
||||
\Magento\Framework\App\Area::AREA_ADMINHTML,
|
||||
\Magento\Framework\App\Area::AREA_FRONTEND,
|
||||
\Magento\Framework\App\Area::AREA_WEBAPI_REST,
|
||||
\Magento\Framework\App\Area::AREA_WEBAPI_SOAP,
|
||||
\Magento\Framework\App\Area::AREA_CRONTAB,
|
||||
\Magento\Framework\App\Area::AREA_GRAPHQL
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function parse(TestCase $test, string $scope): array
|
||||
{
|
||||
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
|
||||
$values = [];
|
||||
|
||||
foreach ($annotations[$scope][self::ANNOTATION] ?? [] as $value) {
|
||||
if (!in_array($value, self::ALLOWED_AREAS, true)) {
|
||||
throw new LocalizedException(
|
||||
__(
|
||||
"Invalid annotation format: @%1 %2. The valid format is: @%1 [%3].",
|
||||
self::ANNOTATION,
|
||||
$value,
|
||||
implode('|', self::ALLOWED_AREAS)
|
||||
)
|
||||
);
|
||||
}
|
||||
$values[] = ['area' => $value];
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation\Parser;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Annotation\TestCaseAnnotation;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AppIsolation implements ParserInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const ANNOTATION = 'magentoAppIsolation';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function parse(TestCase $test, string $scope): array
|
||||
{
|
||||
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
|
||||
$values = [];
|
||||
|
||||
foreach ($annotations[$scope][self::ANNOTATION] ?? [] as $value) {
|
||||
if (!in_array($value, ['enabled', 'disabled'])) {
|
||||
throw new LocalizedException(
|
||||
__(
|
||||
"Invalid annotation format: @%1 %2. The valid format is: @%1 enabled|disabled.",
|
||||
self::ANNOTATION,
|
||||
$value
|
||||
)
|
||||
);
|
||||
}
|
||||
$values[] = ['enabled' => $value === 'enabled'];
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation\Parser;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Annotation\TestCaseAnnotation;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class Cache implements ParserInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const ANNOTATION = 'magentoCache';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function parse(TestCase $test, string $scope): array
|
||||
{
|
||||
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
|
||||
$values = [];
|
||||
|
||||
foreach ($annotations[$scope][self::ANNOTATION] ?? [] as $value) {
|
||||
if (!preg_match('/^([a-z_]+)\s(enabled|disabled)$/', $value, $matches)) {
|
||||
throw new LocalizedException(
|
||||
__(
|
||||
"Invalid annotation format: @%1 %2. The valid format is: @%1 [<type>|all] [enabled|disabled].",
|
||||
self::ANNOTATION,
|
||||
$value
|
||||
)
|
||||
);
|
||||
}
|
||||
$values[] = ['type' => $matches[1], 'status' => $matches[2] === 'enabled'];
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation\Parser;
|
||||
|
||||
use Magento\TestFramework\Annotation\TestCaseAnnotation;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ComponentsDir implements ParserInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const ANNOTATION = 'magentoComponentsDir';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function parse(TestCase $test, string $scope): array
|
||||
{
|
||||
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
|
||||
$values = [];
|
||||
|
||||
foreach ($annotations[$scope][self::ANNOTATION] ?? [] as $value) {
|
||||
$values[] = ['path' => $value];
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation\Parser;
|
||||
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class Composite implements ParserInterface
|
||||
{
|
||||
public const STRATEGY_MERGE = 0;
|
||||
public const STRATEGY_REPLACE = 1;
|
||||
|
||||
/**
|
||||
* @var ParserInterface[]
|
||||
*/
|
||||
private array $parsers;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private int $strategy;
|
||||
|
||||
/**
|
||||
* @param ParserInterface[] $parsers
|
||||
* @param int $strategy
|
||||
*/
|
||||
public function __construct(
|
||||
array $parsers,
|
||||
int $strategy = self::STRATEGY_MERGE
|
||||
) {
|
||||
$this->parsers = $parsers;
|
||||
$this->strategy = $strategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function parse(TestCase $testCase, string $scope)
|
||||
{
|
||||
$results = [];
|
||||
foreach ($this->parsers as $parser) {
|
||||
$results[] = $parser->parse($testCase, $scope);
|
||||
}
|
||||
return $this->strategy === self::STRATEGY_MERGE
|
||||
? array_merge(...$results)
|
||||
: (array_reverse(array_filter($results))[0] ?? []);
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation\Parser;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Annotation\TestCaseAnnotation;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DataFixture implements ParserInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $annotation;
|
||||
|
||||
/**
|
||||
* @param string $annotation
|
||||
*/
|
||||
public function __construct(
|
||||
string $annotation
|
||||
) {
|
||||
$this->annotation = $annotation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function parse(TestCase $test, string $scope): array
|
||||
{
|
||||
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
|
||||
$fixtures = [];
|
||||
foreach ($annotations[$scope][$this->annotation] ?? [] as $fixture) {
|
||||
list($factory, $extraParams) = array_pad(array_values(array_filter(explode(' ', $fixture, 2))), 2, '');
|
||||
if (str_contains($factory, '\\') && !is_callable($factory)) {
|
||||
if (class_exists($factory)) {
|
||||
throw new LocalizedException(
|
||||
__(
|
||||
'Support for fixture class %1 in data fixture annotation was dropped' .
|
||||
' in favor to data fixture attribute. Please see documentation',
|
||||
$factory
|
||||
)
|
||||
);
|
||||
}
|
||||
throw new LocalizedException(
|
||||
__(
|
||||
'Data Fixture annotation expects argument #1 to be the fixture filename or a callable,' .
|
||||
' %1 given',
|
||||
$factory
|
||||
)
|
||||
);
|
||||
}
|
||||
if ($extraParams) {
|
||||
throw new LocalizedException(
|
||||
__(
|
||||
'Data Fixture annotation expects only one argument: %1',
|
||||
$fixture
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$fixtures[] = [
|
||||
'name' => null,
|
||||
'factory' => $factory,
|
||||
'data' => [],
|
||||
];
|
||||
}
|
||||
return $fixtures;
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation\Parser;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Annotation\TestCaseAnnotation;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DbIsolation implements ParserInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const ANNOTATION = 'magentoDbIsolation';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function parse(TestCase $test, string $scope): array
|
||||
{
|
||||
$values = [];
|
||||
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
|
||||
|
||||
foreach ($annotations[$scope][self::ANNOTATION] ?? [] as $value) {
|
||||
if (!in_array($value, ['enabled', 'disabled'])) {
|
||||
throw new LocalizedException(
|
||||
__(
|
||||
"Invalid annotation format: @%1 %2. The valid format is: @%1 enabled|disabled.",
|
||||
self::ANNOTATION,
|
||||
$value
|
||||
)
|
||||
);
|
||||
}
|
||||
$values[] = ['enabled' => $value === 'enabled'];
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation\Parser;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\TestFramework\Annotation\TestCaseAnnotation;
|
||||
use Magento\TestFramework\Fixture\ParserInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class IndexerDimensionMode implements ParserInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const ANNOTATION = 'magentoIndexerDimensionMode';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function parse(TestCase $test, string $scope): array
|
||||
{
|
||||
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
|
||||
$values = [];
|
||||
|
||||
foreach ($annotations[$scope][self::ANNOTATION] ?? [] as $value) {
|
||||
$args = explode(' ', $value);
|
||||
if (count($args) !== 2) {
|
||||
throw new LocalizedException(
|
||||
__(
|
||||
"Invalid annotation format: @%1 %2. The valid format is: @%1 <indexer> <dimension>.",
|
||||
self::ANNOTATION,
|
||||
$value
|
||||
)
|
||||
);
|
||||
}
|
||||
$values[] = ['indexer' => $args[0], 'dimension' => $args[1]];
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\Util\Test as TestUtil;
|
||||
|
||||
/**
|
||||
* Returns annotations for given testcase.
|
||||
*/
|
||||
class TestCaseAnnotation
|
||||
{
|
||||
/**
|
||||
* @var TestCaseAnnotation
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
/**
|
||||
* Get instance of test case annotation access service.
|
||||
*
|
||||
* @return TestCaseAnnotation
|
||||
*/
|
||||
public static function getInstance(): TestCaseAnnotation
|
||||
{
|
||||
return self::$instance ?? self::$instance = new TestCaseAnnotation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get annotations for the given test case.
|
||||
*
|
||||
* @param TestCase $testCase
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAnnotations(TestCase $testCase): array
|
||||
{
|
||||
return TestUtil::parseTestMethodAnnotations(
|
||||
get_class($testCase),
|
||||
$testCase->getName(false)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\Framework\AssertionFailedError;
|
||||
|
||||
/**
|
||||
* Validates tests isolation. Makes sure that test does not keep exceed data in DB.
|
||||
*/
|
||||
class TestsIsolation
|
||||
{
|
||||
/**
|
||||
* This variable was created to keep initial data cached
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $dbTableState = [];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $testTypesToCheckIsolation = [
|
||||
'integration',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $isolationLevel = 0;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $dbStateTables = [
|
||||
'catalog_product_entity',
|
||||
'eav_attribute',
|
||||
'catalog_category_entity',
|
||||
'eav_attribute_set',
|
||||
'store',
|
||||
'store_website',
|
||||
'url_rewrite'
|
||||
];
|
||||
|
||||
/**
|
||||
* Pull data from specific table
|
||||
*
|
||||
* @param string $table
|
||||
* @return array
|
||||
*/
|
||||
private function pullDbState(string $table): array
|
||||
{
|
||||
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
|
||||
$resource = ObjectManager::getInstance()->get(ResourceConnection::class);
|
||||
$connection = $resource->getConnection();
|
||||
$select = $connection->select()->from($table);
|
||||
return $connection->fetchAll($select);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create DB snapshot before test run.
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @param array|null $dbIsolationState
|
||||
* @return void
|
||||
*/
|
||||
public function createDbSnapshot(TestCase $test, ?array $dbIsolationState): void
|
||||
{
|
||||
if (null !== $dbIsolationState
|
||||
&& ($dbIsolationState !== ['enabled'])
|
||||
&& ($this->checkIsolationRequired($test))
|
||||
) {
|
||||
++$this->isolationLevel;
|
||||
if ($this->isolationLevel === 1) {
|
||||
$this->saveDbStateBeforeTestRun($test);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check DB isolation when test ended.
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @param array|null $dbIsolationState
|
||||
* @return void
|
||||
*/
|
||||
public function checkTestIsolation(TestCase $test, ?array $dbIsolationState): void
|
||||
{
|
||||
if (null !== $dbIsolationState
|
||||
&& ($dbIsolationState !== ['enabled'])
|
||||
&& ($this->checkIsolationRequired($test))
|
||||
) {
|
||||
--$this->isolationLevel;
|
||||
if ($this->isolationLevel === 1) {
|
||||
$this->checkResidualData($test);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saving DB snapshot before fixtures applying.
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
private function saveDbStateBeforeTestRun(TestCase $test): void
|
||||
{
|
||||
try {
|
||||
if (empty($this->dbTableState)) {
|
||||
foreach ($this->dbStateTables as $table) {
|
||||
$this->dbTableState[$table] = $this->pullDbState($table);
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$test->getTestResultObject()->addFailure($test, new AssertionFailedError($e->getMessage()), 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if test isolation is required for given scope of tests.
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return bool
|
||||
*/
|
||||
private function checkIsolationRequired(TestCase $test): bool
|
||||
{
|
||||
$isRequired = false;
|
||||
if (!$test->getTestResultObject()) {
|
||||
return $isRequired;
|
||||
}
|
||||
$passedClasses = $test->getTestResultObject()->passedClasses();
|
||||
|
||||
if ($passedClasses) {
|
||||
$testFilename = current($passedClasses);
|
||||
|
||||
foreach ($this->testTypesToCheckIsolation as $testType) {
|
||||
if (false !== strpos($testFilename, \sprintf('/dev/tests/%s/', $testType))) {
|
||||
$isRequired = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $isRequired;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if there's residual data in DB after test execution.
|
||||
*
|
||||
* @param TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
private function checkResidualData(TestCase $test): void
|
||||
{
|
||||
$isolationProblem = [];
|
||||
foreach ($this->dbTableState as $table => $isolationData) {
|
||||
try {
|
||||
$diff = $this->dataDiff($isolationData, $this->pullDbState($table));
|
||||
if (!empty($diff)) {
|
||||
$isolationProblem[$table] = $diff;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$test->getTestResultObject()->addFailure($test, new AssertionFailedError($e->getMessage()), 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($isolationProblem)) {
|
||||
$test->getTestResultObject()->addFailure(
|
||||
$test,
|
||||
new AssertionFailedError(
|
||||
"There was a problem with isolation: " . var_export($isolationProblem, true)
|
||||
),
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare data difference for m-dimensional array
|
||||
*
|
||||
* @param array $dataBefore
|
||||
* @param array $dataAfter
|
||||
* @return array
|
||||
*/
|
||||
private function dataDiff(array $dataBefore, array $dataAfter): array
|
||||
{
|
||||
$diff = [];
|
||||
if (count($dataBefore) !== count($dataAfter)) {
|
||||
$diff = \array_slice($dataAfter, count($dataBefore));
|
||||
}
|
||||
|
||||
return $diff;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user