webfactory / doctrine-orm-test-infrastructure
提供创建 Doctrine 2 实体测试基础设施的实用工具。
Requires
- php: ^8.0
- doctrine/common: ^2.11|^3.0
- doctrine/dbal: ^3.2
- doctrine/deprecations: ^1.1
- doctrine/event-manager: ^1.1|^2.0
- doctrine/orm: ^2.14|^3.0
- doctrine/persistence: ^2.0|^3.0
- psr/log: ^2.0|^3.0
- symfony/cache: ^5.0|^6.0|^7.0
Requires (Dev)
- doctrine/collections: ^1.6.8|^2.2.1
- phpunit/phpunit: ^9.6.19
- symfony/error-handler: ^5.4|^6.0|^7.0
- symfony/phpunit-bridge: >= 7.0
- dev-master
- 2.x-dev
- 2.1.0
- 2.0.0
- 1.x-dev
- 1.17.0
- 1.16.0
- 1.15.0
- 1.14.0
- 1.13.0
- 1.12.0
- 1.11.1
- 1.11.0
- 1.10.3
- 1.10.2
- 1.10.1
- 1.10.0
- 1.9.0
- 1.8.1
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- dev-replace-debug-stack
- dev-check-requirements
- dev-remove-deprecations
- dev-drop-annotation
- dev-deprecation-annotations
- dev-deprecate-query-time
- dev-drop-annot-1
This package is auto-updated.
Last update: 2024-09-05 12:51:39 UTC
README
这个库为 Doctrine ORM 实体的测试提供了一些基础设施,具有以下特点:
- 配置 SQLite 内存数据库,在速度和数据库环境之间取得良好的平衡,既真实又隔离。
- 一种导入数据集到数据库的机制,绕过 Doctrine 的缓存。这使从存储库加载实体时的测试环境更加真实。
我们使用它来测试在 Symfony 应用程序中的 Doctrine 存储库和实体。它是一个轻量级的替代方案,与 Symfony 文档中建议的重量级功能测试(我们不建议您跳过这些 - 我们只是想提供另一种途径)。
在无法进行功能测试的非应用程序包中,这是我们测试存储库和实体的唯一方法。
安装
通过 composer 安装(见 https://getcomposer.org.cn/)
composer require --dev webfactory/doctrine-orm-test-infrastructure
使用方法
<?php use Doctrine\ORM\EntityManagerInterface; use Entity\MyEntity; use Entity\MyEntityRepository; use PHPUnit\Framework\TestCase; use Webfactory\Doctrine\ORMTestInfrastructure\ORMInfrastructure; class MyEntityRepositoryTest extends TestCase { private ORMInfrastructure $infrastructure; private MyEntityRepository $repository; protected function setUp(): void { /* This will create an in-memory SQLite database with the necessary schema for the MyEntity entity class and and everything reachable from it through associations. */ $this->infrastructure = ORMInfrastructure::createWithDependenciesFor(MyEntity::class); $this->repository = $this->infrastructure->getRepository(MyEntity::class); } /** * Example test: Asserts imported fixtures are retrieved with findAll(). */ public function testFindAllRetrievesFixtures(): void { $myEntityFixture = new MyEntity(); $this->infrastructure->import($myEntityFixture); $entitiesLoadedFromDatabase = $this->repository->findAll(); /* import() will use a dedicated entity manager, so imported entities do not end up in the identity map. But this also means loading entities from the database will create _different object instances_. So, this does not hold: */ // self::assertContains($myEntityFixture, $entitiesLoadedFromDatabase); // But you can do things like this (you probably want to extract that in a convenient assertion method): self::assertCount(1, $entitiesLoadedFromDatabase); $entityLoadedFromDatabase = $entitiesLoadedFromDatabase[0]; self::assertSame($myEntityFixture->getId(), $entityLoadedFromDatabase->getId()); } /** * Example test for retrieving Doctrine's entity manager. */ public function testSomeFancyThingWithEntityManager(): void { $entityManager = $this->infrastructure->getEntityManager(); // ... } }
迁移到基于属性的映射配置(版本 1.x)
在库的 1.x 版本中,默认情况下,ORMInfrastructure::createWithDependenciesFor()
和 ORMInfrastructure::createOnlyFor()
方法假设 Doctrine ORM 映射是通过注解提供的。注解基础的配置在 ORM 3.0 中不再支持。
为了允许无缝过渡到基于属性或其他类型的映射,可以在创建 ORMInfrastructure
实例时传递映射驱动程序。
如果您想切换到基于属性的映射,请传递一个 new \Doctrine\ORM\Mapping\Driver\AttributeDriver($paths)
,其中 $paths
是存储实体类的目录路径数组。
对于混合(注解和属性)映射配置,您可以使用 \Doctrine\Persistence\Mapping\Driver\MappingDriverChain
。可以通过提供命名空间前缀在驱动程序链上注册多个映射驱动程序。对于每个命名空间前缀,只能使用一个映射驱动程序。
从版本 2.0.0 开始,基于属性的映射将是默认的。
测试库本身
安装通过 composer 管理的依赖项后,只需在库的根目录中运行以下命令:
vendor/bin/phpunit
这使用的是提供的 phpunit.xml.dist - 如果您需要本地更改,请随时创建自己的 phpunit.xml。
祝您测试愉快!
已知问题
请注意,除了此库中的开放问题之外,您还可能遇到任何 Doctrine 问题。特别是要注意其已知的 sqlite 问题。
致谢、版权和许可证
此包最初由德国波恩的 webfactory GmbH 编写,并从那时起收到了其他人的贡献。
webfactory 是一家专注于 PHP(主要是 Symfony)的软件开发公司。如果您是一名寻求新挑战的开发者,我们很乐意听到您的声音!
版权 2012 – 2024 webfactory GmbH,波恩。代码在MIT 许可证下发布。