magical-yuri / magical-girl
测试数据生成器
This package is not auto-updated.
Last update: 2024-09-24 08:22:55 UTC
README
这是一个将测试数据插入到您数据库中的库。
MagicalGirl会为数据库中的表生成适合的随机测试数据,并插入测试数据。
环境
- PHP 5.6
- MySQL 5.6
如何安装
您需要Composer来安装MagicalGirl。
1. 安装Composer
curl -s http://getcomposer.org/installer | php
2. 准备 'composer.json'
{
"require-dev": {
"phpunit/phpunit": "*",
"magical-yuri/magical-girl": "*"
},
"config": {
"bin-dir": "bin/"
}
}
3. 安装MagicalGirl
php composer.phar install
准备使用
1. 初始化
请执行以下命令。
bin/magicalinit
2. 数据库连接设置
请在 'lib/DB/DBConnections.php' 中实现获取 DB-host、DB-user-name 和 DB-password 的方法
以下是一个示例,数据库名为 'example'。
protected static function getExampleHost()
{
return '127.0.0.1';
}
protected static function getExampleUserName()
{
return 'user_name';
}
protected static function getExamplePassword()
{
return 'password';
}
3. 为您的表生成导入器类
执行以下命令,将生成两个文件。
bin/magicalgen [databaseName] [tableName]
lib/DB/TableImporter/[shemaName]/[databaseName][tableName]Importer.php
lib/DB/TableImporter/[shemaName]/base/Base[schemaName][tableName]Importer.php
不要修改名为 'Base...php' 的文件。
每次执行 magicalgen 命令时,它将从运行时的表结构重新生成 'Base...php' 文件。
当您需要自定义导入器时,请只修改 '[databaseName][tableName]Importer.php' 文件。
4. 准备主数据
当您始终需要导入相同的数据时,例如主数据,您需要准备一个 tsv 格式的 master-data 文件。
请按照以下目录结构准备 tsv 文件。
masterData
└── [databaseName]
├── [tableName1].tsv
├── [tableName2].tsv
└── [tableName3].tsv
此 tsv 文件不包含标题。
您可以通过执行以下命令获取此 tsv 文件。
mysql -h 127.0.0.1 -u user -p -B -N -e 'select * from [tableName1]' [databaseName] > [tableName1].tsv
如何使用
例如,您可以通过创建以下文件来使用MagicalGirl。
<?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 类
__construct($option = null)
创建一个 TableImporter 对象。
MagicalGirl期望您为 $option 指定以下值。
默认选项为 OPTION_WITH_TRUNCATE。
- TableImporter::OPTION_WITHOUT_TRUNCATE
- MagicalGirl将在插入测试数据之前截断表。
- TableImporter::OPTION_WITH_TRUNCATE
- MagicalGirl将在插入测试数据之前不截断表。
addRow(array $values = array(), array $ngValues = array())
将测试数据插入到您的表中。
$importer->addRow(
array('fieldName' => 'value1', 'fieldName' => 'value2'),
array('fieldName3' => 'ngValue1', 'fieldName4' => 'ngValue2')
);
@param array $values array(fieldName => value, ...) MagicalGirl将这些值插入到这些列中。
@param array $ngValues array(fieldName => array(value, value, ...)) MagicalGirl永远不会将这些值插入到这些列中。
@access public
@return array $row array(fieldName => value, ...) 实际插入的值
MasterDBImporter 类
import($tsvDirPath = null)
从 tsv 文件导入主数据。
$tsvDirPath 的默认值是 magicalinit 命令生成的 masterData。
@param string $tsvDirPath 包含 tsv 文件的目录路径
@access public
@return void
如何测试 TableImporter 类
我将解释如何测试由 magicalgen 命令自动生成的或您自定义的 TableImporter 类。
您需要 'phpunit'。
例如,您可以通过创建以下文件来测试 TableImporter 类。
<?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'));
}
}
通过执行以下命令进行测试。
bin/phpunit /path/to/MyTableImporterTest.php
许可证
MIT
版权
MagicalGirl版权所有 2014 MagicalYuri。