Replies: 3 comments
-
Sounds interesting. I'd usually do that with a dataset in conjunction with this package. Did you have a particular API in mind? |
Beta Was this translation helpful? Give feedback.
-
I was thinking about this feature (maybe by creating a package, but I didn't have time yet) since few weeks, so yes : I have a little idea about an API. We suppose we want to test this FormRequest: // App/Http/Requests/MyLoginFormRequest.php
class MyLoginFormRequest extends FormRequest
{
public function rules()
{
return [
'email' => ['required', 'email:rfc'],
'password' => ['required', 'min:8'],
];
}
} We call a generic test this way: // tests/Feature/MyLoginFormRequestTest.php
it('fails when a data is missing or wrong', function($data, $field, $value) {
$response = $this->post('/login', $data);
// Some specific stuff, like test if the error code is 422, and the error message is the good one ?
//Or maybe an implicit test, by default ?
})
->withFormRequest(MyLoginFormRequest::class) // The FormRequest to test
->withFormData(['email' => '[email protected]', 'password' => 'PestIsTheBest2022*'); // an array of correct data to fill the form This function loops (like when we use a dataset) over each field in the The result would be something like this: ✓ it fails when a data is missing or wrong with (email, required)
✓ it fails when a data is missing or wrong with (email, remail:fc)
✓ it fails when a data is missing or wrong with (password, required)
✓ it fails when a data is missing or wrong with (password, min:8) We will need a class which knows how to modify a value in the // In Pest core ?
abstract class PestFormRule
{
protected array $data;
protected string $field;
/**
* @param array $data All correct data to fill the form
* @param string $field the field name we want to change its value
*/
public function __construct(array $data, string $field)
{
$this->$data = $data;
$this->$field = $field;
}
abstract public function change(...$params): string
}
class MinRule extends PestFormRule
{
/**
* @param int $length the minimum length we want to test
*/
public function change(...$params)
{
$length = $params[0]; // $length equals 8, because the rule we want to test is 'min:8'
return $this->data[$this->field] = Str::substr($this->data[$this->field], 0, $length - 1);
}
}
class RequiredRule extends PestFormRule
{
/**
* Removes the `$field` key from `$data` array
*/
public function change()
{
return unset($this->data[$this->field]);
}
} Of course, the API could have some optional methods, like these ones: it('fails when a data is missing or wrong', function($data, $field, $value) {
//
})
->withFormRequest(MyLoginFormRequest::class)
->withFormData($data);
->only(['min', 'max']);
it('fails when a data is missing or wrong', function($data, $field, $value) {
//
})
->withFormRequest(MyLoginFormRequest::class)
->withFormData($data);
->except(['password', 'required']);
it('fails when a data is missing or wrong', function($data, $field, $value) {
//
})
->withFormRequest(MyLoginFormRequest::class)
->withFormData($data);
->customRules(['my-custom-rule' => MyCustomPestFormRule::class]); If we have a custom rule unlinked to a PestFormRule class, we just send a warning "Rule $rule can't be tested" (like a method without any assert). |
Beta Was this translation helpful? Give feedback.
-
But there is still one problem: what happens if a developer accidentally removes one rule, from the |
Beta Was this translation helpful? Give feedback.
-
When we create a FormRequest, with some rules, it's usefull to check if we have a correct wrong answer (422 HTTP code with a error message, mostly) when a data is missing or wrong.
I'd like to have a way to give a FormRequest and a correct array of data to a test method (in the same way as the dataset ?), which tries all the FormRequest's rules one-by-one, removing or modifying with wrong values one data at a time from the array.
Beta Was this translation helpful? Give feedback.
All reactions