AbstractResourceTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Migration\ResourceModel;
  7. use Migration\Reader\MapInterface;
  8. /**
  9. * Class AbstractResourceTest
  10. */
  11. class AbstractResourceTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Migration\Config|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $config;
  17. /**
  18. * @var \Migration\ResourceModel\Adapter\Mysql|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $adapter;
  21. /**
  22. * @var \Migration\ResourceModel\AdapterFactory|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $adapterFactorySource;
  25. /**
  26. * @var \Migration\ResourceModel\AdapterFactory|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $adapterFactoryDestination;
  29. /**
  30. * @var \Migration\ResourceModel\DocumentFactory|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $documentFactory;
  33. /**
  34. * @var \Migration\ResourceModel\StructureFactory|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $structureFactory;
  37. /**
  38. * @var \Migration\ResourceModel\Document\Collection|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $documentCollection;
  41. /**
  42. * @var \Migration\ResourceModel\Destination
  43. */
  44. protected $resourceDestination;
  45. /**
  46. * @var \Migration\ResourceModel\Source
  47. */
  48. protected $resourceSource;
  49. /**
  50. * @return void
  51. */
  52. protected function setUp()
  53. {
  54. $this->config = $this->createPartialMock(
  55. \Migration\Config::class,
  56. ['getOption']
  57. );
  58. $this->adapter = $this->createPartialMock(
  59. \Migration\ResourceModel\Adapter\Mysql::class,
  60. ['insertRecords', 'getRecordsCount', 'getDocumentStructure', 'getDocumentList', 'loadPage']
  61. );
  62. $this->adapterFactorySource = $this->createPartialMock(
  63. \Migration\ResourceModel\AdapterFactory::class,
  64. ['create']
  65. );
  66. $this->adapterFactorySource->expects($this->any())
  67. ->method('create')
  68. ->with(['resourceType' => 'source'])
  69. ->will($this->returnValue($this->adapter));
  70. $this->adapterFactoryDestination = $this->createPartialMock(
  71. \Migration\ResourceModel\AdapterFactory::class,
  72. ['create']
  73. );
  74. $this->adapterFactoryDestination->expects($this->any())
  75. ->method('create')
  76. ->with(['resourceType' => 'destination'])
  77. ->will($this->returnValue($this->adapter));
  78. $this->documentFactory = $this->getMockBuilder(\Migration\ResourceModel\DocumentFactory::class)
  79. ->disableOriginalConstructor()
  80. ->setMethods(['create'])
  81. ->getMock();
  82. $this->structureFactory = $this->getMockBuilder(\Migration\ResourceModel\StructureFactory::class)
  83. ->disableOriginalConstructor()
  84. ->setMethods(['create'])
  85. ->getMock();
  86. $this->documentCollection = $this->createPartialMock(
  87. \Migration\ResourceModel\Document\Collection::class,
  88. ['addDocument']
  89. );
  90. $this->resourceDestination = new \Migration\ResourceModel\Destination(
  91. $this->adapterFactoryDestination,
  92. $this->config,
  93. $this->documentFactory,
  94. $this->structureFactory,
  95. $this->documentCollection
  96. );
  97. $this->resourceSource = new \Migration\ResourceModel\Source(
  98. $this->adapterFactorySource,
  99. $this->config,
  100. $this->documentFactory,
  101. $this->structureFactory,
  102. $this->documentCollection
  103. );
  104. }
  105. /**
  106. * @dataProvider getDocumentDataSource()
  107. * @param string $prefix
  108. * @param string $optionName
  109. * @return void
  110. */
  111. public function testGetDocument($prefix, $optionName)
  112. {
  113. $resourceName = 'core_config_data';
  114. $structureData = ['id' => 'int'];
  115. $structure = $this->createMock(\Migration\ResourceModel\Structure::class);
  116. $document = $this->createMock(\Migration\ResourceModel\Document::class);
  117. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  118. ['edition_migrate', 'opensource-to-commerce'],
  119. [$optionName, $prefix]
  120. ]);
  121. $this->documentFactory->expects($this->any())
  122. ->method('create')
  123. ->with($this->equalTo(['structure' => $structure, 'documentName' => $resourceName]))
  124. ->will($this->returnValue($document));
  125. $this->adapter->expects($this->any())
  126. ->method('getDocumentStructure')
  127. ->with($this->equalTo($prefix . $resourceName))
  128. ->willReturn($structureData);
  129. $this->structureFactory->expects($this->any())
  130. ->method('create')
  131. ->with($this->equalTo(['documentName' => $resourceName, 'data' => $structureData]))
  132. ->willReturn($structure);
  133. $this->adapter->expects($this->any())
  134. ->method('getDocumentList')
  135. ->willReturn([$prefix . $resourceName]);
  136. $resource = ($prefix == MapInterface::TYPE_SOURCE) ? $this->resourceSource : $this->resourceDestination;
  137. $this->assertSame($document, $resource->getDocument($resourceName));
  138. }
  139. /**
  140. * @return array
  141. */
  142. public function getDocumentDataSource()
  143. {
  144. return[
  145. [MapInterface::TYPE_SOURCE, 'source_prefix'],
  146. [MapInterface::TYPE_DEST, 'dest_prefix']
  147. ];
  148. }
  149. /**
  150. * @return void
  151. */
  152. public function testGetWrongDocument()
  153. {
  154. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  155. ['edition_migrate', 'opensource-to-commerce'],
  156. ['dest_prefix', 'prefix_']
  157. ]);
  158. $this->adapter->expects($this->any())
  159. ->method('getDocumentList')
  160. ->willReturn(['document']);
  161. $this->assertFalse($this->resourceDestination->getDocument('badDocument'));
  162. }
  163. /**
  164. * @return void
  165. */
  166. public function testGetRecordsCount()
  167. {
  168. $prefix = 'prefix_';
  169. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  170. ['edition_migrate', 'opensource-to-commerce'],
  171. ['dest_prefix', $prefix]
  172. ]);
  173. $resourceName = 'core_config_data';
  174. $this->adapter->expects($this->any())
  175. ->method('getRecordsCount')
  176. ->with($prefix . $resourceName)
  177. ->willReturn(10);
  178. $this->assertEquals(10, $this->resourceDestination->getRecordsCount($resourceName));
  179. }
  180. /**
  181. * @return void
  182. */
  183. public function testGetRecords()
  184. {
  185. $resourceName = 'core_config_data';
  186. $pageNumber = 2;
  187. $this->config->expects($this->any())->method('getOption')->willReturnMap([
  188. ['edition_migrate', 'opensource-to-commerce'],
  189. ['bulk_size', 100],
  190. ['dest_prefix', 100],
  191. ]);
  192. $this->adapter->expects($this->once())->method('loadPage');
  193. $this->resourceDestination->getRecords($resourceName, $pageNumber);
  194. }
  195. /**
  196. * @return void
  197. */
  198. public function testGetAdapter()
  199. {
  200. $this->config
  201. ->expects($this->any())
  202. ->method('getOption')
  203. ->willReturnMap([['edition_migrate', 'opensource-to-commerce']]);
  204. $this->assertSame($this->adapter, $this->resourceDestination->getAdapter());
  205. }
  206. }