h4cc/alice-fixtures-bundle

Symfony2 扩展包,用于使用 Alice 库加载 fixture 数据。

安装次数: 307,421

依赖者: 8

建议者: 0

安全: 0

星标: 75

关注者: 11

分支: 25

开放性问题: 13

类型:symfony-bundle

0.5.1 2015-04-13 19:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:21:17 UTC


README

一个用于在 Symfony2 中灵活使用 nelmio/alicefzaninotto/Faker 的 Symfony2 扩展包。

Build Status Scrutinizer Quality Score Code Coverage SensioLabsInsight

状态

此扩展包应被视为“工作进行中”。所有版本 < 1.0 都可能且将会发生变化。这也意味着,如果您有一个基本的想法进行更改,请随时贡献。任何形式的贡献都始终受欢迎!

简介

此扩展包的目的是提供一个新的方式来使用数据 fixture,与常见的 Doctrine DataFixtures 分离。fixture 的加载应解耦,并易于集成到需要的地方。此扩展包提供从 yaml 和 php 文件加载 fixture 的功能,还可以删除和重新创建 ORM 模式。除了 Doctrine/ORM 之外,还支持 Doctrine/MongoDB-ODM 重新创建模式和持久化 fixture。

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

此扩展包还可以重新创建 ORM 模式。这意味着 所有 由 Doctrine 管理的表都将被删除和重新创建。将出现数据丢失,已警告您

安装

使用 composer 通过名称要求此扩展包

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

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

之后,将扩展包添加到您的 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();
    }
}

请记住,此扩展包具有删除整个数据库的能力,因此在生产中使用时要小心!

配置

您可以全局配置种子以生成随机值,为 Faker 配置区域设置,以及一个全局标志 do_flush,如果 ORM 实体刷新应该省略或不应省略。

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

如果您想使用 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 Sets

加载 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;

命令

此扩展包中包含一些用于加载 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.

默认情况下,所有固定文件或集将共享它们的引用。这样,一个包可以引用另一个包中的固定文件。

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

$ 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接口的对象。

来自不同Bundle的FixtureSet

当使用多个包,如MyCommonBundleMyUserBundle时,有一些事情要做和了解

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

演示应用程序

有一个演示应用程序展示了使用此包的一些方法:[https://github.com/h4cc/AliceDemo](https://github.com/h4cc/AliceDemo)

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

PHPUnit

如果需要,固定文件也可以在PHPUnit测试中加载。在Symfony2环境中访问所需容器的描述如下:[https://symfony.ac.cn/doc/current/book/testing.html#accessing-the-container](https://symfony.ac.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添加自己的Provider

Faker的Provider可以是任何具有公共方法的类。这些方法可以在固定文件中使用,用于自己的测试数据或甚至计算。要注册Provider,创建一个服务并对其进行标记。

示例

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

为Alice添加自己的Processor

Alice的Processor可以用在对象之前之后持久化时进行操作。要注册自己的Processor,创建一个服务并对其进行标记。

示例

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

许可证

此包受MIT许可。