mgleska/repositorymock
最新稳定版本:1.1.0
Composer 安装命令:
composer require mgleska/repositorymock
包简介
A package to facilitate the testing of classes/methods which uses ORM Repository and ORM Entity objects.
README 文档
README
A package to facilitate the testing of classes/methods which uses ORM Repository and ORM Entity objects.
Installation
composer require --dev mgleska/repositorymock:"^1"
Features
-
Provides ready-made mocks for functions:
find()findOneBy()findBy()- by scalar value
- by list of values
- by object
save()remove()
-
Allow to create mocks for Entity objects from array of values.
- simple Entity (without relations)
- multi-level Entity
Doctrine\ORM\Mapping\OneToOneDoctrine\ORM\Mapping\ManyToOneDoctrine\ORM\Mapping\OneToMany
-
Full compatibility with PHPUnit mock. An object of type RepositoryMock can be treated as a regular mock created by PHPUnit.
For example, you can use$mockedRepository->method('myCustomMethod')->willReturn(...). -
Provides the
createFakeObject(string $className, array $objData)method which can be used to create Entity object for use inwillReturn().
Particularly useful for multi-level Entities and those where theidfield has no setter (because it is autoincrement). -
Provides the ability to create test scenarios for update/edit and create/delete actions.
The mock for thesave()function stores the data sent towards the database. Which gives the possibility to observe the "state of the database" after the tested operation$sut->action()is completed.save()does:- update if entity exists
- insert with autoincrement for
id
- than
getStoreContent()method allow to make assertions on entities stored in database.
Usage
- Mock for simple
find().
// src/Service/SutService.php class SutService { public function __construct( private readonly Repository $repository, ) { } public function getFirst(): ?Entity { return $this->repository->find(1); } }
// tests/Service/SutServiceTest.php use RepositoryMock\RepositoryMockObject; use RepositoryMock\RepositoryMockTrait; ... class SutServiceTest extends TestCase { use RepositoryMockTrait; private Sutservice $sut; private Repository|RepositoryMockObject $repository; protected function setUp(): void { $this->repository = $this->createRepositoryMock(Repository::class); $this->sut = new SutService( $this->repository, ); } #[Test] public function getFirstFound(): void { $this->repository->loadStore([ [ // values of selected properties of Entity 'id' => 1, 'name' => 'test', ], ]); $result = $this->sut->getFirst(); $this->assertInstanceOf(Entity::class, $result); } #[Test] public function getFirstNotFound(): void { $this->repository->loadStore([ [ 'id' => 22, 'name' => 'test 22', ], ]); $result = $this->sut->getFirst(); $this->assertNull($result); } }
- More usage examples in file
tests/SutServiceTest.php.
统计信息
- 总下载量: 32
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-28