A helper trait for PHPUnit 10+ for easier creation of services with dependencies in unit testing
Simply run
composer require --dev pkly/phpunit-service-create-trait
Currently compatible only with PHPUnit 10 (11?)
In any of your PHPUnit test cases simply
class MyTestCase extends \PHPUnit\Framework\TestCase {
use \Pkly\ServiceMockHelperTrait;
private AnyClass $service;
public function setUp(): void {
$this->service = $this->createRealMockedServiceInstance(AnyClass::class);
}
public function testSomething(): void
{
$mock = $this->createMock(MyEntity::class);
$this->getMockedService(EntityManagerInterface::class)
->expects($this->once())
->method('delete')
->with($mock);
$this->service->deleteSomething($mock);
}
}
Any dependencies in the constructor as well as methods marked with Symfony's #[Required]
attribute will be automatically plugged in with mocks.
This allows you to write complex tests without wasting time updating your construct calls each time you modify something.
Simply assign the proper parameter name in either $constructor
or $required
in the appropriate methods.
That will use your object instead of creating one for you, keep in mind you cannot retrieve it via $this->getMockedService()
.
Sure, works the same, just use createRealPartialMockedServiceInstance
instead of createRealMockedServiceInstance
, in that case you must
also specify the methods to override in your mock. Returned instance is T&MockObject
.
I'll add them shortly, for now this code is being used thoroughly in a few of the projects I work at and I grew tired of updating it across multiple repositories. It's also very simple, so I doubt anyone is going to complain.
Sure, hit me up with an issue if you wish.