ConfigTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\Order;
  7. use Magento\Framework\DataObject;
  8. use Magento\Sales\Model\ResourceModel\Order\Status\Collection;
  9. /**
  10. * Class ConfigTest
  11. */
  12. class ConfigTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Sales\Model\Order\Config
  16. */
  17. protected $salesConfig;
  18. /**
  19. * @var \Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $orderStatusCollectionFactoryMock;
  22. /**
  23. * @var \Magento\Sales\Model\Order\StatusFactory|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $statusFactoryMock;
  26. /**
  27. * @var \Magento\Sales\Model\Order\Status
  28. */
  29. protected $orderStatusModel;
  30. /**
  31. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $storeManagerMock;
  34. /**
  35. * @return void
  36. */
  37. protected function setUp()
  38. {
  39. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  40. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  41. $this->orderStatusModel = $objectManager->getObject(\Magento\Sales\Model\Order\Status::class, [
  42. 'storeManager' => $this->storeManagerMock,
  43. ]);
  44. $this->statusFactoryMock = $this->getMockBuilder(\Magento\Sales\Model\Order\StatusFactory::class)
  45. ->disableOriginalConstructor()
  46. ->setMethods(['load', 'create'])
  47. ->getMock();
  48. $this->orderStatusCollectionFactoryMock = $this->createPartialMock(
  49. \Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory::class,
  50. ['create']
  51. );
  52. $this->salesConfig = $objectManager
  53. ->getObject(
  54. \Magento\Sales\Model\Order\Config::class,
  55. [
  56. 'orderStatusFactory' => $this->statusFactoryMock,
  57. 'orderStatusCollectionFactory' => $this->orderStatusCollectionFactoryMock
  58. ]
  59. );
  60. }
  61. /**
  62. * @return void
  63. */
  64. public function testGetInvisibleOnFrontStatuses()
  65. {
  66. $statuses = [
  67. new DataObject(
  68. [
  69. 'status' => 'canceled',
  70. 'is_default' => 1,
  71. 'visible_on_front' => 1,
  72. ]
  73. ),
  74. new DataObject(
  75. [
  76. 'status' => 'complete',
  77. 'is_default' => 1,
  78. 'visible_on_front' => 0,
  79. ]
  80. ),
  81. new DataObject(
  82. [
  83. 'status' => 'processing',
  84. 'is_default' => 1,
  85. 'visible_on_front' => 1,
  86. ]
  87. ),
  88. new DataObject(
  89. [
  90. 'status' => 'pending_payment',
  91. 'is_default' => 1,
  92. 'visible_on_front' => 0,
  93. ]
  94. ),
  95. ];
  96. $expectedResult = ['complete', 'pending_payment'];
  97. $collectionMock = $this->createPartialMock(Collection::class, ['create', 'joinStates']);
  98. $this->orderStatusCollectionFactoryMock->expects($this->once())
  99. ->method('create')
  100. ->will($this->returnValue($collectionMock));
  101. $collectionMock->expects($this->once())
  102. ->method('joinStates')
  103. ->will($this->returnValue($statuses));
  104. $result = $this->salesConfig->getInvisibleOnFrontStatuses();
  105. $this->assertSame($expectedResult, $result);
  106. }
  107. /**
  108. * @return void
  109. */
  110. public function testGetStateLabelByStateAndStatus()
  111. {
  112. $statuses = [
  113. new DataObject(
  114. [
  115. 'status' => 'fraud',
  116. 'state' => 'processing',
  117. 'label' => 'Suspected Fraud',
  118. ]
  119. ),
  120. new DataObject(
  121. [
  122. 'status' => 'processing',
  123. 'state' => 'processing',
  124. 'label' => 'Processing',
  125. ]
  126. )
  127. ];
  128. $collectionMock = $this->createPartialMock(Collection::class, ['create', 'joinStates']);
  129. $this->orderStatusCollectionFactoryMock->expects($this->once())
  130. ->method('create')
  131. ->will($this->returnValue($collectionMock));
  132. $collectionMock->expects($this->once())
  133. ->method('joinStates')
  134. ->will($this->returnValue($statuses));
  135. $result = $this->salesConfig->getStateLabelByStateAndStatus('processing', 'fraud');
  136. $this->assertSame('Suspected Fraud', $result->getText());
  137. }
  138. /**
  139. * Test get statuses
  140. *
  141. * @dataProvider getStatusesDataProvider
  142. *
  143. * @param string $state
  144. * @param bool $joinLabels
  145. * @param DataObject[] $collectionData
  146. * @param array $expectedResult
  147. */
  148. public function testGetStatuses($state, $joinLabels, $collectionData, $expectedResult)
  149. {
  150. $collectionMock = $this->createPartialMock(
  151. Collection::class,
  152. ['create', 'joinStates', 'addStateFilter', 'orderByLabel']
  153. );
  154. $this->orderStatusCollectionFactoryMock->expects($this->any())
  155. ->method('create')
  156. ->will($this->returnValue($collectionMock));
  157. $collectionMock->expects($this->once())
  158. ->method('addStateFilter')
  159. ->will($this->returnSelf());
  160. $collectionMock->expects($this->once())
  161. ->method('orderByLabel')
  162. ->will($this->returnValue($collectionData));
  163. $collectionMock->expects($this->once())
  164. ->method('joinStates')
  165. ->will($this->returnValue($collectionData));
  166. $this->statusFactoryMock->method('create')
  167. ->willReturnSelf();
  168. $this->statusFactoryMock->method('load')
  169. ->willReturn($this->orderStatusModel);
  170. $storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
  171. $storeMock->method('getId')
  172. ->willReturn(1);
  173. $this->storeManagerMock->method('getStore')
  174. ->with($this->anything())
  175. ->willReturn($storeMock);
  176. $this->orderStatusModel->setData('store_labels', [1 => 'Pending label']);
  177. $result = $this->salesConfig->getStateStatuses($state, $joinLabels);
  178. $this->assertSame($expectedResult, $result);
  179. // checking data cached in private property
  180. $this->assertSame($result, $this->salesConfig->getStateStatuses($state, $joinLabels));
  181. }
  182. /**
  183. * Data provider for testGetStatuses
  184. *
  185. * @return array
  186. */
  187. public function getStatusesDataProvider()
  188. {
  189. return [
  190. 'processing state' => [
  191. 'state' => 'processing',
  192. 'joinLabels' => false,
  193. 'collectionData' => [
  194. new DataObject(
  195. [
  196. 'status' => 'fraud',
  197. 'state' => 'processing',
  198. 'store_label' => 'Suspected Fraud',
  199. ]
  200. ),
  201. new DataObject(
  202. [
  203. 'status' => 'processing',
  204. 'state' => 'processing',
  205. 'store_label' => 'Processing',
  206. ]
  207. ),
  208. ],
  209. 'expectedResult' => [
  210. 0 => 'fraud',
  211. 1 => 'processing'
  212. ],
  213. ],
  214. 'pending state' => [
  215. 'state' => 'pending',
  216. 'joinLabels' => true,
  217. 'collectionData' => [
  218. new DataObject(
  219. [
  220. 'status' => 'pending_status',
  221. 'state' => 'pending',
  222. 'store_label' => 'Pending label',
  223. ]
  224. ),
  225. ],
  226. 'expectedResult' => [
  227. 'pending_status' => 'Pending label'
  228. ],
  229. ],
  230. ];
  231. }
  232. }