I'm trying to validate a required (possibly) empty array in my FormRequest
.
I'm usign the present
and array
validation rules, since i want the value to be passed, but it coul be an empty array.
The problem is that my test fail on an empty string (i.e.: ''
), telling me that my request doesn't throw an expected ValidationException
with that value.
My test cover the following values for the given field:
input value | expected result | test outcome |
---|---|---|
null |
error present | passed |
'a string' |
error present | passed |
'' |
error present | failed |
123 |
error present | passed |
123.456 |
error present | passed |
true |
error present | passed |
[] |
error not present | passed |
['a', 'b'] |
error not present | passed |
How can i test that expected request parameter is present, is an array and is possibly an empty array?
Update #1: My Code
The request
class TestRequest extends FormRequest
{
public function rules()
{
return [
'array_field' => ['present', 'array'],
];
}
}
The test
public function testTest()
{
$tests = [
[
'value' => null,
'outcome' => 'failure',
'message' => 'Failed asserting that request returned error for when null is passed.',
],
[
'value' => 'string',
'outcome' => 'failure',
'message' => 'Failed asserting that request returned error when non empty string is passed.',
],
[
'value' => '',
'outcome' => 'failure',
'message' => 'Failed asserting that request returned error when empty string is passed.',
],
[
'value' => 123,
'outcome' => 'failure',
'message' => 'Failed asserting that request returned error when integer is passed.',
],
[
'value' => 123.456,
'outcome' => 'failure',
'message' => 'Failed asserting that request returned error when float is passed.',
],
[
'value' => true,
'outcome' => 'failure',
'message' => 'Failed asserting that request returned error when boolean is passed.',
],
[
'value' => [],
'outcome' => 'success',
'message' => 'Failed asserting that request returned no error when empty array is passed.',
],
[
'value' => ['a', 'b'],
'outcome' => 'success',
'message' => 'Failed asserting that request returned no error when filled array is passed.',
],
];
foreach ($tests as $test) {
try {
$request = new TestRequest([
'array_field' => $test['value']
]);
$request
->setContainer(app())
->setRedirector(app(Redirector::class))
->validateResolved();
} catch (ValidationException $e) {
}
if ('failure' == $test['outcome']) {
$this->assertTrue(
isset($e),
'Failed asserting that request throw an exception for invalid ' . json_encode($test['value']) . ' value.'
);
$this->assertArrayHasKey(
'array_field',
$e->errors(),
$test['message']
);
unset($e);
} else {
$this->assertFalse(
isset($e),
$test['message']
);
}
}
}
PhpUnit output
Update #2: the combinations of rules used
I've tested with
present
+array
array
required
+array
but none of those make the validation pass.
Update #3: the end
Found this old question that depict my same situation: it seems impossible to achive my goal with available validation rules; this comment states that the closer solution is to use ConvertEmptyStringsToNull
to convert empty strings to nulls and test validation just against the null
value.