1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Spiral \RoadRunner \Tests \Http \Unit ;
6+
7+ use PHPUnit \Framework \TestCase ;
8+ use Spiral \RoadRunner \Http \HttpWorker ;
9+ use Spiral \RoadRunner \Payload ;
10+ use Spiral \RoadRunner \Tests \Http \Unit \Stub \TestRelay ;
11+ use Spiral \RoadRunner \Worker ;
12+
13+ class StreamResponseTest extends TestCase
14+ {
15+ private TestRelay $ relay ;
16+ private Worker $ worker ;
17+
18+ protected function tearDown (): void
19+ {
20+ unset($ this ->relay , $ this ->worker );
21+ parent ::tearDown ();
22+ }
23+
24+ /**
25+ * Regular case
26+ */
27+ public function testRegularCase (): void
28+ {
29+ $ worker = $ this ->getWorker ();
30+ $ this ->getRelay ()
31+ ->addFrame (status: 200 , body: 'Hello, World! ' , headers: ['Content-Type ' => 'text/plain ' ], stream: true );
32+
33+ self ::assertTrue ($ worker ->hasPayload ());
34+ self ::assertInstanceOf (Payload::class, $ payload = $ worker ->waitPayload ());
35+ self ::assertSame ('Hello, World! ' , $ payload ->body );
36+ }
37+
38+ /**
39+ * Test stream response with multiple frames
40+ */
41+ public function testStreamResponseWithMultipleFrames (): void
42+ {
43+ $ httpWorker = $ this ->makeHttpWorker ();
44+
45+ $ httpWorker ->respondStream (200 , (function () {
46+ yield 'Hel ' ;
47+ yield 'lo, ' ;
48+ yield ' Wo ' ;
49+ yield 'rld ' ;
50+ yield '! ' ;
51+ })());
52+
53+ self ::assertFalse ($ this ->worker ->hasPayload ());
54+ self ::assertSame ('Hello, World! ' , $ this ->getRelay ()->getReceivedBody ());
55+ }
56+
57+ public function testStopStreamResponse (): void
58+ {
59+ $ httpWorker = $ this ->makeHttpWorker ();
60+
61+ $ httpWorker ->respondStream (200 , (function () {
62+ yield 'Hel ' ;
63+ yield 'lo, ' ;
64+ $ this ->getRelay ()->addStopStreamFrame ();
65+ try {
66+ yield ' Wo ' ;
67+ } catch (\Throwable $ e ) {
68+ return ;
69+ }
70+ yield 'rld ' ;
71+ yield '! ' ;
72+ })());
73+
74+ self ::assertSame ('Hello, ' , $ this ->getRelay ()->getReceivedBody ());
75+ }
76+
77+ private function getRelay (): TestRelay
78+ {
79+ return $ this ->relay ??= new TestRelay ();
80+ }
81+
82+ private function getWorker (): Worker
83+ {
84+ return $ this ->worker ??= new Worker ($ this ->getRelay (), false );
85+ }
86+
87+ private function makeHttpWorker (): HttpWorker
88+ {
89+ return new HttpWorker ($ this ->getWorker ());
90+ }
91+ }
0 commit comments