OrderStatusTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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\Rss;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * Class OrderStatusTest
  10. * @package Magento\Sales\Model\Rss
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class OrderStatusTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Sales\Model\Rss\OrderStatus
  17. */
  18. protected $model;
  19. /**
  20. * @var ObjectManagerHelper
  21. */
  22. protected $objectManagerHelper;
  23. /**
  24. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $objectManager;
  27. /**
  28. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $urlInterface;
  31. /**
  32. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $requestInterface;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $orderStatusFactory;
  39. /**
  40. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $timezoneInterface;
  43. /**
  44. * @var \PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $orderFactory;
  47. /**
  48. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $scopeConfigInterface;
  51. /**
  52. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $order;
  55. /**
  56. * @var array
  57. */
  58. protected $feedData = [
  59. 'title' => 'Order # 100000001 Notification(s)',
  60. 'link' => 'http://magento.com/sales/order/view/order_id/1',
  61. 'description' => 'Order # 100000001 Notification(s)',
  62. 'charset' => 'UTF-8',
  63. 'entries' => [
  64. [
  65. 'title' => 'Details for Order #100000001',
  66. 'link' => 'http://magento.com/sales/order/view/order_id/1',
  67. 'description' => '<p>Notified Date: <br/>Comment: Some comment<br/></p>',
  68. ],
  69. [
  70. 'title' => 'Order #100000001 created at ',
  71. 'link' => 'http://magento.com/sales/order/view/order_id/1',
  72. 'description' => '<p>Current Status: Pending<br/>Total: 15.00<br/></p>'
  73. ],
  74. ],
  75. ];
  76. protected function setUp()
  77. {
  78. $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  79. $this->urlInterface = $this->createMock(\Magento\Framework\UrlInterface::class);
  80. $this->requestInterface = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  81. $this->orderStatusFactory =
  82. $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Rss\OrderStatusFactory::class)
  83. ->setMethods(['create'])
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->timezoneInterface = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
  87. $this->orderFactory = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
  88. $this->scopeConfigInterface = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  89. $this->order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  90. ->setMethods([
  91. '__sleep',
  92. '__wakeup',
  93. 'getIncrementId',
  94. 'getId',
  95. 'getCustomerId',
  96. 'load',
  97. 'getStatusLabel',
  98. 'formatPrice',
  99. 'getGrandTotal',
  100. ])->disableOriginalConstructor()->getMock();
  101. $this->order->expects($this->any())->method('getId')->will($this->returnValue(1));
  102. $this->order->expects($this->any())->method('getIncrementId')->will($this->returnValue('100000001'));
  103. $this->order->expects($this->any())->method('getCustomerId')->will($this->returnValue(1));
  104. $this->order->expects($this->any())->method('getStatusLabel')->will($this->returnValue('Pending'));
  105. $this->order->expects($this->any())->method('formatPrice')->will($this->returnValue('15.00'));
  106. $this->order->expects($this->any())->method('getGrandTotal')->will($this->returnValue(15));
  107. $this->order->expects($this->any())->method('load')->with(1)->will($this->returnSelf());
  108. $this->objectManagerHelper = new ObjectManagerHelper($this);
  109. $this->model = $this->objectManagerHelper->getObject(
  110. \Magento\Sales\Model\Rss\OrderStatus::class,
  111. [
  112. 'objectManager' => $this->objectManager,
  113. 'urlBuilder' => $this->urlInterface,
  114. 'request' => $this->requestInterface,
  115. 'orderResourceFactory' => $this->orderStatusFactory,
  116. 'localeDate' => $this->timezoneInterface,
  117. 'orderFactory' => $this->orderFactory,
  118. 'scopeConfig' => $this->scopeConfigInterface
  119. ]
  120. );
  121. }
  122. public function testGetRssData()
  123. {
  124. $this->orderFactory->expects($this->once())->method('create')->willReturn($this->order);
  125. $requestData = base64_encode('{"order_id":1,"increment_id":"100000001","customer_id":1}');
  126. $this->requestInterface->expects($this->any())->method('getParam')->with('data')->willReturn($requestData);
  127. $resource = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Rss\OrderStatus::class)
  128. ->setMethods(['getAllCommentCollection'])
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $comment = [
  132. 'entity_type_code' => 'order',
  133. 'increment_id' => '100000001',
  134. 'created_at' => '2014-10-09 18:25:50',
  135. 'comment' => 'Some comment',
  136. ];
  137. $resource->expects($this->once())->method('getAllCommentCollection')->willReturn([$comment]);
  138. $this->orderStatusFactory->expects($this->once())->method('create')->willReturn($resource);
  139. $this->urlInterface->expects($this->any())->method('getUrl')
  140. ->with('sales/order/view', ['order_id' => 1])
  141. ->will($this->returnValue('http://magento.com/sales/order/view/order_id/1'));
  142. $this->assertEquals($this->feedData, $this->model->getRssData());
  143. }
  144. /**
  145. * @expectedException \InvalidArgumentException
  146. * @expectedExceptionMessage Order not found.
  147. */
  148. public function testGetRssDataWithError()
  149. {
  150. $this->orderFactory->expects($this->once())->method('create')->willReturn($this->order);
  151. $requestData = base64_encode('{"order_id":"1","increment_id":true,"customer_id":true}');
  152. $this->requestInterface->expects($this->any())->method('getParam')->with('data')->willReturn($requestData);
  153. $this->orderStatusFactory->expects($this->never())->method('create');
  154. $this->urlInterface->expects($this->never())->method('getUrl');
  155. $this->assertEquals($this->feedData, $this->model->getRssData());
  156. }
  157. public function testIsAllowed()
  158. {
  159. $this->scopeConfigInterface->expects($this->once())->method('getValue')
  160. ->with('rss/order/status', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
  161. ->will($this->returnValue(true));
  162. $this->assertTrue($this->model->isAllowed());
  163. }
  164. /**
  165. * @param string $requestData
  166. * @param string $result
  167. * @dataProvider getCacheKeyDataProvider
  168. */
  169. public function testGetCacheKey($requestData, $result)
  170. {
  171. $this->requestInterface->expects($this->any())->method('getParam')
  172. ->with('data')
  173. ->will($this->returnValue($requestData));
  174. $this->orderFactory->expects($this->once())->method('create')->will($this->returnValue($this->order));
  175. $this->assertEquals('rss_order_status_data_' . $result, $this->model->getCacheKey());
  176. }
  177. /**
  178. * @return array
  179. */
  180. public function getCacheKeyDataProvider()
  181. {
  182. return [
  183. [base64_encode('{"order_id":1,"increment_id":"100000001","customer_id":1}'), md5('11000000011')],
  184. [base64_encode('{"order_id":"1","increment_id":true,"customer_id":true}'), '']
  185. ];
  186. }
  187. public function testGetCacheLifetime()
  188. {
  189. $this->assertEquals(600, $this->model->getCacheLifetime());
  190. }
  191. }