ActionTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\Action;
  7. use \Magento\Framework\App\Action\Action;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. class ActionTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\Framework\App\Test\Unit\Action\ActionFake */
  12. protected $action;
  13. /** @var ObjectManagerHelper */
  14. protected $objectManagerHelper;
  15. /**
  16. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $_requestMock;
  19. /**
  20. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $_responseMock;
  23. /**
  24. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $_eventManagerMock;
  27. /**
  28. * @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $_actionFlagMock;
  31. /**
  32. * @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $_redirectMock;
  35. /**
  36. * @var \Magento\Framework\App\ViewInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $viewMock;
  39. /**
  40. * @var \Magento\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $pageConfigMock;
  43. /**
  44. * Full action name
  45. */
  46. const FULL_ACTION_NAME = 'module/controller/someaction';
  47. /**
  48. * Route name
  49. */
  50. const ROUTE_NAME = 'module/controller/actionroute';
  51. /**
  52. * Action name
  53. */
  54. const ACTION_NAME = 'someaction';
  55. /**
  56. * Controller name
  57. */
  58. const CONTROLLER_NAME = 'controller';
  59. /**
  60. * Module name
  61. */
  62. const MODULE_NAME = 'module';
  63. public static $actionParams = ['param' => 'value'];
  64. protected function setUp()
  65. {
  66. $this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  67. $this->_actionFlagMock = $this->createMock(\Magento\Framework\App\ActionFlag::class);
  68. $this->_redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
  69. $this->_requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  70. ->disableOriginalConstructor()->getMock();
  71. $this->_responseMock = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
  72. $this->pageConfigMock = $this->createPartialMock(\Magento\Framework\View\Page\Config::class, ['getConfig']);
  73. $this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
  74. $this->viewMock->expects($this->any())->method('getPage')->will($this->returnValue($this->pageConfigMock));
  75. $this->pageConfigMock->expects($this->any())->method('getConfig')->will($this->returnValue(1));
  76. $this->objectManagerHelper = new ObjectManagerHelper($this);
  77. $this->action = $this->objectManagerHelper->getObject(
  78. \Magento\Framework\App\Test\Unit\Action\ActionFake::class,
  79. [
  80. 'request' => $this->_requestMock,
  81. 'response' => $this->_responseMock,
  82. 'eventManager' => $this->_eventManagerMock,
  83. 'redirect' => $this->_redirectMock,
  84. 'actionFlag' => $this->_actionFlagMock,
  85. 'view' => $this->viewMock,
  86. ]
  87. );
  88. \Magento\Framework\Profiler::disable();
  89. }
  90. public function testDispatchPostDispatch()
  91. {
  92. $this->_requestMock->expects($this->exactly(3))->method('getFullActionName')->will(
  93. $this->returnValue(self::FULL_ACTION_NAME)
  94. );
  95. $this->_requestMock->expects($this->exactly(2))->method('getRouteName')->will(
  96. $this->returnValue(self::ROUTE_NAME)
  97. );
  98. $expectedEventParameters = ['controller_action' => $this->action, 'request' => $this->_requestMock];
  99. $this->_eventManagerMock->expects($this->at(0))->method('dispatch')->with(
  100. 'controller_action_predispatch',
  101. $expectedEventParameters
  102. );
  103. $this->_eventManagerMock->expects($this->at(1))->method('dispatch')->with(
  104. 'controller_action_predispatch_' . self::ROUTE_NAME,
  105. $expectedEventParameters
  106. );
  107. $this->_eventManagerMock->expects($this->at(2))->method('dispatch')->with(
  108. 'controller_action_predispatch_' . self::FULL_ACTION_NAME,
  109. $expectedEventParameters
  110. );
  111. $this->_requestMock->expects($this->once())->method('isDispatched')->will($this->returnValue(true));
  112. $this->_actionFlagMock->expects($this->at(0))->method('get')->with('', Action::FLAG_NO_DISPATCH)->will(
  113. $this->returnValue(false)
  114. );
  115. // _forward expectations
  116. $this->_requestMock->expects($this->once())->method('initForward');
  117. $this->_requestMock->expects($this->once())->method('setParams')->with(self::$actionParams);
  118. $this->_requestMock->expects($this->once())->method('setControllerName')->with(self::CONTROLLER_NAME);
  119. $this->_requestMock->expects($this->once())->method('setModuleName')->with(self::MODULE_NAME);
  120. $this->_requestMock->expects($this->once())->method('setActionName')->with(self::ACTION_NAME);
  121. $this->_requestMock->expects($this->once())->method('setDispatched')->with(false);
  122. // _redirect expectations
  123. $this->_redirectMock->expects($this->once())->method('redirect')->with(
  124. $this->_responseMock,
  125. self::FULL_ACTION_NAME,
  126. self::$actionParams
  127. );
  128. $this->_actionFlagMock->expects($this->at(1))->method('get')->with('', Action::FLAG_NO_POST_DISPATCH)->will(
  129. $this->returnValue(false)
  130. );
  131. $this->_eventManagerMock->expects($this->at(3))->method('dispatch')->with(
  132. 'controller_action_postdispatch_' . self::FULL_ACTION_NAME,
  133. $expectedEventParameters
  134. );
  135. $this->_eventManagerMock->expects($this->at(4))->method('dispatch')->with(
  136. 'controller_action_postdispatch_' . self::ROUTE_NAME,
  137. $expectedEventParameters
  138. );
  139. $this->_eventManagerMock->expects($this->at(5))->method('dispatch')->with(
  140. 'controller_action_postdispatch',
  141. $expectedEventParameters
  142. );
  143. $this->assertEquals($this->_responseMock, $this->action->dispatch($this->_requestMock));
  144. }
  145. }