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: 2024-10-21 11:17:10 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()
    {
        // ...
    }
}

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