mgleska / repositorymock
A package to facilitate the testing of classes/methods which uses ORM Repository and ORM Entity objects.
Installs: 32
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/mgleska/repositorymock
Requires
- php: >= 8.1
- doctrine/collections: ^2.2
- doctrine/orm: ^3
- phpunit/phpunit: ^11.1
Requires (Dev)
- phpstan/phpstan: ^1.11
- phpstan/phpstan-doctrine: ^1.4
- squizlabs/php_codesniffer: ^3.10
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.