PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/framework/Module/Test/Unit/Setup/MigrationTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 251 lines | 164 code | 28 blank | 59 comment | 6 complexity | cf7b405623ca47ecfac96d682b4fbb5a MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Tests for resource setup model needed for migration process between Magento versions
  8. */
  9. namespace Magento\Framework\Module\Test\Unit\Setup;
  10. class MigrationTest extends \PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * Result of update class aliases to compare with expected.
  14. * Used in callback for \Magento\Framework\DB\Select::update.
  15. *
  16. * @var array
  17. */
  18. protected $_actualUpdateResult;
  19. /**
  20. * Where conditions to compare with expected.
  21. * Used in callback for \Magento\Framework\DB\Select::where.
  22. *
  23. * @var array
  24. */
  25. protected $_actualWhere;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Select
  28. */
  29. protected $_selectMock;
  30. protected function tearDown()
  31. {
  32. unset($this->_actualUpdateResult);
  33. unset($this->_actualWhere);
  34. unset($this->_selectMock);
  35. }
  36. /**
  37. * Retrieve all necessary objects mocks which used inside customer storage
  38. *
  39. * @param int $tableRowsCount
  40. * @param array $tableData
  41. * @param array $aliasesMap
  42. *
  43. * @return array
  44. */
  45. protected function _getModelDependencies($tableRowsCount = 0, $tableData = [], $aliasesMap = [])
  46. {
  47. $this->_selectMock = $this->getMock('Magento\Framework\DB\Select', [], [], '', false);
  48. $this->_selectMock->expects($this->any())->method('from')->will($this->returnSelf());
  49. $this->_selectMock->expects(
  50. $this->any()
  51. )->method(
  52. 'where'
  53. )->will(
  54. $this->returnCallback([$this, 'whereCallback'])
  55. );
  56. $connectionMock = $this->getMock(
  57. 'Magento\Framework\DB\Adapter\Pdo\Mysql',
  58. ['select', 'update', 'fetchAll', 'fetchOne'],
  59. [],
  60. '',
  61. false
  62. );
  63. $connectionMock->expects($this->any())->method('select')->will($this->returnValue($this->_selectMock));
  64. $connectionMock->expects(
  65. $this->any()
  66. )->method(
  67. 'update'
  68. )->will(
  69. $this->returnCallback([$this, 'updateCallback'])
  70. );
  71. $connectionMock->expects($this->any())->method('fetchAll')->will($this->returnValue($tableData));
  72. $connectionMock->expects($this->any())->method('fetchOne')->will($this->returnValue($tableRowsCount));
  73. return [
  74. 'resource_config' => 'not_used',
  75. 'connection_config' => 'not_used',
  76. 'module_config' => 'not_used',
  77. 'base_dir' => 'not_used',
  78. 'path_to_map_file' => 'not_used',
  79. 'connection' => $connectionMock,
  80. 'core_helper' => $this->getMock('Magento\Framework\Json\Helper\Data', [], [], '', false, false),
  81. 'aliases_map' => $aliasesMap
  82. ];
  83. }
  84. /**
  85. * Callback for \Magento\Framework\DB\Select::update
  86. *
  87. * @param string $table
  88. * @param array $bind
  89. * @param array $where
  90. */
  91. public function updateCallback($table, array $bind, $where)
  92. {
  93. $fields = array_keys($bind);
  94. $replacements = array_values($bind);
  95. $this->_actualUpdateResult[] = [
  96. 'table' => $table,
  97. 'field' => $fields[0],
  98. 'to' => $replacements[0],
  99. 'from' => $where,
  100. ];
  101. }
  102. /**
  103. * Callback for \Magento\Framework\DB\Select::where
  104. *
  105. * @param string $condition
  106. * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Select
  107. */
  108. public function whereCallback($condition)
  109. {
  110. if (null === $this->_actualWhere) {
  111. $this->_actualWhere = [];
  112. }
  113. if (!empty($condition) && false === strpos(
  114. $condition,
  115. ' IS NOT NULL'
  116. ) && !in_array(
  117. $condition,
  118. $this->_actualWhere
  119. )
  120. ) {
  121. $this->_actualWhere[] = $condition;
  122. }
  123. return $this->_selectMock;
  124. }
  125. /**
  126. * @covers \Magento\Framework\Module\Setup\Migration::appendClassAliasReplace
  127. */
  128. public function testAppendClassAliasReplace()
  129. {
  130. $setupMock = $this->getMockForAbstractClass('\Magento\Framework\Setup\ModuleDataSetupInterface');
  131. $filesystemMock = $this->getMock('Magento\Framework\Filesystem', [], [], '', false);
  132. $migrationData = $this->getMock('Magento\Framework\Module\Setup\MigrationData', [], [], '', false);
  133. $setupModel = new \Magento\Framework\Module\Setup\Migration(
  134. $setupMock,
  135. $filesystemMock,
  136. $migrationData,
  137. 'app/etc/aliases_to_classes_map.json'
  138. );
  139. $setupModel->appendClassAliasReplace(
  140. 'tableName',
  141. 'fieldName',
  142. 'entityType',
  143. 'fieldContentType',
  144. ['pk_field1', 'pk_field2'],
  145. 'additionalWhere'
  146. );
  147. $expectedRulesList = [
  148. 'tableName' => [
  149. 'fieldName' => [
  150. 'entity_type' => 'entityType',
  151. 'content_type' => 'fieldContentType',
  152. 'pk_fields' => ['pk_field1', 'pk_field2'],
  153. 'additional_where' => 'additionalWhere',
  154. ],
  155. ],
  156. ];
  157. $this->assertAttributeEquals($expectedRulesList, '_replaceRules', $setupModel);
  158. // Check that replace for the same field is not set twice
  159. $setupModel->appendClassAliasReplace(
  160. 'tableName',
  161. 'fieldName',
  162. 'newEntityType',
  163. 'newFieldContentType',
  164. ['new_pk_field1', 'new_pk_field2'],
  165. 'newAdditionalWhere'
  166. );
  167. $this->assertAttributeEquals($expectedRulesList, '_replaceRules', $setupModel);
  168. }
  169. /**
  170. * @dataProvider updateClassAliasesDataProvider
  171. */
  172. public function testDoUpdateClassAliases($replaceRules, $tableData, $expected, $aliasesMap = [])
  173. {
  174. $this->markTestIncomplete('Requires refactoring of class that is tested, covers to many methods');
  175. $this->_actualUpdateResult = [];
  176. $tableRowsCount = count($tableData);
  177. $setupMock = $this->getMockForAbstractClass('\Magento\Framework\Setup\ModuleDataSetupInterface');
  178. $filesystemMock = $this->getMock('Magento\Framework\Filesystem', [], [], '', false);
  179. $migrationData = $this->getMock('Magento\Framework\Module\Setup\MigrationData', [], [], '', false);
  180. $setupModel = new \Magento\Framework\Module\Setup\Migration(
  181. $setupMock,
  182. $filesystemMock,
  183. $migrationData,
  184. 'app/etc/aliases_to_classes_map.json',
  185. $this->_getModelDependencies($tableRowsCount, $tableData, $aliasesMap)
  186. );
  187. foreach ($replaceRules as $replaceRule) {
  188. call_user_func_array([$setupModel, 'appendClassAliasReplace'], $replaceRule);
  189. }
  190. $setupModel->doUpdateClassAliases();
  191. $this->assertEquals($expected['updates'], $this->_actualUpdateResult);
  192. if (isset($expected['where'])) {
  193. $this->assertEquals($expected['where'], $this->_actualWhere);
  194. }
  195. if (isset($expected['aliases_map'])) {
  196. $this->assertAttributeEquals($expected['aliases_map'], '_aliasesMap', $setupModel);
  197. }
  198. }
  199. /**
  200. * Data provider for updating class aliases
  201. *
  202. * @return array
  203. */
  204. public function updateClassAliasesDataProvider()
  205. {
  206. return [
  207. 'plain text replace model' => include __DIR__ . '/_files/data_content_plain_model.php',
  208. 'plain text replace resource' => include __DIR__ . '/_files/data_content_plain_resource.php',
  209. 'plain text replace with pk field' => include __DIR__ . '/_files/data_content_plain_pk_fields.php',
  210. 'xml replace' => include __DIR__ . '/_files/data_content_xml.php',
  211. 'wiki markup replace' => include __DIR__ . '/_files/data_content_wiki.php',
  212. 'serialized php replace' => include __DIR__ . '/_files/data_content_serialized.php'
  213. ];
  214. }
  215. /**
  216. * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem
  217. */
  218. protected function _getFilesystemMock()
  219. {
  220. $mock = $this->getMockBuilder('Magento\Framework\Filesystem')->disableOriginalConstructor()->getMock();
  221. return $mock;
  222. }
  223. }