How to use Faker – PHP library that generates fake data

how to use faker php dummy data
As a developer almost every project that I am working needs some dummy data. Because without data I cannot test my application in which I am working. In the old days of my programming I usually create a form and insert dummy data. To be very honest I hate that process because data insertion took lot of time.

But now lots of PHP libraries are available to tackle this problem. Faker is one of them. Faker is a fake data library inspired by PERL’s Data::Faker and by RUBY’s Faker. As per official definition.

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.

Faker Installation:

There are 3 ways to download Faker. Composer, GIT and direct download .In this tutorial I am going to download Faker via GIT.

GIT Link:

https://github.com/fzaninotto/Faker.git

Direct Download Link:

https://github.com/fzaninotto/Faker/archive/master.zip

Composer Command:

composer require fzaninotto/faker

After downloading Faker via GIT its directory structure will look like this.

faker directory strucure

For now there are 2 important folders, one is src and second is test. SRC folder has all the source of Faker Library. And in the test folder you can see its documentation and test file. I am going to work on test directory. Now I am going to create a file as hello-world.php and include faker autoload file and create its instance.

Also visit Insert dummy data into MYSQL using PHP

Usage:

Hello-world.php:

I created $faker as an instance. Factory::create() can take a locale as an argument and returned localized data but by default it takes en_US. You can check available Faker locale in the src\Faker\Provider directory. $faker->name returns different name so as $faker->address and $faker->text .


Get Data Using Loop:

If I want to get multiple values of faker then.

Formatters:

Formatters are the faker properties (name, address, text). Faker provides loads of formatters under “providers”. I am going to show some of the most frequent used formatters. If you want to read all of them then you can visit https://github.com/fzaninotto/Faker#formatters

Base Providers:

Lorem Providers:

Person Providers: (Default en_US)

This provider is worked with localization. If you pass different locale parameter in Factory::create() then result will come up according to that.

PhoneNumber Providers: (Default en_US)

This provider is also worked like person providers.

Company Providers: (Default en_US)

Text Providers: (Default en_US)

DateTime Providers:

Internet Providers:

UserAgent Providers:

Payment Providers:

Color Providers:

File Providers:

Image Providers:

Miscellaneous Providers:

Conclusion:

In testing we always need some data to insert in database. Instead of manually insert value against every column, faker allows you to generate various kinds of random data for testing.

 

Posted in PHP

Author: Ahsan Zameer

I am a web developer and love to search new stuff on web. Trying different approaches and ideas to make web developing more interesting and enjoyable.