FrontControllerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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;
  7. use Magento\Framework\Exception\NotFoundException;
  8. class FrontControllerTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\App\FrontController
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $request;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $routerList;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $router;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Response\Http
  28. */
  29. protected $response;
  30. protected function setUp()
  31. {
  32. $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  33. ->disableOriginalConstructor()
  34. ->setMethods(['isDispatched', 'setDispatched', 'initForward', 'setActionName'])
  35. ->getMock();
  36. $this->router = $this->createMock(\Magento\Framework\App\RouterInterface::class);
  37. $this->routerList = $this->createMock(\Magento\Framework\App\RouterList::class);
  38. $this->response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  39. $this->model = new \Magento\Framework\App\FrontController($this->routerList, $this->response);
  40. }
  41. /**
  42. * @expectedException \LogicException
  43. * @expectedExceptionMessage Front controller reached 100 router match iterations
  44. */
  45. public function testDispatchThrowException()
  46. {
  47. $validCounter = 0;
  48. $callbackValid = function () use (&$validCounter) {
  49. return $validCounter++%10 ? false : true;
  50. };
  51. $this->routerList->expects($this->any())->method('valid')->will($this->returnCallback($callbackValid));
  52. $this->router->expects($this->any())
  53. ->method('match')
  54. ->with($this->request)
  55. ->will($this->returnValue(false));
  56. $this->routerList->expects($this->any())
  57. ->method('current')
  58. ->will($this->returnValue($this->router));
  59. $this->request->expects($this->any())->method('isDispatched')->will($this->returnValue(false));
  60. $this->model->dispatch($this->request);
  61. }
  62. public function testDispatched()
  63. {
  64. $this->routerList->expects($this->any())
  65. ->method('valid')
  66. ->will($this->returnValue(true));
  67. $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  68. $controllerInstance = $this->getMockBuilder(\Magento\Framework\App\Action\Action::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $controllerInstance->expects($this->any())
  72. ->method('dispatch')
  73. ->with($this->request)
  74. ->will($this->returnValue($response));
  75. $this->router->expects($this->at(0))
  76. ->method('match')
  77. ->with($this->request)
  78. ->will($this->returnValue(false));
  79. $this->router->expects($this->at(1))
  80. ->method('match')
  81. ->with($this->request)
  82. ->will($this->returnValue($controllerInstance));
  83. $this->routerList->expects($this->any())
  84. ->method('current')
  85. ->will($this->returnValue($this->router));
  86. $this->request->expects($this->at(0))->method('isDispatched')->will($this->returnValue(false));
  87. $this->request->expects($this->at(1))->method('setDispatched')->with(true);
  88. $this->request->expects($this->at(2))->method('isDispatched')->will($this->returnValue(true));
  89. $this->assertEquals($response, $this->model->dispatch($this->request));
  90. }
  91. public function testDispatchedNotFoundException()
  92. {
  93. $this->routerList->expects($this->any())
  94. ->method('valid')
  95. ->will($this->returnValue(true));
  96. $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  97. $controllerInstance = $this->getMockBuilder(\Magento\Framework\App\Action\Action::class)
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $controllerInstance->expects($this->any())
  101. ->method('dispatch')
  102. ->with($this->request)
  103. ->will($this->returnValue($response));
  104. $this->router->expects($this->at(0))
  105. ->method('match')
  106. ->with($this->request)
  107. ->willThrowException(new NotFoundException(new \Magento\Framework\Phrase('Page not found.')));
  108. $this->router->expects($this->at(1))
  109. ->method('match')
  110. ->with($this->request)
  111. ->will($this->returnValue($controllerInstance));
  112. $this->routerList->expects($this->any())
  113. ->method('current')
  114. ->will($this->returnValue($this->router));
  115. $this->request->expects($this->at(0))->method('isDispatched')->will($this->returnValue(false));
  116. $this->request->expects($this->at(1))->method('initForward');
  117. $this->request->expects($this->at(2))->method('setActionName')->with('noroute');
  118. $this->request->expects($this->at(3))->method('setDispatched')->with(false);
  119. $this->request->expects($this->at(4))->method('isDispatched')->will($this->returnValue(false));
  120. $this->request->expects($this->at(5))->method('setDispatched')->with(true);
  121. $this->request->expects($this->at(6))->method('isDispatched')->will($this->returnValue(true));
  122. $this->assertEquals($response, $this->model->dispatch($this->request));
  123. }
  124. }