@@ -11,7 +11,9 @@ functions as: `time()`, `str_contains()`, `rand`, etc.
1111
1212- [ Installation] ( #installation )
1313- [ Usage] ( #usage )
14- - [ Register a hook] ( #register-a-hook )
14+ - [ Register a PHPUnit Extension] ( #register-a-phpunit-extension )
15+ - [ PHPUnit 9] ( #phpunit-9 )
16+ - [ PHPUnit 10 and higher] ( #phpunit-10-and-higher )
1517 - [ Register mocks] ( #register-mocks )
1618 - [ Runtime mocks] ( #runtime-mocks )
1719 - [ Pre-defined mock] ( #pre-defined-mock )
@@ -34,7 +36,9 @@ composer require xepozz/internal-mocker --dev
3436
3537The main idea is pretty simple: register a Listener for PHPUnit and call the Mocker extension first.
3638
37- ### Register a hook
39+ ### Register a PHPUnit Extension
40+
41+ #### PHPUnit 9
3842
39431 . Create new file ` tests/MockerExtension.php `
40442 . Paste the following code into the created file:
@@ -73,6 +77,64 @@ The main idea is pretty simple: register a Listener for PHPUnit and call the Moc
7377 </extensions >
7478 ```
7579
80+ #### PHPUnit 10 and higher
81+
82+ 1. Create new file `tests/MockerExtension.php`
83+ 2. Paste the following code into the created file:
84+ ```php
85+ <?php
86+ declare(strict_types=1);
87+
88+ namespace App\Tests;
89+
90+ use PHPUnit\Event\Test\PreparationStarted;
91+ use PHPUnit\Event\Test\PreparationStartedSubscriber;
92+ use PHPUnit\Event\TestSuite\Started;
93+ use PHPUnit\Event\TestSuite\StartedSubscriber;
94+ use PHPUnit\Runner\Extension\Extension;
95+ use PHPUnit\Runner\Extension\Facade;
96+ use PHPUnit\Runner\Extension\ParameterCollection;
97+ use PHPUnit\TextUI\Configuration\Configuration;
98+ use Xepozz\InternalMocker\Mocker;
99+ use Xepozz\InternalMocker\MockerState;
100+
101+ final class MockerExtension implements Extension
102+ {
103+ public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
104+ {
105+ $facade->registerSubscribers(
106+ new class () implements StartedSubscriber {
107+ public function notify(Started $event): void
108+ {
109+ MockerExtension::load();
110+ }
111+ },
112+ new class implements PreparationStartedSubscriber {
113+ public function notify(PreparationStarted $event): void
114+ {
115+ MockerState::resetState();
116+ }
117+ },
118+ );
119+ }
120+
121+ public static function load(): void
122+ {
123+ $mocks = [];
124+
125+ $mocker = new Mocker();
126+ $mocker->load($mocks);
127+ MockerState::saveState();
128+ }
129+ }
130+ ```
131+ 3. Register the hook as extension in `phpunit.xml.dist`
132+ ```xml
133+ <extensions >
134+ <bootstrap class =" App\Tests\MockerExtension" />
135+ </extensions >
136+ ```
137+
76138Here you have registered extension that will be called every time when you run `./vendor/bin/phpunit`.
77139
78140By default, all functions will be generated and saved into `/vendor/bin/xepozz/internal-mocker/data/mocks.php` file.
0 commit comments