Skip to content

Make the library phpunit 10 compatible #15

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

Merged
merged 10 commits into from
Nov 29, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ on:
pull_request: null
push:
branches:
- main
- master

jobs:
code_analysis:
strategy:
fail-fast: false
matrix:
php:
- "8.1"
- "8.2"
- "8.3"
actions:
-
name: "PHPStan"
Expand All @@ -20,19 +24,19 @@ jobs:
name: "PHPUnit"
run: vendor/bin/phpunit

name: ${{ matrix.actions.name }}
name: PHP${{ matrix.php }}:${{ matrix.actions.name }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

# see https://github.com/shivammathur/setup-php
- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: ${{ matrix.php }})
coverage: none

# composer install cache - https://github.com/ramsey/composer-install
- uses: "ramsey/composer-install@v1"
- uses: "ramsey/composer-install@v2"

- run: ${{ matrix.actions.run }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
vendor/
.phpunit.result.cache
.phpunit.cache
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
"description": "Library for asserting things that happen asynchronously with PHPUnit",
"license": "MIT",
"type": "library",
"keywords": ["PHPUnit", "asynchronicity", "assertion"],
"keywords": [
"PHPUnit",
"asynchronicity",
"assertion"
],
"authors": [
{
"name": "Matthias Noback",
Expand All @@ -22,10 +26,11 @@
}
},
"require": {
"php": "^7.3 || ^8.0"
"php": "^8.1"
},
"require-dev": {
"phpstan/phpstan": "^0.9.2",
"phpunit/phpunit": "^9.0"
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.0",
"ext-pcntl": "*"
}
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ parameters:
paths:
- src
- tests
ignoreErrors: []
31 changes: 16 additions & 15 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache">
<coverage/>
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</source>
</phpunit>
20 changes: 14 additions & 6 deletions src/Asynchronicity/PHPUnit/Eventually.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Asynchronicity\PHPUnit;

use Asynchronicity\Polling\IncorrectUsage;
use Asynchronicity\Polling\Interrupted;
use Asynchronicity\Polling\Poller;
use Asynchronicity\Polling\SystemClock;
Expand All @@ -11,17 +12,24 @@

final class Eventually extends Constraint
{
private $timeoutMilliseconds;
private $waitMilliseconds;
private int $timeoutMilliseconds;
private int $waitMilliseconds;

public function __construct(int $timeoutMilliseconds = 5000, int $waitMilliseconds = 500)
{
$this->timeoutMilliseconds = $timeoutMilliseconds;
$this->waitMilliseconds = $waitMilliseconds;
}

public function evaluate($probe, string $description = '', bool $returnResult = false): ?bool
/**
* @throws Interrupted
*/
public function evaluate(mixed $probe, string $description = '', bool $returnResult = false): ?bool
{
if (!is_callable($probe)) {
throw new IncorrectUsage();
}

try {
$poller = new Poller();
$poller->poll(
Expand All @@ -39,13 +47,13 @@ public function evaluate($probe, string $description = '', bool $returnResult =
return true;
}

protected function failureDescription($other): string
protected function failureDescription(mixed $other): string
{
return 'the given probe was satisfied within the provided timeout';
}

public function toString(): string
{
throw new \BadMethodCallException('Not implemented');
return 'Eventually';
}
}
}
26 changes: 22 additions & 4 deletions tests/Asynchronicity/PHPUnit/EventuallyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

final class EventuallyTest extends TestCase
{
/**
* @var Eventually
*/
private $constraint;
private Eventually $constraint;

/**
* @var callable
Expand Down Expand Up @@ -101,6 +98,27 @@ public function it_accepts_a_closure_as_probe(): void
}));
}

/**
* @test
*/
public function it_throws_an_exception_if_the_probe_is_not_callable(): void
{
$constraint = new Eventually();

$this->expectException(IncorrectUsage::class);
$constraint->evaluate('foobar');
}

/**
* @test
*/
public function it_is_stringable(): void
{
$constraint = new Eventually();

$this->assertSame('Eventually', $constraint->toString());
}

private function probeAlwaysFails(): void
{
$this->probe = function (): void {
Expand Down
2 changes: 1 addition & 1 deletion tests/Asynchronicity/PHPUnit/FileHasBeenCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

final class FileHasBeenCreated
{
private $path;
private string $path;

public function __construct(string $path)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Asynchronicity/PHPUnit/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ final class IntegrationTest extends TestCase
public function it_waits_until_a_child_process_does_something(): void
{
if (!\extension_loaded('pcntl')) {
$this->markTestSkipped('Requires PCNTL extension');
self::markTestSkipped('Requires PCNTL extension');
}

$timeoutMilliseconds = 2000;
$waitMilliseconds = 1000;

$file = sys_get_temp_dir().'/'.uniqid('phpunit-asynchronicity', true);
$this->assertFileNotExists($file);
self::assertFileDoesNotExist($file);

$pid = pcntl_fork();
if ($pid === -1) {
Expand Down
5 changes: 3 additions & 2 deletions tests/Asynchronicity/Polling/PollerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
namespace Asynchronicity\Polling;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

final class PollerTest extends TestCase
{
private $waitTimeInMilliseconds = 1000;
private int $waitTimeInMilliseconds = 1000;

/**
* @var callable
Expand All @@ -21,7 +22,7 @@ final class PollerTest extends TestCase
private $poller;

/**
* @var Clock
* @var Clock&MockObject
*/
private $clock;

Expand Down
22 changes: 8 additions & 14 deletions tests/Asynchronicity/Polling/TimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class TimeoutTest extends TestCase
private $timeout;

/**
* @var MockObject & Clock
* @var MockObject&Clock
*/
private $clock;

Expand Down Expand Up @@ -86,20 +86,14 @@ public function it_fails_when_start_is_not_called_first(): void
$this->timeout->hasTimedOut();
}

/**
* @param int[] $microtimes
*/
private function clockReturnsMicrotimes(array $microtimes): void
{
static $at;
if ($at === null) {
$at = 0;
}

foreach ($microtimes as $microtime) {
$this->clock
->expects($this->at($at))
->method('getMicrotime')
->will($this->returnValue($microtime));

$at++;
}
$this->clock
->expects($this->exactly(count($microtimes)))
->method('getMicrotime')
->willReturnOnConsecutiveCalls(...$microtimes);
}
}