ReorderTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Helper;
  7. use \Magento\Sales\Helper\Reorder;
  8. class ReorderTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Sales\Helper\Reorder
  12. */
  13. protected $helper;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Config\ScopeConfigInterface
  16. */
  17. protected $scopeConfigMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Sales\Model\Store
  20. */
  21. protected $storeParam;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Sales\Model\Order
  24. */
  25. protected $orderMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Model\Session
  28. */
  29. protected $customerSessionMock;
  30. /**
  31. * @var \Magento\Sales\Api\OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $repositoryMock;
  34. /**
  35. * @return void
  36. */
  37. protected function setUp()
  38. {
  39. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config::class)
  40. ->setMethods(['getValue'])
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $contextMock = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $contextMock->expects($this->any())
  47. ->method('getScopeConfig')
  48. ->willReturn($this->scopeConfigMock);
  49. $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->repositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
  53. ->getMockForAbstractClass();
  54. $this->helper = new \Magento\Sales\Helper\Reorder(
  55. $contextMock,
  56. $this->customerSessionMock,
  57. $this->repositoryMock
  58. );
  59. $this->storeParam = $this->getMockBuilder(\Magento\Sales\Model\Store::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. }
  66. /**
  67. * Tests that the store config is checked if orders can be reordered.
  68. *
  69. * @dataProvider getScopeConfigValue
  70. * @return void
  71. */
  72. public function testIsAllowedScopeConfigReorder($scopeConfigValue)
  73. {
  74. $this->setupScopeConfigMock($scopeConfigValue);
  75. $this->assertEquals($scopeConfigValue, $this->helper->isAllowed($this->storeParam));
  76. }
  77. /**
  78. * Tests that the store config is still checked with a null store.
  79. *
  80. * @dataProvider getScopeConfigValue
  81. * @return void
  82. */
  83. public function testIsAllowScopeConfigReorderNotAllowWithStore($scopeConfigValue)
  84. {
  85. $this->storeParam = null;
  86. $this->setupScopeConfigMock($scopeConfigValue);
  87. $this->assertEquals($scopeConfigValue, $this->helper->isAllow());
  88. }
  89. /**
  90. * @return array
  91. */
  92. public function getScopeConfigValue()
  93. {
  94. return [
  95. [true],
  96. [false]
  97. ];
  98. }
  99. /**
  100. * Sets up the scope config mock with a specified return value.
  101. *
  102. * @param bool $returnValue
  103. * @return void
  104. */
  105. protected function setupScopeConfigMock($returnValue)
  106. {
  107. $this->scopeConfigMock->expects($this->once())
  108. ->method('getValue')
  109. ->with(
  110. Reorder::XML_PATH_SALES_REORDER_ALLOW,
  111. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  112. $this->storeParam
  113. )
  114. ->will($this->returnValue($returnValue));
  115. }
  116. /**
  117. * Tests that if the store does not allow reorders, it does not matter what the Order returns.
  118. *
  119. * @return void
  120. */
  121. public function testCanReorderStoreNotAllowed()
  122. {
  123. $this->setupOrderMock(false);
  124. $this->repositoryMock->expects($this->once())
  125. ->method('get')
  126. ->with(1)
  127. ->willReturn($this->orderMock);
  128. $this->assertFalse($this->helper->canReorder(1));
  129. }
  130. /**
  131. * Tests what happens if the customer is not logged in and the store does allow re-orders.
  132. *
  133. * @return void
  134. */
  135. public function testCanReorderCustomerNotLoggedIn()
  136. {
  137. $this->setupOrderMock(true);
  138. $this->customerSessionMock->expects($this->once())
  139. ->method('isLoggedIn')
  140. ->will($this->returnValue(false));
  141. $this->repositoryMock->expects($this->once())
  142. ->method('get')
  143. ->with(1)
  144. ->willReturn($this->orderMock);
  145. $this->assertTrue($this->helper->canReorder(1));
  146. }
  147. /**
  148. * Tests what happens if the customer is logged in and the order does or does not allow reorders.
  149. *
  150. * @param bool $orderCanReorder
  151. * @return void
  152. * @dataProvider getOrderCanReorder
  153. */
  154. public function testCanReorderCustomerLoggedInAndOrderCanReorder($orderCanReorder)
  155. {
  156. $this->setupOrderMock(true);
  157. $this->customerSessionMock->expects($this->once())
  158. ->method('isLoggedIn')
  159. ->will($this->returnValue(true));
  160. $this->orderMock->expects($this->once())
  161. ->method('canReorder')
  162. ->will($this->returnValue($orderCanReorder));
  163. $this->repositoryMock->expects($this->once())
  164. ->method('get')
  165. ->with(1)
  166. ->willReturn($this->orderMock);
  167. $this->assertEquals($orderCanReorder, $this->helper->canReorder(1));
  168. }
  169. /**
  170. * Sets up the order mock to return a store config which will return a specified value on a getValue call.
  171. *
  172. * @param bool $storeScopeReturnValue
  173. * @return void
  174. */
  175. protected function setupOrderMock($storeScopeReturnValue)
  176. {
  177. $this->setupScopeConfigMock($storeScopeReturnValue);
  178. $this->orderMock->expects($this->once())
  179. ->method('getStore')
  180. ->will($this->returnValue($this->storeParam));
  181. }
  182. /**
  183. * @return array
  184. */
  185. public function getOrderCanReorder()
  186. {
  187. return [
  188. [true],
  189. [false]
  190. ];
  191. }
  192. }