Faker – The ultimate Lorem Ipsum for PHP
From time to time we all have to work with data which doesn’t exist. The best example is web development. Front-end developers create HTML and CSS before any web content is created. The common practice is to use Lorem Impsum which proved to be effective strategy. It’s not much different with back-end programming. We need dummy data at least as often as front-end devs. The most popular cases are:
- emulating user input
- emulating external service
- filling database (for development, presentation or performance test)
- mocking object for Unit Test
It’s always better to work with real or at least close to real content. First of all there is a psychological aspect – it looks better. When your database is filled with identical rows even a completed code will look like work in progress. Quality data gives more confidence. It’s good to know will code work with something more then a fix string. Finally there is a practical reason. A good example would be performance test. Without good realistic data you might measure values which will never occur in the real live.
Generating good dummy data manually is a cumbersome process. It’s very tempting to take shortcuts. Fortunately there is no need to do it “by hand”.
Faker is a PHP library that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.
To install Faker download package from GitHub or use the Composer. I prefer to use Composer. Give it a try If you don’t have it.
1 |
$ curl -s http://getcomposer.org/installer | php |
Create (or edit) composer.json
1 |
$ vim composer.json |
and add “fzaninotto/faker”: “v1.1.0″.
1 2 3 4 5 |
{ "require": { "fzaninotto/faker": "1.1.0" } } |
Now you are ready to install Faker.
1 |
$ php composer.phar install |
Once you are all setup with the library you can try this example.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php require_once "vendor/autoload.php"; $faker = Faker\Factory::create(); for( $x=0 ; $x<5 ; $x++ ) { printf( "%s <%s> from %s\n", $faker->name, $faker->email, $faker->city ); } |
It should return something like this:
1 2 3 4 5 6 |
$ php test.php Francisco Boehm <terry.andre@kulas.com> from Jasminmouth Richard Hettinger <dharris@prosacco.biz> from North Shad Ethyl Hudson <lowell14@cassin.biz> from Metzmouth Ansley Reilly <gusikowski.selina@gmail.com> from Schaeferburgh Willa Hayes MD <kankunding@johnsonparker.info> from East Nicole |
Faker can provide all sort of random data like: Addresses, Telephone Numbers, Companies, Domains, Emails, Ips, DataTime and of course Lorem Ipsum. To see full list of all available features have a look into the manual.
If that is not enough Faker has one more very cool feature. It can work with popular ORMs like Propel, Doctrine2 and Mandango.
1 2 3 4 5 6 7 |
<?php $generator = \Faker\Factory::create(); $populator = new Faker\ORM\Propel\Populator($generator); $populator->addEntity('Author', 5); $populator->addEntity('Book', 10); $insertedPKs = $populator->execute(); |
The example will insert 5 Authors and 10 Books into a database. Faker tries to figure out on it’s own what type of data is required for each attribute. It’s obviously unable to guess everything but still, it does a great job.
Faker is great and very usefully library. It provides impressive features wrapped in a clear API with good documentation. When you need to generate some dummy data Faker is the way to go.