madflow/alice-bundle

使用Alice和Faker管理固定数据的Symfony包。

维护者

详细信息

github.com/madflow/AliceBundle

源代码

支持包维护!
theofidry

安装量: 2,562

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 260

类型:symfony-bundle

2.9.0 2021-02-23 08:45 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.root_dir%'
        - '%kernel.project_dir%'

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

基本用法

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

然后在以下位置之一创建一个数据固定文件:

  • 如果您使用flex,则使用fixtures
  • 如果您有一个无bundle的Symfony应用,则使用app/Resources/fixtures
  • src/AppBundle/Resources/fixtures或您想放置数据固定的任何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加载您的数据固定。

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

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

数据库测试

此bundle提供了受Laravel启发的助手,专门用于数据库测试:RefreshDatabaseTraitReloadDatabaseTraitRecreateDatabaseTrait。这些trait允许在每次PHPUnit测试之前轻松地将数据库重置到已知状态:它会清除数据库然后加载数据固定。

它们在编写功能测试以及使用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并加载数据固定。

<?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 manager的名称
  • self::$bundles:查找数据固定的bundle列表
  • self::$append:追加数据固定而不是清除
  • 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