I'm trying to write an API test case for a controller function using codeception, and I'm hitting an issue where the route to the controller function does not appear to be evaluated correctly, and the evaluation seems to be different depending on what I have in my test case.
Here is a code sample from my test case:
use \ApiTester;
class CustomerRegisterCest
{
// tests
public function testGetRegister(ApiTester $I)
{
$I->sendGET('register');
$I->seeResponseCodeIs(200);
}
public function testPostRegister(ApiTester $I)
{
$I->sendPOST('register', [
// set the data in here
]);
$I->seeResponseCodeIs(200);
}
I have a routes.php file containing these routes:
Route::get('/', ['as' => 'home', 'uses' => 'HomeController@getIndex']);
Route::get('register', ['as' => 'getRegister', 'uses' =>'RegistrationController@getRegister']);
Route::post('register', ['as' => 'postRegister', 'uses' => 'RegistrationController@postRegister']);
I have inserted some debug statements into my controller classes so that I can see what routes get run, like this:
Log::debug('GET register'); // or GET index or POST register, etc
At the moment I have stripped down everything from my controller classes so that ONLY the debug statements are included.
When I run the test case as above, I get the following debug output:
GET register
GET index
... so it appears that sendPOST('register', ...) actually routes to the GET route for "/" instead of the POST route for "/register". Outside of the test case everything works normally -- I can POST to the register routes fine, routing appears to work OK, the problem only appears inside a codeception test case.
If I change the test case so that I am doing the sendGET and the sendPOST inside the same function call, for example like this:
// tests
public function testPostRegister(ApiTester $I)
{
$I->sendGET('register');
$I->seeResponseCodeIs(200);
$I->sendPOST('register', [
// set the data in here
]);
$I->seeResponseCodeIs(200);
}
then I see this debug output:
GET register
GET register
... so that by inserting the sendGET into the same function as the sendPOST, it has changed the sendPOST behaviour so that it now routes to the GET route for register instead of the GET route for index (but still won't route to the correct POST route).
I have tried turning xdebug on and don't have any clues from the xdebug output as to what's going on either.