Skip to content

Commit

Permalink
Delegatge running tests in fibers to wyrihaximus/react-phpunit-run-te…
Browse files Browse the repository at this point in the history
…sts-in-fiber
  • Loading branch information
WyriHaximus committed Mar 30, 2024
1 parent 2cdee4c commit ef17834
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 123 deletions.
18 changes: 0 additions & 18 deletions composer-require-checker.json

This file was deleted.

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"react/event-loop": "^1.5.0",
"react/promise": "^3.1",
"wyrihaximus/phpstan-react": "^1",
"wyrihaximus/react-phpunit-run-tests-in-fiber": "^1.0",
"wyrihaximus/test-utilities": "^5.5.4 || ^6"
},
"require-dev": {
Expand Down
55 changes: 54 additions & 1 deletion composer.lock

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

3 changes: 0 additions & 3 deletions etc/qa/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ parameters:
excludePaths:
- tests/bootstrap.php
ignoreErrors:
- '#Parameter \#1 \$name of method PHPUnit\\Framework\\TestCase::setName\(\) expects string, string\|null given.#'
- '#Trying to invoke array\{\$this\(WyriHaximus\\AsyncTestUtilities\\AsyncTestCase\), string\|null\} but it might not be a callable.#'
- '#Method WyriHaximus\\AsyncTestUtilities\\CallableStub::__invoke\(\) is not final, but since the containing class is abstract, it should be.#'
- '#Parameter \#1 \$name of method ReflectionClass\<\$this\(WyriHaximus\\AsyncTestUtilities\\AsyncTestCase\)\>::getMethod\(\) expects string, string\|null given.#'
- '#Call to deprecated method expectCallableExactly\(\)#'
- '#Call to deprecated method expectCallableOnce\(\)#'
ergebnis:
Expand Down
18 changes: 9 additions & 9 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"timeout": 120,
"timeout": 300,
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "infection-log.txt"
"text": "./var/infection.log",
"summary": "./var/infection-summary.log",
"json": "./var/infection.json",
"perMutator": "./var/infection-per-mutator.md"
},
"mutators": {
"@default": true,
"InstanceOf_": false,
"ArrayItemRemoval": {
"ignore": [
"WyriHaximus\\AsyncTestUtilities\\AsyncTestCase::provideEventLoop"
]
}
"@default": true
},
"phpUnit": {
"configDir": "./etc/qa/"
}
}
13 changes: 0 additions & 13 deletions phpunit.xml.dist

This file was deleted.

75 changes: 3 additions & 72 deletions src/AsyncTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
namespace WyriHaximus\AsyncTestUtilities;

use PHPUnit\Framework\MockObject\Rule\InvokedCount;
use React\EventLoop\Loop;
use ReflectionClass;
use WyriHaximus\React\PHPUnit\RunTestsInFibersTrait;
use WyriHaximus\TestUtilities\TestCase;

use function React\Async\async;
use function React\Async\await;

abstract class AsyncTestCase extends TestCase
{
private const INVOKE_ARRAY = ['__invoke'];
use RunTestsInFibersTrait;

private string|null $realTestName = null;
private const INVOKE_ARRAY = ['__invoke'];

/** @deprecated With the move to fibers there is no need for these rarely used methods anymore. (Unless proven otherwise of course.) */
final protected function expectCallableExactly(int $amount): callable
Expand Down Expand Up @@ -48,69 +44,4 @@ private function getCallableMock(InvokedCount $invokedCount): callable
/** @psalm-suppress InvalidReturnStatement */
return $mock;
}

/** @codeCoverageIgnore Invoked before code coverage data is being collected. */
final public function setName(string $name): void
{
/** @psalm-suppress InternalMethod */
parent::setName($name);

$this->realTestName = $name;
}

/** @internal */
final protected function runAsyncTest(mixed ...$args): mixed
{
/**
* @psalm-suppress InternalMethod
* @psalm-suppress PossiblyNullArgument
*/
parent::setName($this->realTestName);

$timeout = 30;
$reflectionClass = new ReflectionClass($this::class);
foreach ($reflectionClass->getAttributes() as $classAttribute) {
$classTimeout = $classAttribute->newInstance();
if (! ($classTimeout instanceof TimeOut)) {
continue;
}

$timeout = $classTimeout->timeout;
}

/**
* @psalm-suppress InternalMethod
* @psalm-suppress PossiblyNullArgument
*/
foreach ($reflectionClass->getMethod($this->realTestName)->getAttributes() as $methodAttribute) {
$methodTimeout = $methodAttribute->newInstance();
if (! ($methodTimeout instanceof TimeOut)) {
continue;
}

$timeout = $methodTimeout->timeout;
}

$timeout = Loop::addTimer($timeout, static fn () => Loop::stop());

try {
/**
* @psalm-suppress MixedArgument
* @psalm-suppress UndefinedInterfaceMethod
*/
return await(async(
fn (): mixed => ([$this, $this->realTestName])(...$args),
)());
} finally {
Loop::cancelTimer($timeout);
}
}

final protected function runTest(): mixed
{
/** @psalm-suppress InternalMethod */
parent::setName('runAsyncTest');

return parent::runTest();
}
}
8 changes: 7 additions & 1 deletion src/TimeOut.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
namespace WyriHaximus\AsyncTestUtilities;

use Attribute;
use WyriHaximus\React\PHPUnit\TimeOutInterface;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
final readonly class TimeOut
final readonly class TimeOut implements TimeOutInterface
{
public function __construct(
public int|float $timeout,
) {
}

public function timeout(): int|float
{
return $this->timeout;
}
}
10 changes: 4 additions & 6 deletions tests/AsyncTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ public function testExpectCallableExactly(): void
{
$callable = $this->expectCallableExactly(3);

Loop::futureTick($callable);
Loop::futureTick($callable);
Loop::futureTick($callable);
Loop::run();
$callable();
$callable();
$callable();
}

public function testExpectCallableOnce(): void
{
Loop::futureTick($this->expectCallableOnce());
Loop::run();
$this->expectCallableOnce()();
}
}

0 comments on commit ef17834

Please sign in to comment.