Zend_Controller_Response_Exception: Unable to send headers;

when running the entire test for the zend application, this line:

protected function _getResp() { if (is_null($this->_response)) $this->_response = new Zend_Controller_Response_Http(); return $this->_response; } ....... $this->_getResp()->setHeader('Content-Type', 'text/html; charset=utf-8', true); 

generates the following error:

Zend_Controller_Response_Exception: Unable to send headers; headers have already been sent to / usr / share / php 5 / PEAR / PHPUnit / Util / Printer.php, line 173

and as a result, the test fails

+4
source share
2 answers

This is because PHPUnit generates output before your test even runs. You need to enter Zend_Controller_Response_HttpTestCase in the test case. This subclass of Zend_Controller_Response_Http does not actually send headers or output anything, and it will not throw an exception because it does not matter if the output has already been sent.

Just add the following method to the class above.

 public function setResp(Zend_Controller_Response_Http $resp) { $this->_response = $resp; } 

Create a new Zend_Controller_Response_HttpTestCase and pass it to setResp() object you are testing. It will also allow you to confirm that the correct headers were sent along with the output.

+3
source

In my case, I have custom request and response objects: My_Controller_Request_Rest and My_Controller_Response_Rest .

What I did for this, I created new My_Controller_Request_RestTestCase and My_Controller_Response_RestTestCase , which extend Zend_Controller_Request_HttpTestCase and Zend_Controller_Response_HttpTestCase respectively.

What David Harkness suggested actually solves the problem. The only thing your objects need to extend is the HttpTestCase class corresponding to each class.

You need to create setters for each object, since you are not allowed to set them directly.

I have the following ControllerTestCase code:

tests/application/controllers/ControllerTestCase.php

 abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase { /** * Application instance. * @var Zend_Application */ protected $application; /** * Setup test suite. * * @return void */ public function setUp() { $this->_setupInitializers(); $this->bootstrap = array( $this, 'applicationBootstrap', ); parent::setUp(); $this->setRequest(new My_Controller_Request_RestTestCase()); $this->setResponse(new My_Controller_Response_RestTestCase()); } } 

My custom request and response objects have the following signature:

library/My/Controller/Request/Rest.php

 class My_Controller_Request_Rest extends Zend_Controller_Request_Http { // Nothing fancy. } 

library/My/Controller/Response/Rest.php

 class Bonzai_Controller_Response_Rest extends Zend_Controller_Response_Http { // Nothing fancy either } 

Now this is what I could not figure out how to avoid repeating the same code in library/My/Controller/Request/Rest.php and library/My/Controller/Controller/Request/RestTestCase.php . The code in my case is the same in Request / Rest.php and Request / RestTestCase.php, as well as in Response / Rest.php and Response / RestTestCase.php, but they extend Zend_Controller_(Request|Response)_HttpTestCase .

I hope I get it. I know the message is outdated, but I think it’s worth a little more to increase it.

0
source

Source: https://habr.com/ru/post/1381469/


All Articles