Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MigrationTest.php
Go to the documentation of this file.
1 <?php
11 
12 class MigrationTest extends \PHPUnit\Framework\TestCase
13 {
21 
28  protected $_actualWhere;
29 
33  protected $_selectMock;
34 
35  protected function tearDown()
36  {
37  unset($this->_actualUpdateResult);
38  unset($this->_actualWhere);
39  unset($this->_selectMock);
40  }
41 
51  protected function _getModelDependencies($tableRowsCount = 0, $tableData = [], $aliasesMap = [])
52  {
53  $this->_selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
54  $this->_selectMock->expects($this->any())->method('from')->will($this->returnSelf());
55  $this->_selectMock->expects(
56  $this->any()
57  )->method(
58  'where'
59  )->will(
60  $this->returnCallback([$this, 'whereCallback'])
61  );
62 
63  $connectionMock = $this->createPartialMock(
64  \Magento\Framework\DB\Adapter\Pdo\Mysql::class,
65  ['select', 'update', 'fetchAll', 'fetchOne']
66  );
67  $connectionMock->expects($this->any())->method('select')->will($this->returnValue($this->_selectMock));
68  $connectionMock->expects(
69  $this->any()
70  )->method(
71  'update'
72  )->will(
73  $this->returnCallback([$this, 'updateCallback'])
74  );
75  $connectionMock->expects($this->any())->method('fetchAll')->will($this->returnValue($tableData));
76  $connectionMock->expects($this->any())->method('fetchOne')->will($this->returnValue($tableRowsCount));
77 
78  return [
79  'resource_config' => 'not_used',
80  'connection_config' => 'not_used',
81  'module_config' => 'not_used',
82  'base_dir' => 'not_used',
83  'path_to_map_file' => 'not_used',
84  'connection' => $connectionMock,
85  'core_helper' => $this->createMock(\Magento\Framework\Json\Helper\Data::class),
86  'aliases_map' => $aliasesMap
87  ];
88  }
89 
97  public function updateCallback($table, array $bind, $where)
98  {
99  $fields = array_keys($bind);
100  $replacements = array_values($bind);
101 
102  $this->_actualUpdateResult[] = [
103  'table' => $table,
104  'field' => $fields[0],
105  'to' => $replacements[0],
106  'from' => $where,
107  ];
108  }
109 
116  public function whereCallback($condition)
117  {
118  if (null === $this->_actualWhere) {
119  $this->_actualWhere = [];
120  }
121  if (!empty($condition) && false === strpos(
122  $condition,
123  ' IS NOT NULL'
124  ) && !in_array(
125  $condition,
126  $this->_actualWhere
127  )
128  ) {
129  $this->_actualWhere[] = $condition;
130  }
131  return $this->_selectMock;
132  }
133 
137  public function testAppendClassAliasReplace()
138  {
139  $setupMock = $this->getMockForAbstractClass(\Magento\Framework\Setup\ModuleDataSetupInterface::class);
140  $filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
141  $migrationData = $this->createMock(\Magento\Framework\Module\Setup\MigrationData::class);
142 
143  $setupModel = new \Magento\Framework\Module\Setup\Migration(
144  $setupMock,
145  $filesystemMock,
146  $migrationData,
147  'app/etc/aliases_to_classes_map.json',
148  [],
149  $this->getSerializerMock()
150  );
151 
152  $setupModel->appendClassAliasReplace(
153  'tableName',
154  'fieldName',
155  'entityType',
156  'fieldContentType',
157  ['pk_field1', 'pk_field2'],
158  'additionalWhere'
159  );
160 
161  $expectedRulesList = [
162  'tableName' => [
163  'fieldName' => [
164  'entity_type' => 'entityType',
165  'content_type' => 'fieldContentType',
166  'pk_fields' => ['pk_field1', 'pk_field2'],
167  'additional_where' => 'additionalWhere',
168  ],
169  ],
170  ];
171 
172  $this->assertAttributeEquals($expectedRulesList, '_replaceRules', $setupModel);
173 
174  // Check that replace for the same field is not set twice
175  $setupModel->appendClassAliasReplace(
176  'tableName',
177  'fieldName',
178  'newEntityType',
179  'newFieldContentType',
180  ['new_pk_field1', 'new_pk_field2'],
181  'newAdditionalWhere'
182  );
183  $this->assertAttributeEquals($expectedRulesList, '_replaceRules', $setupModel);
184  }
185 
189  public function testDoUpdateClassAliases($replaceRules, $tableData, $expected, $aliasesMap = [])
190  {
191  $this->markTestIncomplete('Requires refactoring of class that is tested, covers to many methods');
192 
193  $this->_actualUpdateResult = [];
194  $tableRowsCount = count($tableData);
195 
196  $setupMock = $this->getMockForAbstractClass(\Magento\Framework\Setup\ModuleDataSetupInterface::class);
197  $filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
198  $migrationData = $this->createMock(\Magento\Framework\Module\Setup\MigrationData::class);
199 
200  $setupModel = new \Magento\Framework\Module\Setup\Migration(
201  $setupMock,
202  $filesystemMock,
203  $migrationData,
204  'app/etc/aliases_to_classes_map.json',
205  $this->_getModelDependencies($tableRowsCount, $tableData, $aliasesMap),
206  $this->getSerializerMock()
207  );
208 
209  foreach ($replaceRules as $replaceRule) {
210  call_user_func_array([$setupModel, 'appendClassAliasReplace'], $replaceRule);
211  }
212 
213  $setupModel->doUpdateClassAliases();
214 
215  $this->assertEquals($expected['updates'], $this->_actualUpdateResult);
216 
217  if (isset($expected['where'])) {
218  $this->assertEquals($expected['where'], $this->_actualWhere);
219  }
220 
221  if (isset($expected['aliases_map'])) {
222  $this->assertAttributeEquals($expected['aliases_map'], '_aliasesMap', $setupModel);
223  }
224  }
225 
232  {
233  return [
234  'plain text replace model' => include __DIR__ . '/_files/data_content_plain_model.php',
235  'plain text replace resource' => include __DIR__ . '/_files/data_content_plain_resource.php',
236  'plain text replace with pk field' => include __DIR__ . '/_files/data_content_plain_pk_fields.php',
237  'xml replace' => include __DIR__ . '/_files/data_content_xml.php',
238  'wiki markup replace' => include __DIR__ . '/_files/data_content_wiki.php',
239  'serialized php replace' => include __DIR__ . '/_files/data_content_serialized.php'
240  ];
241  }
242 
246  protected function _getFilesystemMock()
247  {
248  $mock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)->disableOriginalConstructor()->getMock();
249  return $mock;
250  }
251 
256  private function getSerializerMock()
257  {
258  $serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
259  ->getMock();
260 
261  $serializerMock->expects($this->any())
262  ->method('unserialize')
263  ->willReturnCallback(
264  function ($serializedData) {
265  return json_decode($serializedData, true);
266  }
267  );
268  return $serializerMock;
269  }
270 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$fields
Definition: details.phtml:14
testDoUpdateClassAliases($replaceRules, $tableData, $expected, $aliasesMap=[])
$table
Definition: trigger.php:14
_getModelDependencies($tableRowsCount=0, $tableData=[], $aliasesMap=[])