Skip to content

Commit e094c17

Browse files
authored
Merge pull request #10 from xepozz/phpunit-10
Support PHPUnit 10
2 parents ac35931 + f776d44 commit e094c17

File tree

11 files changed

+166
-1952
lines changed

11 files changed

+166
-1952
lines changed

.github/workflows/phpunit.yaml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ name: "PHPUnit"
66

77
jobs:
88
phpunit:
9-
name: PHP ${{ matrix.php }}-${{ matrix.os }}
9+
name: PHPUnit ${{ matrix.phpunit }} PHP ${{ matrix.php }}-${{ matrix.os }}
1010
runs-on: ${{ matrix.os }}
1111
strategy:
1212
matrix:
@@ -17,6 +17,17 @@ jobs:
1717
- "8.1"
1818
- "8.2"
1919
- "8.3"
20+
phpunit:
21+
- 9
22+
- 10
23+
- 11
24+
exclude:
25+
- php: "8.0"
26+
phpunit: 10
27+
- php: "8.0"
28+
phpunit: 11
29+
- php: "8.1"
30+
phpunit: 11
2031
steps:
2132
- name: Checkout
2233
uses: actions/checkout@v2.3.4
@@ -31,11 +42,19 @@ jobs:
3142
- name: Install dependencies
3243
run: composer install
3344

45+
- name: Switching PHPUnit version
46+
run: composer req --dev phpunit/phpunit:^${{ matrix.phpunit }} -W
47+
48+
- name: Run tests with code coverage.
49+
if: matrix.phpunit == '9'
50+
run: composer test -- -c phpunit9.xml
51+
3452
- name: Run tests with code coverage.
35-
run: composer test
53+
if: matrix.phpunit != '9'
54+
run: composer test -- -c phpunit.xml
3655

3756
- name: Upload coverage to Codecov.
3857
if: matrix.os == 'ubuntu-latest'
3958
uses: codecov/codecov-action@v3
4059
with:
41-
files: ./coverage.xml
60+
files: ./coverage.xml

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/data/
44
/tests/data/
55

6-
.phpunit.result.cache
6+
.phpunit.result.cache
7+
composer.lock

README.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3537
The 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

3943
1. Create new file `tests/MockerExtension.php`
4044
2. 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+
76138
Here you have registered extension that will be called every time when you run `./vendor/bin/phpunit`.
77139

78140
By default, all functions will be generated and saved into `/vendor/bin/xepozz/internal-mocker/data/mocks.php` file.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"yiisoft/var-dumper": "^1.2"
1212
},
1313
"require-dev": {
14-
"phpunit/phpunit": "^9.5"
14+
"phpunit/phpunit": "^9 || ^10 || ^11"
1515
},
1616
"autoload": {
1717
"psr-4": {
@@ -24,6 +24,6 @@
2424
}
2525
},
2626
"scripts": {
27-
"test": "php -ddisable_functions=time,header,headers_sent vendor/bin/phpunit"
27+
"test": "php -ddisable_functions=time,header,headers_sent vendor/bin/phpunit --random-order-seed=1"
2828
}
2929
}

0 commit comments

Comments
 (0)