File tree Expand file tree Collapse file tree 1 file changed +51
-1
lines changed
Expand file tree Collapse file tree 1 file changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -216,4 +216,54 @@ class SomeService
216216 // ...
217217 }
218218}
219- ```
219+ ```
220+
221+ ##### Data Providers
222+
223+ Sometimes you may face unpleasant situation when mocked function is not mocking without forced using ` namespace `
224+ + ` function ` .
225+ It may mean that you are trying make PHP interpreter file in ` @dataProvider ` .
226+ Be careful of it and as a workaround I may suggest you to call the mocker in test's constructor.
227+ So first move all code from your extension method ` executeBeforeFirstTest ` to new static method
228+ and call it in both ` executeBeforeFirstTest ` and ` __construct ` methods.
229+
230+ ``` php
231+ final class MyTest extends \PHPUnit\Framework\TestCase
232+ {
233+ public function __construct(?string $name = null, array $data = [], $dataName = '')
234+ {
235+ \App\Tests\MockerExtension::load();
236+ parent::__construct($name, $data, $dataName);
237+ }
238+
239+ /// ...
240+ }
241+ ```
242+
243+ ``` php
244+ final class MockerExtension implements BeforeTestHook, BeforeFirstTestHook
245+ {
246+ public function executeBeforeFirstTest(): void
247+ {
248+ self::load();
249+ }
250+
251+ public static function load(): void
252+ {
253+ $mocks = [];
254+
255+ $mocker = new Mocker();
256+ $mocker->load($mocks);
257+ MockerState::saveState();
258+ }
259+
260+ public function executeBeforeTest(string $test): void
261+ {
262+ MockerState::resetState();
263+ }
264+ }
265+ ```
266+
267+ That all because of PHPUnit 9.5 and lower event management system.
268+ Data Provider functionality starts to work before any events so it's impossible to mock the function at the beginning of
269+ the runtime.
You can’t perform that action at this time.
0 commit comments