IoServerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Ratchet\Server;
  3. use Ratchet\Server\IoServer;
  4. use React\EventLoop\StreamSelectLoop;
  5. use React\EventLoop\LoopInterface;
  6. use React\Socket\Server;
  7. /**
  8. * @covers Ratchet\Server\IoServer
  9. */
  10. class IoServerTest extends \PHPUnit_Framework_TestCase {
  11. protected $server;
  12. protected $app;
  13. protected $port;
  14. protected $reactor;
  15. protected function tickLoop(LoopInterface $loop) {
  16. $loop->futureTick(function () use ($loop) {
  17. $loop->stop();
  18. });
  19. $loop->run();
  20. }
  21. public function setUp() {
  22. $this->app = $this->getMock('\\Ratchet\\MessageComponentInterface');
  23. $loop = new StreamSelectLoop;
  24. $this->reactor = new Server(0, $loop);
  25. $uri = $this->reactor->getAddress();
  26. $this->port = parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_PORT);
  27. $this->server = new IoServer($this->app, $this->reactor, $loop);
  28. }
  29. public function testOnOpen() {
  30. $this->app->expects($this->once())->method('onOpen')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'));
  31. $client = stream_socket_client("tcp://localhost:{$this->port}");
  32. $this->tickLoop($this->server->loop);
  33. //$this->assertTrue(is_string($this->app->last['onOpen'][0]->remoteAddress));
  34. //$this->assertTrue(is_int($this->app->last['onOpen'][0]->resourceId));
  35. }
  36. public function testOnData() {
  37. $msg = 'Hello World!';
  38. $this->app->expects($this->once())->method('onMessage')->with(
  39. $this->isInstanceOf('\\Ratchet\\ConnectionInterface')
  40. , $msg
  41. );
  42. $client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  43. socket_set_option($client, SOL_SOCKET, SO_REUSEADDR, 1);
  44. socket_set_option($client, SOL_SOCKET, SO_SNDBUF, 4096);
  45. socket_set_block($client);
  46. socket_connect($client, 'localhost', $this->port);
  47. $this->tickLoop($this->server->loop);
  48. socket_write($client, $msg);
  49. $this->tickLoop($this->server->loop);
  50. socket_shutdown($client, 1);
  51. socket_shutdown($client, 0);
  52. socket_close($client);
  53. $this->tickLoop($this->server->loop);
  54. }
  55. public function testOnClose() {
  56. $this->app->expects($this->once())->method('onClose')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'));
  57. $client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  58. socket_set_option($client, SOL_SOCKET, SO_REUSEADDR, 1);
  59. socket_set_option($client, SOL_SOCKET, SO_SNDBUF, 4096);
  60. socket_set_block($client);
  61. socket_connect($client, 'localhost', $this->port);
  62. $this->tickLoop($this->server->loop);
  63. socket_shutdown($client, 1);
  64. socket_shutdown($client, 0);
  65. socket_close($client);
  66. $this->tickLoop($this->server->loop);
  67. }
  68. public function testFactory() {
  69. $this->assertInstanceOf('\\Ratchet\\Server\\IoServer', IoServer::factory($this->app, 0));
  70. }
  71. public function testNoLoopProvidedError() {
  72. $this->setExpectedException('RuntimeException');
  73. $io = new IoServer($this->app, $this->reactor);
  74. $io->run();
  75. }
  76. public function testOnErrorPassesException() {
  77. $conn = $this->getMock('\\React\\Socket\\ConnectionInterface');
  78. $conn->decor = $this->getMock('\\Ratchet\\ConnectionInterface');
  79. $err = new \Exception("Nope");
  80. $this->app->expects($this->once())->method('onError')->with($conn->decor, $err);
  81. $this->server->handleError($err, $conn);
  82. }
  83. public function onErrorCalledWhenExceptionThrown() {
  84. $this->markTestIncomplete("Need to learn how to throw an exception from a mock");
  85. $conn = $this->getMock('\\React\\Socket\\ConnectionInterface');
  86. $this->server->handleConnect($conn);
  87. $e = new \Exception;
  88. $this->app->expects($this->once())->method('onMessage')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'), 'f')->will($e);
  89. $this->app->expects($this->once())->method('onError')->with($this->instanceOf('\\Ratchet\\ConnectionInterface', $e));
  90. $this->server->handleData('f', $conn);
  91. }
  92. }