PHP code example of nathanburkett / mesa-phpunit

1. Go to this page and download the library: Download nathanburkett/mesa-phpunit 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/ */

    

nathanburkett / mesa-phpunit example snippets


 namespace Foo\Bar\Baz;

use PHPUnit\Framework\TestCase;
use NathanBurkett\Mesa\Fixture\PHPUnit\GeneratesTestCases;

class FooTest extends TestCase
{
    use GeneratesTestCases;
}



// Location __DIR__ . '/DataSets/FooTest/OutputTestCases.php

use PHPUnit\Framework\TestCase;
use PHPUnit\MockObject\MockObject;

return [
    'Expects \'foo\' output' => [
        'setupDependencyOneExpectations' => function (TestCase $test, MockObject $dependencyOne): MockObject {
            $dependencyOne->expects($test->once())->method('foo')->willReturn(true);
            return $dependencyOne;
        },
        'config' => 'Some config',
        'expected' => 'FooBarBaz',
    ],
];


 namespace Foo\Bar\Baz;

use PHPUnit\Framework\TestCase;
use NathanBurkett\Mesa\Fixture\PHPUnit\GeneratesTestCases;

class FooTest extends TestCase
{
    use GeneratesTestCases;
    
    /**
     * @dataProvider generateOutputTestCases
     * 
     * @param DependencyOne $dependencyOne
     * @param string $config
     * @param string $expected
     */
    public function testOutput(DependencyOne $dependencyOne, string $config, string $expected)
    {
        $service = new FooService($dependencyOne, $config);
        $actual = $service->run();
        $this->assertEquals($expected, $actual);
    }
    
    /**
     * @return \Generator
     */
    public function generateOutputTestCases(): \Generator
    {
        yield from $this->generateTestCases(
            'OutputTestCases.php' // this defaults to looking for test cases in __DIR__ . /DataSets/{ClassName}/{string}
            // [$this, 'setupOutputTestCase'] Optional setup func
        );
    }
    
    /**
     * @param array $testCase
     * @return array
     */
    public function setupOutputTestCase(array $testCase): array
    {
        $dependencyOne = $this->getMockBuilder(DependencyOne::class)
                              ->disableOriginalConstructor()
                              ->getMock();
        
        return [
            $testCase['setupDependencyOneExpectations']($this, $dependencyOne),
            $testCase['config'],
            $testCase['expected'],
        ];
    }
}

 namespace Foo\Bar\Baz;

use NathanBurkett\Mesa\Fixture\PHPUnit\PathResolver\PHPUnitDataSetPathResolver;

class NewPHPUnitPathResolver extends PHPUnitDataSetPathResolver
{
    /**
     * @var string 
     */
    public const DEFAULT_DIRECTORY = 'TestCases';
}

 namespace Foo\Bar\Baz;

use PHPUnit\Framework\TestCase;
use Foo\Bar\Baz\NewPHPUnitPathResolver;
use NathanBurkett\Mesa\Fixture\PHPUnit\GeneratesTestCases;
use NathanBurkett\Mesa\Fixture\PHPUnit\PathResolver\PathResolver;

class FooTest extends TestCase
{
    use GeneratesTestCases;
    
    /**
     * @return PathResolver
     */
    protected function getTestCasePathResolver(): PathResolver
    {
        return new NewPHPUnitPathResolver((string) $this->testCaseContext, new \ReflectionClass($this));
    }
}