Skip to content

Commit 4344466

Browse files
committed
Add unit tests for waitRequest method
1 parent fadfc23 commit 4344466

File tree

4 files changed

+244
-6
lines changed

4 files changed

+244
-6
lines changed

src/HttpWorker.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ private function requestFromProto(string $body, RequestProto $message): Request
193193
uploads: $uploads,
194194
attributes: [
195195
Request::PARSED_BODY_ATTRIBUTE_NAME => $message->getParsed(),
196-
] + \iterator_to_array($message->getAttributes()),
196+
] + \array_map(
197+
static fn(array $values) => \array_shift($values),
198+
$this->headerValueToArray($message->getAttributes()),
199+
),
197200
query: $query,
198201
body: $body,
199202
parsed: $message->getParsed(),

tests/Unit/HttpWorkerTest.php

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunner\Tests\Http\Unit;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\TestCase;
9+
use RoadRunner\HTTP\DTO\V1\HeaderValue;
10+
use Spiral\RoadRunner\Http\HttpWorker;
11+
use Spiral\RoadRunner\Http\Request;
12+
use Spiral\RoadRunner\Payload;
13+
use Spiral\RoadRunner\WorkerInterface;
14+
15+
final class HttpWorkerTest extends TestCase
16+
{
17+
private const REQUIRED_PAYLOAD_DATA = [
18+
'rawQuery' => 'first=value&arr[]=foo+bar&arr[]=baz',
19+
'remoteAddr' => '127.0.0.1',
20+
'protocol' => 'HTTP/1.1',
21+
'method' => 'GET',
22+
'uri' => 'http://localhost',
23+
'parsed' => false,
24+
];
25+
26+
private const REQUIRED_REQUEST_DATA = [
27+
'remoteAddr' => '127.0.0.1',
28+
'protocol' => 'HTTP/1.1',
29+
'method' => 'GET',
30+
'uri' => 'http://localhost',
31+
'attributes' => [Request::PARSED_BODY_ATTRIBUTE_NAME => false],
32+
'query' => ['first' => 'value', 'arr' => ['foo bar', 'baz']],
33+
'parsed' => false
34+
];
35+
36+
#[DataProvider('requestDataProvider')]
37+
public function testWaitRequestFromArray(array $header, array $expected): void
38+
{
39+
$worker = $this->createMock(WorkerInterface::class);
40+
$worker->expects($this->once())
41+
->method('waitPayload')
42+
->willReturn(new Payload(null, \json_encode($header)));
43+
44+
$worker = new HttpWorker($worker);
45+
46+
$this->assertEquals(new Request(...$expected), $worker->waitRequest());
47+
}
48+
49+
#[DataProvider('requestDataProvider')]
50+
public function testWaitRequestFromProto(array $header, array $expected): void
51+
{
52+
$request = self::createProtoRequest($header);
53+
54+
$worker = $this->createMock(WorkerInterface::class);
55+
$worker->expects($this->once())
56+
->method('waitPayload')
57+
->willReturn(new Payload(null, $request->serializeToString()));
58+
59+
$worker = new HttpWorker($worker);
60+
61+
$this->assertEquals(new Request(...$expected), $worker->waitRequest());
62+
}
63+
64+
#[DataProvider('emptyRequestDataProvider')]
65+
public function testWaitRequestWithEmptyData(?Payload $payload): void
66+
{
67+
$worker = $this->createMock(WorkerInterface::class);
68+
$worker->expects($this->once())
69+
->method('waitPayload')
70+
->willReturn($payload);
71+
72+
$worker = new HttpWorker($worker);
73+
74+
$this->assertEquals(null, $worker->waitRequest());
75+
}
76+
77+
public static function requestDataProvider(): \Traversable
78+
{
79+
yield [self::REQUIRED_PAYLOAD_DATA, self::REQUIRED_REQUEST_DATA];
80+
yield [
81+
\array_merge(self::REQUIRED_PAYLOAD_DATA, ['parsed' => true]),
82+
\array_merge(
83+
self::REQUIRED_REQUEST_DATA,
84+
['parsed' => true, 'attributes' => [Request::PARSED_BODY_ATTRIBUTE_NAME => true]]
85+
)
86+
];
87+
yield [
88+
\array_merge(self::REQUIRED_PAYLOAD_DATA, [
89+
'headers' => [
90+
'Content-Type' => ['application/x-www-form-urlencoded'],
91+
111 => ['invalid-non-string-key'],
92+
'' => ['invalid-empty-string-key'],
93+
],
94+
]),
95+
\array_merge(self::REQUIRED_REQUEST_DATA, [
96+
'headers' => ['Content-Type' => ['application/x-www-form-urlencoded']],
97+
])
98+
];
99+
yield [
100+
\array_merge(self::REQUIRED_PAYLOAD_DATA, [
101+
'cookies' => [
102+
'theme' => 'light',
103+
],
104+
]),
105+
\array_merge(self::REQUIRED_REQUEST_DATA, [
106+
'cookies' => ['theme' => 'light'],
107+
])
108+
];
109+
yield [
110+
\array_merge(self::REQUIRED_PAYLOAD_DATA, [
111+
'uploads' => [
112+
'single-file' => [
113+
'name' => 'test.png',
114+
'mime' => 'image/png',
115+
'size' => 123,
116+
'error' => 0,
117+
'tmpName' => '/tmp/php/php1h4j1o',
118+
],
119+
'multiple' => [
120+
[
121+
'name' => 'test.png',
122+
'mime' => 'image/png',
123+
'size' => 123,
124+
'error' => 0,
125+
'tmpName' => '/tmp/php/php1h4j1o',
126+
],
127+
[
128+
'name' => 'test2.jpg',
129+
'mime' => 'image/jpeg',
130+
'size' => 1235,
131+
'error' => 0,
132+
'tmpName' => '/tmp/php/php2h4j1o',
133+
]
134+
],
135+
'nested' => [
136+
'some-key' => [
137+
'name' => 'test.png',
138+
'mime' => 'image/png',
139+
'size' => 123,
140+
'error' => 0,
141+
'tmpName' => '/tmp/php/php1h4j1o',
142+
],
143+
]
144+
],
145+
]),
146+
\array_merge(self::REQUIRED_REQUEST_DATA, [
147+
'uploads' => [
148+
'single-file' => [
149+
'name' => 'test.png',
150+
'mime' => 'image/png',
151+
'size' => 123,
152+
'error' => 0,
153+
'tmpName' => '/tmp/php/php1h4j1o',
154+
],
155+
'multiple' => [
156+
[
157+
'name' => 'test.png',
158+
'mime' => 'image/png',
159+
'size' => 123,
160+
'error' => 0,
161+
'tmpName' => '/tmp/php/php1h4j1o',
162+
],
163+
[
164+
'name' => 'test2.jpg',
165+
'mime' => 'image/jpeg',
166+
'size' => 1235,
167+
'error' => 0,
168+
'tmpName' => '/tmp/php/php2h4j1o',
169+
]
170+
],
171+
'nested' => [
172+
'some-key' => [
173+
'name' => 'test.png',
174+
'mime' => 'image/png',
175+
'size' => 123,
176+
'error' => 0,
177+
'tmpName' => '/tmp/php/php1h4j1o',
178+
],
179+
]
180+
],
181+
])
182+
];
183+
yield [
184+
\array_merge(self::REQUIRED_PAYLOAD_DATA, [
185+
'attributes' => [
186+
'foo' => 'bar',
187+
],
188+
]),
189+
\array_merge(self::REQUIRED_REQUEST_DATA, [
190+
'attributes' => [
191+
Request::PARSED_BODY_ATTRIBUTE_NAME => false,
192+
'foo' => 'bar'
193+
],
194+
])
195+
];
196+
}
197+
198+
public static function emptyRequestDataProvider(): \Traversable
199+
{
200+
yield [null];
201+
yield [new Payload(null, null)];
202+
}
203+
204+
private static function createProtoRequest(array $values): \RoadRunner\HTTP\DTO\V1\Request
205+
{
206+
$toHeaderValue = static function (string $key, bool $wrap = true) use (&$values): void {
207+
if (isset($values[$key])) {
208+
foreach ($values[$key] as $valueKey => $value) {
209+
$values[$key][$valueKey] = new HeaderValue(['value' => $wrap ? [$value] : $value]);
210+
}
211+
}
212+
};
213+
214+
$toHeaderValue('headers', false);
215+
$toHeaderValue('attributes');
216+
$toHeaderValue('cookies');
217+
218+
return new \RoadRunner\HTTP\DTO\V1\Request([
219+
'remote_addr' => $values['remoteAddr'],
220+
'protocol' => $values['protocol'],
221+
'method' => $values['method'],
222+
'uri' => $values['uri'],
223+
'header' => $values['headers'] ?? [],
224+
'cookies' => $values['cookies'] ?? [],
225+
'raw_query' => $values['rawQuery'],
226+
'parsed' => $values['parsed'],
227+
'uploads' => \json_encode($values['uploads'] ?? []),
228+
'attributes' => $values['attributes'] ?? [],
229+
]);
230+
}
231+
232+
protected function tearDown(): void
233+
{
234+
(new \ReflectionProperty(HttpWorker::class, 'codec'))->setValue(null);
235+
}
236+
}

tests/Unit/PSR7WorkerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Spiral\RoadRunner\Tests\Http\Unit;
46

57
use Nyholm\Psr7\Factory\Psr17Factory;
@@ -8,7 +10,7 @@
810
use Spiral\RoadRunner\Http\PSR7Worker;
911
use Spiral\RoadRunner\Worker;
1012

11-
class PSR7WorkerTest extends TestCase
13+
final class PSR7WorkerTest extends TestCase
1214
{
1315
public function testHttpWorkerIsAvailable(): void
1416
{
@@ -31,7 +33,5 @@ protected function tearDown(): void
3133
while (--$level > 0) {
3234
\ob_end_clean();
3335
}
34-
35-
parent::tearDown(); // TODO: Change the autogenerated stub
3636
}
3737
}

tests/Unit/StreamResponseTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
use Spiral\RoadRunner\Tests\Http\Unit\Stub\TestRelay;
1111
use Spiral\RoadRunner\Worker;
1212

13-
class StreamResponseTest extends TestCase
13+
final class StreamResponseTest extends TestCase
1414
{
1515
private TestRelay $relay;
1616
private Worker $worker;
1717

1818
protected function tearDown(): void
1919
{
2020
unset($this->relay, $this->worker);
21-
parent::tearDown();
2221
}
2322

2423
/**

0 commit comments

Comments
 (0)