16 class HttpTest extends \PHPUnit\Framework\TestCase
70 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
71 $cookieReaderMock = $this->getMockBuilder(\
Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class)
72 ->disableOriginalConstructor()
75 ->disableOriginalConstructor()
77 $pathInfoProcessorMock = $this->getMockBuilder(\
Magento\Framework\
App\Request\PathInfoProcessorInterface::class)
78 ->disableOriginalConstructor()
80 $converterMock = $this->getMockBuilder(\
Magento\Framework\Stdlib\StringUtils::class)
81 ->disableOriginalConstructor()
82 ->setMethods([
'cleanString'])
85 ->disableOriginalConstructor()
87 $this->requestMock = $this->getMockBuilder(\
Magento\Framework\
App\Request\Http::class)
88 ->setConstructorArgs([
89 'cookieReader' => $cookieReaderMock,
90 'converter' => $converterMock,
91 'routeConfig' => $routeConfigMock,
92 'pathInfoProcessor' => $pathInfoProcessorMock,
95 ->setMethods([
'getFrontName'])
97 $this->areaListMock = $this->getMockBuilder(\
Magento\Framework\
App\AreaList::class)
98 ->disableOriginalConstructor()
99 ->setMethods([
'getCodeByFrontName'])
102 ->disableOriginalConstructor()
103 ->setMethods([
'load'])
105 $this->objectManagerMock = $this->createMock(\
Magento\Framework\ObjectManagerInterface::class);
107 $this->frontControllerMock = $this->getMockBuilder(\
Magento\Framework\
App\FrontControllerInterface::class)
108 ->disableOriginalConstructor()
109 ->setMethods([
'dispatch'])
111 $this->eventManagerMock = $this->getMockBuilder(\
Magento\Framework\
Event\Manager::class)
112 ->disableOriginalConstructor()
113 ->setMethods([
'dispatch'])
115 $this->filesystemMock = $this->createMock(\
Magento\Framework\Filesystem::class);
117 $this->http = $this->objectManager->getObject(
120 'objectManager' => $this->objectManagerMock,
121 'eventManager' => $this->eventManagerMock,
122 'areaList' => $this->areaListMock,
123 'request' => $this->requestMock,
124 'response' => $this->responseMock,
125 'configLoader' => $this->configLoaderMock,
126 'filesystem' => $this->filesystemMock,
134 private function setUpLaunch()
136 $frontName =
'frontName';
137 $areaCode =
'areaCode';
138 $this->requestMock->expects($this->once())->method(
'getFrontName')->will($this->returnValue($frontName));
139 $this->areaListMock->expects($this->once())
140 ->method(
'getCodeByFrontName')
141 ->with($frontName)->will($this->returnValue($areaCode));
142 $this->configLoaderMock->expects($this->once())
143 ->method(
'load')->with($areaCode)->will($this->returnValue([]));
144 $this->objectManagerMock->expects($this->once())->method(
'configure')->with([]);
145 $this->objectManagerMock->expects($this->once())
147 ->with(\
Magento\Framework\
App\FrontControllerInterface::class)
148 ->will($this->returnValue($this->frontControllerMock));
149 $this->frontControllerMock->expects($this->once())
151 ->with($this->requestMock)
152 ->will($this->returnValue($this->responseMock));
157 $this->setUpLaunch();
158 $this->eventManagerMock->expects($this->once())
161 'controller_front_send_response_before',
162 [
'request' => $this->requestMock,
'response' => $this->responseMock]
164 $this->assertSame($this->responseMock, $this->http->launch());
173 $this->setUpLaunch();
174 $this->frontControllerMock->expects($this->once())->method(
'dispatch')->with($this->requestMock)->will(
175 $this->returnCallback(
177 throw new \Exception(
'Message');
181 $this->http->launch();
186 $dir = $this->getMockForAbstractClass(\
Magento\Framework\
Filesystem\Directory\ReadInterface::class);
187 $dir->expects($this->once())->method(
'getAbsolutePath')->willReturn(
__DIR__);
188 $this->filesystemMock->expects($this->once())
189 ->method(
'getDirectoryRead')
192 $this->responseMock->expects($this->once())->method(
'setRedirect')->with(
'/_files/');
193 $this->responseMock->expects($this->once())->method(
'sendHeaders');
194 $bootstrap = $this->getBootstrapNotInstalled();
195 $bootstrap->expects($this->once())->method(
'getParams')->willReturn([
196 'SCRIPT_NAME' =>
'/index.php',
198 'SCRIPT_FILENAME' =>
__DIR__ .
'/index.php',
201 $this->assertTrue($this->http->catchException(
$bootstrap,
new \Exception(
'Test Message')));
206 $this->filesystemMock->expects($this->once())
207 ->method(
'getDirectoryRead')
208 ->will($this->throwException(
new \Exception(
'strange error')));
209 $this->responseMock->expects($this->once())->method(
'setHttpResponseCode')->with(500);
210 $this->responseMock->expects($this->once())->method(
'setHeader')->with(
'Content-Type',
'text/plain');
211 $constraint = new \PHPUnit\Framework\Constraint\StringStartsWith(
'1 exception(s):');
212 $this->responseMock->expects($this->once())->method(
'setBody')->with(
$constraint);
213 $this->responseMock->expects($this->once())->method(
'sendResponse');
214 $bootstrap = $this->getBootstrapNotInstalled();
215 $bootstrap->expects($this->once())->method(
'getParams')->willReturn(
216 [
'DOCUMENT_ROOT' =>
'something',
'SCRIPT_FILENAME' =>
'something/else']
218 $this->assertTrue($this->http->catchException(
$bootstrap,
new \Exception(
'Test')));
223 $this->responseMock->expects($this->once())->method(
'setRedirect');
224 $this->responseMock->expects($this->once())->method(
'sendHeaders');
226 $bootstrap->expects($this->once())->method(
'isDeveloperMode')->willReturn(
false);
227 $this->assertTrue($this->http->catchException(
238 private function getBootstrapNotInstalled()
241 $bootstrap->expects($this->once())->method(
'isDeveloperMode')->willReturn(
true);
defined('TESTS_BP')||define('TESTS_BP' __DIR__
testHandleDeveloperMode()
const PARAM_NOT_INSTALLED_URL_PATH
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
testCatchExceptionSessionException()
testHandleDeveloperModeNotInstalled()