theiconic / fixtures
将不同格式的数据集(如Yaml、XML等)加载到数据库中。
v1.5.3
2017-03-03 04:43 UTC
Requires
- php: >=5.4
- symfony/filesystem: ~2.5 || ^3.0
- symfony/finder: ~2.5 || ^3.0
- symfony/yaml: ~2.5 || ^3.0
Requires (Dev)
- phpunit/phpunit: ^5.3 || ^6.0
- satooshi/php-coveralls: dev-master
This package is auto-updated.
Last update: 2024-09-22 07:12:41 UTC
README
描述
这是一个固定数据管理器,允许您轻松地将来自不同格式的测试数据集加载到测试数据库中。具有可扩展的设计,它目前支持以下格式的固定数据集
- Yaml
- JSON
- MySQL XML 导出
它目前支持以下数据库
- MySQL
- Redis
固定数据管理器还允许您以编程方式替换固定数据中的占位符。请看下面的示例。
使用方法
假设您有两个固定数据,分别用于国家和员工表,文件名为country.xml和employee.yml。您可以用几行代码将它们加载到一个MySQL数据库中
use TheIconic\Fixtures\FixtureManager\FixtureManager; // Declare an array with the path to your fixtures $fixtures = ['./fixtures/country.xml', './fixtures/employee.yml']; // Create a new Fixture Manager passing such array $fixtureManager = FixtureManager::create($fixtures); // Persist the fixtures into your test database as follow // Create a Default PDO Persister (currently MySQL is default) // Here you pass host, database name, username and password $fixtureManager->setDefaultPDOPersister('127.0.0.1', 'test_database', 'root', '123abc'); // Finally, insert fixtures into database, each table will be cleaned before insertion $fixtureManager->persist(); // You may clean your database if needed at any point doing $fixtureManager->cleanStorage();
就这样!
此外,FixtureManager具有流畅的接口,这意味着您可以将方法调用链在一起...
$fixtureManager ->setDefaultPDOPersister('127.0.0.1', 'test_database', 'root', '123abc') ->cleanStorage() ->persist();
在固定数据文件的值中,您可以放置占位符而不是实际值,这允许您动态地更改值,例如,放置当前日期。
为此,在创建固定数据管理器时,传递一个包含“fx:placeholder:
use TheIconic\Fixtures\FixtureManager\FixtureManager; // Declare an array with the path to your fixtures $fixtures = ['./fixtures/employee.yml']; // Create a new Fixture Manager passing such array $fixtureManager = FixtureManager::create( $fixtures, [ 'employee' => [ 'fx:placeholder:age' => 33 ] ] );
员工.xml文件将是(注意"age"字段的值)
<?xml version="1.0"?> <mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <database name="my_database"> <table_data name="employee"> <row> <field name="id">1</field> <field name="name">John Smith</field> <field name="age"/>fx:placeholder:age</field> </row> </table_data> </database> </mysqldump>
您可以传递任何想要的值,只需在将其传递给create静态方法之前计算它即可。
安装
使用Composer安装此包。
{ "require": { "theiconic/fixtures": "~1.5" } }
固定数据文件示例
XML,当处理具有NULL值和日期的固定数据时使用。
<?xml version="1.0"?> <mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <database name="my_database"> <table_data name="country"> <row> <field name="id">1</field> <field name="name">Australia</field> <field name="size" xsi:nil="true" /> </row> </table_data> </database> </mysqldump>
例如,您可以从命令行生成MySQL XML导出,如下所示
mysqldump -u <USERNAME> -p<PASSWORD> --xml -t my_database my_table > ~/fixtures/my_table.xml
Yaml,对于没有NULL值或日期的简单数据使用。
country: - id_country: 2 iso2_code: AD iso3_code: AND name: Andorra
JSON
{ "country": [ { "id_country": 2, "iso2_code": "AD", "iso3_code": "AND", "name": "Andorra" } ] }
贡献者
- Jorge A. Borges - 主要开发者 (http://jorgeborges.me)
- Aaron Weatherall - (http://www.aaronweatherall.com)
- Ollie Brennan - (http://www.epicarena.com)
许可证
THE ICONIC 数据库固定数据集在MIT许可证下发布。