1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Spiral \RoadRunner \Tests \Worker \Unit ;
6+
7+ use PHPUnit \Framework \TestCase ;
8+ use Spiral \Goridge \Frame ;
9+ use Spiral \RoadRunner \Exception \RoadRunnerException ;
10+ use Spiral \RoadRunner \Message \Command \GetProcessId ;
11+ use Spiral \RoadRunner \Message \Command \Pong ;
12+ use Spiral \RoadRunner \Message \Command \StreamStop ;
13+ use Spiral \RoadRunner \Message \Command \WorkerStop ;
14+ use Spiral \RoadRunner \PayloadFactory ;
15+
16+ final class PayloadFactoryTest extends TestCase
17+ {
18+ public function testFromFrameWithStopFlag (): void
19+ {
20+ $ frame = new Frame ("{} " , []);
21+ $ frame ->byte10 = Frame::BYTE10_STOP ;
22+ $ payload = PayloadFactory::fromFrame ($ frame );
23+
24+ $ this ->assertInstanceOf (StreamStop::class, $ payload );
25+ }
26+
27+ public function testFromFrameWithPongFlag (): void
28+ {
29+ $ frame = new Frame ("{} " , []);
30+ $ frame ->byte10 = Frame::BYTE10_PONG ;
31+ $ payload = PayloadFactory::fromFrame ($ frame );
32+
33+ $ this ->assertInstanceOf (Pong::class, $ payload );
34+ }
35+
36+ public function testFromFrameWithoutSpecificFlags (): void
37+ {
38+ $ frame = new Frame ("test " , [0 ]);
39+ $ payload = PayloadFactory::fromFrame ($ frame );
40+
41+ $ this ->assertNotNull ($ payload );
42+ $ this ->assertSame ("test " , $ payload ->body );
43+ $ this ->assertSame ("" , $ payload ->header );
44+ }
45+
46+ public function testMakeControlWithWorkerStop (): void
47+ {
48+ $ json = \json_encode (['stop ' => true ]);
49+ $ frame = new Frame ($ json );
50+ $ frame ->setFlag (Frame::CONTROL );
51+
52+ $ payload = PayloadFactory::fromFrame ($ frame );
53+ $ this ->assertInstanceOf (WorkerStop::class, $ payload );
54+ }
55+
56+ public function testMakeControlWithGetProcessId (): void
57+ {
58+ $ json = \json_encode (['pid ' => true ]);
59+ $ frame = new Frame ($ json );
60+ $ frame ->setFlag (Frame::CONTROL );
61+
62+ $ payload = PayloadFactory::fromFrame ($ frame );
63+ $ this ->assertInstanceOf (GetProcessId::class, $ payload );
64+ }
65+
66+ public function testFromFrameWithControlFlag (): void
67+ {
68+ $ frame = new Frame (null , [], Frame::CONTROL );
69+
70+ $ this ->expectException (RoadRunnerException::class);
71+ $ this ->expectExceptionMessage ('Invalid task header, JSON payload is expected: Syntax error ' );
72+ PayloadFactory::fromFrame ($ frame );
73+ }
74+
75+ public function testMakeControlWithException (): void
76+ {
77+ $ this ->expectException (RoadRunnerException::class);
78+ $ this ->expectExceptionMessage ('Invalid task header, undefined control package ' );
79+ $ json = json_encode ([]);
80+ $ frame = new Frame ($ json );
81+ $ frame ->setFlag (Frame::CONTROL );
82+
83+ PayloadFactory::fromFrame ($ frame );
84+ }
85+
86+ public function testMakePayload (): void
87+ {
88+ $ this ->markTestIncomplete ('Not implemented yet. ' );
89+ }
90+ }
0 commit comments