Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arielenter's avatar

Making a test for the “string” validation in Laravel

When thinking about the purpose of the “string” validation, I immediately think about how html forms are capable of sending files. In that case, the “string” validation does really make sense to me.

Using the UploadedFile, I’m able to make a test in the lines of “x field can’t be a file”.

Meanwhile, when testing the “email” validation, I simply sent a wrongly formatted string and call the test “x field must be an email”.

In my mind, the purpose of most validations like “Integer”, “Date” or “email”, are more about the way a string is formatted rather than the data type that is being sent, so I always sent string values on those tests and call it a day.

But the specific case of the “string” validation really bugs me. Should I make a test where every single data type supported by php is sent to test that a field is truly a “string”?

As far as I know, html forms only send strings or files, but with phpunit I can send any type of value and see it rightfully return with an error when the “string” validation is used. So that made me think that maybe other applications other that html forms could theoretically send other data types too.

So anyway, maybe I’m just over thinking it, but I was wondering if this is a thing or not. Thank you.

0 likes
4 replies
arielenter's avatar

After a few minutes of more meddling I found out it appears that, when sending some other value other than a string through phpunit ,the validation "required" seems to suggest that the value is taken as not being sent at all, which makes me believe that "post" request can only use strings. If so, then it's easy to understand why a test like the one I was suggesting wouldn't be necessary.

jlrdw's avatar

Besides files anything in the request is a string, even a number is a string in the request. And higher languages you have to cast the request.

Also the type is file, if it's a file.

arielenter's avatar

@jlrdw I would have notice that phpunit was not sending anything at all when something other than a string value was being passed, if only I had added some way to see what was the error message that was being return when both "required" and "string" validations were used on my test. I was simply using assertSessionHasErrors and amusing the error being returned was due to the fact that a different type of value other than a string was being passed, while the real error was the fact that phpunit was not passing anything at all.

arielenter's avatar

At the end, the real conclusion is that the post request protocol can only handle string values or files. When files are being send, a content type attribute is also use in conjunction, so there is no fear or need to test if, for instance, a integer or an array is sent because those types of value are not detected. Something like that.

Please or to participate in this conversation.