HistoryTest.php 5.0 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. class HistoryTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Sales\Block\Order\History
  11. */
  12. protected $model;
  13. /**
  14. * @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $context;
  17. /**
  18. * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $orderCollectionFactory;
  21. /**
  22. * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $orderCollectionFactoryInterface;
  25. /**
  26. * @var \Magento\Framework\App\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $objectManager;
  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\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $pageConfig;
  41. /**
  42. * @var \Magento\Framework\View\Page\Title|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $pageTitleMock;
  45. protected function setUp()
  46. {
  47. $this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
  48. $this->orderCollectionFactory =
  49. $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class)
  50. ->disableOriginalConstructor()->setMethods(['create'])->getMock();
  51. $this->orderCollectionFactoryInterface =
  52. $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface::class)
  53. ->disableOriginalConstructor()->setMethods(['create'])->getMock();
  54. $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  55. $this->objectManager->expects($this->any())
  56. ->method('get')
  57. ->will($this->returnValue($this->orderCollectionFactoryInterface));
  58. \Magento\Framework\App\ObjectManager::setInstance($this->objectManager);
  59. $this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  60. ->setMethods(['getCustomerId'])->disableOriginalConstructor()->getMock();
  61. $this->orderConfig = $this->getMockBuilder(\Magento\Sales\Model\Order\Config::class)
  62. ->setMethods(['getVisibleOnFrontStatuses'])->disableOriginalConstructor()->getMock();
  63. $this->pageConfig = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
  64. ->disableOriginalConstructor()->getMock();
  65. $this->pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. }
  69. public function testConstructMethod()
  70. {
  71. $data = [];
  72. $customerId = 25;
  73. $this->customerSession->expects($this->once())
  74. ->method('getCustomerId')
  75. ->will($this->returnValue($customerId));
  76. $statuses = ['pending', 'processing', 'comlete'];
  77. $this->orderConfig->expects($this->once())
  78. ->method('getVisibleOnFrontStatuses')
  79. ->will($this->returnValue($statuses));
  80. $orderCollection = $this->createPartialMock(
  81. \Magento\Sales\Model\ResourceModel\Order\Collection::class,
  82. ['addFieldToSelect', 'addFieldToFilter', 'setOrder']
  83. );
  84. $this->context->expects($this->any())
  85. ->method('getPageConfig')
  86. ->willReturn($this->pageConfig);
  87. $orderCollection->expects($this->at(0))
  88. ->method('addFieldToSelect')
  89. ->with($this->equalTo('*'))
  90. ->will($this->returnSelf());
  91. $orderCollection->expects($this->at(1))
  92. ->method('addFieldToFilter')
  93. ->with('status', $this->equalTo(['in' => $statuses]))
  94. ->will($this->returnSelf());
  95. $orderCollection->expects($this->at(2))
  96. ->method('setOrder')
  97. ->with('created_at', 'desc')
  98. ->will($this->returnSelf());
  99. $this->orderCollectionFactoryInterface->expects($this->atLeastOnce())
  100. ->method('create')
  101. ->will($this->returnValue($orderCollection));
  102. $this->pageConfig->expects($this->atLeastOnce())
  103. ->method('getTitle')
  104. ->willReturn($this->pageTitleMock);
  105. $this->pageTitleMock->expects($this->atLeastOnce())
  106. ->method('set')
  107. ->willReturnSelf();
  108. $this->model = new \Magento\Sales\Block\Order\History(
  109. $this->context,
  110. $this->orderCollectionFactory,
  111. $this->customerSession,
  112. $this->orderConfig,
  113. $data
  114. );
  115. $this->assertEquals($orderCollection, $this->model->getOrders());
  116. }
  117. }