|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | use React\Datagram\Socket; |
| 4 | +use React\Datagram\Factory; |
4 | 5 | use Clue\React\Block; |
| 6 | +use React\Promise; |
5 | 7 |
|
6 | 8 | class FactoryTest extends TestCase |
7 | 9 | { |
| 10 | + private $loop; |
| 11 | + private $resolver; |
8 | 12 | private $factory; |
9 | 13 |
|
10 | 14 | public function setUp() |
11 | 15 | { |
12 | 16 | $this->loop = React\EventLoop\Factory::create(); |
13 | | - $this->factory = new React\Datagram\Factory($this->loop, $this->createResolverMock()); |
| 17 | + $this->resolver = $this->createResolverMock(); |
| 18 | + $this->factory = new Factory($this->loop, $this->resolver); |
14 | 19 | } |
15 | 20 |
|
16 | 21 | public function testCreateClient() |
@@ -64,4 +69,54 @@ public function testCreateServerRandomPort() |
64 | 69 |
|
65 | 70 | $capturedServer->close(); |
66 | 71 | } |
| 72 | + |
| 73 | + public function testCreateClientWithIpWillNotUseResolver() |
| 74 | + { |
| 75 | + $this->resolver->expects($this->never())->method('resolve'); |
| 76 | + |
| 77 | + $client = Block\await($this->factory->createClient('127.0.0.1:0'), $this->loop); |
| 78 | + $client->close(); |
| 79 | + } |
| 80 | + |
| 81 | + public function testCreateClientWithHostnameWillUseResolver() |
| 82 | + { |
| 83 | + $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\resolve('127.0.0.1')); |
| 84 | + |
| 85 | + $client = Block\await($this->factory->createClient('example.com:0'), $this->loop); |
| 86 | + $client->close(); |
| 87 | + } |
| 88 | + |
| 89 | + public function testCreateClientWithHostnameWillRejectIfResolverRejects() |
| 90 | + { |
| 91 | + $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\reject(new \RuntimeException('test'))); |
| 92 | + |
| 93 | + $this->setExpectedException('RuntimeException'); |
| 94 | + Block\await($this->factory->createClient('example.com:0'), $this->loop); |
| 95 | + } |
| 96 | + |
| 97 | + public function testCreateClientWithHostnameWillRejectIfNoResolverIsGiven() |
| 98 | + { |
| 99 | + $this->factory = new Factory($this->loop); |
| 100 | + |
| 101 | + $this->setExpectedException('Exception'); |
| 102 | + Block\await($this->factory->createClient('example.com:0'), $this->loop); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @expectedException Exception |
| 107 | + * @expectedExceptionMessage Unable to create client socket |
| 108 | + */ |
| 109 | + public function testCreateClientWithInvalidHostnameWillReject() |
| 110 | + { |
| 111 | + Block\await($this->factory->createClient('/////'), $this->loop); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @expectedException Exception |
| 116 | + * @expectedExceptionMessage Unable to create server socket |
| 117 | + */ |
| 118 | + public function testCreateServerWithInvalidHostnameWillReject() |
| 119 | + { |
| 120 | + Block\await($this->factory->createServer('/////'), $this->loop); |
| 121 | + } |
67 | 122 | } |
0 commit comments