odolbeau / datagen
dev-master
2020-12-09 09:51 UTC
Requires
- php: >7.2
- psr/log: ^1.0
Requires (Dev)
- doctrine/doctrine-bundle: ^2.2
- phpunit/phpunit: ^8.5
- shapin/stripe: dev-master
- symfony/framework-bundle: ^4.4|^5.1
- symfony/http-kernel: ^4.4|^5.1
- symfony/phpunit-bridge: ^4.4|^5.1
- symfony/yaml: ^4.4|^5.1
Suggests
- doctrine/dbal: Needed to load DBAL fixtures
- shapin/stripe: Needed to load Stripe fixtures
This package is auto-updated.
Last update: 2020-12-09 09:52:01 UTC
README
Datagen 是一个用于处理测试数据加载的 PHP 库。
安装
推荐通过 Composer 安装 Datagen。需要 shapin/datagen
包
$ composer require shapin/datagen
使用
主要入口点是 Shapin\Datagen\Datagen
类及其 load
方法。
use Doctrine\DBAL\DriverManager; use Shapin\Stripe\StripeClient; use Symfony\Component\HttpClient\HttpClient; use Shapin\Datagen\Datagen; use Shapin\Datagen\DBAL\Processor as DBALProcessor; use Shapin\Datagen\Loader; use Shapin\Datagen\Stripe\Processor as StripeProcessor; use Shapin\Datagen\ReferenceManager; // Create a Loader for your fixtures $loader = new Loader(); $loader->addFixture(new MyAwesomeFixture(), ['group1', 'group2']); $loader->addFixture(new AnotherAwesomeFixture()); // Groups are faculative $referenceManager = new ReferenceManager(); $connectionParams = [ 'dbname' => 'testDB', 'user' => 'user', 'password' => 'pass', 'host' => 'localhost', 'driver' => 'pdo_mysql', ]; $connection = DriverManager::getConnection($connectionParams); $httpClient = HttpClient::create([ 'base_uri' => 'http://127.0.0.1:12111/v1/', 'auth_bearer' => 'api_key', 'headers' => [ 'Content-Type' => 'application/json', ], ]); $stripeClient = new StripeClient($httpClient); // Create your processors (see next section for more information regarding supported fixtures) $processors = [ new DBALProcessor($connection, $referenceManager), new StripeProcessor($stripeClient, $referenceManager), ]; // Create a Datagen $datagen = new Datagen($loader, $processors); // Load everything! $datagen->load();
Symfony
此库包含一个捆绑包,以便与 Symfony 无缝集成。目前没有任何 symfony 配方,因此您需要手动将捆绑包注册到您的 Kernel 中
public function registerBundles() { $contents = require $this->getProjectDir().'/config/bundles.php'; foreach ($contents as $class => $envs) { if ($envs[$this->environment] ?? $envs['all'] ?? false) { yield new $class(); } } // No symfony recipe (yet) for Datagen if (in_array($this->environment, ['dev', 'test'])) { yield new \Shapin\Datagen\Bridge\Symfony\Bundle\ShapinDatagenBundle(); } }
注册后,您将能够访问新的命令来玩转 datagen。
创建测试数据
DBAL
为了创建一个表并填充它,扩展 Table
基类
<?php use Shapin\Datagen\DBAL\Table; use Doctrine\DBAL\Schema\Schema; use Ramsey\Uuid\Uuid; class Category extends Table { protected static $tableName = 'category'; protected static $order = 15; /** * {@inheritdoc} */ public function addTableToSchema(Schema $schema) { $table = $schema->createTable(self::$tableName); $table->addColumn('id', 'uuid'); $table->addColumn('name', 'string'); $table->addColumn('description', 'text', ['notnull' => false]); $table->setPrimaryKey(['id']); } /** * {@inheritdoc} */ public function getRows(): iterable { yield 'category_1' => [ 'id' => Uuid::uuid4(), 'name' => 'Category 1', 'description' => 'My awesome first category', ]; yield 'another_category' => [ 'id' => Uuid::uuid4(), 'name' => 'Another category', ]; } }
Stripe
为了创建 Stripe 的测试数据,您需要使用 shapintv/stripe 库。
例如,如果您想创建一个产品
<?php use Shapin\Datagen\Stripe\Fixture; class StripeProduct extends Fixture { protected static $order = 30; /** * {@inheritdoc} */ public function getObjectName(): string { return 'product'; } /** * {@inheritdoc} */ public function getObjects(): iterable { yield 'my_product' => [ 'id' => 'my_product', 'name' => 'This is my product!', 'type' => 'product', ]; } }
等等!
组
当将您的测试数据添加到 Loader 时,您可以使用第二个参数指定一些组。当调用 Datagen::load
方法时,您可以指定要包含和/或排除的组。
// Load everything $datagen->load(); // Load only fixtures from a given group $datagen->load(['group1']); // Do not load fixtures from a given group $datagen->load([], ['group2']); // Load all fixtures from group 1, ignoring group2. If a fixture is in both group, it will be ignored. $datagen->load(['group1'], ['group2']);
引用
您可以为所有测试数据命名,以便在其他地方使用。
<?php use Doctrine\DBAL\Schema\Schema; use Shapin\Datagen\DBAL\Table; use Ramsey\Uuid\Uuid; class Category extends Table { protected static $tableName = 'category'; protected static $order = 15; /** * {@inheritdoc} */ public function addTableToSchema(Schema $schema) { } /** * {@inheritdoc} */ public function getRows(): iterable { // This fixture is not named and won't be referencable. yield [ 'id' => Uuid::uuid4(), 'name' => 'Dead category', ]; // The name of this fixture is "category_1". We'll use it later! yield 'category_1' => [ 'id' => Uuid::uuid4(), 'name' => 'Category 1', 'description' => 'My awesome first category', ]; } } class Subcategory extends Table { protected static $tableName = 'subcategory'; protected static $order = 25; /** * {@inheritdoc} */ public function addTableToSchema(Schema $schema) { } /** * {@inheritdoc} */ public function getRows(): iterable { yield 'subcategory_1' => [ 'id' => Uuid::uuid4(), 'category_id' => 'REF:category.category_1.id', // Here is our reference! 'name' => 'Subcategory 1', ]; } }
许可
Datagen 根据 MIT 许可发布。有关详细信息,请参阅打包的 LICENSE 文件。