I’m working on a project that allows users to interact via SMS text messages. I’ve subclassed Zend Framework’s request and response objects to get requests from an SMS API and then send back responses. It works when I “test” it via the development environment but I’d really like to do unit testing.
But inside the test case class, it’s not using my request object, it’s using Zend_Controller_Request_HttpTestCase. I’m pretty sure I’d have the same problem with the response object, I’m just not at that point yet.
My simplified test class:
class Sms_IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase {
...
public function testHelpMessage() {
// will output "Zend_Controller_Request_HttpTestCase"
print get_class($this->getRequest());
...
}
}
If I override the request and response object before running the tests like so:
public function setUp()
{
$this->bootstrap = new Zend_Application(APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini');
parent::setUp();
$this->_request = new Sms_Model_Request();
$this->_response = new Sms_Model_Response();
}
I’m unable to set up my tests by using the methods in Zend_Controller_Request_HttpTestCase like setMethod and setRawBody before calling the front controller to dispatch.
How can I unit test my controllers after I’ve subclassed the request and response objects?
What I ended up doing was copying the entire code of the Request and Response test case objects into subclassed versions of my own request and response classes. Here’s the gist of the request object:
Sublcassing the Request and Response objects and pasting the entire code of RequestTestCase:
and then setting them in the setUp() function of the ControllerTest:
Then it worked.