I am following the Testing setup for Slim with PHPUnit @ http://there4development.com/blog/2013/10/13/unit-testing-slim-framework-applications-with-phpunit/
Initially I was having all my logics in the anonymous function
$app->get('/video/', function () use ($app) {
// all code goes here
}
and testing via PHPUnit worked great...
public function testVideoCountInPage1() {
$this->get('/video/');
$this->assertEquals(200, $this->response->status());
$rawResponse = $this->response->body();
$jsonResponse = json_decode($rawResponse);
$this->assertSame(20, count($jsonResponse->data));
}
But now, I split the core logic in `get('/video/') into multiple functions like this:
$app->get('/video/', function () use ($app) {
// some logic
$db = openDB($dbConfig);
$page = findPageParameter($app->request()->params());
// some logic
}
function openDB($dbConfig) {
// open DB here
return $db;
}
function findPageParameter($params) {
// find page here
return (int)$page;
}
Still I get proper response for calling /video
endpoint. But the Unit Tests fail, saying
.PHP Fatal error: Cannot redeclare openDB() (previously declared in /var/www/traffic/app/routes/video.php:69) in /var/www/traffic/app/routes/video.php on line 75
Update:
That error got fixed, once I replaced couple of require
with require_once
. But now assertions in the Tests fails saying
1) videoTest::testVideoCountInPage1
Failed asserting that 404 matches expected 200.
when I call the same end point http://localhost/traffic/index.php/video
, I am getting status 200 with the proper results. When the PHPUnit calls the same endpoint, it returns 404
Update 2:
The Unit Tests, where I test the individual functions openDB()
and findPageParameter()
works fine. Only the end-end testing of SLIM REST APIs fail with 404...
Reference:
- video.php (https://github.com/GethuGames/Traffic-Violation-Portal-REST-API/blob/toDB/app/routes/video.php)
- videoTest.php (https://github.com/GethuGames/Traffic-Violation-Portal-REST-API/blob/toDB/tests/integration/videoTest.php) (End-End testing that fails)
- videoUnitTest.php (https://github.com/GethuGames/Traffic-Violation-Portal-REST-API/blob/toDB/tests/unit/videoUnitTest.php) (Unit Tests that work)
- Entire Project @ https://github.com/GethuGames/Traffic-Violation-Portal-REST-API/tree/toDB