-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from clue-labs/coverage
Update test suite to ensure 100% code coverage
- Loading branch information
Showing
12 changed files
with
306 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,9 @@ | |
"autoload-dev": { | ||
"psr-4": { | ||
"FrameworkX\\Tests\\": "tests/" | ||
} | ||
}, | ||
"files": [ | ||
"tests/FiberStub.php" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
|
||
if (!class_exists(Fiber::class)) { | ||
class Fiber | ||
{ | ||
/** @var callable */ | ||
private $callback; | ||
|
||
/** @var mixed */ | ||
private $return; | ||
|
||
/** @var bool */ | ||
private $started = false; | ||
|
||
/** @var bool */ | ||
private $terminated = false; | ||
|
||
/** @var bool */ | ||
private static $halt = false; | ||
|
||
/** @var ?Fiber */ | ||
private static $suspended = null; | ||
|
||
public function __construct(callable $callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* @param mixed ...$args | ||
* @return mixed | ||
* @throws \Throwable | ||
*/ | ||
public function start(...$args) | ||
{ | ||
if ($this->started) { | ||
throw new \FiberError(); | ||
} | ||
$this->started = true; | ||
|
||
if (self::$halt) { | ||
assert(self::$suspended === null); | ||
self::$suspended = $this; | ||
return null; | ||
} | ||
|
||
try { | ||
return $this->return = ($this->callback)(...$args); | ||
} finally { | ||
$this->terminated = true; | ||
} | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @return mixed | ||
* @throws \BadMethodCallException | ||
*/ | ||
public function resume($value = null) | ||
{ | ||
throw new \BadMethodCallException(); | ||
} | ||
|
||
/** | ||
* @param Throwable $exception | ||
* @return mixed | ||
* @throws \BadMethodCallException | ||
*/ | ||
public function throw(Throwable $exception) | ||
{ | ||
throw new \BadMethodCallException(); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
* @throws FiberError | ||
*/ | ||
public function getReturn() | ||
{ | ||
if (!$this->terminated) { | ||
throw new \FiberError(); | ||
} | ||
|
||
return $this->return; | ||
} | ||
|
||
public function isStarted(): bool | ||
{ | ||
return $this->started; | ||
} | ||
|
||
public function isSuspended(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isRunning(): bool | ||
{ | ||
return $this->started && !$this->terminated; | ||
} | ||
|
||
public function isTerminated(): bool | ||
{ | ||
return $this->terminated; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @return mixed | ||
* @throws \Throwable | ||
*/ | ||
public static function suspend($value = null) | ||
{ | ||
throw new \BadMethodCallException(); | ||
} | ||
|
||
public static function getCurrent(): ?Fiber | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function mockSuspend(): void | ||
{ | ||
assert(self::$halt === false); | ||
self::$halt = true; | ||
} | ||
|
||
/** | ||
* @internal | ||
* @throws void | ||
*/ | ||
public static function mockResume(): void | ||
{ | ||
assert(self::$halt === true); | ||
assert(self::$suspended instanceof self); | ||
|
||
$fiber = self::$suspended; | ||
assert($fiber->started); | ||
assert(!$fiber->terminated); | ||
|
||
self::$halt = false; | ||
self::$suspended = null; | ||
|
||
/** @throws void */ | ||
$fiber->return = ($fiber->callback)(); | ||
$fiber->terminated = true; | ||
} | ||
} | ||
|
||
final class FiberError extends Error { | ||
|
||
} | ||
} |
Oops, something went wrong.