PHP code example of xepozz / internal-mocker

1. Go to this page and download the library: Download xepozz/internal-mocker 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/ */

    

xepozz / internal-mocker example snippets


    
    declare(strict_types=1);
    
    namespace App\Tests;
    
    use PHPUnit\Runner\BeforeTestHook;
    use PHPUnit\Runner\BeforeFirstTestHook;
    use Xepozz\InternalMocker\Mocker;
    use Xepozz\InternalMocker\MockerState;
    
    final class MockerExtension implements BeforeTestHook, BeforeFirstTestHook
    {
        public function executeBeforeFirstTest(): void
        {
            $mocks = [];
    
            $mocker = new Mocker();
            $mocker->load($mocks);
            MockerState::saveState();
        }
   
        public function executeBeforeTest(string $test): void
        {
            MockerState::resetState();
        }
    }
    

    
    declare(strict_types=1);
    
    namespace App\Tests;
    
    use PHPUnit\Event\Test\PreparationStarted;
    use PHPUnit\Event\Test\PreparationStartedSubscriber;
    use PHPUnit\Event\TestSuite\Started;
    use PHPUnit\Event\TestSuite\StartedSubscriber;
    use PHPUnit\Runner\Extension\Extension;
    use PHPUnit\Runner\Extension\Facade;
    use PHPUnit\Runner\Extension\ParameterCollection;
    use PHPUnit\TextUI\Configuration\Configuration;
    use Xepozz\InternalMocker\Mocker;
    use Xepozz\InternalMocker\MockerState;
    
    final class MockerExtension implements Extension
    {
        public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
        {
            $facade->registerSubscribers(
                new class () implements StartedSubscriber {
                    public function notify(Started $event): void
                    {
                        MockerExtension::load();
                    }
                },
                new class implements PreparationStartedSubscriber {
                    public function notify(PreparationStarted $event): void
                    {
                        MockerState::resetState();
                    }
                },
            );
        }
    
        public static function load(): void
        {
            $mocks = [];
    
            $mocker = new Mocker();
            $mocker->load($mocks);
            MockerState::saveState();
        }
    }
    

$mocker = new Mocker('/path/to/your/mocks.php');

$mocks = [
    [
        'namespace' => 'App\Service',
        'name' => 'time',
    ],
];

MockerState::addCondition(
   'App\Service', // namespace
   'time', // function name
   [], // arguments
   100 // result
);

MockerState::addCondition(
   '', // namespace
   'headers_sent', // function name
   [null, null], // both arguments are references and they are not initialized yet on the function call
   fn (&$file, &$line) => $file = $line = 123, // callback result
);


namespace App\Tests;

use App\Service;
use PHPUnit\Framework\TestCase;

class ServiceTest extends TestCase
{
    public function testRun2(): void
    {
        $service = new Service();

        MockerState::addCondition(
            'App\Service',
            'time',
            [],
            100
        );

        $this->assertEquals(100, $service->doSomething());
   }
}

$mocks = [
    [
        'namespace' => 'App\Service',
        'name' => 'time',
        'result' => 150,
        'arguments' => [],
    ],
];

$traces = MockerState::getTraces('App\Service', 'time');

[
    [
        'arguments' => [], // arguments of the function
        'trace' => [], // the result of debug_backtrace function
        'result' => 1708764835, // result of the function
    ],
    // ...
]

$mocker = new Mocker(stubPath: '/path/to/your/stubs.php');

$mocks[] = [
   'namespace' => '',
   'name' => 'time',
   'function' => fn () => `date +%s`,
];

final class MyTest extends \PHPUnit\Framework\TestCase
{
    public function __construct(?string $name = null, array $data = [], $dataName = '')
    {
        \App\Tests\MockerExtension::load();
        parent::__construct($name, $data, $dataName);
    }
    
    /// ...
}

final class MockerExtension implements BeforeTestHook, BeforeFirstTestHook
{
    public function executeBeforeFirstTest(): void
    {
        self::load();
    }

    public static function load(): void
    {
        $mocks = [];

        $mocker = new Mocker();
        $mocker->load($mocks);
        MockerState::saveState();
    }

    public function executeBeforeTest(string $test): void
    {
        MockerState::resetState();
    }
}
bash
php -ddisable_functions=${functions} ./vendor/bin/phpunit
json
{
  "scripts": {
    "test": "php -ddisable_functions=time,serialize,header,date ./vendor/bin/phpunit"
  }
}