sebastianknott/test-utils

Some classes i like to use for testing

0.1 2024-05-21 10:22 UTC

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();
    }
}