Skip to content

Commit

Permalink
Add BC layer to support 9 and 10 phpunits
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Mar 4, 2024
1 parent 1fd0373 commit fde253d
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 30 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/phpunit.yaml
Expand Up @@ -17,6 +17,9 @@ jobs:
- "8.1"
- "8.2"
- "8.3"
phpunit:
- 9
- 10
steps:
- name: Checkout
uses: actions/checkout@v2.3.4
Expand All @@ -31,11 +34,14 @@ jobs:
- name: Install dependencies
run: composer install

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

- name: Run tests with code coverage.
run: composer test
run: composer test -- -c phpunit${{ matrix.phpunit }}.xml

- name: Upload coverage to Codecov.
if: matrix.os == 'ubuntu-latest'
uses: codecov/codecov-action@v3
with:
files: ./coverage.xml
files: ./coverage.xml
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -11,7 +11,7 @@
"yiisoft/var-dumper": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^10"
"phpunit/phpunit": "^10.5"
},
"autoload": {
"psr-4": {
Expand Down
59 changes: 35 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions phpunit.xml
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php"
colors="true" failOnSkipped="false" failOnRisky="false" failOnWarning="false" stopOnFailure="false"
executionOrder="default" resolveDependencies="true">
<extensions>
<bootstrap class="Xepozz\InternalMocker\Tests\MockerExtension"/>
</extensions>
<php>
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="Tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
22 changes: 22 additions & 0 deletions phpunit10.xml
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php"
colors="true" failOnSkipped="false" failOnRisky="false" failOnWarning="false" stopOnFailure="false"
executionOrder="default" resolveDependencies="true">
<extensions>
<bootstrap class="Xepozz\InternalMocker\Tests\MockerExtension"/>
</extensions>
<php>
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="Tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions phpunit.xml.dist → phpunit9.xml
@@ -1,5 +1,5 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
failOnSkipped="false"
Expand All @@ -11,7 +11,7 @@
>

<extensions>
<bootstrap class="Xepozz\InternalMocker\Tests\MockerExtension"/>
<extension class="Xepozz\InternalMocker\Tests\MockerExtension"/>
</extensions>
<php>
<ini name="error_reporting" value="-1"/>
Expand Down
43 changes: 42 additions & 1 deletion tests/MockerExtension.php
@@ -1,25 +1,65 @@
<?php

declare(strict_types=1);

namespace Xepozz\InternalMocker\Tests;

use PHPUnit\Event\Test\PreparationStarted;
use PHPUnit\Event\Test\PreparationStartedSubscriber;
use PHPUnit\Event\TestSuite\Started;
use PHPUnit\Event\TestSuite\StartedSubscriber;
use PHPUnit\Runner\BeforeFirstTestHook;
use PHPUnit\Runner\BeforeTestHook;
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 BeforeFirstTestHook, BeforeTestHook
if (interface_exists(Extension::class)) {
abstract class PHPUnitExtension implements Extension
{
}
} else {
abstract class PHPUnitExtension implements BeforeFirstTestHook, BeforeTestHook
{
}
}

final class MockerExtension extends PHPUnitExtension
{
// phpunit 9 compatibility
public function executeBeforeFirstTest(): void
{
self::load();
}

// phpunit 9 compatibility
public function executeBeforeTest(string $test): void
{
MockerState::resetState();
}

// phpunit 10 compatibility
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 = [
Expand Down Expand Up @@ -89,4 +129,5 @@ public static function load(): void
$mocker->load($mocks);
MockerState::saveState();
}

}

0 comments on commit fde253d

Please sign in to comment.