Helper.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Migration\TestFramework;
  7. /**
  8. * Helper for preparing databases, initialize ObjectManager
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class Helper
  12. {
  13. /**
  14. * @var \Migration\TestFramework\Helper
  15. */
  16. protected static $instance;
  17. /**
  18. * @var \Magento\Framework\ObjectManagerInterface
  19. */
  20. protected $objectManager;
  21. /**
  22. * @var \Magento\Framework\Shell
  23. */
  24. protected $shell;
  25. /**
  26. * @var string
  27. */
  28. protected $magentoDir;
  29. /**
  30. * @var string
  31. */
  32. protected $dbFixturePath;
  33. /**
  34. * @var string
  35. */
  36. public $configPath;
  37. /**
  38. * @var string
  39. */
  40. protected $testSuite;
  41. /**
  42. * @var array
  43. */
  44. protected $testFixtures;
  45. /**
  46. * @var string
  47. */
  48. protected $currentFixture;
  49. /**
  50. * @param \Magento\Framework\Shell $shell
  51. * @param string $magentoDir
  52. * @param string $dbFixturePath
  53. * @throws \Exception
  54. */
  55. public function __construct(
  56. \Magento\Framework\Shell $shell,
  57. $magentoDir,
  58. $dbFixturePath
  59. ) {
  60. $this->shell = $shell;
  61. $this->magentoDir = $magentoDir;
  62. $this->dbFixturePath = $dbFixturePath;
  63. }
  64. /**
  65. * Initializes and returns singleton instance of this class
  66. *
  67. * @return Helper
  68. */
  69. public static function getInstance()
  70. {
  71. if (!self::$instance) {
  72. $shell = new \Magento\Framework\Shell(new \Magento\Framework\Shell\CommandRenderer());
  73. $magentoDir = require __DIR__ . '/../../../etc/magento_path.php';
  74. $dbFixturePath = __DIR__ . '/../resource/';
  75. self::$instance = new Helper(
  76. $shell,
  77. $magentoDir,
  78. $dbFixturePath
  79. );
  80. }
  81. return self::$instance;
  82. }
  83. /**
  84. * Getter for ObjectManager
  85. *
  86. * @return \Magento\Framework\ObjectManagerInterface
  87. */
  88. public function getObjectManager()
  89. {
  90. if (!$this->objectManager) {
  91. $this->objectManager = $this->initObjectManager();
  92. }
  93. $this->objectManager->configure([
  94. 'preferences' => [
  95. \Migration\App\ProgressBar\LogLevelProcessor::class => \Migration\TestFramework\ProgressBar::class
  96. ],
  97. \Migration\Logger\Logger::class => [
  98. 'arguments' => [
  99. 'handlers' => [
  100. 'quiet' => [
  101. 'instance' => \Migration\TestFramework\QuietLogHandler::class
  102. ]
  103. ]
  104. ]
  105. ],
  106. \Migration\ResourceModel\Source::class => ['shared' => false],
  107. \Migration\ResourceModel\Destination::class => ['shared' => false],
  108. ]);
  109. return $this->objectManager;
  110. }
  111. /**
  112. * Init ObjectManager
  113. *
  114. * @return \Magento\Framework\ObjectManagerInterface
  115. */
  116. protected function initObjectManager()
  117. {
  118. $dirList = new \Magento\Framework\App\Filesystem\DirectoryList($this->magentoDir);
  119. $driverPool = new \Magento\Framework\Filesystem\DriverPool;
  120. $configFilePool = new \Magento\Framework\Config\File\ConfigFilePool;
  121. return (new \Magento\Framework\App\ObjectManagerFactory($dirList, $driverPool, $configFilePool))->create([]);
  122. }
  123. /**
  124. * Reinstall Db for source and destination
  125. *
  126. * @param string $fixturePath
  127. * @throws \Exception
  128. * @return void
  129. */
  130. protected function reinstallDb($fixturePath)
  131. {
  132. $mysqlConfigPath = dirname(__DIR__) . '/etc/mysql.php';
  133. if (!is_file($mysqlConfigPath)) {
  134. throw new \Exception('Database configuration file does not exist: ' . $mysqlConfigPath);
  135. }
  136. $resourceSource = $fixturePath . '/source.sql';
  137. $resourceDestination = $fixturePath . '/dest.sql';
  138. if (file_exists($this->dbFixturePath . $fixturePath)) {
  139. $resourceSource = $this->dbFixturePath . $fixturePath . '/source.sql';
  140. $resourceDestination = $this->dbFixturePath . $fixturePath . '/dest.sql';
  141. } elseif (!file_exists($fixturePath)) {
  142. throw new \Exception('Database fixture not found: ' . $fixturePath);
  143. }
  144. $config = include $mysqlConfigPath;
  145. $this->shell->execute(
  146. 'mysql --host=%s --user=%s --password=%s -e %s',
  147. [
  148. $config['source_db_host'],
  149. $config['source_db_user'],
  150. $config['source_db_pass'],
  151. "DROP DATABASE IF EXISTS `{$config['source_db_name']}`"
  152. ]
  153. );
  154. $this->shell->execute(
  155. 'mysql --host=%s --user=%s --password=%s -e %s',
  156. [
  157. $config['source_db_host'],
  158. $config['source_db_user'],
  159. $config['source_db_pass'],
  160. "CREATE DATABASE IF NOT EXISTS `{$config['source_db_name']}`"
  161. ]
  162. );
  163. $this->shell->execute(
  164. 'mysql --host=%s --user=%s --password=%s --database=%s < %s',
  165. [
  166. $config['source_db_host'],
  167. $config['source_db_user'],
  168. $config['source_db_pass'],
  169. $config['source_db_name'],
  170. $resourceSource
  171. ]
  172. );
  173. $this->shell->execute(
  174. 'mysql --host=%s --user=%s --password=%s -e %s',
  175. [
  176. $config['dest_db_host'],
  177. $config['dest_db_user'],
  178. $config['dest_db_pass'],
  179. "DROP DATABASE IF EXISTS `{$config['dest_db_name']}`"
  180. ]
  181. );
  182. $this->shell->execute(
  183. 'mysql --host=%s --user=%s --password=%s -e %s',
  184. [
  185. $config['dest_db_host'],
  186. $config['dest_db_user'],
  187. $config['dest_db_pass'],
  188. "CREATE DATABASE `{$config['dest_db_name']}`"
  189. ]
  190. );
  191. $this->shell->execute(
  192. 'mysql --host=%s --user=%s --password=%s --database=%s < %s',
  193. [
  194. $config['dest_db_host'],
  195. $config['dest_db_user'],
  196. $config['dest_db_pass'],
  197. $config['dest_db_name'],
  198. $resourceDestination
  199. ]
  200. );
  201. }
  202. /**
  203. * getter for config path
  204. *
  205. * @return string
  206. */
  207. public function getConfigPath()
  208. {
  209. return $this->configPath;
  210. }
  211. /**
  212. * @return string
  213. */
  214. public function getTestSuite()
  215. {
  216. return $this->testSuite;
  217. }
  218. /**
  219. * @param string $testSuite
  220. * @return $this
  221. */
  222. public function setTestSuite($testSuite)
  223. {
  224. $this->testSuite = $testSuite;
  225. return $this;
  226. }
  227. /**
  228. * @param array $annotations
  229. * @return void
  230. */
  231. public function loadFixture($annotations)
  232. {
  233. $fixture = 'default';
  234. $annotations = array_replace($annotations['class'], $annotations['method']);
  235. if (!empty($annotations['dbFixture'])) {
  236. $fixtureName = $this->getFixturePrefix() . reset($annotations['dbFixture']);
  237. $fixture = (is_dir($this->dbFixturePath . $fixtureName))
  238. ? $fixtureName
  239. : reset($annotations['dbFixture']);
  240. }
  241. if (!isset($this->testFixtures[$this->getTestSuite()]) || $this->currentFixture != $fixture) {
  242. $this->reinstallDb($fixture);
  243. $this->testFixtures[$this->getTestSuite()] = $fixture;
  244. $this->currentFixture = $fixture;
  245. }
  246. }
  247. /**
  248. * Check if fixture prefix defined and return it
  249. *
  250. * @return string
  251. */
  252. public function getFixturePrefix()
  253. {
  254. $prefix = null;
  255. if (defined('FIXTURE_PREFIX')) {
  256. $prefix = FIXTURE_PREFIX;
  257. }
  258. return (string)$prefix;
  259. }
  260. }