-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace ShockedPlot7560\PmmpUnit\utils; | ||
|
||
use Closure; | ||
use DaveRandom\CallbackValidator\CallbackType; | ||
use DaveRandom\CallbackValidator\ReturnType; | ||
use Generator; | ||
use React\Promise\Deferred; | ||
use React\Promise\PromiseInterface; | ||
use SOFe\AwaitGenerator\Await; | ||
use Throwable; | ||
|
||
class AwaitGeneratorDecorator implements PromiseInterface { | ||
Check failure on line 14 in src/utils/AwaitGeneratorDecorator.php GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)
Check failure on line 14 in src/utils/AwaitGeneratorDecorator.php GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)
|
||
private Deferred $delegate; | ||
Check failure on line 15 in src/utils/AwaitGeneratorDecorator.php GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)
Check failure on line 15 in src/utils/AwaitGeneratorDecorator.php GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)
|
||
|
||
public function __construct( | ||
Closure|Generator $generateGenerator | ||
) { | ||
$this->delegate = new Deferred(); | ||
|
||
if ($generateGenerator instanceof Closure) { | ||
$type = new CallbackType(new ReturnType('Generator')); | ||
$type->isSatisfiedBy($generateGenerator); | ||
} | ||
|
||
try { | ||
Await::f2c(function () use ($generateGenerator) { | ||
if ($generateGenerator instanceof Closure) { | ||
$generateGenerator = $generateGenerator(); | ||
} | ||
$result = yield from $generateGenerator; | ||
$this->delegate->resolve($result); | ||
}); | ||
} catch (Throwable $e) { | ||
$this->delegate->reject($e); | ||
} | ||
} | ||
|
||
public function then(?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface { | ||
Check failure on line 40 in src/utils/AwaitGeneratorDecorator.php GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)
Check failure on line 40 in src/utils/AwaitGeneratorDecorator.php GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)
|
||
return $this->delegate->promise()->then($onFulfilled, $onRejected); | ||
} | ||
|
||
public function catch(callable $onRejected) : PromiseInterface { | ||
return $this->delegate->promise()->catch($onRejected); | ||
} | ||
|
||
public function finally(callable $onFulfilledOrRejected) : PromiseInterface { | ||
return $this->delegate->promise()->finally($onFulfilledOrRejected); | ||
} | ||
|
||
public function cancel() : void { | ||
$this->delegate->promise()->cancel(); | ||
} | ||
|
||
public function otherwise(callable $onRejected) : PromiseInterface { | ||
return $this->delegate->promise()->otherwise($onRejected); | ||
} | ||
|
||
public function always(callable $onFulfilledOrRejected) : PromiseInterface { | ||
return $this->delegate->promise()->always($onFulfilledOrRejected); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace ShockedPlot7560\PmmpUnit\tests\normal; | ||
|
||
use Exception; | ||
use Generator; | ||
use pocketmine\scheduler\Task; | ||
use React\Promise\PromiseInterface; | ||
use ShockedPlot7560\PmmpUnit\framework\TestCase; | ||
use ShockedPlot7560\PmmpUnit\PmmpUnit; | ||
use ShockedPlot7560\PmmpUnit\utils\AwaitGeneratorDecorator; | ||
use SOFe\AwaitGenerator\Await; | ||
|
||
class AwaitGeneratorDecoratorTest extends TestCase { | ||
public function testClosureCallback() : PromiseInterface { | ||
Check failure on line 15 in tests/pmmpunit/suitetest/normal/tests/AwaitGeneratorDecoratorTest.php GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)
Check failure on line 15 in tests/pmmpunit/suitetest/normal/tests/AwaitGeneratorDecoratorTest.php GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)
|
||
$start = microtime(true); | ||
|
||
$decorator = new AwaitGeneratorDecorator(function () : Generator { | ||
return $this->sleep(); | ||
}); | ||
|
||
return $decorator->then(function () use ($start) : void { | ||
$this->assertTrue(microtime(true) - $start >= 0.9); | ||
}); | ||
} | ||
|
||
public function testGeneratorCallback() : PromiseInterface { | ||
Check failure on line 27 in tests/pmmpunit/suitetest/normal/tests/AwaitGeneratorDecoratorTest.php GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)
Check failure on line 27 in tests/pmmpunit/suitetest/normal/tests/AwaitGeneratorDecoratorTest.php GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)
|
||
$start = microtime(true); | ||
|
||
$decorator = new AwaitGeneratorDecorator($this->sleep()); | ||
|
||
return $decorator->then(function () use ($start) : void { | ||
$this->assertTrue(microtime(true) - $start >= 0.9); | ||
}); | ||
} | ||
|
||
private function sleep() : Generator { | ||
yield from Await::promise(function ($resolve, $reject) { | ||
$task = new class($resolve, $reject) extends Task { | ||
private $resolve; | ||
private $reject; | ||
|
||
public function __construct($resolve, $reject) { | ||
$this->resolve = $resolve; | ||
$this->reject = $reject; | ||
} | ||
|
||
public function onRun() : void { | ||
($this->resolve)(); | ||
} | ||
|
||
public function onCancel() : void { | ||
($this->reject)(new Exception("Task cancelled")); | ||
} | ||
}; | ||
PmmpUnit::getInstance()->getScheduler()->scheduleDelayedTask($task, 20); | ||
}); | ||
} | ||
} |