enabel/alice-bundle

Symfony 扩展,用于管理 Alice 和 Faker 的数据固定

维护者

详细信息

github.com/BTCCTB/AliceBundle

来源

安装: 30

依赖者: 0

建议者: 0

安全: 0

星级: 0

关注者: 1

分支: 260

类型:symfony-bundle

v2.6 2019-12-09 09:59 UTC

README

这是一个用于管理 Symfony 的固定数据的扩展,使用 nelmio/alicefzaninotto/Faker

数据库支持由 FidryAliceDataFixtures 完成。查看此项目以了解支持的数据库/ORM。

警告:这是 HautelookAliceBundle 2.0 的文档。如果您想查看 1.x 的文档,请访问 此处

Package version Build Status SensioLabsInsight Dependency Status Scrutinizer Code Quality Code Coverage Slack

何时使用此扩展?

HautelookAliceBundle 变化很大,最初它是一个简单的 nelmio/alice 扩展,然后开始添加一些额外的功能来丰富它。

HautelookAliceBundle 1.x 是第一个达到一定成熟度的里程碑

  • 轻松从命令中加载一组固定数据
  • 能够为多个环境定义不同的固定数据集
  • 使用自定义 Faker 提供程序自定义数据生成
  • 使用处理器自定义加载行为

HautelookAliceBundle 2.x 发生了很大变化,尽管变化不大。在 1.x 中,由于 nelmio/alice 2.x 的限制,将大量复杂性引入了扩展,并且最好的解决方案是权宜之计(例如,处理循环引用的缺失)。这些复杂性中的许多都已推回到 nelmio/alice 3.x,它具有更灵活的设计。因此

  • nelmio/alice 3.x 允许您以优雅的方式轻松创建具有随机数据的 PHP 对象
  • FidryAliceDataFixtures 是 nelmio/alice 3.x 的持久层。如果您需要持久化加载的对象,则需要此软件包。它为您提供了灵活性,可以在每次加载之间清除数据,或者将加载包装在事务中,例如,在测试完成后简单地回滚,而不是调用昂贵的清除。
  • hautelook/alice-bundle 2.x 提供了高级功能,包括固定数据发现(找到适当的文件并加载它们)和数据库测试的助手。如果您只需要加载特定文件集进行测试,则 FidryAliceDataFixtures 就足够了。

文档

  1. 安装
  2. 基本用法
  3. 数据库测试
  4. 高级用法
    1. 启用数据库
    2. 环境特定固定数据
    3. 固定数据参数
      1. Alice 参数
      2. 应用程序参数
    4. 使用服务工厂
    5. 按特定顺序加载固定数据
      1. 按特定顺序加载固定数据
      2. 按特定顺序持久化类
  5. 自定义 Faker 提供程序
  6. 自定义 Alice 处理器
  7. 资源

安装

使用 Symfony Flex(推荐)

# If you do not have Doctrine installed yet:
composer require doctrine-orm

composer require --dev hautelook/alice-bundle

您现在可以使用 AliceBundle,可以跳到下一节!

如果没有 Flex,您将必须安装 doctrine/orm 并在 app/AppKernel.php 或您的 Kernel 类所在的任何地方相应地注册扩展

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        // ...
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
    ];

    if (in_array($this->getEnvironment(), ['dev', 'test'])) {
        //...
        $bundles[] = new Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle();
        $bundles[] = new Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle();
        $bundles[] = new Hautelook\AliceBundle\HautelookAliceBundle();
    }

    return $bundles;
}

根据您的需求配置扩展,例如

# config/packages/dev/hautelook_alice.yaml

hautelook_alice:
    fixtures_path: 'fixtures' # Path to which to look for fixtures relative to the project directory or the bundle path. May be a string or an array of strings.
    root_dirs:
        - '%kernel.project_dir%'
        - '%kernel.project_dir%'

如果您使用的是非灵活架构,您可能希望使用 Resources/fixtures 而不是 fixtures

基本用法

假设您正在使用 Doctrine,请确保您已安装了 doctrine/doctrine-bundledoctrine/data-fixtures 包。

然后在以下位置之一创建一个 fixture 文件:

  • 如果您使用的是 flex,则使用 fixtures
  • 如果您有一个无 bundle 的非灵活的 Symfony 应用程序,则使用 app/Resources/fixtures
  • src/AppBundle/Resources/fixtures 或您希望放置 fixture 的任何 bundle
# fixtures/dummy.yaml

App\Entity\Dummy:
    dummy_{1..10}:
        name: <name()>
        related_dummy: '@related_dummy*'
# fixtures/related_dummy.yaml

App\Entity\RelatedDummy:
    related_dummy_{1..10}:
        name: <name()>

然后只需使用 doctrine 命令 php bin/console hautelook:fixtures:load 加载您的 fixture。

如果您只想加载某个 bundle 的 fixture,请执行 php bin/console hautelook:fixtures:load -b MyFirstBundle -b MySecondBundle

查看更多.
下一章: 高级使用

数据库测试

该 bundle 提供了一些优秀的助手,灵感来自 Laravel,专门用于数据库测试:RefreshDatabaseTraitReloadDatabaseTraitRecreateDatabaseTrait。这些 trait 允许在每次 PHPUnit 测试之前轻松地将数据库重置到已知状态:它清除数据库然后加载 fixture。

它们在编写 功能测试 和使用 Panther 时非常有用。

为了提高性能,RefreshDatabaseTrait 只填充一次数据库,然后将每个测试用例包装在一个事务中,在执行完成后(无论成功或失败)都会回滚

<?php

namespace App\Tests;

use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class NewsTest extends WebTestCase
{
    use RefreshDatabaseTrait;

    public function postCommentTest()
    {
        $client = static::createClient(); // The transaction starts just after the boot of the Symfony kernel
        $crawler = $client->request('GET', '/my-news');
        $form = $crawler->filter('#post-comment')->form(['new-comment' => 'Symfony is so cool!']);
        $client->submit($form);
        // At the end of this test, the transaction will be rolled back (even if the test fails)
    }
}

有时,将测试用例包装在事务中是不可能的。例如,当使用 Panther 时,数据库更改是由另一个 PHP 进程执行的,因此它不起作用。在这种情况下,使用 ReloadDatabase trait。它将在每次测试之前清除 DB 并加载 fixture

<?php

namespace App\Tests;

use Hautelook\AliceBundle\PhpUnit\ReloadDatabaseTrait;
use Symfony\Component\Panther\PantherTestCase;

class NewsTest extends PantherTestCase // Be sure to extends KernelTestCase, WebTestCase or PantherTestCase
{
    use ReloadDatabaseTrait;

    public function postCommentTest()
    {
        $client = static::createPantherClient();// The database will be reset after every boot of the Symfony kernel

        $crawler = $client->request('GET', '/my-news');
        $form = $crawler->filter('#post-comment')->form(['new-comment' => 'Symfony is so cool!']);
        $client->submit($form);
    }
}

这种策略在使用 Panther 时不起作用,因为数据库的更改是由另一个进程在外部事务中执行的。

这两个 trait 都提供了多个配置选项作为受保护的静态属性

  • self::$manager:要使用的 Doctrine 管理器的名称
  • self::$bundles:要查找 fixture 的 bundle 列表
  • self::$append:附加 fixture 而不是清除
  • self::$purgeWithTruncate:使用 TRUNCATE 清除
  • self::$shard:要使用的 Doctrine shard 的名称
  • self::$connection:要使用的 Doctrine 连接的名称

setUpBeforeClass 方法中使用它们。

<?php

namespace App\Tests;

use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class NewsTest extends WebTestCase
{
    use RefreshDatabaseTrait;

    public static function setUpBeforeClass()
    {
        self::$append = true;
    }

    // ...
}

最后,如果您正在为测试使用内存中的 SQLite,请使用 RecreateDatabaseTrait 来创建数据库模式

<?php

namespace App\Tests;

use Hautelook\AliceBundle\PhpUnit\RecreateDatabaseTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class NewsTest extends WebTestCase
{
    use RecreateDatabaseTrait;

    // ...
}

资源

致谢

此 bundle 最初由 Baldur RENSCHHauteLook 开发。现在由 Théo FIDRY 维护。

其他贡献者.

许可协议

license