DummyGenerator is dummy/fake data generator for PHP. It's a fork of Faker, heavily rewritten at core, but overall is same easy to use. In example:
$generator = new DummyGenerator(DefinitionContainerBuilder::all());
echo $generator->firstName();
Full list of stuff you can generate is available here.
Faker died for our because of being hard to maintain - more on that here and Faker 2.0 seems to be dead because of "death by committee" kind of stuff.
I needed simple dummy data generator for PHP 8.3 and with modern architecture in mind, so DummyGenerator came to life.
- required PHP >= 8.3
- PHPStan level 8 friendly
- PHPUnit tests for core and extensions (yep, some just check for not empty, but hey, it's random data)
- all
mt_rand
/array_rand
replaced with\Random\Randomizer
- no static methods, only one magic method (
__call()
in generator) - interfaces and dependency injection for everything (all core implementations can be replaced with different ones)
- implementations can be changed on the fly with
addDefinition()
- language providers removed from core (that makes it ~9.5Mb smaller)
- removed database providers (core is only for dummy data generation)
- removed
HmlLorem
,Uuid
(you can use any uuid generator like Symfony, Ramsey...) - removed
File::filePath()
since it was interacting with system, not only generating dummy data
There are two Randomizer implementations, default Randomizer
and if someone need it there is XoshiroRandomizer
that allows to use seed
for testing purposes (check BiasedTest
).
Providers are gone, but here are sample providers en_US
,en_GB
and pl_PL
to show how to make them / convert from old Faker.
When writing tests or populating test database you need to came up with various data, like first name, last name, some dates, maybe description, location coordinates and so on. When you deal with multi-language site and want to have it also multilanguage - you need to came up with every language names or address format.
All of that can be done by hand, but it's much easier to do $generator->firstName()
and just don't care about what name it will be. Load provider and don't care about given locale names or phone formats.
Another use case - imagine you have description with 100 chars limit and want to test if it properly gives error when more is passed - instead of copying some text you can just use $generator->text(150)
to get ~150 characters long text.
Last but not least - it make sure your tests will get random data on each run, not every single time same value. If your code is good and tests correct - then it should be no problem. If tests start failing from time to time - then what you think, where is the problem:
- with code
- with tests
- with random data, it should not be random
I leave answer to you. And yes, there might be cases when data should not be random, but usually it's not that case ;)
composer require johnykvsky/dummygenerator --dev
Everybody like quick start - it's here, you're welcome.
For quick info about how to do various stuff visit howto
There is script\ExtensionsDocs.php
that can be used to generate list of available extensions and their methods (look at generate-spec.php
)
Since --repeat
is still missing in PHPUnit here is Linux shell script for running tests multiple times.
- clear only given provider on
addDefinition
?