How should I organize validation in Laravel to be able to test/TDD?
I think I need to throw ValidationException
from services instead of doing something in controllers, to be able to assert it in unit tests.
For example I have changePassword($user, $password, $newPassword)
method in UserService
where I need to check that $password
is correct and that $newPassword
is not empty, etc.
Should I do it like here https://stackoverflow.com/a/23595760/964478? Create a validator via Validator::make
somewhere and
if ($validator->fails())
{
throw new ValidationException($validator);
}
And in unit tests
public function should_not_change_password_if_incorrect_current_password()
{
$user = ......;
$this->expectException(ValidationException::class);
$this->userService->changePassword($user, 'incorrect', 'newPassword');
}
Or there are better ways?
Or I should not unit test it and just do acceptance testing (checking HTTP response, session, ...)?