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
This package is auto-updated.
Last update: 2025-04-23 15:06:29 UTC
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() { // ... } } $facade = new BundleFacade(); $bundle = $facade->build(SomeClass::class); // Get the SUT $sut = $bundle->sut; // Access the mocked dependencies by parameter name $bundle['myDependency']->expects()->someMethod()->once();
Type of Mocks
It supports multiple types of mocking libraries (Mockery, Phake, Prophecy). Just use the provided build method. The default mocking library is Mockery.
[...] $factory->build(SomeClass::class); // Returns MockeryBundle $factory->buildMockeryBundle(SomeClass::class); // Returns MockeryBundle $factory->buildPhakeBundle(SomeClass::class); // Returns PhakeBundle $factory->buildProphecyBundle(SomeClass::class); // Returns ProphecyBundle [...]
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()); $bundle->sut->run(); } }