Skip to content

Commit 7d2ffd3

Browse files
authored
Merge pull request #1 from reactphp-parallel/quick-create-factory-method
Quick factory method to create runtimes with the default autoloader
2 parents 47ac97f + 2f999cc commit 7d2ffd3

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"ext-parallel": "*",
1515
"react-parallel/future-to-promise-converter": "dev-master",
1616
"react/event-loop": "^1.1",
17-
"react/promise": "^2.7"
17+
"react/promise": "^2.7",
18+
"wyrihaximus/constants": "^1.4.3"
1819
},
1920
"require-dev": {
2021
"wyrihaximus/async-test-utilities": "^2.0.0-alpha1",

composer.lock

Lines changed: 41 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ parameters:
22
ignoreErrors:
33
- '#Do not throw the \\Exception base class. Instead, extend the \\Exception base class. More info: http:\/\/bit.ly\/subtypeexception#'
44
- '#Invalid type Throwable\|null to throw.#'
5-
- '#Thrown exceptions in a catch block must bundle the previous exception \(see throw statement line 88\). More info: http:\/\/bit.ly\/bundleexception#'
6-
- '#Thrown exceptions in a catch block must bundle the previous exception \(see throw statement line 114\). More info: http:\/\/bit.ly\/bundleexception#'
5+
- '#Thrown exceptions in a catch block must bundle the previous exception \(see throw statement line 97\). More info: http:\/\/bit.ly\/bundleexception#'
6+
- '#Thrown exceptions in a catch block must bundle the previous exception \(see throw statement line 126\). More info: http:\/\/bit.ly\/bundleexception#'
77

88
includes:
99
- vendor/wyrihaximus/async-test-utilities/rules.neon

src/Runtime.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
use React\Promise\PromiseInterface;
99
use ReactParallel\FutureToPromiseConverter\FutureToPromiseConverter;
1010
use function React\Promise\resolve;
11+
use const WyriHaximus\Constants\ComposerAutoloader\LOCATION;
1112

1213
final class Runtime
1314
{
1415
private ParallelRuntime $runtime;
1516

1617
private FutureToPromiseConverter $futureToPromiseConverter;
1718

19+
public static function create(FutureToPromiseConverter $futureToPromiseConverter): self
20+
{
21+
return new self($futureToPromiseConverter, LOCATION);
22+
}
23+
1824
public function __construct(FutureToPromiseConverter $futureToPromiseConverter, string $autoload)
1925
{
2026
$this->futureToPromiseConverter = $futureToPromiseConverter;

tests/RuntimeTest.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
*/
1717
final class RuntimeTest extends AsyncTestCase
1818
{
19-
public function testConvertSuccess(): void
19+
/**
20+
* @test
21+
*/
22+
public function convertSuccess(): void
2023
{
2124
$loop = Factory::create();
22-
$runtime = new Runtime(new FutureToPromiseConverter($loop), \dirname(__DIR__) . '/vendor/autoload.php');
25+
$runtime = Runtime::create(new FutureToPromiseConverter($loop));
2326

2427
/** @var ExtendedPromiseInterface $promise */
2528
$promise = $runtime->run(function (): int {
@@ -38,13 +41,16 @@ public function testConvertSuccess(): void
3841
self::assertSame(3, $three);
3942
}
4043

41-
public function testConvertFailure(): void
44+
/**
45+
* @test
46+
*/
47+
public function convertFailure(): void
4248
{
4349
self::expectException(\Exception::class);
4450
self::expectExceptionMessage('Rethrow exception');
4551

4652
$loop = Factory::create();
47-
$runtime = new Runtime(new FutureToPromiseConverter($loop), \dirname(__DIR__) . '/vendor/autoload.php');
53+
$runtime = Runtime::create(new FutureToPromiseConverter($loop));
4854

4955
/** @var ExtendedPromiseInterface $promise */
5056
$promise = $runtime->run(function (): void {
@@ -63,13 +69,16 @@ public function testConvertFailure(): void
6369
self::assertSame(3, $three);
6470
}
6571

66-
public function testWeClosedTheThread(): void
72+
/**
73+
* @test
74+
*/
75+
public function weClosedTheThread(): void
6776
{
6877
self::expectException(Closed::class);
6978
self::expectExceptionMessage('Runtime closed');
7079

7180
$loop = Factory::create();
72-
$runtime = new Runtime(new FutureToPromiseConverter($loop), \dirname(__DIR__) . '/vendor/autoload.php');
81+
$runtime = Runtime::create(new FutureToPromiseConverter($loop));
7382

7483
/** @var ExtendedPromiseInterface $promise */
7584
$promise = timedPromise($loop, 1, $runtime)->then(function (Runtime $runtime) {
@@ -89,13 +98,16 @@ public function testWeClosedTheThread(): void
8998
}
9099
}
91100

92-
public function testWeKilledTheThread(): void
101+
/**
102+
* @test
103+
*/
104+
public function weKilledTheThread(): void
93105
{
94106
self::expectException(Closed::class);
95107
self::expectExceptionMessage('Runtime closed');
96108

97109
$loop = Factory::create();
98-
$runtime = new Runtime(new FutureToPromiseConverter($loop), \dirname(__DIR__) . '/vendor/autoload.php');
110+
$runtime = Runtime::create(new FutureToPromiseConverter($loop));
99111

100112
/** @var ExtendedPromiseInterface $promise */
101113
$promise = timedPromise($loop, 1, $runtime)->then(function (Runtime $runtime) {

0 commit comments

Comments
 (0)