Skip to content

Commit

Permalink
Merge pull request #65 from jolicode/fix/test-ref
Browse files Browse the repository at this point in the history
fix(assertion): fix assertion added to the invalid test by using a fiber storage
  • Loading branch information
joelwurtz committed Apr 10, 2024
2 parents 2c87578 + 246bcee commit 95cb1f0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
12 changes: 1 addition & 11 deletions src/Assert/AssertCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Asynit\Assert;

use Asynit\Attribute\OnCreate;
use Asynit\Test;
use bovigo\assert\predicate\Predicate;

use function bovigo\assert\counting;
Expand Down Expand Up @@ -50,14 +48,6 @@

trait AssertCaseTrait
{
protected Test $test;

#[OnCreate]
public function setUpTest(Test $test): void
{
$this->test = $test;
}

/**
* Asserts that an array has a specified key.
*
Expand Down Expand Up @@ -535,7 +525,7 @@ public function assertContainsSubset(array $other, array $subset, bool $strict =

public function assert($value, callable $predicate, ?string $description = null): bool
{
return (new Assertion($value, exporter(), $this->test))
return (new Assertion($value, exporter()))
->evaluate(counting(Predicate::castFrom($predicate)), $description);
}

Expand Down
8 changes: 3 additions & 5 deletions src/Assert/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Asynit\Assert;

use Asynit\Runner\TestStorage;
use Asynit\Test;
use bovigo\assert\Assertion as BaseAssertion;
use bovigo\assert\AssertionFailure;
Expand All @@ -16,18 +17,15 @@ class Assertion extends BaseAssertion

private Exporter $exporter;

private Test $test;

/**
* constructor.
*/
public function __construct($value, Exporter $exporter, Test $test)
public function __construct($value, Exporter $exporter)
{
parent::__construct($value, $exporter);

$this->value = $value;
$this->exporter = $exporter;
$this->test = $test;
}

public function evaluate(Predicate $predicate, ?string $description = null): bool
Expand All @@ -50,7 +48,7 @@ public function evaluate(Predicate $predicate, ?string $description = null): boo
throw new AssertionFailure($message);
}

$this->test->addAssertion($this->describeSuccess($predicate, $description));
TestStorage::get()?->addAssertion($this->describeSuccess($predicate, $description));

return $result;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Runner/PoolRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function loop(Pool $pool): void

$futures[$test->getIdentifier()] = async(function () use ($test, &$futures) {
$lock = $this->semaphore->acquire();
TestStorage::set($test);

$this->run($test);
$lock->release();

Expand Down Expand Up @@ -100,7 +102,7 @@ private function getTestCase(Test $test): object
continue;
}

$testCase->{$reflectionMethod->getName()}($test);
$testCase->{$reflectionMethod->getName()}();
}

$this->testCases[$reflectionClass->getName()] = $testCase;
Expand Down
36 changes: 36 additions & 0 deletions src/Runner/TestStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Asynit\Runner;

use Asynit\Test;

/** @internal */
final class TestStorage
{
private static ?\WeakMap $localStorage = null;

public static function set(Test $test): void
{
$fiber = \Fiber::getCurrent();

if (null === $fiber) {
return;
}

self::$localStorage ??= new \WeakMap();
self::$localStorage[$fiber] = $test;
}

public static function get(): ?Test
{
$fiber = \Fiber::getCurrent();

if (null === $fiber) {
return null;
}

self::$localStorage ??= new \WeakMap();

return self::$localStorage[$fiber] ?? null;
}
}

0 comments on commit 95cb1f0

Please sign in to comment.