Skip to content

Commit 99d46da

Browse files
committed
Refresh code, add Github Actions workflow
1 parent 260882c commit 99d46da

File tree

7 files changed

+59
-20
lines changed

7 files changed

+59
-20
lines changed

.github/workflows/phpunit.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: PHPUnit
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
8+
jobs:
9+
phpunit:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- name: Setup PHP
15+
uses: shivammathur/setup-php@v2
16+
with:
17+
php-version: '8.4'
18+
19+
- name: Install Dependencies
20+
run: composer install --prefer-dist --no-progress --no-suggest
21+
22+
- name: Run PHPUnit
23+
run: ./unittest

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
"minimum-stability": "dev",
2222
"require": {
2323
"php": ">=8.2.0",
24+
"react/http": "^3@dev",
2425
"react/promise": "3.x-dev",
25-
"react/http": "1.x-dev",
2626
"react/async": "4.x-dev"
2727
},
2828
"require-dev": {
29-
"phpunit/phpunit": "10"
29+
"phpunit/phpunit": "12"
3030
},
3131
"autoload": {
3232
"psr-4": {

phpunit.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
<phpunit colors="true" bootstrap="src/bootstrap.php">
1+
<phpunit colors="true" bootstrap="src/bootstrap.php"
2+
displayDetailsOnTestsThatTriggerDeprecations="true"
3+
displayDetailsOnTestsThatTriggerErrors="true"
4+
displayDetailsOnTestsThatTriggerNotices="true"
5+
displayDetailsOnTestsThatTriggerWarnings="true"
6+
displayDetailsOnPhpunitDeprecations="true">
27
<testsuites>
38
<testsuite name="default">
49
<directory>test/</directory>

src/NonZeroExitException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
declare (strict_types = 1);
33
namespace Greendrake\AsyncProcess;
4+
45
class NonZeroExitException extends \RuntimeException
56
{
6-
}
7+
}

src/Promise.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<?php
22
declare (strict_types = 1);
33
namespace Greendrake\AsyncProcess;
4+
45
use Ds\Set;
56
use function React\Async\await;
67
use Psr\Http\Message\RequestInterface as Request;
78
use React\Http\Browser;
8-
use React\Http\Server;
9+
use React\Http\HttpServer;
910
use React\Promise as BasePromise;
1011
use React\Socket\ConnectionInterface;
1112
use React\Socket\Server as SocketServer;
1213

13-
class Promise {
14+
class Promise
15+
{
1416

1517
protected BasePromise\Deferred $deferred;
1618
protected BasePromise\Promise $promise;
@@ -19,7 +21,8 @@ class Promise {
1921
private static $sigHoldHandlerSetup = false;
2022
private static $sigHoldDefaultHandler;
2123

22-
public function __construct(protected string $command) {
24+
public function __construct(protected string $command)
25+
{
2326
$this->deferred = new BasePromise\Deferred;
2427
$this->promise = $this->deferred->promise();
2528
if (!self::$sigHoldHandlerSetup) {
@@ -64,7 +67,7 @@ public function __construct(protected string $command) {
6467
});
6568
});
6669
// Actually run the one-off HTTP server to wait for what the forked process has to say:
67-
$server = new Server(function (Request $request) use (&$result) {
70+
$server = new HttpServer(function (Request $request) use (&$result) {
6871
$result = unserialize((string) $request->getBody());
6972
});
7073
$server->listen($socket);
@@ -74,12 +77,12 @@ public function __construct(protected string $command) {
7477
pcntl_signal(SIGCHLD, self::$sigHoldDefaultHandler);
7578
$browser = new Browser;
7679
// Define the function that will report results back to the parent:
77-
$reportBack = function (int $forkExitCode = 0, ?int $jobExitCode = null, ?array $result = null, ?\Throwable $error = null) use ($browser, $httpAddress) {
80+
$reportBack = function (int $forkExitCode = 0, ?int $jobExitCode = null, ?array $result = null, ? \Throwable $error = null) use ($browser, $httpAddress) {
7881
await($browser->post('http://' . $httpAddress, [], serialize([
7982
'success' => $error === null,
8083
'result' => $result,
8184
'code' => $jobExitCode,
82-
'error' => $error,
85+
'error' => $error
8386
]))->catch(function () {
8487
// Don't give a fuck. This is the forked background process, and if anything is wrong, no one is gonna hear anyway.
8588
}));
@@ -127,7 +130,8 @@ public function __construct(protected string $command) {
127130
}
128131
}
129132

130-
public function getPid(): int {
133+
public function getPid(): int
134+
{
131135
return $this->pid;
132136
}
133137

@@ -136,7 +140,8 @@ public function get(): BasePromise\PromiseInterface
136140
return $this->promise;
137141
}
138142

139-
private static function findUnusedPort(): int {
143+
private static function findUnusedPort(): int
144+
{
140145
$tried = new Set;
141146
$add = function (int $port) use ($tried) {
142147
$tried->add($port);
@@ -148,7 +153,8 @@ private static function findUnusedPort(): int {
148153
return $port;
149154
}
150155

151-
private static function isPortOpen(int $port): bool {
156+
private static function isPortOpen(int $port): bool
157+
{
152158
$result = false;
153159
try {
154160
if ($pf = fsockopen('127.0.0.1', $port)) {
@@ -163,4 +169,4 @@ private static function isPortOpen(int $port): bool {
163169
return $result;
164170
}
165171

166-
}
172+
}

src/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
declare (strict_types = 1);
33
require_once __DIR__ . '/../vendor/autoload.php';
44
set_error_handler(function ($severity, $message, $file, $line) {
5-
throw new \ErrorException($message, 0, $severity, $file, $line);
6-
});
5+
throw new \ErrorException($message, 0, $severity, $file, $line);
6+
});
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
<?php
22
declare (strict_types = 1);
33
namespace Greendrake\AsyncProcess;
4+
45
use function React\Async\await;
56
use PHPUnit\Framework\TestCase;
67

7-
class Test extends TestCase {
8+
class MainTest extends TestCase
9+
{
810

9-
public function testFailure() {
11+
public function testFailure()
12+
{
1013
$this->expectException(NonZeroExitException::class);
1114
$p = new Promise('no-bananas');
1215
await($p->get());
1316
}
1417

15-
public function testSuccess() {
18+
public function testSuccess()
19+
{
1620
$p = new Promise('a=$( expr 10 - 3 ); echo $a');
1721
$result = await($p->get());
1822
$this->assertSame('7', $result);
1923
}
2024

21-
}
25+
}

0 commit comments

Comments
 (0)