NewOrderTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 NewOrderTest
  10. * @package Magento\Sales\Model\Rss
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class NewOrderTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Sales\Model\Rss\NewOrder
  17. */
  18. protected $model;
  19. /**
  20. * @var ObjectManagerHelper
  21. */
  22. protected $objectManagerHelper;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $orderFactory;
  27. /**
  28. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $urlBuilder;
  31. /**
  32. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $timezoneInterface;
  35. /**
  36. * @var \Magento\Framework\Stdlib\DateTime|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $dateTime;
  39. /**
  40. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $scopeConfigInterface;
  43. /**
  44. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $eventManager;
  47. /**
  48. * @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $layout;
  51. /**
  52. * @var \Magento\Framework\App\Rss\UrlBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $rssUrlBuilderInterface;
  55. /**
  56. * @var array
  57. */
  58. protected $feedData = [
  59. 'title' => 'New Orders',
  60. 'link' => 'http://magento.com/backend/rss/feed/index/type/new_order',
  61. 'description' => 'New Orders',
  62. 'charset' => 'UTF-8',
  63. 'entries' => [
  64. [
  65. 'title' => 'Order #100000001 created at 2014-09-10 17:39:50',
  66. 'link' => 'http://magento.com/sales/order/view/order_id/1',
  67. 'description' => 'Order Description',
  68. ],
  69. ],
  70. ];
  71. protected function setUp()
  72. {
  73. $this->orderFactory = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
  74. $this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
  75. $this->timezoneInterface = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
  76. $this->dateTime = $this->createMock(\Magento\Framework\Stdlib\DateTime::class);
  77. $this->scopeConfigInterface = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  78. $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  79. $this->layout = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
  80. $this->rssUrlBuilderInterface = $this->getMockBuilder(\Magento\Framework\App\Rss\UrlBuilderInterface::class)
  81. ->setMethods(['getUrl'])
  82. ->disableOriginalConstructor()->getMock();
  83. $this->objectManagerHelper = new ObjectManagerHelper($this);
  84. $this->model = $this->objectManagerHelper->getObject(
  85. \Magento\Sales\Model\Rss\NewOrder::class,
  86. [
  87. 'orderFactory' => $this->orderFactory,
  88. 'urlBuilder' => $this->urlBuilder,
  89. 'rssUrlBuilder' => $this->rssUrlBuilderInterface,
  90. 'localeDate' => $this->timezoneInterface,
  91. 'dateTime' => $this->dateTime,
  92. 'scopeConfig' => $this->scopeConfigInterface,
  93. 'eventManager' => $this->eventManager,
  94. 'layout' => $this->layout
  95. ]
  96. );
  97. }
  98. public function testIsAllowed()
  99. {
  100. $this->assertTrue($this->model->isAllowed());
  101. }
  102. public function testGetData()
  103. {
  104. $this->dateTime->expects($this->once())->method('formatDate')->will($this->returnValue(date('Y-m-d H:i:s')));
  105. $this->rssUrlBuilderInterface->expects($this->once())->method('getUrl')
  106. ->with(['_secure' => true, '_nosecret' => true, 'type' => 'new_order'])
  107. ->will($this->returnValue('http://magento.com/backend/rss/feed/index/type/new_order'));
  108. $this->timezoneInterface->expects($this->once())->method('formatDate')
  109. ->will($this->returnValue('2014-09-10 17:39:50'));
  110. $order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  111. ->setMethods(['__sleep', '__wakeup', 'getResourceCollection', 'getIncrementId', 'getId', 'getCreatedAt'])
  112. ->disableOriginalConstructor()->getMock();
  113. $order->expects($this->once())->method('getId')->will($this->returnValue(1));
  114. $order->expects($this->once())->method('getIncrementId')->will($this->returnValue('100000001'));
  115. $order->expects($this->once())->method('getCreatedAt')->will($this->returnValue(time()));
  116. $collection = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Collection::class)
  117. ->setMethods(['addAttributeToFilter', 'addAttributeToSort', 'getIterator'])
  118. ->disableOriginalConstructor()->getMock();
  119. $collection->expects($this->once())->method('addAttributeToFilter')->will($this->returnSelf());
  120. $collection->expects($this->once())->method('addAttributeToSort')->will($this->returnSelf());
  121. $collection->expects($this->once())->method('getIterator')
  122. ->will($this->returnValue(new \ArrayIterator([$order])));
  123. $order->expects($this->once())->method('getResourceCollection')->will($this->returnValue($collection));
  124. $this->orderFactory->expects($this->once())->method('create')->will($this->returnValue($order));
  125. $this->eventManager->expects($this->once())->method('dispatch')->will($this->returnSelf());
  126. $block = $this->createPartialMock(\Magento\Sales\Block\Adminhtml\Order\Details::class, ['setOrder', 'toHtml']);
  127. $block->expects($this->once())->method('setOrder')->with($order)->will($this->returnSelf());
  128. $block->expects($this->once())->method('toHtml')->will($this->returnValue('Order Description'));
  129. $this->layout->expects($this->once())->method('getBlockSingleton')->will($this->returnValue($block));
  130. $this->urlBuilder->expects($this->once())->method('getUrl')
  131. ->will($this->returnValue('http://magento.com/sales/order/view/order_id/1'));
  132. $this->assertEquals($this->feedData, $this->model->getRssData());
  133. }
  134. public function testGetCacheKey()
  135. {
  136. $this->assertEquals('rss_new_orders_data', $this->model->getCacheKey());
  137. }
  138. public function testGetCacheLifetime()
  139. {
  140. $this->assertEquals(60, $this->model->getCacheLifetime());
  141. }
  142. public function getFeeds()
  143. {
  144. $this->assertEmpty($this->model->getFeeds());
  145. }
  146. }