2017 © Pedro Peláez
 

silverstripe-module mock-dataobjects

Allows DataObjects to self-populate intelligently using fake data

image

unclecheese/mock-dataobjects

Allows DataObjects to self-populate intelligently using fake data

  • Monday, September 4, 2017
  • by unclecheese
  • Repository
  • 5 Watchers
  • 18 Stars
  • 3,068 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 9 Open issues
  • 7 Versions
  • 2 % Grown

The README.md

Mock DataObjects for SilverStripe

This module provides intelligent content generation functionality to all DataObjects. The object introspects its fields and assigns an example value based on the field type and the name of the field. It also provides a command line utility for generating mock data programatically as well as various UI features in the CMS to support the creating and populating DataObjects., (*1)

Installation

Installation via Composer is highly recommended, as this module has external dependencies., (*2)

composer require unclecheese/mock-dataobjects:dev-master

Example

class StaffMember extends DataObject {

    private static $db = array (
        'FirstName' => 'Varchar(255)',
        'LastName' => 'Varchar(255)',
        'EmailAddress' => 'Varchar(255)',
        'Address' => 'Varchar(255)',
        'City' => 'Varchar(255)',
        'PostalCode' => 'Varchar(255)',
        'Country' => 'Varchar(255)',
        'Company' => 'Varchar(255)',
        'Website' => 'Varchar(255)',
        'PhoneNumber' => 'Varchar(255)',
    );


    private static $has_one = array (
        'Photo' => 'Image',
        'StaffHolder' => 'StaffHolder',
    );
}
$staff = new StaffMember();
$staff->fill();

Result:, (*3)

Screenshot, (*4)

Implementation

You can use the features of the MockDataObjects module in many ways, including executable code, a command-line interface, and from within the CMS., (*5)

From the CMS

Adding mock children to a parent page:

Right click on the parent page and choose "Add mock children.", (*6)

Screenshot, (*7)

Choose options, and create, (*8)

Screenshot, (*9)

Adding items to a grid

Just click on "add mock data" and set your options., (*10)

Screenshot, (*11)

Populating existing records

Click on "fill with mock data", (*12)

Screenshot, (*13)

In the execution pipeline

$myDataObject->fill();

As demonstrated above, the ->fill() method populates a DataObject with mock data. There are a few options you can pass to this method., (*14)

$myDataObject->fill(array(
     'only_empty' => true, // only fill in empty fields
     'include_relations' => true, // Include has_many and many_many relations
     'relation_create_limit' => 5, // If no existing records for many_many or has_one relations, limit creation
     'download_images' => false, // Don't download images from the web. Use existing.
));

From the command line

Create 50 new records. Use existing files for file relationships., (*15)

mockdata generate Product -count 50 --no-downloads

Populate existing records with new data., (*16)

mockdata populate Product

Add new records to the has_many relation on a given page., (*17)

mockdata generate StaffMember -parent-field StaffPageID -parent "our-staff"

Localisation

Mock data values are localised to the current locale as defined by i18n::get_locale()., (*18)

i18n::set_locale('en_US');
$staff = new StaffMember();
$staff->fill();
echo $staff->PhoneNumber; // (102) 806-3915

i18n::set_locale('fr_FR');
$staff = new StaffMember();
$staff->fill();
echo $staff->PhoneNumber; // +33 8 17 54 64 62

Field name hooks

For generic database fields, such as Varchar, the mock data generator is informed by the field name in order to create more realistic data. These hooks are defined in the language file., (*19)

en:
  MockDataObject:
    FIRSTNAME: "Firstname, FirstName"
    LASTNAME: "Surname, LastName, Lastname"
    FULLNAME: "FullName"
    CITY: "City, Town"
    STATE: "State"
    ADDRESS: "Address, Address1, Address2"
    POSTCODE: "Zip, Zipcode, ZipCode"
    COUNTRYCODE: "Country, CountryCode"
    PHONENUMBER: "Phone, PhoneNumber, Fax, Cell, Mobile, Telephone, Phonenumber"
    EMAIL: "Email, EmailAddress"
    COMPANY: "Company, CompanyName, Organization"
    URL: "URL, Website, URI"
    LATITUDE: "Latitude, Lat"
    LONGITUDE: "Longitude, Lon"

A comma-separated list of possible field names are mapped to an entity, so that a field named "EmailAddress" or "Email" creates a fake email address, and "Phone" or "Telephone" creates a fake phone number., (*20)

An example language file for French might look like this:, (*21)

fr:
  MockDataObject:
    FIRSTNAME: "Prenom"
    LASTNAME: "NomDeFamille, Nom"
    CITY: "Ville"

Model-independent data generation

Sometimes it is useful to generate mock data before the model has been created, such as when frontend development is happening before backend development. For that purpose, every DataObject comes with a $Fake method to access the fake data generator., (*22)



$Fake.Words

$Fake.Paragraphs(2,5) $Fake.Image.SetWidth(100)

What we can do for you

  • $Fake.Number $Fake.Words

Contact Us

$Fake.Company<br /> $Fake.FullName<br /> $Fake.Address<br /> $Fake.Address<br /> $Fake.City, $Fake.State $Fake.PostalCode<br /> $Fake.PhoneNumber<br /> <a href="mailto:$Fake.Email">$Fake.Email</a>

Find us on a map!

$Fake.Latitude, $Fake.Longitude

Cleaning up

Records of all mockdata creation are stored in the MockDataLog table, which maps a record ID to a class name. You can clean this table using the task dev/tasks/MockDataTask cleanup <classname>. To clear all mock data, leave the class name argument null., (*23)

Be very careful about converting mock data records into authentic records, as this task will clean them up without knowing that you have populated them with valid data!, (*24)

Troubleshooting

Just ring Uncle Cheese., (*25)

The Versions

04/09 2017

dev-master

9999999-dev

Allows DataObjects to self-populate intelligently using fake data

  Sources   Download

BSD-3-Clause

The Requires

 

content silverstripe seeding dataobject fake

04/09 2017

1.1.0

1.1.0.0

Allows DataObjects to self-populate intelligently using fake data

  Sources   Download

BSD-3-Clause

The Requires

 

content silverstripe seeding dataobject fake

04/09 2017

1.0.4

1.0.4.0

Allows DataObjects to self-populate intelligently using fake data

  Sources   Download

BSD-3-Clause

The Requires

 

content silverstripe seeding dataobject fake

18/10 2016

1.0.3

1.0.3.0

Allows DataObjects to self-populate intelligently using fake data

  Sources   Download

BSD-3-Clause

The Requires

 

content silverstripe seeding dataobject fake

19/03 2015

1.0.2

1.0.2.0

Allows DataObjects to self-populate intelligently using fake data

  Sources   Download

BSD-3-Clause

The Requires

 

content silverstripe seeding dataobject fake

12/11 2014

1.0.1

1.0.1.0

Allows DataObjects to self-populate intelligently using fake data

  Sources   Download

BSD-3-Clause

The Requires

 

content silverstripe seeding dataobject fake

08/11 2013

1.0.0

1.0.0.0

Allows DataObjects to self-populate intelligently using fake data

  Sources   Download

BSD-3-Clause

The Requires

 

content silverstripe seeding dataobject fake