bettergist/hautelook-alice-bundle

现在已丢失的Symfony扩展包的存档,用于使用Alice和Faker管理固定数据。由Bettergist集体存档。

资助包维护!
theofidry

安装数: 14,478

依赖者: 0

建议者: 0

安全性: 0

星标: 2

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

2.7.2 2021-09-12 02:01 UTC

This package is not auto-updated.

Last update: 2024-09-14 11:52:02 UTC


README

一个用于使用 Symfonynelmio/alicefzaninotto/Faker 管理固定数据的bundle。

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

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

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

何时使用此bundle?

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

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

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

HautelookAliceBundle 2.x 变化很大,尽管变化不大。在1.x中,由于 nelmio/alice 2.x 的限制,bundle 中引入了很多复杂性,并且最多是解决方案(如无法处理循环引用)。这些复杂性中很大一部分已经推回到 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类所在位置相应地注册bundle。

<?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;
}

根据您的需求配置bundle,例如

# 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.root_dir%'
        - '%kernel.project_dir%'

如果您使用的是非Flex架构,您可能需要使用 Resources/fixtures 而不是 fixtures

基本用法

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

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

  • fixtures 如果您使用flex
  • app/Resources/fixtures 如果您有一个无bundle的Symfony应用程序
  • 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。这些特质允许在每次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 特质。它将在每次测试之前清除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时不起作用,因为数据库更改是由另一个进程在事务外执行的。

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

  • 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;

    // ...
}

资源

鸣谢

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

其他贡献者.

许可协议

license