PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/setup/src/Magento/Setup/Test/Unit/Fixtures/OrdersFixtureTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 267 lines | 241 code | 9 blank | 17 comment | 0 complexity | d8f7b525b3b554603c9d0c6d584aef7e MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Setup\Test\Unit\Fixtures;
  7. use \Magento\Setup\Fixtures\OrdersFixture;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class OrdersFixtureTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Setup\Fixtures\FixtureModel
  15. */
  16. private $fixtureModelMock;
  17. /**
  18. * @var \Magento\Setup\Fixtures\OrdersFixture
  19. */
  20. private $model;
  21. public function setUp()
  22. {
  23. $this->fixtureModelMock = $this->getMock(\Magento\Setup\Fixtures\FixtureModel::class, [], [], '', false);
  24. $this->model = new OrdersFixture($this->fixtureModelMock);
  25. }
  26. /**
  27. *
  28. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  29. */
  30. public function testExecute()
  31. {
  32. $mockObjectNames = [
  33. \Magento\Quote\Model\ResourceModel\Quote::class,
  34. \Magento\Quote\Model\ResourceModel\Quote\Address::class,
  35. \Magento\Quote\Model\ResourceModel\Quote\Item::class,
  36. \Magento\Quote\Model\ResourceModel\Quote\Item\Option::class,
  37. \Magento\Quote\Model\ResourceModel\Quote\Payment::class,
  38. \Magento\Quote\Model\ResourceModel\Quote\Address\Rate::class,
  39. \Magento\Reports\Model\ResourceModel\Event::class,
  40. \Magento\Sales\Model\ResourceModel\Order::class,
  41. \Magento\Sales\Model\ResourceModel\Order\Grid::class,
  42. \Magento\Sales\Model\ResourceModel\Order\Item::class,
  43. \Magento\Sales\Model\ResourceModel\Order\Payment::class,
  44. \Magento\Sales\Model\ResourceModel\Order\Status\History::class,
  45. \Magento\Eav\Model\ResourceModel\Entity\Store::class
  46. ];
  47. $mockObjects = [];
  48. foreach ($mockObjectNames as $mockObjectName) {
  49. $mockObject = $this->getMock($mockObjectName, ['getTable'], [], '', false);
  50. $path = explode('\\', $mockObjectName);
  51. $name = array_pop($path);
  52. if (strcasecmp($mockObjectName, \Magento\Sales\Model\ResourceModel\Order::class) == 0) {
  53. $mockObject->expects($this->exactly(2))
  54. ->method('getTable')
  55. ->willReturn(strtolower($name) . '_table_name');
  56. } else {
  57. $mockObject->expects($this->once())
  58. ->method('getTable')
  59. ->willReturn(strtolower($name) . '_table_name');
  60. }
  61. $mockObjects[] = [$mockObjectName, $mockObject];
  62. }
  63. $connectionInterfaceMock = $this->getMockForAbstractClass(
  64. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  65. [],
  66. '',
  67. true,
  68. true,
  69. true,
  70. []
  71. );
  72. $connectionInterfaceMock->expects($this->exactly(14))
  73. ->method('getTableName')
  74. ->willReturn('table_name');
  75. $resourceMock = $this->getMock(\Magento\Framework\App\ResourceConnection::class, [], [], '', false);
  76. $resourceMock->expects($this->exactly(15))
  77. ->method('getConnection')
  78. ->willReturn($connectionInterfaceMock);
  79. $websiteMock = $this->getMock(\Magento\Store\Model\Website::class, ['getId', 'getName'], [], '', false);
  80. $websiteMock->expects($this->once())
  81. ->method('getId')
  82. ->willReturn('website_id');
  83. $websiteMock->expects($this->once())
  84. ->method('getName')
  85. ->willReturn('website_name');
  86. $groupMock = $this->getMock(\Magento\Store\Model\Group::class, ['getName'], [], '', false);
  87. $groupMock->expects($this->once())
  88. ->method('getName')
  89. ->willReturn('group_name');
  90. $storeMock = $this->getMock(
  91. \Magento\Store\Model\Store::class,
  92. [
  93. 'getStoreId',
  94. 'getWebsite',
  95. 'getGroup',
  96. 'getName',
  97. 'getRootCategoryId'
  98. ],
  99. [],
  100. '',
  101. false
  102. );
  103. $storeMock->expects($this->once())
  104. ->method('getStoreId')
  105. ->willReturn(1);
  106. $storeMock->expects($this->exactly(2))
  107. ->method('getWebsite')
  108. ->willReturn($websiteMock);
  109. $storeMock->expects($this->once())
  110. ->method('getGroup')
  111. ->willReturn($groupMock);
  112. $storeMock->expects($this->once())
  113. ->method('getName')
  114. ->willReturn('store_name');
  115. $storeMock->expects($this->once())
  116. ->method('getRootCategoryId')
  117. ->willReturn(1);
  118. $storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManager::class, [], [], '', false);
  119. $storeManagerMock->expects($this->once())
  120. ->method('getStores')
  121. ->willReturn([$storeMock]);
  122. $contextMock = $this->getMock(\Magento\Framework\Model\ResourceModel\Db\Context::class, [], [], '', false);
  123. $abstractDbMock = $this->getMockForAbstractClass(
  124. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  125. [$contextMock],
  126. '',
  127. true,
  128. true,
  129. true,
  130. ['getAllChildren']
  131. );
  132. $abstractDbMock->expects($this->once())
  133. ->method('getAllChildren')
  134. ->will($this->returnValue([1]));
  135. $categoryMock = $this->getMock(\Magento\Catalog\Model\Category::class, [], [], '', false);
  136. $categoryMock->expects($this->once())
  137. ->method('getResource')
  138. ->willReturn($abstractDbMock);
  139. $categoryMock->expects($this->once())
  140. ->method('getPath')
  141. ->willReturn('path/to/category');
  142. $categoryMock->expects($this->exactly(2))
  143. ->method('getName')
  144. ->willReturn('category_name');
  145. $categoryMock->expects($this->exactly(5))
  146. ->method('load')
  147. ->willReturnSelf();
  148. $productMock =
  149. $this->getMock(\Magento\Catalog\Model\Product::class, ['load', 'getSku', 'getName'], [], '', false);
  150. $productMock->expects($this->exactly(2))
  151. ->method('load')
  152. ->willReturnSelf();
  153. $productMock->expects($this->exactly(2))
  154. ->method('getSku')
  155. ->willReturn('product_sku');
  156. $productMock->expects($this->exactly(2))
  157. ->method('getName')
  158. ->willReturn('product_name');
  159. $selectMock = $this->getMock(\Magento\Framework\DB\Select::class, [], [], '', false);
  160. $collectionMock =
  161. $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection::class, [], [], '', false);
  162. $collectionMock->expects($this->once())
  163. ->method('getSelect')
  164. ->willReturn($selectMock);
  165. $collectionMock->expects($this->once())
  166. ->method('getAllIds')
  167. ->willReturn([1, 1]);
  168. array_push(
  169. $mockObjects,
  170. [\Magento\Store\Model\StoreManager::class, [], $storeManagerMock],
  171. [\Magento\Catalog\Model\Category::class, $categoryMock],
  172. [\Magento\Catalog\Model\Product::class, $productMock],
  173. [\Magento\Framework\App\ResourceConnection::class, $resourceMock],
  174. [\Magento\Catalog\Model\ResourceModel\Product\Collection::class, [], $collectionMock]
  175. );
  176. $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManager\ObjectManager::class, [], [], '', false);
  177. $objectManagerMock->expects($this->exactly(32))
  178. ->method('get')
  179. ->will($this->returnValueMap($mockObjects));
  180. $objectManagerMock->expects($this->exactly(2))
  181. ->method('create')
  182. ->will($this->returnValueMap($mockObjects));
  183. $this->fixtureModelMock
  184. ->expects($this->once())
  185. ->method('getValue')
  186. ->willReturn(1);
  187. $this->fixtureModelMock
  188. ->expects($this->exactly(34))
  189. ->method('getObjectManager')
  190. ->willReturn($objectManagerMock);
  191. $this->model->execute();
  192. }
  193. public function testNoFixtureConfigValue()
  194. {
  195. $connectionMock = $this->getMockForAbstractClass(
  196. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  197. [],
  198. '',
  199. true,
  200. true,
  201. true,
  202. []
  203. );
  204. $connectionMock->expects($this->never())
  205. ->method('query');
  206. $resourceMock = $this->getMock(\Magento\Framework\App\ResourceConnection::class, [], [], '', false);
  207. $resourceMock->expects($this->never())
  208. ->method('getConnection')
  209. ->with($this->equalTo('write'))
  210. ->willReturn($connectionMock);
  211. $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManager\ObjectManager::class, [], [], '', false);
  212. $objectManagerMock->expects($this->never())
  213. ->method('get')
  214. ->with($this->equalTo(\Magento\Framework\App\ResourceConnection::class))
  215. ->willReturn($resourceMock);
  216. $this->fixtureModelMock
  217. ->expects($this->never())
  218. ->method('getObjectManagerMock')
  219. ->willReturn($objectManagerMock);
  220. $this->fixtureModelMock
  221. ->expects($this->once())
  222. ->method('getValue')
  223. ->willReturn(false);
  224. $this->model->execute();
  225. }
  226. public function testGetActionTitle()
  227. {
  228. $this->assertSame('Generating orders', $this->model->getActionTitle());
  229. }
  230. public function testIntroduceParamLabels()
  231. {
  232. $this->assertSame([
  233. 'orders' => 'Orders'
  234. ], $this->model->introduceParamLabels());
  235. }
  236. }