paul / alice-fixtures-bundle

Symfony2 Bundle 用于使用 Alice 库加载 fixture 数据。

安装: 14

依赖者: 0

建议者: 0

安全: 0

星级: 0

观察者: 3

分支: 25

类型:symfony-bundle

0.5.3 2016-05-22 13:33 UTC

This package is auto-updated.

Last update: 2024-08-29 03:29:51 UTC


README

这是一个用于在 Symfony2 中灵活使用 nelmio/alicefzaninotto/Faker 的 Symfony2 Bundle。

Build Status Scrutinizer Quality Score Code Coverage SensioLabsInsight

状态

此 Bundle 应被视为“工作正在进行中”。每个版本 < 1.0 都可能和将会改变。这也意味着,如果您有一个关于变更的基本想法,请随时贡献。任何形式的贡献都是受欢迎的!

简介

此 Bundle 的目标是提供一种新的数据 fixture 工作方式,与常见的 Doctrine DataFixtures 相分离。加载 fixture 应该是解耦的,并且易于集成到所需的地方。此 Bundle 提供了从 yaml 和 php 文件中加载 fixture,以及删除和重新创建 ORM Schema。除了 Doctrine/ORM,还支持 Doctrine/MongoDB-ODM,用于重新创建 schema 和持久化 fixture。

如果您正在寻找一个提供将 Alice 与 Doctrine DataFixtures 集成的 Bundle,请查看 hautelook/AliceBundle

此 Bundle 还能够重新创建 ORM schema。这意味着 所有 由 Doctrine 管理的表都将被删除和重新创建。将会出现数据丢失,已警告您

安装

只需使用 composer 通过其名称要求此 Bundle。

$ php composer.phar require h4cc/alice-fixtures-bundle

跟踪 'dev-master' 分支以获取最新的开发版本。但建议使用更稳定的版本标签,如果可用。

之后,将 Bundle 添加到您的 Kernel 中,最可能在 "dev" 或 "test" 环境中。

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
    );

    if (in_array($this->getEnvironment(), array('dev', 'test'))) {
        // ...
        $bundles[] = new h4cc\AliceFixturesBundle\h4ccAliceFixturesBundle();
    }
}

请注意,此 Bundle 能够删除整个数据库,因此在生产中使用时请小心!

配置

您可以通过全局配置种子(用于随机值)、Faker 的区域设置以及全局标志 do_flush(如果 ORM 持久化实体时是否省略 ORM 持久化)。

可以定义自定义 'schema_manager' 服务 ID,Doctrine 的实体或 ObjectManagers 将由当前对象类和映射确定。

如果您想使用 doctrine/orm 或 doctrine/mongodb-odm 的默认服务,请相应地设置 "doctrine" 的值,可以是 "orm" 或 "mongodb-odm"。此设置将仅根据 ManagerRegistry 和 'schema_manager' 调整服务 ID,如果您尚未自行设置它们。

使用单个默认管理器的简单配置

# app/config/config_dev.yml

h4cc_alice_fixtures:
    locale: en_US                               # default
    seed: 1                                     # default
    do_flush: true                              # default

    schema_tool: null                           # default
    doctrine: orm                               # default

或者,您可以使用多个管理器

# app/config/config_dev.yml

h4cc_alice_fixtures:
    default_manager: orm
    managers:
        orm:
            locale: en_US                               # default
            seed: 1                                     # default
            do_flush: true                              # default

            schema_tool: null                           # default
            doctrine: orm                               # default
        mongodb:
            locale: en_US
            seed: 1
            do_flush: true

            schema_tool: null
            doctrine: mongodb-odm

查看此命令以获取始终更新的配置参考

$ php app/console config:dump-reference h4cc_alice_fixtures

使用方法

Fixture

Fixture 定义在 YAML 或 PHP 文件中,如 nelmio/alice 所述。两个 Alice 默认加载器 'Yaml' 和 'Base' 已可用。如果您想集成自己的加载器,请修补 Loader Factory 服务以符合您的需求 ('h4cc_alice_fixtures.loader.factory')。

可以使用 FixtureManagerInterface::loadFiles(array $files, $type='yaml'); 从单个 fixture 文件中加载实体。使用 FixtureManagerInterface::persist(array $entities, $drop=false); 持久化它们。

示例

$files = array(__DIR__.'/fixtures/Users.yml', __DIR__.'/fixtures/Articles.yml');
// Or use $manager = $this->get('h4cc_alice_fixtures.[my_manager_name]_manager'); to use an other manager.
$manager = $this->get('h4cc_alice_fixtures.manager');

// Step 1, load entities.
$objects = $manager->loadFiles($files, 'yaml');

// Manipulate or add own objects here ...

// Step 2, persist them.
$manager->persist($objects, true);

Fixture 集合

加载 fixture 的更高级方法是通过使用 "FixtureSets"。将其视为针对多个 fixture 文件和选项的 fixture 配置对象。

示例

$manager = $this->getContainer()->get('h4cc_alice_fixtures.manager');

// Get a FixtureSet with __default__ options.
$set = $manager->createFixtureSet();
$set->addFile(__DIR__.'/fixtures/Users.yml', 'yaml');
$set->addFile(__DIR__.'/fixtures/Articles.yml', 'yaml');

// Change locale for this set only.
$set->setLocale('de_DE');
// Define a custom random seed for "predictable randomness".
$set->setSeed(42);
// Enable persisting of objects
$set->setDoPersist(true);
// Enable dropping and recreating current ORM schema.
$set->setDoDrop(true);

return $set;

命令

此 Bundle 包含一些用于加载 fixture 的命令。它们也被分为加载普通文件或 FixtureSets。

h4cc_alice_fixtures
  h4cc_alice_fixtures:load:files        Load fixture files using alice and faker.
  h4cc_alice_fixtures:load:sets         Load fixture sets using alice and faker.

默认情况下,所有 fixture 文件或集合将共享其引用。这样,一个 Bundle 可以从另一个 Bundle 中引用 fixture。

使用所有可用选项加载单个文件的示例

$ php app/console h4cc_alice_fixtures:load:files --manager=default --type=yaml --seed=42 --local=de_DE --no-persist --drop src/Acme/DemoBundle/Fixtures/Users.yml src/Acme/DemoBundle/Fixtures/Articles.yml

加载给定FixtureSet的示例命令

$ php app/console h4cc_alice_fixtures:load:sets src/Acme/DemoBundle/Fixtures/UsersAndArticlesSet.php

当您的FixtureSet存储在默认路径'DataFixtures/Alice/',并且以... 'Set.php'结尾时,它们可以自动找到。就像Doctrine Datafixtures中的固定数据一样。要加载默认的固定集,只需执行此命令即可

$ php app/console h4cc_alice_fixtures:load:sets

加载束的顺序由它们在AppKernel中定义的顺序决定。可以通过在您的固定集中使用“order”值来自定义这一点。默认为'1',顺序值越低,越先加载。

预配置的FixtureSet示例

<?php

// Creating a fixture set with own configuration,
$set = new h4cc\AliceFixturesBundle\Fixtures\FixtureSet(array(
    'locale' => 'de_DE',
    'seed' => 42,
    'do_drop' => true,
    'do_persist' => true,
    'order' => 5
));

$set->addFile(__DIR__.'/Users.yml', 'yaml');
$set->addFile(__DIR__.'/Articles.yml', 'yaml');

return $set;

这样的文件必须返回一个实现了FixtureSetInterface接口的对象。

来自不同束的FixtureSet

当使用多个束,如MyCommonBundleMyUserBundle时,有一些事情要做和知道

  • h4cc_alice_fixtures:load:sets加载FixtureSet的顺序将与`AppKernel::registerBundles()`中的顺序相同。
  • 所有FixtureSet都将使用全局引用状态。这样,MyUserBundle就可以使用来自MyCommonBundle的实体。
  • 由于只能引用已加载的实体,因此FixtureSet之间的循环引用是不可能的。

演示应用程序

有一个演示应用程序显示了使用此束的一些方法:https://github.com/h4cc/AliceDemo

如果您喜欢,随时可以添加更多演示。

PHPUnit

如果需要,也可以在PHPUnit测试中加载固定数据。在Symfony2环境中访问所需容器的方法在此处描述:https://symfony.com.cn/doc/current/book/testing.html#accessing-the-container

示例

// Ensuring the same fixtures for each testcase.
public function setUp()
{
    // This may be slow ... have an eye on that.
    $client = static::createClient();
    $manager = $client->getContainer()->get('h4cc_alice_fixtures.manager');
    $manager->load(require(__DIR__.'/fixtures/FixtureSet.php'));
}

Selenium

这种灵活处理数据固定数据的方式为与selenium/behat/mink一起工作提供了简单的方法。例如,您可以在开发路由后创建一个控制器,由selenium调用以确保特定数据集。

为Faker添加自己的提供商

Faker的提供商可以是任何有公共方法的类。这些方法可以在固定文件中用于自定义测试数据或甚至计算。要注册提供商,请创建一个服务并将其标记。

示例

services:
    your.faker.provider:
        class: YourProviderClass
        tags:
            -  { name: h4cc_alice_fixtures.provider }

为Alice添加自己的处理器

Alice处理器可用于在持久化之前和之后操作对象。要注册自己的处理器,请创建一个服务并将其标记。

示例

services:
    your.alice.processor:
        class: YourProcessorClass
        tags:
            -  { name: h4cc_alice_fixtures.processor }

许可

此束受MIT许可。