IndexTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Adminhtml\Index;
  7. /**
  8. * @covers \Magento\Customer\Controller\Adminhtml\Index\Index
  9. */
  10. class IndexTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Customer\Controller\Adminhtml\Index\Index
  14. */
  15. protected $indexController;
  16. /**
  17. * @var \Magento\Backend\App\Action\Context
  18. */
  19. protected $context;
  20. /**
  21. * @var \Magento\Framework\App\Request|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $requestMock;
  24. /**
  25. * @var \Magento\Backend\Model\View\Result\ForwardFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $resultForwardFactoryMock;
  28. /**
  29. * @var \Magento\Backend\Model\View\Result\Forward|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $resultForwardMock;
  32. /**
  33. * @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $resultPageFactoryMock;
  36. /**
  37. * @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $resultPageMock;
  40. /**
  41. * @var \Magento\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $pageConfigMock;
  44. /**
  45. * @var \Magento\Framework\View\Page\Title|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $pageTitleMock;
  48. /**
  49. * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $sessionMock;
  52. protected function setUp()
  53. {
  54. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  55. ->disableOriginalConstructor()->getMock();
  56. $this->resultForwardFactoryMock = $this->getMockBuilder(
  57. \Magento\Backend\Model\View\Result\ForwardFactory::class
  58. )
  59. ->disableOriginalConstructor()
  60. ->setMethods(['create'])
  61. ->getMock();
  62. $this->resultForwardMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->resultPageFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
  69. ->disableOriginalConstructor()
  70. ->setMethods(['setActiveMenu', 'getConfig', 'addBreadcrumb'])
  71. ->getMock();
  72. $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
  79. ->disableOriginalConstructor()
  80. ->setMethods(['unsCustomerData', 'unsCustomerFormData'])
  81. ->getMock();
  82. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  83. $this->context = $objectManager->getObject(
  84. \Magento\Backend\App\Action\Context::class,
  85. [
  86. 'request' => $this->requestMock,
  87. 'session' => $this->sessionMock
  88. ]
  89. );
  90. $this->indexController = $objectManager->getObject(
  91. \Magento\Customer\Controller\Adminhtml\Index\Index::class,
  92. [
  93. 'context' => $this->context,
  94. 'resultForwardFactory' => $this->resultForwardFactoryMock,
  95. 'resultPageFactory' => $this->resultPageFactoryMock
  96. ]
  97. );
  98. }
  99. /**
  100. * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
  101. */
  102. public function testExecute()
  103. {
  104. $this->prepareExecute();
  105. $this->resultPageFactoryMock->expects($this->once())
  106. ->method('create')
  107. ->willReturn($this->resultPageMock);
  108. $this->resultPageMock->expects($this->once())
  109. ->method('setActiveMenu')
  110. ->with('Magento_Customer::customer_manage');
  111. $this->resultPageMock->expects($this->once())
  112. ->method('getConfig')
  113. ->willReturn($this->pageConfigMock);
  114. $this->pageConfigMock->expects($this->once())
  115. ->method('getTitle')
  116. ->willReturn($this->pageTitleMock);
  117. $this->pageTitleMock->expects($this->once())
  118. ->method('prepend')
  119. ->with('Customers');
  120. $this->resultPageMock->expects($this->atLeastOnce())
  121. ->method('addBreadcrumb')
  122. ->withConsecutive(
  123. ['Customers', 'Customers'],
  124. ['Manage Customers', 'Manage Customers']
  125. );
  126. $this->sessionMock->expects($this->once())
  127. ->method('unsCustomerData');
  128. $this->sessionMock->expects($this->once())
  129. ->method('unsCustomerFormData');
  130. $this->assertInstanceOf(
  131. \Magento\Framework\View\Result\Page::class,
  132. $this->indexController->execute()
  133. );
  134. }
  135. /**
  136. * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
  137. */
  138. public function testExecuteAjax()
  139. {
  140. $this->prepareExecute(true);
  141. $this->resultForwardFactoryMock->expects($this->once())
  142. ->method('create')
  143. ->willReturn($this->resultForwardMock);
  144. $this->resultForwardMock->expects($this->once())
  145. ->method('forward')
  146. ->with('grid')
  147. ->willReturnSelf();
  148. $this->assertInstanceOf(
  149. \Magento\Backend\Model\View\Result\Forward::class,
  150. $this->indexController->execute()
  151. );
  152. }
  153. /**
  154. * @param bool $ajax
  155. */
  156. protected function prepareExecute($ajax = false)
  157. {
  158. $this->requestMock->expects($this->once())
  159. ->method('getQuery')
  160. ->with('ajax')
  161. ->willReturn($ajax);
  162. }
  163. }