123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\HTTP\Test\Unit\PhpEnvironment;
- use \Magento\Framework\HTTP\PhpEnvironment\Response;
- class ResponseTest extends \PHPUnit\Framework\TestCase
- {
- /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\HTTP\PhpEnvironment\Response */
- protected $response;
- /** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\Http\Headers */
- protected $headers;
- protected function setUp()
- {
- $this->response = $this->createPartialMock(
- \Magento\Framework\HTTP\PhpEnvironment\Response::class,
- ['getHeaders', 'send', 'clearHeader']
- );
- $this->headers = $this->createPartialMock(
- \Zend\Http\Headers::class,
- ['has', 'get', 'current', 'removeHeader']
- );
- }
- protected function tearDown()
- {
- unset($this->response);
- }
- public function testGetHeader()
- {
- $this->response
- ->expects($this->once())
- ->method('getHeaders')
- ->will($this->returnValue($this->headers));
- $this->headers
- ->expects($this->once())
- ->method('has')
- ->will($this->returnValue(true));
- $this->headers
- ->expects($this->once())
- ->method('get')
- ->will($this->returnValue(true));
- $this->assertTrue($this->response->getHeader('testName'));
- }
- public function testGetHeaderWithoutHeader()
- {
- $this->response
- ->expects($this->once())
- ->method('getHeaders')
- ->will($this->returnValue($this->headers));
- $this->headers
- ->expects($this->once())
- ->method('has')
- ->will($this->returnValue(false));
- $this->headers
- ->expects($this->never())
- ->method('get')
- ->will($this->returnValue(false));
- $this->assertFalse($this->response->getHeader('testName'));
- }
- public function testAppendBody()
- {
- $response = new Response();
- $response->appendBody('testContent');
- $this->assertContains('testContent', $response->getBody());
- }
- public function testSendResponseWithException()
- {
- $this->assertNull($this->response->sendResponse());
- }
- public function testSetHeaderWithoutReplacing()
- {
- $this->response
- ->expects($this->once())
- ->method('getHeaders')
- ->will($this->returnValue($this->headers));
- $this->response
- ->expects($this->never())
- ->method('clearHeader')
- ->with('testName');
- $this->response->setHeader('testName', 'testValue');
- }
- public function testSetHeaderWithReplacing()
- {
- $this->response
- ->expects($this->once())
- ->method('getHeaders')
- ->will($this->returnValue($this->headers));
- $this->response
- ->expects($this->once())
- ->method('clearHeader')
- ->with('testName');
- $this->response->setHeader('testName', 'testValue', true);
- }
- public function testClearHeaderIfHeaderExistsAndWasFound()
- {
- $response = $this->response = $this->createPartialMock(
- \Magento\Framework\HTTP\PhpEnvironment\Response::class,
- ['getHeaders', 'send']
- );
- $this->headers->addHeaderLine('Header-name: header-value');
- $header = \Zend\Http\Header\GenericHeader::fromString('Header-name: header-value');
- $this->headers
- ->expects($this->once())
- ->method('has')
- ->with('Header-name')
- ->will($this->returnValue(true));
- $this->headers
- ->expects($this->once())
- ->method('get')
- ->with('Header-name')
- ->will($this->returnValue($header));
- $this->headers
- ->expects($this->once())
- ->method('removeHeader')
- ->with($header)
- ->will($this->returnValue(true));
- $response
- ->expects($this->once())
- ->method('getHeaders')
- ->will($this->returnValue($this->headers));
- $response->clearHeader('Header-name');
- }
- public function testClearHeaderAndHeaderNotExists()
- {
- $response = $this->response = $this->createPartialMock(
- \Magento\Framework\HTTP\PhpEnvironment\Response::class,
- ['getHeaders', 'send']
- );
- $this->headers->addHeaderLine('Header-name: header-value');
- $header = \Zend\Http\Header\GenericHeader::fromString('Header-name: header-value');
- $this->headers
- ->expects($this->once())
- ->method('has')
- ->with('Header-name')
- ->will($this->returnValue(false));
- $this->headers
- ->expects($this->never())
- ->method('get')
- ->with('Header-name')
- ->will($this->returnValue($header));
- $this->headers
- ->expects($this->never())
- ->method('removeHeader')
- ->with($header);
- $response
- ->expects($this->once())
- ->method('getHeaders')
- ->will($this->returnValue($this->headers));
- $response->clearHeader('Header-name');
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessageRegExp /Invalid HTTP response code/
- */
- public function testHttpResponseCodeWithException()
- {
- $this->response->setHttpResponseCode(1);
- }
- /**
- * Test for setRedirect method.
- *
- * @covers \Magento\Framework\HTTP\PhpEnvironment\Response::setRedirect
- */
- public function testSetRedirect()
- {
- /** @var \Magento\Framework\App\Response\Http $response */
- $response = $this->createPartialMock(
- \Magento\Framework\HTTP\PhpEnvironment\Response::class,
- ['setHeader', 'setHttpResponseCode', 'sendHeaders']
- );
- $response
- ->expects($this->once())
- ->method('setHeader')
- ->with('Location', 'testUrl', true)
- ->will($this->returnSelf());
- $response
- ->expects($this->once())
- ->method('setHttpResponseCode')
- ->with(302)
- ->will($this->returnSelf());
- $response->setRedirect('testUrl');
- }
- }
|