I want to test all my api endpoints with codeception.
I started off with a more model based approach:
class UserCest
{
public function it_can_request_its_own_status (ApiTester $I) {
$user = $I->amLoggedIn();
$I->sendGET('/users/'.$user->id.'/status');
$I->seeResponseCodeIs(200);
$I->seeResponseContainsJson(['status' => 'active']);
}
}
Now I realized that I might want to map the test directly to the routes to make it more obvious what route is tested. I thought that this would therefore rather refer to the controller that is used by the routes rather than models:
class UserControllerCest
{
public users__user__status (ApiTester $I) {
$route = __FUNCTION__;
$user = $I->amLoggedIn();
$url = $this->fillRouteWithUserId($route, $user);
$I->sendGET($url);
$I->seeResponseCodeIs(200);
$I->seeResponseContainsJson(['status' => 'active']);
}
}
Or I could even map the Cests directly to each route:
class users__user__statusCest
{
protected $route = '/users/{user}/status';
public it_reports_the_status_for_authenticated_users (ApiTester $I) {
$user = $I->amLoggedIn();
$url = $this->fillRouteWithUserId($this->route, $user);
$I->sendGET($url);
$I->seeResponseCodeIs(200);
$I->seeResponseContainsJson(['status' => 'active']);
}
public it_requires_authentication (ApiTester $I) {
$user = $I->create(User::class)
$url = $this->fillRouteWithUserId($this->route, $user);
$I->sendGET($url);
$I->seeResponseCodeIs(401);
}
}
What (other) approach is suitable for testing my API?