l0wskilled / alice-bundle-odm
Symfony 扩展,用于使用 Alice 和 Faker 管理数据设置。
Requires
- php: ^7.1
- doctrine/data-fixtures: ^1.2
- doctrine/doctrine-bundle: ^1.8
- doctrine/mongodb-odm: ^2.0.0@beta
- doctrine/mongodb-odm-bundle: ^4.0.0@beta
- doctrine/orm: ^2.5.11
- psr/log: ^1.0
- symfony/finder: ^3.4 || ^4.0
- symfony/framework-bundle: ^3.4 || ^4.0
- theofidry/alice-data-fixtures: ^1.0
Requires (Dev)
- doctrine/persistence: ^1.0
- phpspec/prophecy: ^1.7
- phpunit/phpunit: ^7.0
- symfony/phpunit-bridge: ^3.4 || ^4.0
- dev-master / 2.x-dev
- v2.5.1.2
- 2.5.1.1
- v2.5.1
- 2.5.0
- v2.4.1
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v2.0.0-rc.0
- v2.0.0-beta.6
- v2.0.0-beta.5
- v2.0.0-beta.4
- v2.0.0-beta.3
- v2.0.0-beta.2
- v2.0.0-beta.1
- v2.0.0-beta.0
- 1.x-dev
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.0
- v1.0.0-beta.5
- v1.0.0-beta.4
- v1.0.0-beta.3
- v1.0.0-beta.2
- v1.0.0-beta
- 0.2
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1
This package is auto-updated.
Last update: 2024-09-21 02:41:54 UTC
README
为了与 MongoDB 兼容而进行的丑陋更改。
一个用于使用 Symfony 和 nelmio/alice 以及 fzaninotto/Faker 管理数据设置的 Symfony 扩展。
数据库支持由 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() { $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-less 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启发的、针对数据库测试的出色辅助工具: RefreshMongoDbTrait
、ReloadMongoDbTrait
和 RecreateMongoDbTrait
。这些特性允许在每次PHPUnit测试之前轻松地将数据库重置到已知状态:它清除数据库然后加载固定文件。
为了提高性能,RefreshMongoDbTrait
只在数据库中填充一次,然后在每个测试中包裹一个事务,该事务在执行结束后(无论成功或失败)都会回滚。
<?php namespace App\Tests; use Hautelook\AliceBundle\PhpUnit\RefreshMongoDbTrait; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class NewsTest extends WebTestCase { use RefreshMongoDbTrait; 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\ReloadMongoDbTrait; use Symfony\Component\Panther\PantherTestCase; class NewsTest extends PantherTestCase // Be sure to extends KernelTestCase, WebTestCase or PantherTestCase { use ReloadMongoDbTrait; 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::$shard
:要使用的Doctrine分片名称self::$connection
:要使用的Doctrine连接名称
在 setUpBeforeClass
方法中使用它们。
<?php namespace App\Tests; use Hautelook\AliceBundle\PhpUnit\RefreshMongoDbTrait; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class NewsTest extends WebTestCase { use RefreshMongoDbTrait; public static function setUpBeforeClass() { self::$append = true; } // ... }
最后,如果您正在使用内存SQLite进行测试,请使用 RecreateMongoDbTrait
来创建“即时”的数据库架构
<?php namespace App\Tests; use Hautelook\AliceBundle\PhpUnit\RecreateMongoDbTrait; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class NewsTest extends WebTestCase { use RecreateMongoDbTrait; // ... }
资源
- Behat扩展: AliceBundleExtension
- 用于直接从Doctrine实体生成与AliceBundle兼容的固定文件的bundle: AliceGeneratorBundle
- 升级指南
- 变更日志
致谢
此bundle最初由 Baldur RENSCH 和 HauteLook 开发。现在由 Théo FIDRY 维护。