johnykvsky / dummygenerator
Dummy / fake data generator for modern PHP
Installs: 1 144
Dependents: 2
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- php: ^8.3
- psr/clock: ^1.0
Requires (Dev)
- phpstan/extension-installer: ^1.4.3
- phpstan/phpstan: ^2.1.18
- phpunit/phpunit: ^12.2.0
- slevomat/coding-standard: ^8.19.1
- squizlabs/php_codesniffer: ^3.13.2
Suggests
- ext-intl: Required for better transliteration i.e. in Internet Extension
- ext-mbstring: Required for multibyte Unicode string functionality.
README
Installation
composer require johnykvsky/dummygenerator --dev
About
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 = DummyGeneratorFactory::create(); // all extensions are loaded echo $generator->firstName();
Documentation
Documentation is available under https://johnykvsky.github.io/dummydocs/
But why...?
Faker died for our because of being hard to maintain as mentioned in sunsetting-faker.
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, with modern architecture in mind. This is how DummyGenerator came to life.
Changes in compare to Faker
- 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 generator ~9.5Mb smaller
- changed
DateTime
extension, it supportsDateTimeInterface
for methods params (not only strings) - changed
Uuid
, it supportsv4
only, useuuid4()
- removed database providers (core is only for dummy data generation)
- removed
HmlLorem
- removed
File::filePath()
since it was interacting with system, not only generating dummy data - added
Enum
, to get random values from PHP enums - added
String
, to generate random string from given pool - added support for
SystemClock
, PSR-20 implementation of Clock
This package also fixes problem with FakerPHP __destruct()
messing up with seed()
, plus various other issues.
There are two Randomizer implementations available:
- default
Randomizer
- additional
XoshiroRandomizer
, which supportsseed()
- to be used in tests
Languages
One of main points of DummyGenerator is to keep core language agnostic. This is why all languages has been removed from core.
Person
extension provides only ~15 names than can be used as first name, last name, part of email etc. If you want more, check dummyproviders to get full providers for en_US
,en_GB
and pl_PL
.
I have created them to show how to make them / convert from old Faker, to allow anyone to work on other languages.
Keep in mind:
- core will stay language agnostic
- I have no current plans to support any language
- I have no current plans to work on extending/improving existing language providers.
- if someone like to make a PR to improve/extend one of mentioned languages - I will gladly look at it.
- I will not accept PRs with other languages - but I will gladly link in this readme to repositories with them.
Why PHP >= 8.3
Because of introduced in PHP 8.3:
Randomizer::getFloat()
Randomizer::getBytesFromString()
- and not so important but nice: typed class constants
What is this fake / dummy data
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 ;)
Other stuff
There is script\ExtensionsDocs.php
that can be used to generate list of available extensions and their methods (look at generate-spec.php
)
Since PHPUnit is still missing --repeat
, in repository phpunit-repeat you can find Linux shell script for running tests multiple times.
TODO (ideas, not promises)
- alternative DateTimeExtension with support only for DateTimeImmutable params (without strings)?