I am very new to Laravel, and I need some help with refactoring my code. Now, method is not covered in tests in 100% because I cannot mock validator objet and ints responses. I have following method in my cotroller
public function store(Request $request)
{
$data = $request->only([
'name',
'email',
'message',
]);
$validator = Validator::make($data, $this->validatorRules);
if ($validator->fails()) {
return $this->response->errorFromValidator($validator);
}
$savedItem = $this->repository->store($data);
if (!$savedItem) {
return $this->response->error('Cannot save');
}
return $this->response->succesItem($savedItem);
}
I tried to inject validator in controller's constructor:
function __construct(Response $response, Repository $repository, Validator $validator)
{
$this->response = $response;
$this->repository = $repository;
$this->validator= $validator;
}
and use it in method:
$this->validator::make($data, $this->validatorRules);
but I was getting syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM). How can I abstract validator outside the method, so I can mock validator in tests?
Aucun commentaire:
Enregistrer un commentaire