Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
HttpTest.php
Go to the documentation of this file.
1 <?php
8 
12 
16 class HttpTest extends \PHPUnit\Framework\TestCase
17 {
21  protected $objectManager;
22 
26  protected $responseMock;
27 
31  protected $http;
32 
37 
41  protected $eventManagerMock;
42 
46  protected $requestMock;
47 
51  protected $objectManagerMock;
52 
56  protected $areaListMock;
57 
61  protected $configLoaderMock;
62 
66  protected $filesystemMock;
67 
68  protected function setUp()
69  {
70  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
71  $cookieReaderMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class)
72  ->disableOriginalConstructor()
73  ->getMock();
74  $routeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Route\ConfigInterface\Proxy::class)
75  ->disableOriginalConstructor()
76  ->getMock();
77  $pathInfoProcessorMock = $this->getMockBuilder(\Magento\Framework\App\Request\PathInfoProcessorInterface::class)
78  ->disableOriginalConstructor()
79  ->getMock();
80  $converterMock = $this->getMockBuilder(\Magento\Framework\Stdlib\StringUtils::class)
81  ->disableOriginalConstructor()
82  ->setMethods(['cleanString'])
83  ->getMock();
84  $objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
85  ->disableOriginalConstructor()
86  ->getMock();
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,
93  'objectManager' => $objectManagerMock
94  ])
95  ->setMethods(['getFrontName'])
96  ->getMock();
97  $this->areaListMock = $this->getMockBuilder(\Magento\Framework\App\AreaList::class)
98  ->disableOriginalConstructor()
99  ->setMethods(['getCodeByFrontName'])
100  ->getMock();
101  $this->configLoaderMock = $this->getMockBuilder(\Magento\Framework\App\ObjectManager\ConfigLoader::class)
102  ->disableOriginalConstructor()
103  ->setMethods(['load'])
104  ->getMock();
105  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
106  $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
107  $this->frontControllerMock = $this->getMockBuilder(\Magento\Framework\App\FrontControllerInterface::class)
108  ->disableOriginalConstructor()
109  ->setMethods(['dispatch'])
110  ->getMock();
111  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\Manager::class)
112  ->disableOriginalConstructor()
113  ->setMethods(['dispatch'])
114  ->getMock();
115  $this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
116 
117  $this->http = $this->objectManager->getObject(
118  \Magento\Framework\App\Http::class,
119  [
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,
127  ]
128  );
129  }
130 
134  private function setUpLaunch()
135  {
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())
146  ->method('get')
147  ->with(\Magento\Framework\App\FrontControllerInterface::class)
148  ->will($this->returnValue($this->frontControllerMock));
149  $this->frontControllerMock->expects($this->once())
150  ->method('dispatch')
151  ->with($this->requestMock)
152  ->will($this->returnValue($this->responseMock));
153  }
154 
155  public function testLaunchSuccess()
156  {
157  $this->setUpLaunch();
158  $this->eventManagerMock->expects($this->once())
159  ->method('dispatch')
160  ->with(
161  'controller_front_send_response_before',
162  ['request' => $this->requestMock, 'response' => $this->responseMock]
163  );
164  $this->assertSame($this->responseMock, $this->http->launch());
165  }
166 
171  public function testLaunchException()
172  {
173  $this->setUpLaunch();
174  $this->frontControllerMock->expects($this->once())->method('dispatch')->with($this->requestMock)->will(
175  $this->returnCallback(
176  function () {
177  throw new \Exception('Message');
178  }
179  )
180  );
181  $this->http->launch();
182  }
183 
185  {
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')
190  ->with(DirectoryList::ROOT)
191  ->willReturn($dir);
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',
197  'DOCUMENT_ROOT' => __DIR__,
198  'SCRIPT_FILENAME' => __DIR__ . '/index.php',
200  ]);
201  $this->assertTrue($this->http->catchException($bootstrap, new \Exception('Test Message')));
202  }
203 
204  public function testHandleDeveloperMode()
205  {
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']
217  );
218  $this->assertTrue($this->http->catchException($bootstrap, new \Exception('Test')));
219  }
220 
222  {
223  $this->responseMock->expects($this->once())->method('setRedirect');
224  $this->responseMock->expects($this->once())->method('sendHeaders');
225  $bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
226  $bootstrap->expects($this->once())->method('isDeveloperMode')->willReturn(false);
227  $this->assertTrue($this->http->catchException(
228  $bootstrap,
229  new \Magento\Framework\Exception\SessionException(new \Magento\Framework\Phrase('Test'))
230  ));
231  }
232 
238  private function getBootstrapNotInstalled()
239  {
240  $bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
241  $bootstrap->expects($this->once())->method('isDeveloperMode')->willReturn(true);
242  $bootstrap->expects($this->once())->method('getErrorCode')->willReturn(Bootstrap::ERR_IS_INSTALLED);
243  return $bootstrap;
244  }
245 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
Definition: bootstrap.php:73