|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Frederic G. Østby |
| 5 | + * @license http://www.makoframework.com/license |
| 6 | + */ |
| 7 | + |
| 8 | +namespace mako\tests\unit\http\response\senders; |
| 9 | + |
| 10 | +use JsonSerializable; |
| 11 | +use mako\http\Request; |
| 12 | +use mako\http\Response; |
| 13 | +use mako\http\response\Headers; |
| 14 | +use mako\http\response\senders\EventStream; |
| 15 | +use mako\http\response\senders\stream\event\Event; |
| 16 | +use mako\http\response\senders\stream\event\Field; |
| 17 | +use mako\http\response\senders\stream\event\Type; |
| 18 | +use mako\tests\TestCase; |
| 19 | +use Mockery; |
| 20 | +use Mockery\MockInterface; |
| 21 | +use PHPUnit\Framework\Attributes\Group; |
| 22 | +use Stringable; |
| 23 | + |
| 24 | +#[Group('unit')] |
| 25 | +class EventStreamTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * |
| 29 | + */ |
| 30 | + protected function getRequest(): MockInterface&Request |
| 31 | + { |
| 32 | + return Mockery::mock(Request::class); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * |
| 37 | + */ |
| 38 | + protected function getResponse(): MockInterface&Response |
| 39 | + { |
| 40 | + $headers = Mockery::mock(Headers::class); |
| 41 | + |
| 42 | + $headers->shouldReceive('add')->once()->with('Connection', 'keep-alive'); |
| 43 | + $headers->shouldReceive('add')->once()->with('Cache-Control', 'no-cache'); |
| 44 | + $headers->shouldReceive('add')->once()->with('X-Accel-Buffering', 'no'); |
| 45 | + |
| 46 | + $response = Mockery::mock(Response::class); |
| 47 | + |
| 48 | + $response->shouldReceive('setType')->once()->with('text/event-stream', 'UTF-8'); |
| 49 | + $response->shouldReceive('sendHeaders')->once(); |
| 50 | + |
| 51 | + (function () use ($headers): void { |
| 52 | + $this->headers = $headers; |
| 53 | + })->bindTo($response, Response::class)(); |
| 54 | + |
| 55 | + return $response; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * |
| 60 | + */ |
| 61 | + public function testBasicEventStream(): void |
| 62 | + { |
| 63 | + $eventStream = Mockery::mock(EventStream::class, [function () { |
| 64 | + yield new Event( |
| 65 | + new Field(Type::DATA, 'hello, world!') |
| 66 | + ); |
| 67 | + }]); |
| 68 | + |
| 69 | + $eventStream->makePartial()->shouldAllowMockingProtectedMethods(); |
| 70 | + |
| 71 | + $eventStream->shouldReceive('eraseAndDisableOutputBuffers')->once(); |
| 72 | + |
| 73 | + $eventStream->shouldReceive('sendEvent')->once()->with("data: hello, world!\n\n"); |
| 74 | + |
| 75 | + $eventStream->send($this->getRequest(), $this->getResponse()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * |
| 80 | + */ |
| 81 | + public function testEventStreamWithMultipleFields(): void |
| 82 | + { |
| 83 | + $eventStream = Mockery::mock(EventStream::class, [function () { |
| 84 | + yield new Event( |
| 85 | + new Field(Type::EVENT, 'greeting'), |
| 86 | + new Field(Type::DATA, 'hello, world!') |
| 87 | + ); |
| 88 | + }]); |
| 89 | + |
| 90 | + $eventStream->makePartial()->shouldAllowMockingProtectedMethods(); |
| 91 | + |
| 92 | + $eventStream->shouldReceive('eraseAndDisableOutputBuffers')->once(); |
| 93 | + |
| 94 | + $eventStream->shouldReceive('sendEvent')->once()->with("event: greeting\ndata: hello, world!\n\n"); |
| 95 | + |
| 96 | + $eventStream->send($this->getRequest(), $this->getResponse()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * |
| 101 | + */ |
| 102 | + public function testEventStreamWithMultipleEvents(): void |
| 103 | + { |
| 104 | + $eventStream = Mockery::mock(EventStream::class, [function () { |
| 105 | + yield new Event( |
| 106 | + new Field(Type::EVENT, 'greeting'), |
| 107 | + new Field(Type::DATA, 'first hello') |
| 108 | + ); |
| 109 | + yield new Event( |
| 110 | + new Field(Type::EVENT, 'greeting'), |
| 111 | + new Field(Type::DATA, 'second hello') |
| 112 | + ); |
| 113 | + }]); |
| 114 | + |
| 115 | + $eventStream->makePartial()->shouldAllowMockingProtectedMethods(); |
| 116 | + |
| 117 | + $eventStream->shouldReceive('eraseAndDisableOutputBuffers')->once(); |
| 118 | + |
| 119 | + $eventStream->shouldReceive('sendEvent')->once()->with("event: greeting\ndata: first hello\n\n"); |
| 120 | + $eventStream->shouldReceive('sendEvent')->once()->with("event: greeting\ndata: second hello\n\n"); |
| 121 | + |
| 122 | + $eventStream->send($this->getRequest(), $this->getResponse()); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * |
| 127 | + */ |
| 128 | + public function testEventStreamWithStringable(): void |
| 129 | + { |
| 130 | + $eventStream = Mockery::mock(EventStream::class, [function () { |
| 131 | + yield new Event( |
| 132 | + new Field(Type::DATA, new class implements Stringable { |
| 133 | + public function __toString(): string |
| 134 | + { |
| 135 | + return 'this is a string'; |
| 136 | + } |
| 137 | + }) |
| 138 | + ); |
| 139 | + }]); |
| 140 | + |
| 141 | + $eventStream->makePartial()->shouldAllowMockingProtectedMethods(); |
| 142 | + |
| 143 | + $eventStream->shouldReceive('eraseAndDisableOutputBuffers')->once(); |
| 144 | + |
| 145 | + $eventStream->shouldReceive('sendEvent')->once()->with("data: this is a string\n\n"); |
| 146 | + |
| 147 | + $eventStream->send($this->getRequest(), $this->getResponse()); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * |
| 152 | + */ |
| 153 | + public function testEventStreamWithJsonSerializable(): void |
| 154 | + { |
| 155 | + $eventStream = Mockery::mock(EventStream::class, [function () { |
| 156 | + yield new Event( |
| 157 | + new Field(Type::DATA, new class implements JsonSerializable { |
| 158 | + public function jsonSerialize(): mixed |
| 159 | + { |
| 160 | + return [1, 2, 3]; |
| 161 | + } |
| 162 | + }) |
| 163 | + ); |
| 164 | + }]); |
| 165 | + |
| 166 | + $eventStream->makePartial()->shouldAllowMockingProtectedMethods(); |
| 167 | + |
| 168 | + $eventStream->shouldReceive('eraseAndDisableOutputBuffers')->once(); |
| 169 | + |
| 170 | + $eventStream->shouldReceive('sendEvent')->once()->with("data: [1,2,3]\n\n"); |
| 171 | + |
| 172 | + $eventStream->send($this->getRequest(), $this->getResponse()); |
| 173 | + } |
| 174 | +} |
0 commit comments