Skip to content

Commit d1e750b

Browse files
committed
Test fetch function with ResponseSimulator
1 parent 114dfaa commit d1e750b

File tree

5 files changed

+175
-1
lines changed

5 files changed

+175
-1
lines changed

src/RequestResolver.php

+4
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ public function tick():void {
149149
$this->responseList[$i]->endDeferredResponse();
150150
$this->responseList[$i] = null;
151151
}
152+
if($this->deferredList[$i]) {
153+
$this->deferredList[$i]->resolve($this->responseList[$i]);
154+
$this->deferredList[$i] = null;
155+
}
152156
}
153157
}
154158

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
namespace Gt\Fetch\Test\Helper;
3+
4+
class ResponseSimulator {
5+
const RANDOM_BODY_WORDS = ["pursuit","forest","gravel","timber","wonder","eject","slogan","monkey","construct","earthquake","respect","publish","forward","circle","summer","define","highlight","refuse","salon","theater","lily","earwax","variant","account","resource"];
6+
static protected $headerCallback;
7+
static protected $bodyCallback;
8+
static protected $headerBuffer;
9+
static protected $bodyBuffer;
10+
static protected $started = false;
11+
12+
static public function setHeaderCallback(callable $callback) {
13+
self::$headerCallback = $callback;
14+
}
15+
16+
static public function setBodyCallback(callable $callback) {
17+
self::$bodyCallback = $callback;
18+
}
19+
20+
static public function start() {
21+
self::$started = true;
22+
self::$headerBuffer = self::generateRandomHeaders();
23+
self::$bodyBuffer = self::generateRandomBody();
24+
}
25+
26+
static protected function generateRandomHeaders():array {
27+
$headers = [];
28+
29+
$headers []= "HTTP/0.0 999 OK";
30+
$headers []= "Date: " . date("D, d M Y H:i:s T");
31+
$headers []= "Repository: PhpGt/Fetch";
32+
33+
$length = rand(1, 10);
34+
for($i = 0; $i < $length; $i++) {
35+
$randIndex = array_rand(self::RANDOM_BODY_WORDS);
36+
$key = self::RANDOM_BODY_WORDS[$randIndex];
37+
$value = uniqid();
38+
$headers []= "$key: $value";
39+
}
40+
41+
foreach($headers as $i => $h) {
42+
$headers[$i] .= "\r\n";
43+
}
44+
45+
$headers []= "\r\n";
46+
47+
return $headers;
48+
}
49+
50+
static protected function generateRandomBody():string {
51+
$body = "";
52+
$length = rand(10, 100);
53+
for($i = 0; $i < $length; $i++) {
54+
$randIndex = array_rand(self::RANDOM_BODY_WORDS);
55+
$body .= self::RANDOM_BODY_WORDS[$randIndex];
56+
$body .= " ";
57+
}
58+
59+
return $body;
60+
}
61+
62+
static public function hasStarted():bool {
63+
return self::$started;
64+
}
65+
66+
static public function sendChunk($ch):int {
67+
if(!empty(self::$headerBuffer)) {
68+
$data = array_shift(self::$headerBuffer);
69+
70+
call_user_func(
71+
self::$headerCallback,
72+
$ch,
73+
$data
74+
);
75+
76+
return 1;
77+
}
78+
elseif(!empty(self::$bodyBuffer)) {
79+
$data = self::$bodyBuffer;
80+
self::$bodyBuffer = "";
81+
82+
call_user_func(
83+
self::$bodyCallback,
84+
$ch,
85+
$data
86+
);
87+
88+
return 1;
89+
}
90+
else {
91+
return 0;
92+
}
93+
}
94+
}

test/unit/Helper/TestCurl.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Gt\Fetch\Test\Helper;
3+
4+
use Gt\Curl\Curl;
5+
6+
class TestCurl extends Curl {
7+
protected $id;
8+
9+
public function __construct(string $url = null) {
10+
$this->id = uniqid("dummy-curl-");
11+
parent::__construct($url);
12+
}
13+
14+
public function setOpt(int $option, $value):bool {
15+
if($option === CURLOPT_HEADERFUNCTION) {
16+
ResponseSimulator::setHeaderCallback($value);
17+
}
18+
19+
if($option === CURLOPT_WRITEFUNCTION) {
20+
ResponseSimulator::setBodyCallback($value);
21+
}
22+
23+
return true;
24+
}
25+
26+
public function getHandle() {
27+
return $this;
28+
}
29+
}

test/unit/Helper/TestCurlMulti.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
namespace Gt\Fetch\Test\Helper;
3+
4+
use Gt\Curl\CurlInterface;
5+
use Gt\Curl\CurlMulti;
6+
7+
class TestCurlMulti extends CurlMulti {
8+
protected $ch;
9+
10+
public function add(CurlInterface $curl):void {
11+
$this->ch = $curl;
12+
}
13+
14+
public function exec(int &$stillRunning):int {
15+
if(!ResponseSimulator::hasStarted()) {
16+
ResponseSimulator::start();
17+
}
18+
19+
$stillRunning += ResponseSimulator::sendChunk($this->ch);
20+
return CURLM_OK;
21+
}
22+
}

test/unit/HttpTest.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
11
<?php
22
namespace Gt\Fetch;
33

4-
use Gt\Fetch\Test\Helper\Helper;
4+
use Gt\Curl\Curl;
5+
use Gt\Curl\CurlInterface;
6+
use Gt\Curl\CurlMulti;
7+
use Gt\Curl\CurlMultiInterface;
8+
use Gt\Fetch\Test\Helper\TestCurl;
9+
use Gt\Fetch\Test\Helper\TestCurlMulti;
10+
use Gt\Http\Request;
11+
use Gt\Http\Uri;
512
use PHPUnit\Framework\TestCase;
13+
use Psr\Http\Message\RequestInterface;
14+
use Psr\Http\Message\ResponseInterface;
615

716
class HttpTest extends TestCase {
17+
public function testFetch() {
18+
$fakeStatus = null;
819

20+
$http = new Http(
21+
[],
22+
0.01,
23+
TestCurl::class,
24+
TestCurlMulti::class
25+
);
26+
$http->fetch("test://should-return")
27+
->then(function(BodyResponse $response)use(&$fakeStatus) {
28+
$fakeStatus = $response->status;
29+
});
30+
31+
$http->wait();
32+
self::assertEquals(999, $fakeStatus);
33+
}
934
}

0 commit comments

Comments
 (0)