RequestTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\HTTP\Test\Unit\PhpEnvironment;
  7. use \Magento\Framework\HTTP\PhpEnvironment\Request;
  8. use Zend\Stdlib\Parameters;
  9. class RequestTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var Request
  13. */
  14. protected $model;
  15. /**
  16. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager | \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $objectManager;
  19. /**
  20. * @var \Magento\Framework\Stdlib\Cookie\CookieReaderInterface | \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $cookieReader;
  23. /**
  24. * @var \Magento\Framework\Stdlib\StringUtils | \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $converter;
  27. /**
  28. * @var array
  29. */
  30. private $serverArray;
  31. protected function setUp()
  32. {
  33. $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  34. $this->cookieReader = $this->createMock(\Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class);
  35. $this->converter = $this->createMock(\Magento\Framework\Stdlib\StringUtils::class);
  36. // Stash the $_SERVER array to protect it from modification in test
  37. $this->serverArray = $_SERVER;
  38. }
  39. public function tearDown()
  40. {
  41. $_SERVER = $this->serverArray;
  42. }
  43. /**
  44. * @param null $uri
  45. * @return Request
  46. */
  47. private function getModel($uri = null)
  48. {
  49. return new Request($this->cookieReader, $this->converter, $uri);
  50. }
  51. public function testSetPathInfoWithNullValue()
  52. {
  53. $this->model = $this->getModel();
  54. $actual = $this->model->setPathInfo();
  55. $this->assertEquals($this->model, $actual);
  56. }
  57. public function testSetPathInfoWithValue()
  58. {
  59. $this->model = $this->getModel();
  60. $expected = 'testPathInfo';
  61. $this->model->setPathInfo($expected);
  62. $this->assertEquals($expected, $this->model->getPathInfo());
  63. }
  64. public function testSetPathInfoWithQueryPart()
  65. {
  66. $uri = 'http://test.com/node?queryValue';
  67. $this->model = $this->getModel($uri);
  68. $this->model->setPathInfo();
  69. $this->assertEquals('/node', $this->model->getPathInfo());
  70. }
  71. /**
  72. * @param string $name
  73. * @param string $default
  74. * @param string $result
  75. * @dataProvider getServerProvider
  76. */
  77. public function testGetServer($name, $default, $result)
  78. {
  79. $this->model = $this->getModel();
  80. $this->model->setServer(new Parameters([
  81. 'HTTPS' => 'off',
  82. 'DOCUMENT_ROOT' => '/test',
  83. 'HTTP_ACCEPT' => '',
  84. 'HTTP_CONNECTION' => 'http-connection',
  85. 'HTTP_REFERER' => 'http-referer',
  86. 'HTTP_X_FORWARDED_FOR' => 'x-forwarded',
  87. 'HTTP_USER_AGENT' => 'user-agent',
  88. 'PATH_INFO' => 'path-info',
  89. 'QUERY_STRING' => '',
  90. 'REMOTE_HOST' => 'remote-host',
  91. 'REQUEST_METHOD' => 'GET',
  92. 'REQUEST_URI' => 'request-uri',
  93. 'SERVER_NAME' => 'server-name',
  94. ]));
  95. $this->assertEquals($result, $this->model->getServer($name, $default));
  96. }
  97. /**
  98. * @return array
  99. */
  100. public function getServerProvider()
  101. {
  102. return [
  103. ['HTTPS', '', 'off'],
  104. ['DOCUMENT_ROOT', '', '/test'],
  105. ['ORIG_PATH_INFO', 'orig-path-info', 'orig-path-info'],
  106. ['PATH_INFO', '', 'path-info'],
  107. ['QUERY_STRING', '', ''],
  108. ['REMOTE_HOST', 'test', 'remote-host'],
  109. ['REQUEST_METHOD', '', 'GET'],
  110. ['REQUEST_URI', 'test', 'request-uri'],
  111. ['SERVER_NAME', 'test', 'server-name'],
  112. ['HTTP_ACCEPT', 'http-accept', ''],
  113. ['HTTP_CONNECTION', '', 'http-connection'],
  114. ['HTTP_HOST', 'http-host', 'http-host'],
  115. ['HTTP_REFERER', '', 'http-referer'],
  116. ['HTTP_USER_AGENT', '', 'user-agent'],
  117. ['HTTP_X_FORWARDED_FOR', '', 'x-forwarded'],
  118. ['Accept', 'accept', 'accept'],
  119. ['Connection', '', ''],
  120. ['Host', 'http-host', 'http-host'],
  121. ['Referer', 'referer', 'referer'],
  122. ['User-Agent', '', ''],
  123. ['X-Forwarded-For', 'test', 'test'],
  124. ];
  125. }
  126. public function testGetAliasWhenAliasExists()
  127. {
  128. $this->model = $this->getModel();
  129. $this->model->setAlias('AliasName', 'AliasTarget');
  130. $this->assertEquals('AliasTarget', $this->model->getAlias('AliasName'));
  131. }
  132. public function testGetAliasWhenAliasesIsNull()
  133. {
  134. $this->model = $this->getModel();
  135. $this->assertNull($this->model->getAlias('someValue'));
  136. }
  137. public function testSetPostValue()
  138. {
  139. $this->model = $this->getModel();
  140. $post = ['one' => '111', 'two' => '222'];
  141. $this->model->setPostValue($post);
  142. $this->assertEquals($post, $this->model->getPost()->toArray());
  143. $this->model->setPost(new Parameters([]));
  144. $this->assertEmpty($this->model->getPost()->toArray());
  145. $post = ['post_var' => 'post_value'];
  146. $this->model->setPostValue($post);
  147. $this->model->setPostValue('post_var 2', 'post_value 2');
  148. $this->assertEquals(
  149. ['post_var' => 'post_value', 'post_var 2' => 'post_value 2'],
  150. $this->model->getPost()->toArray()
  151. );
  152. }
  153. public function testGetFiles()
  154. {
  155. $this->model = $this->getModel();
  156. $files = ['one' => '111', 'two' => '222'];
  157. $this->model->setFiles(new Parameters($files));
  158. $this->assertEquals($files, $this->model->getFiles()->toArray());
  159. foreach ($files as $key => $value) {
  160. $this->assertEquals($value, $this->model->getFiles($key));
  161. }
  162. $this->assertNull($this->model->getFiles('no_such_file'));
  163. $this->assertEquals('default', $this->model->getFiles('no_such_file', 'default'));
  164. }
  165. public function testGetBaseUrlWithUrl()
  166. {
  167. $this->model = $this->getModel();
  168. $this->model->setBaseUrl('http:\/test.com\one/two');
  169. $this->assertEquals('http://test.com/one/two', $this->model->getBaseUrl());
  170. }
  171. public function testGetBaseUrlWithEmptyUrl()
  172. {
  173. $this->model = $this->getModel();
  174. $this->assertEmpty($this->model->getBaseUrl());
  175. }
  176. public function testGetAliasWhenAliasSet()
  177. {
  178. $this->model = $this->getModel();
  179. $this->model->setAlias('AliasName', 'AliasTarget');
  180. $this->assertEquals('AliasTarget', $this->model->getAlias('AliasName'));
  181. }
  182. public function testGetAliasWhenAliasAreEmpty()
  183. {
  184. $this->model = $this->getModel();
  185. $this->assertNull($this->model->getAlias(''));
  186. }
  187. public function testGetCookie()
  188. {
  189. $key = "cookieName";
  190. $default = "defaultValue";
  191. $this->cookieReader
  192. ->expects($this->once())
  193. ->method('getCookie')
  194. ->with($key, $default);
  195. $this->getModel()->getCookie($key, $default);
  196. }
  197. public function testGetCookieDefault()
  198. {
  199. $key = "cookieName";
  200. $default = "defaultValue";
  201. $this->cookieReader
  202. ->expects($this->once())
  203. ->method('getCookie')
  204. ->with($key, $default)
  205. ->will($this->returnValue($default));
  206. $this->assertEquals($default, $this->getModel()->getCookie($key, $default));
  207. }
  208. public function testGetCookieNameExists()
  209. {
  210. $key = "cookieName";
  211. $default = "defaultValue";
  212. $value = "cookieValue";
  213. $this->cookieReader
  214. ->expects($this->once())
  215. ->method('getCookie')
  216. ->with($key, $default)
  217. ->will($this->returnValue($value));
  218. $this->assertEquals($value, $this->getModel()->getCookie($key, $default));
  219. }
  220. public function testGetCookieNullName()
  221. {
  222. $nullKey = null;
  223. $default = "defaultValue";
  224. $this->cookieReader
  225. ->expects($this->once())
  226. ->method('getCookie')
  227. ->with($nullKey, $default)
  228. ->will($this->returnValue($default));
  229. $this->assertEquals($default, $this->getModel()->getCookie($nullKey, $default));
  230. }
  231. }