Skip to content

Commit 37ef998

Browse files
async http
1 parent ca7ad5c commit 37ef998

File tree

6 files changed

+50
-14
lines changed

6 files changed

+50
-14
lines changed

.phpunit.cache/test-results

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"AsyncHttpClientTest::test_simple_get_request":8},"times":{"AsyncHttpClientTest::test_simple_get_request":0.02}}

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"ext-sockets": "*"
88
},
99
"require-dev": {
10-
"phpunit/phpunit": "^12.2"
10+
"phpunit/phpunit": "^12.2",
11+
"phpstan/phpstan": "^2.1",
12+
"laravel/pint": "^1.23"
1113
},
1214
"license": "MIT",
1315
"autoload": {
@@ -22,7 +24,7 @@
2224
}
2325
],
2426
"scripts": {
25-
"test": "vendor/bin/phpunit"
27+
"test": "vendor/bin/phpstan"
2628
},
2729
"minimum-stability": "stable",
2830
"prefer-stable": true

phpstan.neon.dist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ parameters:
22
level: max
33
paths:
44
- src
5-
autoload_directories:
6-
- src
5+

phpunit.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheDirectory=".phpunit.cache"
6+
executionOrder="depends,defects"
7+
requireCoverageMetadata="true"
8+
beStrictAboutCoverageMetadata="true"
9+
beStrictAboutOutputDuringTests="true"
10+
displayDetailsOnPhpunitDeprecations="true"
11+
failOnPhpunitDeprecation="true"
12+
failOnRisky="true"
13+
failOnWarning="true" colors="true">
14+
<testsuites>
15+
<testsuite name="default">
16+
<directory>tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
21+
<include>
22+
<directory>src</directory>
23+
</include>
24+
</source>
25+
</phpunit>

src/Http/MultiAsyncHandler.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
<?php
2+
23
namespace Async\Http;
34

45
class MultiAsyncHandler
56
{
67
private array $streams = [];
8+
79
private array $callbacks = [];
810

9-
public function add(callable $generatorCallback, callable $onDone): void {
11+
public function add(callable $generatorCallback, callable $onDone): void
12+
{
1013
$gen = $generatorCallback();
1114
$gen->rewind();
1215
$socket = $this->extractSocket($gen);
1316
if ($socket) {
14-
$this->streams[(int)$socket] = $socket;
15-
$this->callbacks[(int)$socket] = [$gen, $onDone];
17+
$this->streams[(int) $socket] = $socket;
18+
$this->callbacks[(int) $socket] = [$gen, $onDone];
1619
}
1720
}
1821

19-
public function run(): void {
22+
public function run(): void
23+
{
2024
while ($this->streams) {
2125
$read = array_values($this->streams);
22-
$write = null; $except = null;
26+
$write = null;
27+
$except = null;
2328
stream_select($read, $write, $except, 5);
2429

2530
foreach ($read as $socket) {
26-
$id = (int)$socket;
31+
$id = (int) $socket;
2732
[$gen, $callback] = $this->callbacks[$id];
2833
if ($gen->valid()) {
2934
$response = $gen->current();
@@ -34,7 +39,8 @@ public function run(): void {
3439
}
3540
}
3641

37-
private function extractSocket(\Generator $gen): mixed {
42+
private function extractSocket(\Generator $gen): mixed
43+
{
3844
$r = new \ReflectionObject($gen);
3945
foreach ($r->getProperties() as $prop) {
4046
$prop->setAccessible(true);
@@ -43,6 +49,7 @@ private function extractSocket(\Generator $gen): mixed {
4349
return $val;
4450
}
4551
}
52+
4653
return null;
4754
}
4855
}

tests/AsyncHttpClientTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
2+
23
use PHPUnit\Framework\TestCase;
3-
//use Async\Http\AsyncHttpClient;
4+
5+
// use Async\Http\AsyncHttpClient;
46

57
class AsyncHttpClientTest extends TestCase
68
{
7-
public function testSimpleGetRequest()
9+
public function test_simple_get_request()
810
{
9-
$client = new AsyncHttpClient();
11+
$client = new AsyncHttpClient;
1012
foreach ($client->get('https://jsonplaceholder.typicode.com/posts/1') as $response) {
1113
$this->assertStringContainsString('userId', $response->getBody());
1214
}

0 commit comments

Comments
 (0)