ResponseTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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\Response;
  8. class ResponseTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\HTTP\PhpEnvironment\Response */
  11. protected $response;
  12. /** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\Http\Headers */
  13. protected $headers;
  14. protected function setUp()
  15. {
  16. $this->response = $this->createPartialMock(
  17. \Magento\Framework\HTTP\PhpEnvironment\Response::class,
  18. ['getHeaders', 'send', 'clearHeader']
  19. );
  20. $this->headers = $this->createPartialMock(
  21. \Zend\Http\Headers::class,
  22. ['has', 'get', 'current', 'removeHeader']
  23. );
  24. }
  25. protected function tearDown()
  26. {
  27. unset($this->response);
  28. }
  29. public function testGetHeader()
  30. {
  31. $this->response
  32. ->expects($this->once())
  33. ->method('getHeaders')
  34. ->will($this->returnValue($this->headers));
  35. $this->headers
  36. ->expects($this->once())
  37. ->method('has')
  38. ->will($this->returnValue(true));
  39. $this->headers
  40. ->expects($this->once())
  41. ->method('get')
  42. ->will($this->returnValue(true));
  43. $this->assertTrue($this->response->getHeader('testName'));
  44. }
  45. public function testGetHeaderWithoutHeader()
  46. {
  47. $this->response
  48. ->expects($this->once())
  49. ->method('getHeaders')
  50. ->will($this->returnValue($this->headers));
  51. $this->headers
  52. ->expects($this->once())
  53. ->method('has')
  54. ->will($this->returnValue(false));
  55. $this->headers
  56. ->expects($this->never())
  57. ->method('get')
  58. ->will($this->returnValue(false));
  59. $this->assertFalse($this->response->getHeader('testName'));
  60. }
  61. public function testAppendBody()
  62. {
  63. $response = new Response();
  64. $response->appendBody('testContent');
  65. $this->assertContains('testContent', $response->getBody());
  66. }
  67. public function testSendResponseWithException()
  68. {
  69. $this->assertNull($this->response->sendResponse());
  70. }
  71. public function testSetHeaderWithoutReplacing()
  72. {
  73. $this->response
  74. ->expects($this->once())
  75. ->method('getHeaders')
  76. ->will($this->returnValue($this->headers));
  77. $this->response
  78. ->expects($this->never())
  79. ->method('clearHeader')
  80. ->with('testName');
  81. $this->response->setHeader('testName', 'testValue');
  82. }
  83. public function testSetHeaderWithReplacing()
  84. {
  85. $this->response
  86. ->expects($this->once())
  87. ->method('getHeaders')
  88. ->will($this->returnValue($this->headers));
  89. $this->response
  90. ->expects($this->once())
  91. ->method('clearHeader')
  92. ->with('testName');
  93. $this->response->setHeader('testName', 'testValue', true);
  94. }
  95. public function testClearHeaderIfHeaderExistsAndWasFound()
  96. {
  97. $response = $this->response = $this->createPartialMock(
  98. \Magento\Framework\HTTP\PhpEnvironment\Response::class,
  99. ['getHeaders', 'send']
  100. );
  101. $this->headers->addHeaderLine('Header-name: header-value');
  102. $header = \Zend\Http\Header\GenericHeader::fromString('Header-name: header-value');
  103. $this->headers
  104. ->expects($this->once())
  105. ->method('has')
  106. ->with('Header-name')
  107. ->will($this->returnValue(true));
  108. $this->headers
  109. ->expects($this->once())
  110. ->method('get')
  111. ->with('Header-name')
  112. ->will($this->returnValue($header));
  113. $this->headers
  114. ->expects($this->once())
  115. ->method('removeHeader')
  116. ->with($header)
  117. ->will($this->returnValue(true));
  118. $response
  119. ->expects($this->once())
  120. ->method('getHeaders')
  121. ->will($this->returnValue($this->headers));
  122. $response->clearHeader('Header-name');
  123. }
  124. public function testClearHeaderAndHeaderNotExists()
  125. {
  126. $response = $this->response = $this->createPartialMock(
  127. \Magento\Framework\HTTP\PhpEnvironment\Response::class,
  128. ['getHeaders', 'send']
  129. );
  130. $this->headers->addHeaderLine('Header-name: header-value');
  131. $header = \Zend\Http\Header\GenericHeader::fromString('Header-name: header-value');
  132. $this->headers
  133. ->expects($this->once())
  134. ->method('has')
  135. ->with('Header-name')
  136. ->will($this->returnValue(false));
  137. $this->headers
  138. ->expects($this->never())
  139. ->method('get')
  140. ->with('Header-name')
  141. ->will($this->returnValue($header));
  142. $this->headers
  143. ->expects($this->never())
  144. ->method('removeHeader')
  145. ->with($header);
  146. $response
  147. ->expects($this->once())
  148. ->method('getHeaders')
  149. ->will($this->returnValue($this->headers));
  150. $response->clearHeader('Header-name');
  151. }
  152. /**
  153. * @expectedException \InvalidArgumentException
  154. * @expectedExceptionMessageRegExp /Invalid HTTP response code/
  155. */
  156. public function testHttpResponseCodeWithException()
  157. {
  158. $this->response->setHttpResponseCode(1);
  159. }
  160. /**
  161. * Test for setRedirect method.
  162. *
  163. * @covers \Magento\Framework\HTTP\PhpEnvironment\Response::setRedirect
  164. */
  165. public function testSetRedirect()
  166. {
  167. /** @var \Magento\Framework\App\Response\Http $response */
  168. $response = $this->createPartialMock(
  169. \Magento\Framework\HTTP\PhpEnvironment\Response::class,
  170. ['setHeader', 'setHttpResponseCode', 'sendHeaders']
  171. );
  172. $response
  173. ->expects($this->once())
  174. ->method('setHeader')
  175. ->with('Location', 'testUrl', true)
  176. ->will($this->returnSelf());
  177. $response
  178. ->expects($this->once())
  179. ->method('setHttpResponseCode')
  180. ->with(302)
  181. ->will($this->returnSelf());
  182. $response->setRedirect('testUrl');
  183. }
  184. }