|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Tests\Datagram; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use React\Datagram\Buffer; |
| 7 | + |
| 8 | +class BufferTest extends TestCase |
| 9 | +{ |
| 10 | + public function testSendAddsSocketToLoop() |
| 11 | + { |
| 12 | + $socket = stream_socket_client('udp://127.0.0.1:8000'); |
| 13 | + |
| 14 | + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); |
| 15 | + $loop->expects($this->once())->method('addWriteStream')->with($socket); |
| 16 | + |
| 17 | + $client = new Buffer($loop, $socket); |
| 18 | + |
| 19 | + $client->send('foo'); |
| 20 | + } |
| 21 | + |
| 22 | + public function testSendAfterCloseIsNoop() |
| 23 | + { |
| 24 | + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); |
| 25 | + $loop->expects($this->never())->method('addWriteStream'); |
| 26 | + |
| 27 | + $socket = stream_socket_client('udp://127.0.0.1:8000'); |
| 28 | + |
| 29 | + $client = new Buffer($loop, $socket); |
| 30 | + |
| 31 | + $client->close(); |
| 32 | + $client->send('foo'); |
| 33 | + } |
| 34 | + |
| 35 | + public function testCloseAfterSendAddsSocketToLoopRemovesSocketFromLoopAgain() |
| 36 | + { |
| 37 | + $socket = stream_socket_client('udp://127.0.0.1:8000'); |
| 38 | + |
| 39 | + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); |
| 40 | + $loop->expects($this->once())->method('addWriteStream')->with($socket); |
| 41 | + $loop->expects($this->once())->method('removeWriteStream')->with($socket); |
| 42 | + |
| 43 | + $client = new Buffer($loop, $socket); |
| 44 | + |
| 45 | + $client->send('foo'); |
| 46 | + $client->close(); |
| 47 | + } |
| 48 | + |
| 49 | + public function testCloseTwiceEmitsCloseEventOnce() |
| 50 | + { |
| 51 | + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); |
| 52 | + |
| 53 | + $socket = stream_socket_client('udp://127.0.0.1:8000'); |
| 54 | + |
| 55 | + $client = new Buffer($loop, $socket); |
| 56 | + |
| 57 | + $closed = 0; |
| 58 | + $client->on('close', function () use (&$closed) { |
| 59 | + ++$closed; |
| 60 | + }); |
| 61 | + |
| 62 | + $this->assertEquals(0, $closed); |
| 63 | + |
| 64 | + $client->close(); |
| 65 | + |
| 66 | + $this->assertEquals(1, $closed); |
| 67 | + |
| 68 | + $client->close(); |
| 69 | + |
| 70 | + $this->assertEquals(1, $closed); |
| 71 | + } |
| 72 | +} |
0 commit comments