yosimitso / mockdoctrinemanager
模拟并记录Doctrine的管理器
V1.0
2019-11-29 12:47 UTC
Requires (Dev)
- doctrine/common: >=2.0
- doctrine/orm: >=2.0
- phpunit/phpunit: >=6.0
This package is auto-updated.
Last update: 2024-09-29 05:35:41 UTC
README
Yosimitso/DoctrineManagerMock
用法
Mock Doctrine\ORM\EntityManager 实现 EntityManagerInterface,此包记录持久化、删除和刷新的实体,以及基本操作如事务。它允许您检查实体是否正确注册以及您的流程是否得到遵守
请注意,此包是新的,并专注于当前大多数操作,包括
- 持久化
- 刷新
- 删除
- 开始事务
- 提交
- 回滚
- 事务性
请随时提交PR以扩展功能
安装
composer require "yosimitso/doctrinemanagermock:^1.0" --dev
然后只需实例化它
$entityManager = new Yosimitso\MockDoctrineManager\EntityManagerMock;
示例
简单示例,检查您的实体是否真正持久化以及数据是否良好
来自此包实际测试的示例
要测试的类
<?php namespace Yosimitso\MockDoctrineManager\Tests; use Doctrine\ORM\EntityManagerInterface; use Yosimitso\MockDoctrineManager\Tests\Entity\EntityToTest; class ClassToTestService { /** * @var EntityManagerInterface */ private $entityManager; public function __construct(EntityManagerInterface $entityManager) // YOU CAN TYPE ENTITYMANAGERINTERFACE { $this->entityManager = $entityManager; } public function methodToTest($nb) { $newEntity = new EntityToTest; $newEntity->setNb($nb); try { $this->entityManager->beginTransaction(); $this->entityManager->persist($newEntity); $this->entityManager->flush(); $this->entityManager->commit(); } catch (\Exception $e) { $this->entityManager->rollback(); } } }
实体示例
<?php namespace Yosimitso\MockDoctrineManager\Tests\Entity; class EntityToTest { private $nb; public function setNb($nb) // CLASSIC SETTER { $this->nb = $nb; } public function getNb() // CLASSIC GETTER { return $this->nb; } }
测试
<?php namespace Yosimitso\MockDoctrineManager\Tests\Test; use Yosimitso\MockDoctrineManager\EntityManagerMock; use Yosimitso\MockDoctrineManager\Tests\Entity\EntityToTest; use Yosimitso\MockDoctrineManager\Tests\ClassToTestService; use PHPUnit\Framework\TestCase; // ASSUMING YOU'RE USING PHPUNIT, BUT IT WORKS WITH ANY TESTING FRAMEWORK class ClassToTestServiceTest extends TestCase { public function testMethodToTest() { $entityManagerMock = new EntityManagerMock(); // THIS BUNDLE $testedClass = new ClassToTestService($entityManagerMock); // THE CLASS TO TEST $testedClass->methodToTest(10); // THE METHOD TO TEST // ASSERT WE BEGAN THE TRANSACTION $this->assertTrue($entityManagerMock->hasBegunTransaction()); // ASSERT WE PERSISTED THE GOOD ENTITY WITH THE GOOD DATA $this->assertEquals(10, $entityManagerMock->getPersistedEntity(EntityToTest::class)->getNb()); // ASSERT WE FLUSHED THE GOOD ENTITY WITH THE GOOD DATA $this->assertEquals(10, $entityManagerMock->getFlushedEntity(EntityToTest::class)->getNb()); // ASSERT WE COMITTED $this->assertTrue($entityManagerMock->hasCommitted()); // ASSERT WE DIDN'T ROLLBACK $this->assertFalse($entityManagerMock->hasRolledback()); } }
如果我持久化了两个或更多实例呢?
使用 getPersistedEntity 的 'position' 参数,例如,如果我想获取第二个持久化的 "YourClass":getPersistedEntity(YourClass::class, 2);
我的代码使用 'findBy' 方法
此包无法猜测您对 'findBy' 的期望,因此只需像其他任何类一样创建此包的模拟,以模拟 'findBy'。
使用 PHPUnit 的示例
$yourEntity = new Article; // EXAMPLE OF AN ENTITY $yourEntity->setName('hello'); $entityManager = $this->getMockBuilder(EntityManagerMock::class) ->setMethods(['findBy']) ->getMock(); $entityManager->methods('findBy')->willReturn($yourEntity);
API
/* returns this entity persisted in n position (among its namespace) */ getPersistedEntity(object $className, int $position = 1): mixed /* returns this entity removed in n position (among its namespace) */ getRemovedEntity(object $className, int $position = 1): mixed /* returns this entity flushed in n position (among its namespace) */ getFlushedEntity(object $className, int $position = 1): mixed /** returns the list of persisted entites */ getPersistedEntities(): array /** returns the list of flushed entites */ getFlushedEntities(): array hasBegunTransaction(): boolean hasComitted(): boolean hasRolledback(): boolean