FrontControllerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. use Magento\Framework\App\Request\InvalidRequestException;
  8. use Magento\Framework\App\Request\ValidatorInterface;
  9. use Magento\Framework\Exception\NotFoundException;
  10. use Magento\Framework\Phrase;
  11. use PHPUnit\Framework\TestCase;
  12. use Magento\Framework\App\Request\Http as HttpRequest;
  13. use Magento\Framework\Controller\ResultInterface;
  14. use Magento\TestFramework\Helper\Bootstrap;
  15. /**
  16. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. * @magentoAppArea frontend
  19. */
  20. class FrontControllerTest extends TestCase
  21. {
  22. /**
  23. * @var \Magento\Framework\ObjectManagerInterface
  24. */
  25. protected $_objectManager;
  26. /**
  27. * @var FrontController
  28. */
  29. protected $_model;
  30. /**
  31. * @var ValidatorInterface
  32. */
  33. private $fakeRequestValidator;
  34. /**
  35. * @return ValidatorInterface
  36. *
  37. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  38. */
  39. private function createRequestValidator(): ValidatorInterface
  40. {
  41. if (!$this->fakeRequestValidator) {
  42. $this->fakeRequestValidator = new class implements ValidatorInterface {
  43. /**
  44. * @var bool
  45. */
  46. public $valid;
  47. /**
  48. * @inheritDoc
  49. */
  50. public function validate(
  51. RequestInterface $request,
  52. ActionInterface $action
  53. ): void {
  54. if (!$this->valid) {
  55. throw new InvalidRequestException(new NotFoundException(new Phrase('Not found')));
  56. }
  57. }
  58. };
  59. }
  60. return $this->fakeRequestValidator;
  61. }
  62. /**
  63. * @inheritDoc
  64. */
  65. protected function setUp()
  66. {
  67. $this->_objectManager = Bootstrap::getObjectManager();
  68. $this->_model = $this->_objectManager->create(
  69. FrontController::class,
  70. ['requestValidator' => $this->createRequestValidator()]
  71. );
  72. }
  73. /**
  74. * Test dispatching an empty action.
  75. */
  76. public function testDispatch()
  77. {
  78. if (!Bootstrap::canTestHeaders()) {
  79. $this->markTestSkipped('Can\'t test dispatch process without sending headers');
  80. }
  81. $this->fakeRequestValidator->valid = true;
  82. $_SERVER['HTTP_HOST'] = 'localhost';
  83. $this->_objectManager->get(State::class)->setAreaCode('frontend');
  84. $request = $this->_objectManager->get(HttpRequest::class);
  85. /* empty action */
  86. $request->setRequestUri('core/index/index');
  87. $this->assertInstanceOf(
  88. ResultInterface::class,
  89. $this->_model->dispatch($request)
  90. );
  91. }
  92. /**
  93. * Test request validator invalidating given request.
  94. */
  95. public function testInvalidRequest()
  96. {
  97. if (!Bootstrap::canTestHeaders()) {
  98. $this->markTestSkipped('Can\'t test dispatch process without sending headers');
  99. }
  100. $this->fakeRequestValidator->valid = false;
  101. $_SERVER['HTTP_HOST'] = 'localhost';
  102. $this->_objectManager->get(State::class)->setAreaCode('frontend');
  103. $request = $this->_objectManager->get(HttpRequest::class);
  104. /* empty action */
  105. $request->setRequestUri('core/index/index');
  106. $this->assertInstanceOf(
  107. ResultInterface::class,
  108. $this->_model->dispatch($request)
  109. );
  110. }
  111. }