I’ve managed to get PHPUnit setup with my Zend Framework project using Phing to execute the tests at build time via an Eclipse external tool. My tests are found and executed at build time, but for some reason, the PHP in the pages is not being interpreted.
In an effort to figure out why my assertions were failing, I added: echo $this->response->outputBody(); to my test and realized it was echoing raw PHP. If I add echo get_class($this->response); I get Zend_Controller_Response_HttpTestCase as the class name, which from what I gather is correct.
I’ve had this issue with Apache where the file handlers were not setup to send the PHP code through the PHP executable, but as far as I know, this shouldn’t be an issue as Phing/PHPUnit should handle executing everything via the executable directly. I don’t yet know enough about how PHPUnit or Phing works to know what I could be doing wrong. Any suggestions?
Here’s my test class:
<?php
class SearchControllerTest extends ControllerTestCase
{
public function setUp()
{
parent::setUp();
}
public function testSearchPizzaChicago()
{
$this->_search('restaurants', 'chicago, il');
}
private function _search($what, $where)
{
$this->request->setMethod('POST');
$this->request->setPost(
array(
'search_what' => $what,
'search_where' => $where,
)
);
$this->dispatch(Zend_Registry::get('base_url') . '/search/results');
echo $this->response->outputBody();
//echo get_class($this->response);
$this->assertQuery('#results');
}
public function tearDown()
{
/* Tear Down Routine */
}
}
Here’s my console output when I run one of the tests:
Buildfile: C:\workspace\myproject\build.xml
myproject > test:
... raw PHP code here ...
[phpunit] Testsuite: SearchControllerTest
[phpunit] Tests run: 1, Failures: 1, Errors: 0, Incomplete: 0, Skipped: 0, Time elapsed: 0.25874 s
[phpunit] testSearchPizzaChicago FAILED
[phpunit] Failed asserting node DENOTED BY #results EXISTS
[phpunit] C:\workspace\frameworks\ZendFramework-1.11\library\Zend\Test\PHPUnit\Constraint\DomQuery.php:263
[phpunit] C:\workspace\frameworks\ZendFramework-1.11\library\Zend\Test\PHPUnit\ControllerTestCase.php:300
[phpunit] C:\workspace\myproject\tests\application\controllers\SearchControllerTest.php:34
[phpunit] C:\workspace\myproject\tests\application\controllers\SearchControllerTest.php:13
[phpunit] C:\wamp\bin\php\php5.3.5\PEAR\phing.php:37
BUILD FINISHED
Total time: 2.0895 seconds
You’re doing a POST request – if your webserver is operating correctly it would NEVER serve up raw PHP code. Check that
/search/resultsscript to see if it’s being actually executed. Since you’re getting php code, most likely it’s NOT being seen/treated as a php script and its contents are being served up as plain text instead.