Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PHPUnit 10 #10

Merged
merged 13 commits into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions .github/workflows/phpunit.yaml
Expand Up @@ -6,7 +6,7 @@ name: "PHPUnit"

jobs:
phpunit:
name: PHP ${{ matrix.php }}-${{ matrix.os }}
name: PHPUnit ${{ matrix.phpunit }} PHP ${{ matrix.php }}-${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand All @@ -17,6 +17,17 @@ jobs:
- "8.1"
- "8.2"
- "8.3"
phpunit:
- 9
- 10
- 11
exclude:
- php: "8.0"
phpunit: 10
- php: "8.0"
phpunit: 11
- php: "8.1"
phpunit: 11
steps:
- name: Checkout
uses: actions/checkout@v2.3.4
Expand All @@ -31,11 +42,19 @@ jobs:
- name: Install dependencies
run: composer install

- name: Switching PHPUnit version
run: composer req --dev phpunit/phpunit:^${{ matrix.phpunit }} -W

- name: Run tests with code coverage.
if: matrix.phpunit == '9'
run: composer test -- -c phpunit9.xml

- name: Run tests with code coverage.
run: composer test
if: matrix.phpunit != '9'
run: composer test -- -c phpunit.xml

- name: Upload coverage to Codecov.
if: matrix.os == 'ubuntu-latest'
uses: codecov/codecov-action@v3
with:
files: ./coverage.xml
files: ./coverage.xml
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,5 @@
/data/
/tests/data/

.phpunit.result.cache
.phpunit.result.cache
composer.lock
66 changes: 64 additions & 2 deletions README.md
Expand Up @@ -11,7 +11,9 @@ functions as: `time()`, `str_contains()`, `rand`, etc.

- [Installation](#installation)
- [Usage](#usage)
- [Register a hook](#register-a-hook)
- [Register a PHPUnit Extension](#register-a-phpunit-extension)
- [PHPUnit 9](#phpunit-9)
- [PHPUnit 10 and higher](#phpunit-10-and-higher)
- [Register mocks](#register-mocks)
- [Runtime mocks](#runtime-mocks)
- [Pre-defined mock](#pre-defined-mock)
Expand All @@ -34,7 +36,9 @@ composer require xepozz/internal-mocker --dev

The main idea is pretty simple: register a Listener for PHPUnit and call the Mocker extension first.

### Register a hook
### Register a PHPUnit Extension

#### PHPUnit 9

1. Create new file `tests/MockerExtension.php`
2. Paste the following code into the created file:
Expand Down Expand Up @@ -73,6 +77,64 @@ The main idea is pretty simple: register a Listener for PHPUnit and call the Moc
</extensions>
```

#### PHPUnit 10 and higher

1. Create new file `tests/MockerExtension.php`
2. Paste the following code into the created file:
```php
<?php
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();
}
}
```
3. Register the hook as extension in `phpunit.xml.dist`
```xml
<extensions>
<bootstrap class="App\Tests\MockerExtension"/>
</extensions>
```

Here you have registered extension that will be called every time when you run `./vendor/bin/phpunit`.

By default, all functions will be generated and saved into `/vendor/bin/xepozz/internal-mocker/data/mocks.php` file.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -11,7 +11,7 @@
"yiisoft/var-dumper": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9 || ^10 || ^11"
},
"autoload": {
"psr-4": {
Expand All @@ -24,6 +24,6 @@
}
},
"scripts": {
"test": "php -ddisable_functions=time,header,headers_sent vendor/bin/phpunit"
"test": "php -ddisable_functions=time,header,headers_sent vendor/bin/phpunit --random-order-seed=1"
}
}