SourceTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 source test class
  9. */
  10. class SourceTest 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\Source
  26. */
  27. protected $resourceSource;
  28. /**
  29. * @var \Migration\ResourceModel\DocumentFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $documentFactory;
  32. /**
  33. * @var \Migration\ResourceModel\StructureFactory|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $structureFactory;
  36. /**
  37. * @var \Migration\ResourceModel\Document|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $document;
  40. /**
  41. * @var \Migration\ResourceModel\Structure|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $structure;
  44. /**
  45. * @var \Migration\ResourceModel\Document\Collection|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $documentCollection;
  48. /**
  49. * @var int
  50. */
  51. protected $bulkSize = 10;
  52. /**
  53. * @return void
  54. */
  55. protected function setUp()
  56. {
  57. $adapterConfigs = ['resourceType' => 'source'];
  58. $this->config = $this->createPartialMock(
  59. \Migration\Config::class,
  60. ['getOption']
  61. );
  62. $this->adapter = $this->createPartialMock(
  63. \Migration\ResourceModel\Adapter\Mysql::class,
  64. [
  65. 'select',
  66. 'fetchAll',
  67. 'query',
  68. 'loadPage',
  69. 'createDelta',
  70. 'loadChangedRecords',
  71. 'loadDeletedRecords',
  72. 'getDocumentStructure',
  73. 'getRecords'
  74. ]
  75. );
  76. $this->adapterFactory = $this->createPartialMock(
  77. \Migration\ResourceModel\AdapterFactory::class,
  78. ['create']
  79. );
  80. $this->adapterFactory->expects($this->once())
  81. ->method('create')
  82. ->with($adapterConfigs)
  83. ->will($this->returnValue($this->adapter));
  84. $this->documentFactory = $this->getMockBuilder(\Migration\ResourceModel\DocumentFactory::class)
  85. ->setMethods(['create'])
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $this->document = $this->getMockBuilder(\Migration\ResourceModel\Document::class)
  89. ->setMethods(['getStructure'])
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $this->structureFactory = $this->getMockBuilder(\Migration\ResourceModel\StructureFactory::class)
  93. ->setMethods(['create'])
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $this->structure = $this->getMockBuilder(\Migration\ResourceModel\Structure::class)
  97. ->setMethods(['getFields'])
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $this->documentCollection = $this->createMock(\Migration\ResourceModel\Document\Collection::class);
  101. $this->resourceSource = new \Migration\ResourceModel\Source(
  102. $this->adapterFactory,
  103. $this->config,
  104. $this->documentFactory,
  105. $this->structureFactory,
  106. $this->documentCollection
  107. );
  108. }
  109. /**
  110. * @return void
  111. */
  112. public function testLoadPage()
  113. {
  114. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  115. ['edition_migrate', 'opensource-to-commerce'],
  116. ['bulk_size', $this->bulkSize],
  117. ['init_statements_source', 'SET NAMES utf8;']
  118. ]);
  119. $this->adapter->expects($this->any())->method('loadPage')->with('table', 2)->willReturn(['1', '2']);
  120. $this->assertEquals(['1', '2'], $this->resourceSource->loadPage('table', 2));
  121. }
  122. /**
  123. * @return void
  124. */
  125. public function testCreateDelta()
  126. {
  127. $this->adapter->expects($this->once())->method('createDelta')
  128. ->with('spfx_document', 'spfx_m2_cl_document', 'key_field');
  129. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  130. ['edition_migrate', 'opensource-to-commerce'],
  131. [Source::CONFIG_DOCUMENT_PREFIX, 'spfx_'],
  132. ['init_statements_source', 'SET NAMES utf8;']
  133. ]);
  134. $this->resourceSource->createDelta('document', 'key_field');
  135. }
  136. /**
  137. * @return void
  138. */
  139. public function testGetChangedRecords()
  140. {
  141. $this->adapter->expects($this->once())->method('loadChangedRecords')
  142. ->with('document', 'm2_cl_document', 'key_field', 0, 100);
  143. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  144. ['edition_migrate', 'opensource-to-commerce'],
  145. ['source_prefix', ''],
  146. ['bulk_size', 100],
  147. ['init_statements_source', 'SET NAMES utf8;']
  148. ]);
  149. $this->resourceSource->getChangedRecords('document', 'key_field');
  150. }
  151. /**
  152. * @return void
  153. */
  154. public function testGetDeletedRecords()
  155. {
  156. $this->adapter->expects($this->once())->method('loadDeletedRecords')
  157. ->with('m2_cl_document', 'key_field', 0, 100);
  158. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  159. ['edition_migrate', 'opensource-to-commerce'],
  160. ['source_prefix', ''],
  161. ['bulk_size', 100],
  162. ['init_statements_source', 'SET NAMES utf8;']
  163. ]);
  164. $this->resourceSource->getDeletedRecords('document', 'key_field');
  165. }
  166. /**
  167. * @return void
  168. */
  169. public function testGetRecordsWithOneBulkSize()
  170. {
  171. $document = 'doc1';
  172. $structureData = ['id' => 'int'];
  173. $records = [['id' => 0, 'field1' => 'data']];
  174. $fields = ['id' => ['PRIMARY' => true, 'IDENTITY' => true, 'COLUMN_NAME' => 'id']];
  175. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  176. ['edition_migrate', 'opensource-to-opensource'],
  177. ['source_prefix', ''],
  178. ['bulk_size', 1],
  179. ['init_statements_source', 'SET NAMES utf8;']
  180. ]);
  181. $this->adapter
  182. ->expects($this->any())
  183. ->method('getDocumentStructure')
  184. ->with($document)
  185. ->willReturn($structureData);
  186. $this->adapter->expects($this->any())->method('loadPage')->with($document, 0, 1, 'id', 1)->willReturn($records);
  187. $this->structureFactory
  188. ->method('create')
  189. ->with(['documentName' => $document, 'data' => $structureData])
  190. ->willReturn($this->structure);
  191. $this->documentFactory
  192. ->method('create')
  193. ->with(['structure' => $this->structure, 'documentName' => $document])
  194. ->willReturn($this->document);
  195. $this->document->expects($this->any())->method('getStructure')->will($this->returnValue($this->structure));
  196. $this->structure->expects($this->any())->method('getFields')->will($this->returnValue($fields));
  197. $this->resourceSource->setLastLoadedRecord($document, $records[0]);
  198. $this->assertEquals($records, $this->resourceSource->getRecords($document, 0));
  199. }
  200. }