init
This commit is contained in:
+105
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\Component\ComponentRegistrar;
|
||||
use Magento\Framework\Filesystem\Io\File;
|
||||
use Magento\TestFramework\Deploy\CliCommand;
|
||||
use Magento\TestFramework\Deploy\TestModuleManager;
|
||||
use PHPUnit\Util\Test as TestUtil;
|
||||
|
||||
/**
|
||||
* Handler for applying reinstallMagento annotation.
|
||||
*/
|
||||
class CopyModules
|
||||
{
|
||||
/**
|
||||
* @var TestModuleManager
|
||||
*/
|
||||
private $moduleManager;
|
||||
|
||||
/**
|
||||
* @var CliCommand
|
||||
*/
|
||||
private $cliCommand;
|
||||
|
||||
/**
|
||||
* CopyModules constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->moduleManager = new TestModuleManager();
|
||||
$this->cliCommand = new CliCommand($this->moduleManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'startTest' event.
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function startTest(\PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
$annotations = TestUtil::parseTestMethodAnnotations(
|
||||
get_class($test),
|
||||
$test->getName(false)
|
||||
);
|
||||
//This annotation can be declared only on method level
|
||||
if (isset($annotations['method']['moduleName'])) {
|
||||
$moduleNames = $annotations['method']['moduleName'];
|
||||
|
||||
foreach ($moduleNames as $moduleName) {
|
||||
$this->cliCommand->introduceModule($moduleName);
|
||||
//Include module`s registration.php to load it
|
||||
$path = MAGENTO_MODULES_PATH . explode("_", $moduleName)[1] . '/registration.php';
|
||||
include $path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'startTest' event
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
*/
|
||||
public function endTest(\PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
$annotations = TestUtil::parseTestMethodAnnotations(
|
||||
get_class($test),
|
||||
$test->getName(false)
|
||||
);
|
||||
//This annotation can be declared only on method level
|
||||
if (!empty($annotations['method']['moduleName'])) {
|
||||
foreach ($annotations['method']['moduleName'] as $moduleName) {
|
||||
$path = MAGENTO_MODULES_PATH .
|
||||
//Take only module name from Magento_ModuleName
|
||||
explode("_", $moduleName)[1];
|
||||
File::rmdirRecursive($path);
|
||||
$this->unsergisterModuleFromComponentRegistrar($moduleName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister module from component registrar.
|
||||
* The component registrar uses static private variable and does not provide unregister method,
|
||||
* however unregister is required to remove registered modules after they are deleted from app/code.
|
||||
*
|
||||
* @param string $moduleName
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function unsergisterModuleFromComponentRegistrar($moduleName)
|
||||
{
|
||||
$reflection = new \ReflectionClass(ComponentRegistrar::class);
|
||||
$reflectionProperty = $reflection->getProperty('paths');
|
||||
$reflectionProperty->setAccessible(true);
|
||||
$value = $reflectionProperty->getValue();
|
||||
unset($value[ComponentRegistrar::MODULE][$moduleName]);
|
||||
$reflectionProperty->setValue(null, $value);
|
||||
}
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\DB\Adapter\SqlVersionProvider;
|
||||
use Magento\TestFramework\Deploy\CliCommand;
|
||||
use Magento\TestFramework\Deploy\TestModuleManager;
|
||||
use Magento\TestFramework\TestCase\MutableDataInterface;
|
||||
use PHPUnit\Util\Test as TestUtil;
|
||||
|
||||
/**
|
||||
* Handler for applying reinstallMagento annotation.
|
||||
*/
|
||||
class DataProviderFromFile
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const FALLBACK_VALUE = 'default';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public const POSSIBLE_SUFFIXES = [
|
||||
SqlVersionProvider::MYSQL_8_0_VERSION => 'mysql8',
|
||||
SqlVersionProvider::MARIA_DB_10_4_VERSION => 'mariadb10',
|
||||
SqlVersionProvider::MARIA_DB_10_6_VERSION => 'mariadb106',
|
||||
SqlVersionProvider::MYSQL_8_0_29_VERSION => 'mysql829',
|
||||
SqlVersionProvider::MARIA_DB_10_4_27_VERSION => 'mariadb10427',
|
||||
SqlVersionProvider::MARIA_DB_10_6_11_VERSION => 'mariadb10611',
|
||||
SqlVersionProvider::MARIA_DB_10_11_11_VERSION => 'mariadb101111',
|
||||
SqlVersionProvider::MARIA_DB_10_11_VERSION => 'mariadb1011'
|
||||
];
|
||||
|
||||
/**
|
||||
* @var TestModuleManager
|
||||
*/
|
||||
private $moduleManager;
|
||||
|
||||
/**
|
||||
* @var CliCommand
|
||||
*/
|
||||
private $cliCommand;
|
||||
|
||||
/**
|
||||
* CopyModules constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->moduleManager = new TestModuleManager();
|
||||
$this->cliCommand = new CliCommand($this->moduleManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start test.
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function startTest(\PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
$annotations = TestUtil::parseTestMethodAnnotations(
|
||||
get_class($test),
|
||||
$test->getName(false)
|
||||
);
|
||||
//This annotation can be declared only on method level
|
||||
if (isset($annotations['method']['dataProviderFromFile']) && $test instanceof MutableDataInterface) {
|
||||
$test->setData(
|
||||
$this->loadAllFiles(TESTS_MODULES_PATH . "/" . $annotations['method']['dataProviderFromFile'][0])
|
||||
);
|
||||
} elseif (!$test instanceof MutableDataInterface) {
|
||||
throw new \Exception("Test type do not supports @dataProviderFromFile annotation");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish test.
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function endTest(\PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
if ($test instanceof MutableDataInterface) {
|
||||
$test->flushData();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load different db version files for different databases.
|
||||
*
|
||||
* @param string $path The path of the inital file.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function loadAllFiles(string $path): array
|
||||
{
|
||||
$result = [];
|
||||
$pathWithoutExtension = $this->removeFileExtension($path);
|
||||
|
||||
foreach (glob($pathWithoutExtension . '.*') as $file) {
|
||||
/* Search database string in file name like mysql8 with
|
||||
possibility to use version until patch level. */
|
||||
preg_match('/\.([\D]*[\d]*(?:\.[\d]+){0,2})/', $file, $splitedParts);
|
||||
$dbKey = self::FALLBACK_VALUE;
|
||||
|
||||
if (count($splitedParts) > 1) {
|
||||
$database = array_pop($splitedParts);
|
||||
|
||||
if ($this->isValidDatabaseSuffix($database)) {
|
||||
$dbKey = $database;
|
||||
}
|
||||
}
|
||||
|
||||
$result[$dbKey] = include $file;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the file extension from path.
|
||||
*
|
||||
* @param string $path The file path.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function removeFileExtension(string $path)
|
||||
{
|
||||
$result = explode('.', $path);
|
||||
array_pop($result);
|
||||
|
||||
return implode('.', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if database suffix is valid.
|
||||
*
|
||||
* @param string $databaseSuffix The suffix of the database from the file
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isValidDatabaseSuffix(string $databaseSuffix): bool
|
||||
{
|
||||
return in_array($databaseSuffix, self::POSSIBLE_SUFFIXES);
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Module\ModuleResource;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
|
||||
/**
|
||||
* Handler for applying reinstallMagento annotation.
|
||||
*/
|
||||
class ReinstallInstance
|
||||
{
|
||||
/**
|
||||
* @var \Magento\TestFramework\Application
|
||||
*/
|
||||
private $application;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Magento\TestFramework\Application $application
|
||||
*/
|
||||
public function __construct(\Magento\TestFramework\Application $application)
|
||||
{
|
||||
$this->application = $application;
|
||||
}
|
||||
|
||||
public function startTest()
|
||||
{
|
||||
/** @var ObjectManager $objectManager */
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$resourceConnection = $objectManager->create(ResourceConnection::class);
|
||||
$objectManager->removeSharedInstance(ResourceConnection::class);
|
||||
$objectManager->addSharedInstance($resourceConnection, ResourceConnection::class);
|
||||
$this->application->reinitialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTest' event.
|
||||
*
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function endTest()
|
||||
{
|
||||
$this->application->cleanup();
|
||||
$this->application->reinitialize();
|
||||
ModuleResource::flush();
|
||||
}
|
||||
}
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implementation of the @magentoSchemaFixture DocBlock annotation.
|
||||
*/
|
||||
namespace Magento\TestFramework\Annotation;
|
||||
|
||||
use PHPUnit\Util\Test as TestUtil;
|
||||
|
||||
/**
|
||||
* Represents following construction handling:
|
||||
*
|
||||
* @magentoSchemaFixture {link_to_file.php}
|
||||
*/
|
||||
class SchemaFixture
|
||||
{
|
||||
/**
|
||||
* Fixtures base directory.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fixtureBaseDir;
|
||||
|
||||
/**
|
||||
* Fixtures that have been applied.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $appliedFixtures = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $fixtureBaseDir
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function __construct($fixtureBaseDir)
|
||||
{
|
||||
if (!is_dir($fixtureBaseDir)) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
new \Magento\Framework\Phrase("Fixture base directory '%1' does not exist.", [$fixtureBaseDir])
|
||||
);
|
||||
}
|
||||
$this->fixtureBaseDir = realpath($fixtureBaseDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply magento data fixture on.
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
* @return void
|
||||
*/
|
||||
public function startTest(\PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
if ($this->_getFixtures($test)) {
|
||||
$this->_applyFixtures($this->_getFixtures($test));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish test execution.
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
*/
|
||||
public function endTest(\PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
if ($this->_getFixtures($test)) {
|
||||
$this->_revertFixtures();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve fixtures from annotation.
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
* @param string $scope
|
||||
* @return array
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
protected function _getFixtures(\PHPUnit\Framework\TestCase $test, $scope = null)
|
||||
{
|
||||
if ($scope === null) {
|
||||
$annotations = $this->getAnnotations($test);
|
||||
} else {
|
||||
$source = TestUtil::parseTestMethodAnnotations(
|
||||
get_class($test),
|
||||
$test->getName(false)
|
||||
);
|
||||
$annotations = $source[$scope];
|
||||
}
|
||||
$result = [];
|
||||
if (!empty($annotations['magentoSchemaFixture'])) {
|
||||
foreach ($annotations['magentoSchemaFixture'] as $fixture) {
|
||||
if (strpos($fixture, '\\') !== false) {
|
||||
// usage of a single directory separator symbol streamlines search across the source code
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
new \Magento\Framework\Phrase('Directory separator "\\" is prohibited in fixture declaration.')
|
||||
);
|
||||
}
|
||||
$fixtureMethod = [get_class($test), $fixture];
|
||||
if (is_callable($fixtureMethod)) {
|
||||
$result[] = $fixtureMethod;
|
||||
} else {
|
||||
$result[] = $this->fixtureBaseDir . '/' . $fixture;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get annotations for test.
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
* @return array
|
||||
*/
|
||||
private function getAnnotations(\PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
$annotations = TestUtil::parseTestMethodAnnotations(
|
||||
get_class($test),
|
||||
$test->getName(false)
|
||||
);
|
||||
|
||||
return array_replace($annotations['class'], $annotations['method']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute single fixture script.
|
||||
*
|
||||
* @param string|array $fixture
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function _applyOneFixture($fixture)
|
||||
{
|
||||
try {
|
||||
if (is_callable($fixture)) {
|
||||
call_user_func($fixture);
|
||||
} else {
|
||||
include $fixture;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception(
|
||||
sprintf("Error in fixture: %s.\n %s", json_encode($fixture), $e->getMessage()),
|
||||
500,
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute fixture scripts if any.
|
||||
*
|
||||
* @param array $fixtures
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
protected function _applyFixtures(array $fixtures)
|
||||
{
|
||||
/* Execute fixture scripts */
|
||||
foreach ($fixtures as $oneFixture) {
|
||||
/* Skip already applied fixtures */
|
||||
if (in_array($oneFixture, $this->appliedFixtures, true)) {
|
||||
continue;
|
||||
}
|
||||
$this->_applyOneFixture($oneFixture);
|
||||
$this->appliedFixtures[] = $oneFixture;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert changes done by fixtures.
|
||||
*/
|
||||
protected function _revertFixtures()
|
||||
{
|
||||
foreach ($this->appliedFixtures as $fixture) {
|
||||
if (is_callable($fixture)) {
|
||||
$fixture[1] .= 'Rollback';
|
||||
if (is_callable($fixture)) {
|
||||
$this->_applyOneFixture($fixture);
|
||||
}
|
||||
} else {
|
||||
$fileInfo = pathinfo($fixture);
|
||||
$extension = '';
|
||||
if (isset($fileInfo['extension'])) {
|
||||
$extension = '.' . $fileInfo['extension'];
|
||||
}
|
||||
$rollbackScript = $fileInfo['dirname'] . '/' . $fileInfo['filename'] . '_rollback' . $extension;
|
||||
if (file_exists($rollbackScript)) {
|
||||
$this->_applyOneFixture($rollbackScript);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->appliedFixtures = [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user