magical-yuri/magical-girl

Test Data Generator

1.0.0 2014-10-30 09:13 UTC

This package is not auto-updated.

Last update: 2024-03-26 06:00:21 UTC


README

This is a library that insert test-data to your database.
MagicalGirl generate the random value for test-data that suitable for your table in database, and insert the test-data.

Environment

  • PHP 5.6
  • MySQL 5.6

How to Install

You need Composer to install MagicalGirl.

1. Install Composer

curl -s http://getcomposer.org/installer | php

2. Prepare 'composer.json'

{
    "require-dev": {
        "phpunit/phpunit": "*",
        "magical-yuri/magical-girl": "*"
    },
    "config": {
        "bin-dir": "bin/"
    }
}

3. Install MagicalGirl

php composer.phar install

Prepare to Use

1. Initialize

Please execute the following command.

bin/magicalinit

2. Database Connection Setting

Please implement the method to get DB-host, DB-user-name and DB-password in 'lib/DB/DBConnections.php'
The following is an example, that database-name is 'example'.

    protected static function getExampleHost()
    {
        return '127.0.0.1';
    }
    
    protected static function getExampleUserName()
    {
        return 'user_name';
    }
    
    protected static function getExamplePassword()
    {
        return 'password';
    }

3. Generate the Importer class for your Table

Execute the following command, and two files will be generated.

bin/magicalgen [databaseName] [tableName]
  • lib/DB/TableImporter/[shemaName]/[databaseName][tableName]Importer.php
  • lib/DB/TableImporter/[shemaName]/base/Base[schemaName][tableName]Importer.php

Do not modify the file that named 'Base...php'.
Every time you execute magicalgen command, it re-generate the file 'Base...php' from table structure of run time.
When you need to customize the Importer, please modify only the file '[databaseName][tableName]Importer.php'

4. Prepare Master-data

When you always need to import the same data, for example, master-data, you prepare the master-data formed tsv file.
Please prepare tsv file in following directory structure.

masterData
└── [databaseName]
    ├── [tableName1].tsv
    ├── [tableName2].tsv
    └── [tableName3].tsv

This tsv file does not include the header.
You can get this tsv file by executing the following command.

mysql -h 127.0.0.1 -u user -p -B -N -e 'select * from [tableName1]' [databaseName] > [tableName1].tsv

How to Use

For example, you can use MagicalGirl by creating the following file.

<?php
// load MagicalGirl by autoload in composer
require_once(__DIR__ . '/vendor/autoload.php');
use MagicalGirl\TestDBImporter\TestDBImporter;
use MagicalGirl\TestDBImporter\MasterDBImporter;

// require the file that be generated by magicalgen command
require_once(__DIR__ . '/lib/DB/TableImporter/test/TestTable1Importer.php');
require_once(__DIR__ . '/lib/DB/TableImporter/test/TestTable2Importer.php');
 
class MyDBImporter extends TestDBImporter
{
  public function import()
  {
    // generate the Importer object for your tables
    $table1Importer = new TestTable1Importer(); 
    $table2Importer = new TestTable2Importer();

    // insert 20 rows to your tables
    for ($i = 0; $i < 20; $i++) {
      $table1Row = $table1Importer->addRow();
      $table2Row = $table2Importer->addRow(
        array(
          'table1_id' => $table1Row['id']
        )
      );
    }
  }
}

// Usually, following code is written in another file

// import the master-data from tsv files
$importer = new MasterDBImporter();
$importer->import();

// import the transaction-data by using class that is defined in above
$importer = new MyDBImporter();
$importer->import();

API

TableImporter class

__construct($option = null)

Create a TableImporter object.
MagicalGirl expect you to specify following values for $option.
Default option is OPTION_WITH_TRUNCATE.

  • TableImporter::OPTION_WITHOUT_TRUNCATE
    • MagicalGirl will truncate table before insert the test-data.
  • TableImporter::OPTION_WITH_TRUNCATE
    • MagicalGirl will not truncate table before insert the test-data.

addRow(array $values = array(), array $ngValues = array())

Insert test-data to your table.

$importer->addRow(
    array('fieldName' => 'value1', 'fieldName' => 'value2'),
    array('fieldName3' => 'ngValue1', 'fieldName4' => 'ngValue2')
);

@param array $values array(fieldName => value, ...) MagicalGirl will insert these values in these columns.
@param array $ngValues array(fieldName => array(value, value, ...)) MagicalGirl will never insert these values in these columns.
@access public
@return array $row array(fieldName => value, ...) actually inserted values

MasterDBImporter class

import($tsvDirPath = null)

Import the master-data from tsv files.
Default value of $tsvDirPath is masterData, generated by magicalinit command.

@param string $tsvDirPath directory path that has tsv files
@access public
@return void

How to Test the TableImporter class

I will explain how to test The TableImporter class that is auto-generated from magicalgen command or is customized by you.
You need 'phpunit'.

For example, you can test the TableImporter class by creating the following file.

<?php
require_once(__DIR__ . '/../../../../../lib/DB/TableImporter/databaseName/MyTableImporter.php');
require_once(__DIR__ . '/../../../../../vendor/magical-girl/magical-girl/tests/MagicalGirl/TableImporter/TableImporterTest.php');

use Tests\MagicalGirl\TableImporter\TableImporterTest;

class MyTableImporterTest extends TableImporterTest
{
    /**
     * getDefaultValues
     * 
     * MagicalGirl will insert these values in these columns.
     *
     * @access protected
     * @see Test\MagicalGirl\TableImporter\TableImporterTest
     */
    protected function getDefaultValues()
    {
        return array('fieldName' => 'value', 'fieldName2' => 'value2');
    }

    /**
     * getNgValues
     *
     * MagicalGirl will never insert these values in these columns.
     *
     * @access protected
     * @see Test\MagicalGirl\TableImporter\TableImporterTest
     */
    protected function getNgValues()
    {
        return array('fieldName' => array('ngValue'));
    }

}

Testing by executing the following command.

bin/phpunit /path/to/MyTableImporterTest.php

License

MIT

Copyright

MagicalGirl is Copyright 2014 by MagicalYuri.