DestinationTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Migration\ResourceModel;
  7. /**
  8. * ResourceModel destination test class
  9. */
  10. class DestinationTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Migration\Config|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $config;
  16. /**
  17. * @var \Migration\ResourceModel\Adapter\Mysql|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $adapter;
  20. /**
  21. * @var \Migration\ResourceModel\AdapterFactory|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $adapterFactory;
  24. /**
  25. * @var \Migration\ResourceModel\DocumentFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $documentFactory;
  28. /**
  29. * @var \Migration\ResourceModel\StructureFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $structureFactory;
  32. /**
  33. * @var \Migration\ResourceModel\Document\Collection|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $documentCollection;
  36. /**
  37. * @var \Migration\ResourceModel\Destination
  38. */
  39. protected $resourceDestination;
  40. /**
  41. * @return void
  42. */
  43. protected function setUp()
  44. {
  45. $adapterConfigs = ['resourceType' => 'destination'];
  46. $this->config = $this->createPartialMock(
  47. \Migration\Config::class,
  48. ['getOption']
  49. );
  50. $this->adapter = $this->createPartialMock(
  51. \Migration\ResourceModel\Adapter\Mysql::class,
  52. ['insertRecords', 'deleteAllRecords', 'backupDocument', 'rollbackDocument', 'deleteBackup']
  53. );
  54. $this->adapterFactory = $this->createPartialMock(
  55. \Migration\ResourceModel\AdapterFactory::class,
  56. ['create']
  57. );
  58. $this->adapterFactory->expects($this->once())
  59. ->method('create')
  60. ->with($adapterConfigs)
  61. ->will($this->returnValue($this->adapter));
  62. $this->documentFactory = $this->createMock(\Migration\ResourceModel\DocumentFactory::class);
  63. $this->structureFactory = $this->createMock(\Migration\ResourceModel\StructureFactory::class);
  64. $this->documentCollection = $this->createMock(\Migration\ResourceModel\Document\Collection::class);
  65. $this->resourceDestination = new \Migration\ResourceModel\Destination(
  66. $this->adapterFactory,
  67. $this->config,
  68. $this->documentFactory,
  69. $this->structureFactory,
  70. $this->documentCollection
  71. );
  72. }
  73. /**
  74. * @dataProvider saveRecordsDataSet()
  75. * @param string|null @prefix
  76. * @return void
  77. */
  78. public function testSaveRecords($prefix)
  79. {
  80. $resourceName = 'core_config_data';
  81. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  82. ['edition_migrate', 'opensource-to-commerce'],
  83. ['bulk_size', 3],
  84. ['dest_prefix', $prefix],
  85. ['init_statements_destination', 'SET NAMES utf8;']
  86. ]);
  87. $this->adapter->expects($this->at(0))
  88. ->method('insertRecords')
  89. ->with($prefix . $resourceName, [['data' => 'value1'], ['data' => 'value2'], ['data' => 'value3']])
  90. ->will($this->returnSelf());
  91. $this->adapter->expects($this->at(1))
  92. ->method('insertRecords')
  93. ->with($prefix . $resourceName, [['data' => 'value4']])
  94. ->will($this->returnSelf());
  95. $records = $this->createMock(\Migration\ResourceModel\Record\Collection::class);
  96. $records->expects($this->any())
  97. ->method('current')
  98. ->willReturnCallback(function () {
  99. static $count = 0;
  100. $count++;
  101. $data = ['data' => "value$count"];
  102. $record = $this->createPartialMock(
  103. \Migration\ResourceModel\Record::class,
  104. ['getData']
  105. );
  106. $record->expects($this->once())->method('getData')->will($this->returnValue($data));
  107. return $record;
  108. });
  109. $records->expects($this->any())
  110. ->method('valid')
  111. ->willReturnCallback(function () {
  112. static $count = 0;
  113. $count++;
  114. if ($count <= 4) {
  115. return true;
  116. } else {
  117. return false;
  118. }
  119. });
  120. $this->resourceDestination->saveRecords($resourceName, $records);
  121. }
  122. /**
  123. * @return array
  124. */
  125. public function saveRecordsDataSet()
  126. {
  127. return [
  128. ['prefix_'],
  129. [null]
  130. ];
  131. }
  132. /**
  133. * @return void
  134. */
  135. public function testClearDocument()
  136. {
  137. $docName = 'somename';
  138. $this->adapter->expects($this->once())->method('deleteAllRecords')->with('pfx_' . $docName);
  139. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  140. ['edition_migrate', 'opensource-to-commerce'],
  141. ['dest_prefix', 'pfx_'],
  142. ['init_statements_destination', 'SET NAMES utf8;']
  143. ]);
  144. $this->resourceDestination->clearDocument($docName);
  145. }
  146. /**
  147. * @return void
  148. */
  149. public function testBackupDocument()
  150. {
  151. $docName = 'somename';
  152. $this->adapter->expects($this->once())->method('backupDocument')->with('pfx_' . $docName);
  153. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  154. ['edition_migrate', 'opensource-to-commerce'],
  155. ['dest_prefix', 'pfx_'],
  156. ['init_statements_destination', 'SET NAMES utf8;']
  157. ]);
  158. $this->resourceDestination->backupDocument($docName);
  159. }
  160. /**
  161. * @return void
  162. */
  163. public function testRollbackDocument()
  164. {
  165. $docName = 'somename';
  166. $this->adapter->expects($this->once())->method('rollbackDocument')->with('pfx_' . $docName);
  167. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  168. ['edition_migrate', 'opensource-to-commerce'],
  169. ['dest_prefix', 'pfx_'],
  170. ['init_statements_destination', 'SET NAMES utf8;']
  171. ]);
  172. $this->resourceDestination->rollbackDocument($docName);
  173. }
  174. /**
  175. * @return void
  176. */
  177. public function testDeleteDocumentBackup()
  178. {
  179. $docName = 'somename';
  180. $this->adapter->expects($this->once())->method('deleteBackup')->with('pfx_' . $docName);
  181. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  182. ['edition_migrate', 'opensource-to-commerce'],
  183. ['dest_prefix', 'pfx_'],
  184. ['init_statements_destination', 'SET NAMES utf8;']
  185. ]);
  186. $this->resourceDestination->deleteDocumentBackup($docName);
  187. }
  188. }