kangourouge / alice-bundle
[这是一个来自alice-bundle的紧急仓库,将不会得到维护] 使用Alice和Faker管理 fixtures 的Symfony 扩展包。
Requires
- php: ^7.3 || ^8.0
- doctrine/data-fixtures: ^1.2
- doctrine/doctrine-bundle: ^1.8 || ^2.0
- doctrine/orm: ^2.5.11
- doctrine/persistence: ^1.3.4 || ^2.0
- psr/log: ^1.0
- symfony/finder: ^3.4 || ^4.0 || ^5.0
- symfony/framework-bundle: ^3.4.24 || ^4.0 || ^5.0
- theofidry/alice-data-fixtures: ^1.4
Requires (Dev)
- phpspec/prophecy: ^1.7
- phpunit/phpunit: ^8.5
- symfony/phpunit-bridge: ^5.1
This package is not auto-updated.
Last update: 2022-12-03 16:34:34 UTC
README
这是一个来自alice-bundle的紧急仓库,将不会得到维护。
AliceBundle
A Symfony 扩展包,用于管理 nelmio/alice 和 fzaninotto/Faker 的 fixtures。
数据库支持在 FidryAliceDataFixtures 中完成。查看此项目以了解支持哪些数据库/ORM。
警告:这是HautelookAliceBundle 2.0的文档。如果您想查看1.x版本的文档,请点击 这里。
何时使用此扩展包?
HautelookAliceBundle发生了很大变化,最初它是一个简单的 nelmio/alice 扩展包,然后开始提供一些额外的功能来丰富它。
HautelookAliceBundle 1.x 是其使用达到一定成熟度的第一个里程碑
- 可以通过命令轻松加载一组 fixtures
- 能够为多个环境定义不同的 fixtures 集合
- 使用自定义 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 提供高级功能,包括 fixtures 发现(找到合适的文件并加载它们)和数据库测试的辅助工具。如果您只需要为测试加载特定的文件集,则 FidryAliceDataFixtures 足够。
文档
安装
使用 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-bundle
和 doctrine/data-fixtures
包。
然后在以下位置之一创建一个 fixture 文件:
- 如果使用 flex,则为
fixtures
- 如果您有一个非 flex 的无包 Symfony 应用,则为
app/Resources/fixtures
src/AppBundle/Resources/fixtures
或您想放置 fixture 的任何包下
# 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。
如果您只想加载特定包的 fixture,则执行 php bin/console hautelook:fixtures:load -b MyFirstBundle -b MySecondBundle
。
数据库测试
该包提供了一些优秀的助手,灵感来源于 Laravel 的 数据库重置,专为数据库测试而设: RefreshDatabaseTrait
、ReloadDatabaseTrait
和 RecreateDatabaseTrait
。这些 trait 允许在每次 PHPUnit 测试之前轻松地将数据库重置到已知状态:它清除数据库然后加载 fixture。
为了提高性能,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。它将在每次测试之前清除数据库并加载 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 的包列表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; // ... }
资源
- Behat 扩展:AliceBundleExtension
- 用于直接从Doctrine实体生成与AliceBundle兼容的固定值的包:AliceGeneratorBundle
- 升级指南
- 变更日志
致谢
此包最初由Baldur RENSCH和HauteLook开发。现在由Théo FIDRY维护。