Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NewOrderTest.php
Go to the documentation of this file.
1 <?php
7 
9 
15 class NewOrderTest extends \PHPUnit\Framework\TestCase
16 {
20  protected $model;
21 
26 
30  protected $orderFactory;
31 
35  protected $urlBuilder;
36 
40  protected $timezoneInterface;
41 
45  protected $dateTime;
46 
51 
55  protected $eventManager;
56 
60  protected $layout;
61 
66 
70  protected $feedData = [
71  'title' => 'New Orders',
72  'link' => 'http://magento.com/backend/rss/feed/index/type/new_order',
73  'description' => 'New Orders',
74  'charset' => 'UTF-8',
75  'entries' => [
76  [
77  'title' => 'Order #100000001 created at 2014-09-10 17:39:50',
78  'link' => 'http://magento.com/sales/order/view/order_id/1',
79  'description' => 'Order Description',
80  ],
81  ],
82  ];
83 
84  protected function setUp()
85  {
86  $this->orderFactory = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
87  $this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
88  $this->timezoneInterface = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
89  $this->dateTime = $this->createMock(\Magento\Framework\Stdlib\DateTime::class);
90  $this->scopeConfigInterface = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
91  $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
92  $this->layout = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
93  $this->rssUrlBuilderInterface = $this->getMockBuilder(\Magento\Framework\App\Rss\UrlBuilderInterface::class)
94  ->setMethods(['getUrl'])
95  ->disableOriginalConstructor()->getMock();
96  $this->objectManagerHelper = new ObjectManagerHelper($this);
97  $this->model = $this->objectManagerHelper->getObject(
98  \Magento\Sales\Model\Rss\NewOrder::class,
99  [
100  'orderFactory' => $this->orderFactory,
101  'urlBuilder' => $this->urlBuilder,
102  'rssUrlBuilder' => $this->rssUrlBuilderInterface,
103  'localeDate' => $this->timezoneInterface,
104  'dateTime' => $this->dateTime,
105  'scopeConfig' => $this->scopeConfigInterface,
106  'eventManager' => $this->eventManager,
107  'layout' => $this->layout
108  ]
109  );
110  }
111 
112  public function testIsAllowed()
113  {
114  $this->assertTrue($this->model->isAllowed());
115  }
116 
117  public function testGetData()
118  {
119  $this->dateTime->expects($this->once())->method('formatDate')->will($this->returnValue(date('Y-m-d H:i:s')));
120 
121  $this->rssUrlBuilderInterface->expects($this->once())->method('getUrl')
122  ->with(['_secure' => true, '_nosecret' => true, 'type' => 'new_order'])
123  ->will($this->returnValue('http://magento.com/backend/rss/feed/index/type/new_order'));
124 
125  $this->timezoneInterface->expects($this->once())->method('formatDate')
126  ->will($this->returnValue('2014-09-10 17:39:50'));
127 
128  $order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
129  ->setMethods(['__sleep', '__wakeup', 'getResourceCollection', 'getIncrementId', 'getId', 'getCreatedAt'])
130  ->disableOriginalConstructor()->getMock();
131  $order->expects($this->once())->method('getId')->will($this->returnValue(1));
132  $order->expects($this->once())->method('getIncrementId')->will($this->returnValue('100000001'));
133  $order->expects($this->once())->method('getCreatedAt')->will($this->returnValue(time()));
134 
135  $collection = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Collection::class)
136  ->setMethods(['addAttributeToFilter', 'addAttributeToSort', 'getIterator'])
137  ->disableOriginalConstructor()->getMock();
138  $collection->expects($this->once())->method('addAttributeToFilter')->will($this->returnSelf());
139  $collection->expects($this->once())->method('addAttributeToSort')->will($this->returnSelf());
140  $collection->expects($this->once())->method('getIterator')
141  ->will($this->returnValue(new \ArrayIterator([$order])));
142 
143  $order->expects($this->once())->method('getResourceCollection')->will($this->returnValue($collection));
144  $this->orderFactory->expects($this->once())->method('create')->will($this->returnValue($order));
145 
146  $this->eventManager->expects($this->once())->method('dispatch')->will($this->returnSelf());
147 
148  $block = $this->createPartialMock(\Magento\Sales\Block\Adminhtml\Order\Details::class, ['setOrder', 'toHtml']);
149  $block->expects($this->once())->method('setOrder')->with($order)->will($this->returnSelf());
150  $block->expects($this->once())->method('toHtml')->will($this->returnValue('Order Description'));
151 
152  $this->layout->expects($this->once())->method('getBlockSingleton')->will($this->returnValue($block));
153  $this->urlBuilder->expects($this->once())->method('getUrl')
154  ->will($this->returnValue('http://magento.com/sales/order/view/order_id/1'));
155  $this->assertEquals($this->feedData, $this->model->getRssData());
156  }
157 
158  public function testGetCacheKey()
159  {
160  $this->assertEquals('rss_new_orders_data', $this->model->getCacheKey());
161  }
162 
163  public function testGetCacheLifetime()
164  {
165  $this->assertEquals(60, $this->model->getCacheLifetime());
166  }
167 
168  public function getFeeds()
169  {
170  $this->assertEmpty($this->model->getFeeds());
171  }
172 }
$order
Definition: order.php:55
$block
Definition: block.php:8