Download the PHP package pkly/phpunit-service-create-trait without Composer
On this page you can find all versions of the php package pkly/phpunit-service-create-trait. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pkly/phpunit-service-create-trait
More information about pkly/phpunit-service-create-trait
Files in pkly/phpunit-service-create-trait
Package phpunit-service-create-trait
Short Description A helper trait for PHPUnit 10+ for easier creation of services with dependencies in unit testing
License MIT
Informations about the package phpunit-service-create-trait
PHPUnit Service Create Trait
A helper trait for PHPUnit 10+ for easier creation of services with dependencies in unit testing
Installation
Simply run
Currently compatible only with PHPUnit 10 (11?)
Usage
In any of your PHPUnit test cases simply
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.
Okay, but what if I need to use something custom?
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()
.
Partial objects?
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
.
Tests? More examples?
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.
Feature requests?
Sure, hit me up with an issue if you wish.