Mock Data Generator
Generate realistic test data., (*1)
, (*2)
Why?
I work with very data-intensive applications. Sometimes I need large quantities of test data for building test cases and seeding web services, databases, online forms, etc. I wrote this library to assist with this., (*3)
With a simple loop, I can generate a database representing 100,000 people to use in my development and testing., (*4)
Base data sources included in this library
- US City/County/State/Zip Database
- First/Last Names from US Census
- Common U.S. Street Names
Mock Data Generation
Many different types of mock data can be generated with this library. From basic building blocks like numbers and dates to a Person with just about every attribute you need from a Date of Birth to Driver's License and Credit Card., (*5)
Data Realism
This library is designed to create very realistic-looking data., (*6)
- If generating a person:
- If a state is specified:
- The city will be a valid city in that state
- The area code, zip, and county will be correct for that city
- The SSN will be in a valid range for that state
- The email address will contain some portion of their name
- First/middle name will be appropriate for the selected gender
- For credit card numbers:
- The prefix and length will match the type of card generated (MasterCard, Visa, etc.)
Usage
``` php
$generator = new joshmoody\Mock\Generator();, (*7)
$person = $generator->getPerson('AR');
print_r($person);, (*8)
Example output:
joshmoody\Mock\Entities\Person Object
(
[guid] => d36fb161-39a8-ca74-9564-e5f903b6bad1
[unique_hash] => f7382ef385fccc215ac1f4cd3f975ad63898556f
[name] => joshmoody\Mock\Entities\FullName Object
(
[first] => Leonard
[middle] => Gordon
[last] => Roberts
[gender] => M
), (*9)
[company] => Martin Auto
[address] => joshmoody\Mock\Entities\Address Object
(
[line_1] => 6496 6th Street
[line_2] =>
[city] => Fort Smith
[zip] => 72903
[county] => Sebastian
[state] => joshmoody\Mock\Entities\State Object
(
[code] => AR
[name] => Arkansas
)
)
[address2] => joshmoody\Mock\Entities\Address Object
(
[line_1] => 4126 Berkshire Drive
[line_2] => Suite 3352
[city] => Fort Smith
[zip] => 72903
[county] => Sebastian
[state] => joshmoody\Mock\Entities\State Object
(
[code] => AR
[name] => Arkansas
)
)
[internet] => joshmoody\Mock\Entities\Internet Object
(
[domain] => martinauto.com
[username] => lroberts
[email] => leonard@yahoo.com
[url] => http://www.martinauto.com
[ip] => 201.254.71.153
)
[phone] => stdClass Object
(
[home] => 479-123-0338
[mobile] => 479-761-9748
[work] => 479-551-3998
)
[ssn] => 429836996
[dln] => joshmoody\Mock\Entities\DriverLicense Object
(
[number] => 927684836
[state] => AR
[expiration] => 06/2017
)
[credit_card] => joshmoody\Mock\Entities\CreditCard Object
(
[type] => American Express
[number] => 347716769562193
[expiration] => 03/2015
)
[bank_account] => joshmoody\Mock\Entities\BankAccount Object
(
[type] => Checking
[name] => First National
[account] => 935060029
[routing] => 075938878
)
[dob] => DateTime Object
(
[date] => 1994-05-13 03:56:44.000000
[timezone_type] => 3
[timezone] => UTC
)
), (*10)
Each type of data element above may be generated independently.
### Names
Get a full name (first, middle, last, gender).
> Why is gender is included as a property of the name? In the U.S., first and middle names are usually closely associated with gender.
``` php
$name = $generator->getFullName();
/*
joshmoody\Mock\Entities\FullName Object
(
[first] => Laurie
[middle] => Joyce
[last] => Wilson
[gender] => F
)
*/
Or get parts of a name:, (*11)
``` php
$first = $generator->getFirstName('M'); // M=Male, F=Female, null = random.
/*
string(8) "Clarence"
*/, (*12)
$middle = $generator->getMiddleName('M'); // M=Male, F=Female, null = random.
/*
string(4) "Dale"
*/, (*13)
$last = $generator->getLastName();
/*
string(6) "Rogers"
*/, (*14)
### Addresses
Get a full address with street, city, state, zip
``` php
$address = $generator->getAddress();
/*
joshmoody\Mock\Entities\Address Object
(
[line_1] => 2835 Hamilton Street
[line_2] =>
[city] => Hyndman
[zip] => 15545
[county] => Bedford
[state] => joshmoody\Mock\Entities\State Object
(
[code] => PA
[name] => Pennsylvania
)
)
*/
Or gets parts of an address:, (*15)
``` php
$street = $generator->getStreet();
/*
string(15) "2162 9th Street"
*/, (*16)
$apartment = $generator->getApartment();
/*
string(9) "Apt. 6677"
*/, (*17)
$city = $generator->getCity('AR');
/*
string(8) "Little Rock"
*/, (*18)
$state = $generator->getState();
/*
joshmoody\Mock\Entities\State Object
(
[code] => AR
[name] => Arkansas
)
*/, (*19)
$zip = $generator->getZip('AR');
/*
string(5) "72201"
*/, (*20)
### Phone Numbers
```php
$phone = $generator->getPhone([$state_code = false, $zip = false, $include_toll_free = false]);
/*
string(12) "908-519-1084"
*/
Internet
``` php
$internet = $generator->getInternet([$person_name = null, $company = null]);
/*
joshmoody\Mock\Entities\Internet Object
(
[domain] => martinez.us
[username] => swilliams
[email] => stacey.williams@gmail.com
[url] => https://martinez.us
[ip] => 157.116.10.90
)
*/, (*21)
$domain = $generator->getDomain($domain = null);
/*
string(8) "dean.com"
*/, (*22)
$username = $generator->getUsername([$person_name = null]);
/*
string(14) "pedro.thompson"
*/, (*23)
$email = $generator->getEmail([$person_name = null, $domain = null]);
/*
string(20) "fred.harrison@me.com"
*/, (*24)
$url = $generator->getUrl();
/*
string(19) "http://hernandez.us"
*/, (*25)
$ip = $generator->getIp();
/*
string(13) "101.114.68.26"
*/, (*26)
## Random Data
In addition to realistic data generation, you can also use this library to easily pick a random value from an array.
``` php
$color = $generator->fromArray(['Red' , 'White', 'Blue']);
/*
string(3) "Red"
*/
Or get a boolean., (*27)
``` php
$bool = $generator->getBool(); // Returns bool(true) or bool(false);
/*
bool(false)
*/, (*28)
Or get a string representation of a boolean. You define the return values for true/false
``` php
$yes_no = $generator->getBool('Yes', 'No'); // Returns string(Yes) or string(No)
/*
string(3) "Yes"
*/
$aye_nay = $generator->getBool('Aye', 'Nay'); // returns string(Aye) or string (Nay)
/*
string(3) "Nay"
*/
Requirements
- MySQL or SQlite
- PHP >= 7.2 with SQlite PDO extension.
Installation
Installation of this package is easy with Composer. If you aren't familiar with the Composer Dependency Manager for PHP, you should read this first., (*29)
composer require joshmoody/mock-data
Zero Configuration Instructions
The package ships with a sqlite database containing all the data needed for generating random records., (*30)
``` php
$generator = new joshmoody\Mock\Generator();, (*31)
## Reloading Data
You can use the load script to regenerate the sqlite database at any time.
This may be useful if modifying the source data to better fit your needs.
``` bash
$ php bin/load.php
Acknowledgements
The geographic and demographic source data used in this library was derived from several places, including:, (*32)
License and copyright
Licensed under the MIT License.
Copyright (c) 2013, Josh Moody. All rights reserved., (*33)