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

bwrigley's avatar

How to ask Faker to generate text over $x chars long?

Hello,

Hoping this will be really simple.

I have verification on a field to ensure it is no longer than 500 chars:

'about' => ['required', 'max:500'],

And now I want to write a test that will always go over that limit so I can check the correct error message appears.

How can I ask Faker/Generator to generate random text longer than 500 chars?

Thanks!

0 likes
7 replies
Sinnbeck's avatar

Why not just use Str::random(501); ?

1 like
Sinnbeck's avatar

Or this should work

$faker->text(501)
2 likes
bwrigley's avatar

@sinnbeck thanks for the fast response.

Yes I can and I will but I was hoping to generate more 'natural' text for some other tests I am doing.

Perhaps I will isolate this test and just do as you suggest and use faker in the other test.

Do you know of a way to ask Faker to generate text/lorem ipsum longer than n?

bwrigley's avatar

@sinnbeck oops our messages crossed.

sadly no, even

$faker->text(600),

often generates text shorter than 500 chars so my test is flaky.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Oh didnt know :)

How about adding some way longer then?

$faker->sentence(490);
1 like
bwrigley's avatar

Yeah I wondered about that too, but wasn't sure if that will always definitely be longer than 500, but I expect it will.

Sinnbeck's avatar

Well a word is at least on character so setting a sentence to 500 words should absolutely do it :) Notice that it isnt max as text is

1 like

Please or to participate in this conversation.