123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Migration\TestFramework;
- /**
- * Helper for preparing databases, initialize ObjectManager
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Helper
- {
- /**
- * @var \Migration\TestFramework\Helper
- */
- protected static $instance;
- /**
- * @var \Magento\Framework\ObjectManagerInterface
- */
- protected $objectManager;
- /**
- * @var \Magento\Framework\Shell
- */
- protected $shell;
- /**
- * @var string
- */
- protected $magentoDir;
- /**
- * @var string
- */
- protected $dbFixturePath;
- /**
- * @var string
- */
- public $configPath;
- /**
- * @var string
- */
- protected $testSuite;
- /**
- * @var array
- */
- protected $testFixtures;
- /**
- * @var string
- */
- protected $currentFixture;
- /**
- * @param \Magento\Framework\Shell $shell
- * @param string $magentoDir
- * @param string $dbFixturePath
- * @throws \Exception
- */
- public function __construct(
- \Magento\Framework\Shell $shell,
- $magentoDir,
- $dbFixturePath
- ) {
- $this->shell = $shell;
- $this->magentoDir = $magentoDir;
- $this->dbFixturePath = $dbFixturePath;
- }
- /**
- * Initializes and returns singleton instance of this class
- *
- * @return Helper
- */
- public static function getInstance()
- {
- if (!self::$instance) {
- $shell = new \Magento\Framework\Shell(new \Magento\Framework\Shell\CommandRenderer());
- $magentoDir = require __DIR__ . '/../../../etc/magento_path.php';
- $dbFixturePath = __DIR__ . '/../resource/';
- self::$instance = new Helper(
- $shell,
- $magentoDir,
- $dbFixturePath
- );
- }
- return self::$instance;
- }
- /**
- * Getter for ObjectManager
- *
- * @return \Magento\Framework\ObjectManagerInterface
- */
- public function getObjectManager()
- {
- if (!$this->objectManager) {
- $this->objectManager = $this->initObjectManager();
- }
- $this->objectManager->configure([
- 'preferences' => [
- \Migration\App\ProgressBar\LogLevelProcessor::class => \Migration\TestFramework\ProgressBar::class
- ],
- \Migration\Logger\Logger::class => [
- 'arguments' => [
- 'handlers' => [
- 'quiet' => [
- 'instance' => \Migration\TestFramework\QuietLogHandler::class
- ]
- ]
- ]
- ],
- \Migration\ResourceModel\Source::class => ['shared' => false],
- \Migration\ResourceModel\Destination::class => ['shared' => false],
- ]);
- return $this->objectManager;
- }
- /**
- * Init ObjectManager
- *
- * @return \Magento\Framework\ObjectManagerInterface
- */
- protected function initObjectManager()
- {
- $dirList = new \Magento\Framework\App\Filesystem\DirectoryList($this->magentoDir);
- $driverPool = new \Magento\Framework\Filesystem\DriverPool;
- $configFilePool = new \Magento\Framework\Config\File\ConfigFilePool;
- return (new \Magento\Framework\App\ObjectManagerFactory($dirList, $driverPool, $configFilePool))->create([]);
- }
- /**
- * Reinstall Db for source and destination
- *
- * @param string $fixturePath
- * @throws \Exception
- * @return void
- */
- protected function reinstallDb($fixturePath)
- {
- $mysqlConfigPath = dirname(__DIR__) . '/etc/mysql.php';
- if (!is_file($mysqlConfigPath)) {
- throw new \Exception('Database configuration file does not exist: ' . $mysqlConfigPath);
- }
- $resourceSource = $fixturePath . '/source.sql';
- $resourceDestination = $fixturePath . '/dest.sql';
- if (file_exists($this->dbFixturePath . $fixturePath)) {
- $resourceSource = $this->dbFixturePath . $fixturePath . '/source.sql';
- $resourceDestination = $this->dbFixturePath . $fixturePath . '/dest.sql';
- } elseif (!file_exists($fixturePath)) {
- throw new \Exception('Database fixture not found: ' . $fixturePath);
- }
- $config = include $mysqlConfigPath;
- $this->shell->execute(
- 'mysql --host=%s --user=%s --password=%s -e %s',
- [
- $config['source_db_host'],
- $config['source_db_user'],
- $config['source_db_pass'],
- "DROP DATABASE IF EXISTS `{$config['source_db_name']}`"
- ]
- );
- $this->shell->execute(
- 'mysql --host=%s --user=%s --password=%s -e %s',
- [
- $config['source_db_host'],
- $config['source_db_user'],
- $config['source_db_pass'],
- "CREATE DATABASE IF NOT EXISTS `{$config['source_db_name']}`"
- ]
- );
- $this->shell->execute(
- 'mysql --host=%s --user=%s --password=%s --database=%s < %s',
- [
- $config['source_db_host'],
- $config['source_db_user'],
- $config['source_db_pass'],
- $config['source_db_name'],
- $resourceSource
- ]
- );
- $this->shell->execute(
- 'mysql --host=%s --user=%s --password=%s -e %s',
- [
- $config['dest_db_host'],
- $config['dest_db_user'],
- $config['dest_db_pass'],
- "DROP DATABASE IF EXISTS `{$config['dest_db_name']}`"
- ]
- );
- $this->shell->execute(
- 'mysql --host=%s --user=%s --password=%s -e %s',
- [
- $config['dest_db_host'],
- $config['dest_db_user'],
- $config['dest_db_pass'],
- "CREATE DATABASE `{$config['dest_db_name']}`"
- ]
- );
- $this->shell->execute(
- 'mysql --host=%s --user=%s --password=%s --database=%s < %s',
- [
- $config['dest_db_host'],
- $config['dest_db_user'],
- $config['dest_db_pass'],
- $config['dest_db_name'],
- $resourceDestination
- ]
- );
- }
- /**
- * getter for config path
- *
- * @return string
- */
- public function getConfigPath()
- {
- return $this->configPath;
- }
- /**
- * @return string
- */
- public function getTestSuite()
- {
- return $this->testSuite;
- }
- /**
- * @param string $testSuite
- * @return $this
- */
- public function setTestSuite($testSuite)
- {
- $this->testSuite = $testSuite;
- return $this;
- }
- /**
- * @param array $annotations
- * @return void
- */
- public function loadFixture($annotations)
- {
- $fixture = 'default';
- $annotations = array_replace($annotations['class'], $annotations['method']);
- if (!empty($annotations['dbFixture'])) {
- $fixtureName = $this->getFixturePrefix() . reset($annotations['dbFixture']);
- $fixture = (is_dir($this->dbFixturePath . $fixtureName))
- ? $fixtureName
- : reset($annotations['dbFixture']);
- }
- if (!isset($this->testFixtures[$this->getTestSuite()]) || $this->currentFixture != $fixture) {
- $this->reinstallDb($fixture);
- $this->testFixtures[$this->getTestSuite()] = $fixture;
- $this->currentFixture = $fixture;
- }
- }
- /**
- * Check if fixture prefix defined and return it
- *
- * @return string
- */
- public function getFixturePrefix()
- {
- $prefix = null;
- if (defined('FIXTURE_PREFIX')) {
- $prefix = FIXTURE_PREFIX;
- }
- return (string)$prefix;
- }
- }
|