Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ jobs:
matrix:
include:
- operating-system: 'ubuntu-latest'
php-version: '8.1'
php-version: '8.4'

- operating-system: 'ubuntu-latest'
php-version: '8.5'

- operating-system: 'windows-latest'
php-version: '8.1'
php-version: '8.4'
job-description: 'on Windows'

- operating-system: 'macos-latest'
php-version: '8.1'
php-version: '8.4'
job-description: 'on macOS'

name: PHP ${{ matrix.php-version }} ${{ matrix.job-description }}
Expand All @@ -31,20 +34,19 @@ jobs:
git config --global core.eol lf

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: fiber-amphp/ext-fiber@master

- name: Get Composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-dir)"

- name: Cache dependencies
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: composer-${{ runner.os }}-${{ matrix.php-version }}-${{ hashFiles('**/composer.*') }}-${{ matrix.composer-flags }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.php_cs.cache
.php-cs-fixer.cache
build
composer.lock
phpunit.xml
vendor
.phpunit.cache
.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This package can be installed as a [Composer](https://getcomposer.org/) dependen
composer require --dev amphp/phpunit-util
```

The package requires PHP 8.1 or later.
The package requires PHP 8.4 or later and PHPUnit 13.

## Usage

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"issues": "https://github.com/amphp/phpunit-util/issues"
},
"require": {
"php": ">=8.1",
"php": ">=8.4",
"amphp/amp": "^3",
"phpunit/phpunit": "^9",
"revolt/event-loop": "^1 || ^0.2"
"phpunit/phpunit": "^13",
"revolt/event-loop": "^1"
},
"require-dev": {
"amphp/php-cs-fixer-config": "^2"
Expand Down
11 changes: 4 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/13.4/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="Main">
Expand All @@ -20,9 +17,9 @@
<directory suffix=".phpt">test</directory>
</testsuite>
</testsuites>
<coverage>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</source>
</phpunit>
27 changes: 3 additions & 24 deletions src/AsyncTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,8 @@ abstract class AsyncTestCase extends PHPUnitTestCase
/** @var float Minimum runtime in seconds. */
private float $minimumRuntime = 0;

/** @var string Temporary storage for actual test name. */
private string $realTestName;

private bool $setUpInvoked = false;

/**
* @codeCoverageIgnore Invoked before code coverage data is being collected.
*/
final public function setName(string $name): void
{
parent::setName($name);
$this->realTestName = $name;
}

protected function setUp(): void
{
$this->setUpInvoked = true;
Expand All @@ -50,8 +38,7 @@ protected function setUp(): void
});
}

/** @internal */
final protected function runAsyncTest(mixed ...$args): mixed
final protected function invokeTestMethod(string $methodName, array $testArguments): mixed
{
if (!$this->setUpInvoked) {
self::fail(\sprintf(
Expand All @@ -61,16 +48,14 @@ final protected function runAsyncTest(mixed ...$args): mixed
));
}

parent::setName($this->realTestName);

$start = now();

try {
[, $returnValue] = Future\await([
$this->deferredFuture->getFuture(),
async(function () use ($args): mixed {
async(function () use ($methodName, $testArguments): mixed {
try {
$result = ([$this, $this->realTestName])(...$args);
$result = parent::invokeTestMethod($methodName, $testArguments);
if ($result instanceof Future) {
$result = $result->await();
}
Expand Down Expand Up @@ -112,12 +97,6 @@ final protected function runAsyncTest(mixed ...$args): mixed
return $returnValue;
}

final protected function runTest(): mixed
{
parent::setName('runAsyncTest');
return parent::runTest();
}

/**
* Fails the test if it does not run for at least the given amount of time.
*
Expand Down
20 changes: 9 additions & 11 deletions test/AsyncTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Amp\DeferredFuture;
use Amp\Future;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Depends;
use Revolt\EventLoop;
use function Amp\async;
use function Amp\delay;
Expand Down Expand Up @@ -66,7 +68,7 @@ public function testExpectingAnExceptionThrown(): Future
};

$this->expectException(TestException::class);
$this->expectExceptionMessage('threw the error');
$this->expectExceptionMessageIsOrContains('threw the error');

return async($throwException);
}
Expand All @@ -80,27 +82,23 @@ public function testExpectingAnErrorThrown(): Future
});
}

public function provideArguments(): array
public static function provideArguments(): array
{
return [
['foo', 42, true],
];
}

/**
* @dataProvider provideArguments
*/
#[DataProvider('provideArguments')]
public function testArgumentSupport(string $foo, int $bar, bool $baz): void
{
self::assertSame('foo', $foo);
self::assertSame(42, $bar);
self::assertTrue($baz);
}

/**
* @depends testReturningFuture
*/
public function testReturnValueFromDependentTest(string $value = null): void
#[Depends('testReturningFuture')]
public function testReturnValueFromDependentTest(?string $value = null): void
{
self::assertSame('value', $value);
}
Expand Down Expand Up @@ -128,7 +126,7 @@ public function testSetTimeoutWithFuture(): Future
$this->setTimeout(0.1);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Expected test to complete before 0.100s time limit');
$this->expectExceptionMessageIsOrContains('Expected test to complete before 0.100s time limit');

$deferred = new DeferredFuture;

Expand All @@ -142,7 +140,7 @@ public function testSetTimeoutWithAwait(): void
$this->setTimeout(0.1);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Expected test to complete before 0.100s time limit');
$this->expectExceptionMessageIsOrContains('Expected test to complete before 0.100s time limit');

delay(0.2);
}
Expand Down
2 changes: 1 addition & 1 deletion test/InvalidAsyncTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected function setUp(): void
// No call to parent::setUp()

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('without calling the parent method');
$this->expectExceptionMessageIsOrContains('without calling the parent method');
}

public function testMethod()
Expand Down
Loading