sebastianknott / test-utils
Some classes i like to use for testing
Requires
- php: ^8.3
- brianium/paratest: ^v7.4
- fakerphp/faker: ^v1.23.1
- hamcrest/hamcrest-php: ^2.0
- infection/infection: >0.28.0
- mockery/mockery: ^1.6
- phake/phake: ^v4.5
- phpspec/prophecy: ^v1.19
- phpunit/phpunit: ^9.5|^10.0|^11.1.3
- sebastianknott/hamcrest-object-accessor: ^2.0
Requires (Dev)
- phpunit/phpunit: ^11.1.3
- sebastianknott/coding-standard: ^2.1.1
README
Some classes i like to use for testing purposes
System Under Test (SUT) Factory
I don't like to do tedious work, so I created a factory that creates the System Under Test (SUT) for me. The factory creates a new instance of the SUT and mocks its constructor dependencies as good as it can.
It returns a Bundle
object that contains the SUT and the mocks.
class SomeClass { public function __construct(Dependency $myDependency) { // ... } } class Dependency{ public function someMethod() { // ... } } $factory = new BundleFactory(); $bundel = $factory->build(SomeClass::class); // Get the SUT $sut = $bundle->getSut(); // Access the mocked dependencies $sut['myDependency']->expects()->someMethod()->once();
Type of Mocks
It supports two types of mocking libraries (Mockery,
Phake) and can be configured to use one of them by passing the respective
MockTypeEnum
to BundleFactory::build
.
[...] $bundel = $factory->build(SomeClass::class, type: MockTypeEnum::MOCKERY); [...]
Prebuild Parameters
If the SUT has dependencies that are not mockable, you can pass them as a prebuild parameter to the factory. The prebuild parameter is an associative array where the key is the name of the parameter in the suts constructor and the value is the instance.
[...] $prebuildParameters = [ 'myDependency' => new Dependency() ]; $bundel = $factory->build( SomeClass::class, prebuildParameters: $prebuildParameters ); [...]
TestToolsCase
I found myself writing the same boilerplate code over and over again. So I condensed it into a base class that I can extend from.
It does the following:
- Make a sut factory available
self::$bundleFactory
- Make faker available
self::$faker
- Require hamcrest and Mockery
So instead of writing this:
class SomeTest extends \PHPUnit\Framework\TestCase { private $bundleFactory; private $faker; private $mockedDependency; public function setUp() { $this->bundleFactory = new BundleFactory(); $this->faker = \Faker\Factory::create(); $this->faker->seed(9876543255); $this->mockedDependency = \Mockery::mock(Dependency::class); $this->subject = new SomeClass($this->mockedDependency); } public function testSomething() { $this->mockedDependency->expects() ->someMethod(Matchers::stringValue()) ->andReturn($this->faker->word()); $sut->run(); } public function tearDown() { \Mockery::close(); } }
I write that:
class SomeTest extends TestToolsCase { public function testSomething() { $bundle = self::$bundleFactory->build(SomeClass::class); $bundle['myDependency']->expects() ->someMethod(stringValue())->andReturn(self::$faker->word()); $sut = $bundle->getSut(); $sut->run(); } }