I created, using laravel, a php with this method:
public function register(Request $request) {
try {
$validator = \Validator::make($request->input(), [
'email' => 'required|email|unique:users_x_communities',
'first_name' => array('required', 'regex:/^[\pL\s]+$/u'),
'last_name' => array('required', 'regex:/^[\pL\s]+$/u'),
'community_id' => 'required|exists:communities,id',
], [
'email.required' => 'El correo electrónico es requerido.',
'email.email' => 'El correo electrónico es inválido.',
'email.unique' => 'El correo electrónico ya esta en uso.',
'first_name.required' => 'Los nombres son requeridos.',
'first_name.regex' => 'Los nombres deben ser un texto.',
'last_name.required' => 'Los apellidos son requeridos.',
'last_name.regex' => 'Los apellidos deben ser un texto.',
'community_id.required' => 'La comunidad es requerida.',
'community_id.exists' => 'La comunidad no existe.'
]
);
if ($validator->fails()) {
return $this->errorBadRequest($validator->messages());
}
$email = strtolower($request->get('email'));
$firstName = $request->get('first_name');
$lastName = $request->get('last_name');
$communityId = $request->get('community_id');
$status = $this->auth->register($firstName, $lastName, $email, $communityId);
$message = "OK!";
return $this->response->array(compact('status', 'message'));
} catch (Exception $e) {
Log::error('AuthController@register: ' . $e->getMessage());
return $this->validException($e);
}
}
Im interested in making some test in codeception to validate my function, in this case that the 4 fields, email, first_name, last_name and community_id are required.
ive managed this in codeception (ResgisterCest.php):
public function validateifmailisrequired(ApiTester $I) //200
{
//Method created to validate if the mail is required
$I->wantTo('Validate that the mail field is required');
$I->sendPOST($this->url, [
'email' => '',
]);
}
But im kind of new and im not quite sure how to check the response that the email, for example, is required. Im new in this and a bit confused with codeception... how can i achive this? to have codeception tell me that if i sendPost with an empty mail returns me a response of field required or some response that let me test that my email and others are required....
Use seeResponseContainsJson method.
$I->sendPOST($this->url, ['email' => '']);
$I->seeResponseContainsJson(['error' => 'El correo electrónico es requerido.']);
Make the structure of expected array match the structure of response (but don't specify all keys, only those that matter for this test).