Faker – The ultimate Lorem Ipsum for PHP

Posted by on Mar 13, 2013 in PHP Programming | No Comments

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.

Create (or edit) composer.json

and add “fzaninotto/faker”: “v1.1.0″.

Now you are ready to install Faker.

Once you are all setup with the library you can try this example.

It should return something like this:

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.

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.

Leave a Reply