PHP code example of sebastianknott / test-utils

1. Go to this page and download the library: Download sebastianknott/test-utils library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sebastianknott / test-utils example snippets



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

[...]
$bundel = $factory->build(SomeClass::class, type: MockTypeEnum::MOCKERY);
[...]

[...]
$prebuildParameters = [
    'myDependency' => new Dependency()
];
$bundel = $factory->build(
    SomeClass::class,
    prebuildParameters: $prebuildParameters
);
[...]

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

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