SocketWrapperTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace React\ZMQ;
  3. class SocketWrapperTest extends \PHPUnit_Framework_TestCase
  4. {
  5. /** @test */
  6. public function itShouldWrapARealZMQSocket()
  7. {
  8. $loop = $this->getMock('React\EventLoop\LoopInterface');
  9. $socket = $this->getMockBuilder('ZMQSocket')->disableOriginalConstructor()->getMock();
  10. $socket
  11. ->expects($this->once())
  12. ->method('connect')
  13. ->with('tcp://127.0.0.1:5555');
  14. $wrapped = new SocketWrapper($socket, $loop);
  15. $wrapped->connect('tcp://127.0.0.1:5555');
  16. }
  17. /** @test */
  18. public function subscribeShouldSetSocketOption()
  19. {
  20. $loop = $this->getMock('React\EventLoop\LoopInterface');
  21. $socket = $this->getMockBuilder('ZMQSocket')->disableOriginalConstructor()->getMock();
  22. $socket
  23. ->expects($this->once())
  24. ->method('setSockOpt')
  25. ->with(\ZMQ::SOCKOPT_SUBSCRIBE, 'foo');
  26. $wrapped = new SocketWrapper($socket, $loop);
  27. $wrapped->subscribe('foo');
  28. }
  29. /** @test */
  30. public function unsubscribeShouldSetSocketOption()
  31. {
  32. $loop = $this->getMock('React\EventLoop\LoopInterface');
  33. $socket = $this->getMockBuilder('ZMQSocket')->disableOriginalConstructor()->getMock();
  34. $socket
  35. ->expects($this->once())
  36. ->method('setSockOpt')
  37. ->with(\ZMQ::SOCKOPT_UNSUBSCRIBE, 'foo');
  38. $wrapped = new SocketWrapper($socket, $loop);
  39. $wrapped->unsubscribe('foo');
  40. }
  41. /** @test */
  42. public function sendShouldBufferMessages()
  43. {
  44. $loop = $this->getMock('React\EventLoop\LoopInterface');
  45. $loop
  46. ->expects($this->once())
  47. ->method('addWriteStream')
  48. ->with(14);
  49. $socket = $this->getMockBuilder('ZMQSocket')->disableOriginalConstructor()->getMock();
  50. $socket
  51. ->expects($this->any())
  52. ->method('getSockOpt')
  53. ->with(\ZMQ::SOCKOPT_FD)
  54. ->will($this->returnValue(14));
  55. $wrapped = new SocketWrapper($socket, $loop);
  56. $wrapped->send('foobar');
  57. }
  58. /** @test */
  59. public function closeShouldStopListening()
  60. {
  61. $loop = $this->getMock('React\EventLoop\LoopInterface');
  62. $loop
  63. ->expects($this->once())
  64. ->method('removeStream')
  65. ->with(14);
  66. $socket = $this->getMockBuilder('ZMQSocket')->disableOriginalConstructor()->getMock();
  67. $socket
  68. ->expects($this->any())
  69. ->method('getSockOpt')
  70. ->with(\ZMQ::SOCKOPT_FD)
  71. ->will($this->returnValue(14));
  72. $wrapped = new SocketWrapper($socket, $loop);
  73. $wrapped->close();
  74. }
  75. }