|
| 1 | +<?php |
| 2 | + |
| 3 | +use Datagram\Socket; |
| 4 | + |
| 5 | +use React\Promise\When; |
| 6 | + |
| 7 | +use React\Promise\PromiseInterface; |
| 8 | + |
| 9 | +require __DIR__.'/../vendor/autoload.php'; |
| 10 | + |
| 11 | +class FactoryTest extends PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + private $factory; |
| 14 | + |
| 15 | + public function setUp() |
| 16 | + { |
| 17 | + $this->loop = React\EventLoop\Factory::create(); |
| 18 | + $this->factory = new Datagram\Factory($this->loop, $this->createResolverMock()); |
| 19 | + } |
| 20 | + |
| 21 | + public function testCreateClient() |
| 22 | + { |
| 23 | + $promise = $this->factory->createClient('127.0.0.1', 12345); |
| 24 | + |
| 25 | + $capturedClient = $this->getValueFromResolvedPromise($promise); |
| 26 | + $this->assertInstanceOf('Datagram\Socket', $capturedClient); |
| 27 | + |
| 28 | + $capturedClient->close(); |
| 29 | + } |
| 30 | + |
| 31 | + public function testCreateClientLocalhost() |
| 32 | + { |
| 33 | + $promise = $this->factory->createClient('localhost', 12345); |
| 34 | + |
| 35 | + $capturedClient = $this->getValueFromResolvedPromise($promise); |
| 36 | + $this->assertInstanceOf('Datagram\Socket', $capturedClient); |
| 37 | + |
| 38 | + $capturedClient->close(); |
| 39 | + } |
| 40 | + |
| 41 | + public function testCreateClientIpv6() |
| 42 | + { |
| 43 | + $promise = $this->factory->createClient('::1', 12345); |
| 44 | + |
| 45 | + $capturedClient = $this->getValueFromResolvedPromise($promise); |
| 46 | + $this->assertInstanceOf('Datagram\Socket', $capturedClient); |
| 47 | + |
| 48 | + $capturedClient->close(); |
| 49 | + } |
| 50 | + |
| 51 | + public function testCreateServer() |
| 52 | + { |
| 53 | + $promise = $this->factory->createServer(12345, '127.0.0.1'); |
| 54 | + |
| 55 | + $capturedServer = $this->getValueFromResolvedPromise($promise); |
| 56 | + $this->assertInstanceOf('Datagram\Socket', $capturedServer); |
| 57 | + |
| 58 | + $capturedServer->close(); |
| 59 | + } |
| 60 | + |
| 61 | + protected function getValueFromResolvedPromise($promise) |
| 62 | + { |
| 63 | + $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); |
| 64 | + |
| 65 | + $loop = $this->loop; |
| 66 | + $capturedValue = null; |
| 67 | + $promise->then(function ($value) use (&$capturedValue, $loop) { |
| 68 | + $capturedValue = $value; |
| 69 | + $loop->stop(); |
| 70 | + }, $this->expectCallableNever()); |
| 71 | + |
| 72 | + // future-turn resolutions are not enforced, so the value MAY be known here already |
| 73 | + if ($capturedValue === null) { |
| 74 | + $loop->run(); |
| 75 | + } |
| 76 | + |
| 77 | + return $capturedValue; |
| 78 | + } |
| 79 | + |
| 80 | + protected function expectCallableOnce() |
| 81 | + { |
| 82 | + $mock = $this->createCallableMock(); |
| 83 | + $mock |
| 84 | + ->expects($this->once()) |
| 85 | + ->method('__invoke'); |
| 86 | + |
| 87 | + return $mock; |
| 88 | + } |
| 89 | + |
| 90 | + protected function expectCallableNever() |
| 91 | + { |
| 92 | + $mock = $this->createCallableMock(); |
| 93 | + $mock |
| 94 | + ->expects($this->never()) |
| 95 | + ->method('__invoke'); |
| 96 | + |
| 97 | + return $mock; |
| 98 | + } |
| 99 | + |
| 100 | + protected function createCallableMock() |
| 101 | + { |
| 102 | + return $this->getMock('React\Tests\Socket\Stub\CallableStub'); |
| 103 | + } |
| 104 | + |
| 105 | + private function createResolverMock() |
| 106 | + { |
| 107 | + return $this->getMockBuilder('React\Dns\Resolver\Resolver') |
| 108 | + ->disableOriginalConstructor() |
| 109 | + ->getMock(); |
| 110 | + } |
| 111 | +} |
0 commit comments