Skip to content

Commit 8754458

Browse files
committed
support php 8.x
1 parent 114bab5 commit 8754458

File tree

13 files changed

+175
-63
lines changed

13 files changed

+175
-63
lines changed

.github/FUNDING.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# These are supported funding model platforms
2+
3+
github: [Erwane]
4+
liberapay: erwane
5+
buy_me_a_coffee: erwane
6+
patreon: erwane_b
7+
thanks_dev: gh/erwane

.github/workflows/ci.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: CI
33
on:
44
push:
55
branches:
6-
- '1.x'
6+
- '2.x'
77
pull_request:
88
branches:
99
- '*'
@@ -19,8 +19,10 @@ jobs:
1919
fail-fast: false
2020
matrix:
2121
php-version:
22-
- '7.2'
23-
- '7.4'
22+
- '8.1'
23+
- '8.2'
24+
- '8.3'
25+
- '8.4'
2426

2527
steps:
2628
- uses: actions/checkout@v4
@@ -43,15 +45,15 @@ jobs:
4345
env:
4446
XDEBUG_MODE: coverage
4547
run: |
46-
if [[ '${{ matrix.php-version }}' == '7.2' ]]; then
48+
if [[ '${{ matrix.php-version }}' == '8.1' ]]; then
4749
export CODECOVERAGE=1
4850
vendor/bin/phpunit --coverage-clover=coverage.xml
4951
else
5052
vendor/bin/phpunit
5153
fi
5254
5355
- name: Submit code coverage
54-
if: matrix.php-version == '7.2'
56+
if: matrix.php-version == '8.1'
5557
uses: codecov/codecov-action@v5
5658
with:
5759
files: coverage.xml

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ composer.phar
88

99
/coverage.xml
1010
/auth.json
11+
/tmp
1112

1213
# OS generated files #
1314
######################

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# PhpUnit resource helper
22

33
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
4-
[![codecov](https://codecov.io/gh/Erwane/phpunit-resource-helper/branch/1.x/graph/badge.svg?token=hF5HhETnkg)](https://codecov.io/gh/Erwane/phpunit-resource-helper)
5-
[![Build Status](https://github.com/Erwane/phpunit-resource-helper/actions/workflows/ci.yml/badge.svg?branch=1.x)](https://github.com/Erwane/phpunit-resource-helper/actions)
4+
[![codecov](https://codecov.io/gh/Erwane/phpunit-resource-helper/branch/2.x/graph/badge.svg?token=hF5HhETnkg)](https://codecov.io/gh/Erwane/phpunit-resource-helper)
5+
[![Build Status](https://github.com/Erwane/phpunit-resource-helper/actions/workflows/ci.yml/badge.svg?branch=2.x)](https://github.com/Erwane/phpunit-resource-helper/actions)
66
[![Packagist Downloads](https://img.shields.io/packagist/dt/Erwane/phpunit-resource-helper)](https://packagist.org/packages/Erwane/phpunit-resource-helper)
77
[![Packagist Version](https://img.shields.io/packagist/v/Erwane/phpunit-resource-helper)](https://packagist.org/packages/Erwane/phpunit-resource-helper)
88

@@ -15,6 +15,7 @@ Help your phpunit tests to load resources from files, like, json content, raw e-
1515
| branch | This package version | PHP min |
1616
|:------:|----------------------|:-------:|
1717
| 1.x | ^1.0 | PHP 7.2 |
18+
| 2.x | ^2.0 | PHP 8.1 |
1819

1920
## Usage
2021

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
}
2222
],
2323
"require": {
24-
"php": "^7.2",
24+
"php": "^8.1",
2525
"ext-json": "*"
2626
},
2727
"require-dev": {
28-
"cakephp/cakephp-codesniffer": "^4.0",
29-
"phpunit/phpunit": "^8.5",
30-
"symfony/var-dumper": "^v5.0"
28+
"cakephp/cakephp-codesniffer": ">=1",
29+
"phpunit/phpunit": ">=10",
30+
"symfony/var-dumper": ">=v6"
3131
},
3232
"autoload": {
3333
"psr-4": {

phpunit.xml.dist

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
44
bootstrap="tests/bootstrap.php"
5-
colors="true">
5+
colors="true"
6+
cacheDirectory="tmp/phpunit.cache"
7+
displayDetailsOnPhpunitDeprecations="true"
8+
displayDetailsOnTestsThatTriggerDeprecations="true"
9+
displayDetailsOnTestsThatTriggerErrors="true"
10+
displayDetailsOnTestsThatTriggerWarnings="true">
611
<testsuites>
712
<testsuite name="all">
813
<directory>./tests</directory>
914
</testsuite>
1015
</testsuites>
1116

12-
<filter>
13-
<whitelist>
14-
<directory suffix=".php" phpVersion="7.2">src/</directory>
15-
</whitelist>
16-
</filter>
17+
<source>
18+
<include>
19+
<directory suffix=".php">src/</directory>
20+
</include>
21+
</source>
22+
1723
<extensions>
18-
<extension class="ResourceHelper\PHPUnitExtension"></extension>
24+
<bootstrap class="ResourceHelper\PHPUnitExtension"></bootstrap>
1925
</extensions>
2026
</phpunit>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* This file is part of PHPUnit resources helpers library
4+
*
5+
* @copyright Copyright (c) Erwane BRETON
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace ResourceHelper\Extension;
12+
13+
use PHPUnit\Event\Test\AfterTestMethodFinished;
14+
use PHPUnit\Event\Test\AfterTestMethodFinishedSubscriber;
15+
use ResourceHelper\ResourceHelper;
16+
17+
/**
18+
* Clean successful tests tmp directory.
19+
*/
20+
class CleanupSubscriber implements AfterTestMethodFinishedSubscriber
21+
{
22+
public function notify(AfterTestMethodFinished $event): void
23+
{
24+
$methods = $event->calledMethods();
25+
foreach ($methods as $method) {
26+
ResourceHelper::destroyTmpTestDir($method->className() . '::' . $method->methodName());
27+
}
28+
}
29+
}

src/PHPUnitExtension.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@
1010

1111
namespace ResourceHelper;
1212

13-
use PHPUnit\Runner\AfterSuccessfulTestHook;
13+
use PHPUnit\Runner\Extension\Extension;
14+
use PHPUnit\Runner\Extension\Facade;
15+
use PHPUnit\Runner\Extension\ParameterCollection;
16+
use PHPUnit\TextUI\Configuration\Configuration;
17+
use ResourceHelper\Extension\CleanupSubscriber;
1418

1519
/**
16-
* PHPUnit extension to clean up temp files.
20+
* PHPUnit extension.
21+
*
22+
* @codeCoverageIgnore
1723
*/
18-
class PHPUnitExtension implements AfterSuccessfulTestHook
24+
class PHPUnitExtension implements Extension
1925
{
20-
/**
21-
* Cleanup test files if case of successful result.
22-
*
23-
* @param string $test Test name with namespace
24-
* @param float $time Test duration time
25-
* @return void
26-
*/
27-
public function executeAfterSuccessfulTest(string $test, float $time): void
26+
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
2827
{
29-
ResourceHelper::destroyTmpTestDir($test);
28+
$facade->registerSubscriber(new CleanupSubscriber());
3029
}
3130
}

src/ResourceHelper.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class ResourceHelper
2222
/**
2323
* @var string
2424
*/
25-
protected static $_baseDir;
25+
protected static string $_baseDir;
2626

2727
/**
2828
* @var string
2929
*/
30-
protected static $_tmpDir;
30+
protected static string $_tmpDir;
3131

3232
/**
3333
* Get resource base dir.
@@ -88,7 +88,7 @@ public static function setTmpDir(string $tmpDir): void
8888
* @param \PHPUnit\Framework\TestCase|string $test Running test or test method full name
8989
* @return string
9090
*/
91-
public static function getTmpTestPath($test): string
91+
public static function getTmpTestPath(TestCase|string $test): string
9292
{
9393
$name = $test;
9494
if ($test instanceof TestCase) {
@@ -106,7 +106,7 @@ public static function getTmpTestPath($test): string
106106
* @param \PHPUnit\Framework\TestCase|string $test Running test or test method full name
107107
* @return string
108108
*/
109-
public static function createTmpTestDir($test): string
109+
public static function createTmpTestDir(TestCase|string $test): string
110110
{
111111
self::destroyTmpTestDir($test);
112112

@@ -122,7 +122,7 @@ public static function createTmpTestDir($test): string
122122
* @param \PHPUnit\Framework\TestCase|string $test Running test or test method full name
123123
* @return void
124124
*/
125-
public static function destroyTmpTestDir($test): void
125+
public static function destroyTmpTestDir(TestCase|string $test): void
126126
{
127127
$dir = self::getTmpTestPath($test);
128128
if (is_dir($dir)) {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* This file is part of PHPUnit resources helpers library
4+
*
5+
* @copyright Copyright (c) Erwane BRETON
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace ResourceHelper\Test\TestCase\Extension;
12+
13+
use Composer\InstalledVersions;
14+
use PHPUnit\Event\Code;
15+
use PHPUnit\Event\Telemetry;
16+
use PHPUnit\Event\Test\AfterTestMethodFinished;
17+
use PHPUnit\Event\TestData\TestDataCollection;
18+
use PHPUnit\Framework\Attributes\CoversClass;
19+
use PHPUnit\Framework\TestCase;
20+
use PHPUnit\Metadata\MetadataCollection;
21+
use ResourceHelper\Extension\CleanupSubscriber;
22+
use ResourceHelper\ResourceHelper;
23+
use function hrtime;
24+
25+
#[CoversClass(CleanupSubscriber::class)]
26+
class CleanupSubscriberTest extends TestCase
27+
{
28+
final protected function telemetryInfo(): Telemetry\Info
29+
{
30+
return new Telemetry\Info(
31+
new Telemetry\Snapshot(
32+
Telemetry\HRTime::fromSecondsAndNanoseconds(...hrtime()),
33+
Telemetry\MemoryUsage::fromBytes(1000),
34+
Telemetry\MemoryUsage::fromBytes(2000),
35+
new Telemetry\GarbageCollectorStatus(0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, false, false, false, 0),
36+
),
37+
Telemetry\Duration::fromSecondsAndNanoseconds(123, 456),
38+
Telemetry\MemoryUsage::fromBytes(2000),
39+
Telemetry\Duration::fromSecondsAndNanoseconds(234, 567),
40+
Telemetry\MemoryUsage::fromBytes(3000),
41+
);
42+
}
43+
44+
final protected function testValueObject(): Code\TestMethod
45+
{
46+
return new Code\TestMethod(
47+
'FooTest',
48+
'testBar',
49+
'FooTest.php',
50+
1,
51+
Code\TestDoxBuilder::fromClassNameAndMethodName('Foo', 'bar'),
52+
MetadataCollection::fromArray([]),
53+
TestDataCollection::fromArray([]),
54+
);
55+
}
56+
57+
public function testNotify(): void
58+
{
59+
ResourceHelper::createTmpTestDir($this);
60+
61+
$sub = new CleanupSubscriber();
62+
63+
$telemetryInfo = $this->telemetryInfo();
64+
$phpunitVersion = InstalledVersions::getVersion('phpunit/phpunit');
65+
if (version_compare($phpunitVersion, '12.0.0', '>=')) {
66+
$test = $this->testValueObject();
67+
} else {
68+
$test = 'testNotify';
69+
}
70+
$calledMethods = [
71+
new Code\ClassMethod(static::class, 'testNotify'),
72+
];
73+
74+
$event = new AfterTestMethodFinished($telemetryInfo, $test, ...$calledMethods);
75+
76+
$sub->notify($event);
77+
78+
$this->assertDirectoryDoesNotExist(ResourceHelper::getTmpTestPath($this));
79+
}
80+
}

0 commit comments

Comments
 (0)