RecentTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Block\Order;
  7. use Magento\Framework\View\Element\Template\Context;
  8. use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
  9. use Magento\Customer\Model\Session;
  10. use Magento\Sales\Model\Order\Config;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. use Magento\Framework\View\Layout;
  13. use Magento\Store\Api\Data\StoreInterface;
  14. use Magento\Sales\Model\ResourceModel\Order\Collection;
  15. class RecentTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var \Magento\Sales\Block\Order\Recent
  19. */
  20. protected $block;
  21. /**
  22. * @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $context;
  25. /**
  26. * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $orderCollectionFactory;
  29. /**
  30. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $customerSession;
  33. /**
  34. * @var \Magento\Sales\Model\Order\Config|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $orderConfig;
  37. /**
  38. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $storeManagerMock;
  41. protected function setUp()
  42. {
  43. $this->context = $this->createMock(Context::class);
  44. $this->orderCollectionFactory = $this->createPartialMock(
  45. CollectionFactory::class,
  46. ['create']
  47. );
  48. $this->customerSession = $this->createPartialMock(Session::class, ['getCustomerId']);
  49. $this->orderConfig = $this->createPartialMock(
  50. Config::class,
  51. ['getVisibleOnFrontStatuses']
  52. );
  53. $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
  54. ->getMockForAbstractClass();
  55. }
  56. public function testConstructMethod()
  57. {
  58. $attribute = ['customer_id', 'store_id', 'status'];
  59. $customerId = 25;
  60. $storeId = 4;
  61. $layout = $this->createPartialMock(Layout::class, ['getBlock']);
  62. $this->context->expects($this->once())
  63. ->method('getLayout')
  64. ->will($this->returnValue($layout));
  65. $this->customerSession->expects($this->once())
  66. ->method('getCustomerId')
  67. ->will($this->returnValue($customerId));
  68. $statuses = ['pending', 'processing', 'complete'];
  69. $this->orderConfig->expects($this->once())
  70. ->method('getVisibleOnFrontStatuses')
  71. ->will($this->returnValue($statuses));
  72. $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
  73. ->getMockForAbstractClass();
  74. $storeMock = $this->getMockBuilder(StoreInterface::class)->getMockForAbstractClass();
  75. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);
  76. $storeMock->expects($this->any())->method('getId')->willReturn($storeId);
  77. $orderCollection = $this->createPartialMock(Collection::class, [
  78. 'addAttributeToSelect',
  79. 'addFieldToFilter',
  80. 'addAttributeToFilter',
  81. 'addAttributeToSort',
  82. 'setPageSize',
  83. 'load'
  84. ]);
  85. $this->orderCollectionFactory->expects($this->once())
  86. ->method('create')
  87. ->will($this->returnValue($orderCollection));
  88. $orderCollection->expects($this->at(0))
  89. ->method('addAttributeToSelect')
  90. ->with($this->equalTo('*'))
  91. ->will($this->returnSelf());
  92. $orderCollection->expects($this->at(1))
  93. ->method('addAttributeToFilter')
  94. ->with($attribute[0], $this->equalTo($customerId))
  95. ->willReturnSelf();
  96. $orderCollection->expects($this->at(2))
  97. ->method('addAttributeToFilter')
  98. ->with($attribute[1], $this->equalTo($storeId))
  99. ->willReturnSelf();
  100. $orderCollection->expects($this->at(3))
  101. ->method('addAttributeToFilter')
  102. ->with($attribute[2], $this->equalTo(['in' => $statuses]))
  103. ->will($this->returnSelf());
  104. $orderCollection->expects($this->at(4))
  105. ->method('addAttributeToSort')
  106. ->with('created_at', 'desc')
  107. ->will($this->returnSelf());
  108. $orderCollection->expects($this->at(5))
  109. ->method('setPageSize')
  110. ->with('5')
  111. ->will($this->returnSelf());
  112. $orderCollection->expects($this->at(6))
  113. ->method('load')
  114. ->will($this->returnSelf());
  115. $this->block = new \Magento\Sales\Block\Order\Recent(
  116. $this->context,
  117. $this->orderCollectionFactory,
  118. $this->customerSession,
  119. $this->orderConfig,
  120. [],
  121. $this->storeManagerMock
  122. );
  123. $this->assertEquals($orderCollection, $this->block->getOrders());
  124. }
  125. }