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