durimjusaj / alice-bundle
Symfony 扩展包,用于使用 Alice 和 Faker 管理固定数据。
Requires
- php: >=8.2
- doctrine/data-fixtures: ^1.7
- doctrine/doctrine-bundle: ^2.11
- doctrine/orm: ^2.9
- doctrine/persistence: ^3.3
- psr/log: ^3.0
- symfony/finder: ^7.0
- symfony/framework-bundle: ^7.0
- theofidry/alice-data-fixtures: ^1.7
Requires (Dev)
- monolog/monolog: ^3.5
- phpspec/prophecy: ^1.7
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
- symfony/phpunit-bridge: ^7.0
This package is auto-updated.
Last update: 2024-09-06 18:45:19 UTC
README
一个用于使用 Symfony 和 nelmio/alice 以及 fzaninotto/Faker 管理固定数据的扩展包。
数据库支持在 FidryAliceDataFixtures 中实现。查看此项目以了解支持的数据库/ORM。
警告:这是 HautelookAliceBundle 2.0 的文档。如果您想查看 1.x 的文档,请访问 此处。
何时使用此扩展包?
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 就足够了。
文档
安装
使用 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(): iterable { $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
包。
然后,在以下位置之一创建一个固定文件:
- 如果您正在使用flex,请创建
fixtures
文件。 - 如果您有一个非flex的没有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的启发,专门用于数据库测试:RefreshDatabaseTrait
、ReloadDatabaseTrait
和RecreateDatabaseTrait
。这些特质允许在每次PHPUnit测试之前轻松地将数据库重置到已知状态:先清除数据库,然后加载固定文件。
为了提高性能,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
特质。它会在每个测试之前清除数据库并加载固定文件。
<?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
:查找固定文件的bundle列表self::$append
:追加固定文件而不是清除self::$purgeWithTruncate
:使用TRUNCATE来清除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兼容的固定文件的bundle:AliceGeneratorBundle
- 升级指南
- 变更日志
致谢
该bundle最初由Baldur RENSCH和HauteLook开发。现在由Théo FIDRY维护。