19

I'm trying to write a unit test for a controller using Zend and PHPUnit

In the code I get data from php://input

$req = new Zend_Controller_Request_Http();
$data = $req->getRawBody();

My code works fine when I test the real application, but unless I can supply data as a raw http post, $data will always be blank. The getRawBody() method basically calls file_get_contents('php://input'), but how do I override this in order to supply the test data to my application.

4 Answers 4

11

I had the same problem and the way I fixed it was to have the 'php://input' string as a variable that is settable at run time. I know this does not really apply directly to this question as it would require modifying the Zend Framework. But all the same it may be helpful to someone.

For example:

<?php
class Foo {

    public function read() {
        return file_get_contents('php://input');
    } 
}

would become

<?php
class Foo {

    public $_fileIn = 'php://input';

    public function read() {
        return file_get_contents($this->_fileIn);
    }

}

Then in my unit test I can do:

<?php
$obj = new Foo();
$obj->_fileIn = 'my_input_data.dat';
assertTrue('foo=bar', $obj->read());
1
  • 2
    you also could have a protected property and change it to public using reflection in the tests. So for production your code is a little cleaner. (Assuming php 5.3)
    – edorian
    Apr 23, 2011 at 4:02
7

You could try mocking the object in your unit tests. Something like this:

$req = $this->getMock('Zend_Controller_Request_Http', array('getRawBody'));
$req->method('getRawBody')
    ->will($this->returnValue('raw_post_data_to_return'));
1
  • so this would work if I was passing Zend_Controller_Request_Http object into the code I was testing, unfortunately the controller handles all this under the hood. So I think the only way to do it is to change my application to test for the environment I'm in and handle the special case.
    – john ryan
    Jul 12, 2010 at 21:44
2

Provided the $req->getRawBody() is, as you say, the same as file_get_contents('php://input')...

$test = true; /* Set to TRUE when using Unit Tests */

$req = new Zend_Controller_Request_Http();
if( $test )
  $data = file_get_contents( 'testfile.txt' );
else
  $data = $req->getRawBody();

Not a perfect solution, but similar to what I have used in the past when designing scripts to handle piped emails with great success.

2
  • Yeap, not a perfect solution, but how I've decided to implement it too. Thanks.
    – john ryan
    Jul 12, 2010 at 21:45
  • 1
    I wouldn't recommend this solution -- using if statements to perform different code in unit tests versus production. You ideally want your code to execute the same logical statements in both worlds. See @MitMaro's solution below which uses a configurable input path. Nov 10, 2016 at 23:04
0

Zend_Controller_Request_HttpTestCase contains methods for setting and getting various http request/responses.

For example: $req = new Zend_Controller_Request_HttpTestCase; $req->setCookie('cookie', 'TRUE'); $test = $this->controller->cookieAction($req); $this->assertSame($test, TRUE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.