WebsocketServiceTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. use Symfony\Component\Process\PhpProcess;
  3. $serverCode = file_get_contents(__DIR__ . '/Stub/WebsocketServer.php');
  4. $clientCode = file_get_contents(__DIR__ . '/Stub/WebsocketClient.php');
  5. it('tests websocket connection', function () use ($serverCode, $clientCode) {
  6. $serverProcess = new PhpProcess(str_replace(subject: $serverCode, search: '//%action%', replace: <<<PHP
  7. \$worker->onWebSocketConnect = function () {
  8. echo "connected";
  9. };
  10. \$worker->onMessage = function () {};
  11. PHP));
  12. $serverProcess->start();
  13. usleep(250000);
  14. $clientProcess = new PhpProcess(str_replace(subject: $clientCode, search: '//%action%', replace: <<<PHP
  15. \$con->onWebSocketConnect = function(AsyncTcpConnection \$con) {
  16. \$con->send('connect');
  17. };
  18. PHP));
  19. $clientProcess->start();
  20. usleep(250000);
  21. expect(getNonFrameOutput($serverProcess->getOutput()))->toBe('connected')
  22. ->and(getNonFrameOutput($clientProcess->getOutput()))->toBe('');
  23. $serverProcess->stop();
  24. $clientProcess->stop();
  25. });
  26. it('tests server and client sending and receiving messages', function () use ($serverCode, $clientCode) {
  27. $serverProcess = new PhpProcess(str_replace(subject: $serverCode, search: '//%action%', replace: <<<PHP
  28. \$worker->onMessage = function (TcpConnection \$connection, \$data) {
  29. echo \$data;
  30. \$connection->send('Hi');
  31. };
  32. PHP));
  33. $serverProcess->start();
  34. usleep(250000);
  35. $clientProcess = new PhpProcess(str_replace(subject: $clientCode, search: '//%action%', replace: <<<PHP
  36. \$con->onWebSocketConnect = function(AsyncTcpConnection \$con) {
  37. \$con->send('Hello Chance');
  38. };
  39. \$con->onMessage = function(\$con, \$data) {
  40. echo \$data;
  41. };
  42. PHP));
  43. $clientProcess->start();
  44. usleep(250000);
  45. expect(getNonFrameOutput($serverProcess->getOutput()))->toBe('Hello Chance')
  46. ->and(getNonFrameOutput($clientProcess->getOutput()))->toBe('Hi');
  47. $serverProcess->stop();
  48. $clientProcess->stop();
  49. });
  50. it('tests server close connection', function () use ($serverCode, $clientCode) {
  51. $serverProcess = new PhpProcess(str_replace(subject: $serverCode, search: '//%action%', replace: <<<PHP
  52. \$worker->onWebSocketConnect = function (TcpConnection \$connection) {
  53. echo 'close connection';
  54. \$connection->close();
  55. };
  56. \$worker->onMessage = function () {};
  57. PHP));
  58. $serverProcess->start();
  59. usleep(250000);
  60. $clientProcess = new PhpProcess(str_replace(subject: $clientCode, search: '//%action%', replace: <<<PHP
  61. \$con->onWebSocketConnect = function(AsyncTcpConnection \$con) {
  62. \$con->send('connect');
  63. };
  64. \$con->onClose = function () {
  65. echo 'closed';
  66. };
  67. PHP));
  68. $clientProcess->start();
  69. usleep(250000);
  70. expect(getNonFrameOutput($serverProcess->getOutput()))->toBe('close connection')
  71. ->and(getNonFrameOutput($clientProcess->getOutput()))->toBe('closed');
  72. $serverProcess->stop();
  73. $clientProcess->stop();
  74. });
  75. it('tests client close connection', function () use ($serverCode, $clientCode) {
  76. $serverProcess = new PhpProcess(str_replace(subject: $serverCode, search: '//%action%', replace: <<<PHP
  77. \$worker->onMessage = function () {};
  78. \$worker->onClose = function () {
  79. echo 'closed';
  80. };
  81. PHP));
  82. $serverProcess->start();
  83. usleep(250000);
  84. $clientProcess = new PhpProcess(str_replace(subject: $clientCode, search: '//%action%', replace: <<<PHP
  85. \$con->onWebSocketConnect = function(AsyncTcpConnection \$con) {
  86. \$con->send('connect');
  87. echo 'close connection';
  88. \$con->close();
  89. };
  90. PHP));
  91. $clientProcess->start();
  92. usleep(250000);
  93. expect(getNonFrameOutput($serverProcess->getOutput()))->toBe('closed')
  94. ->and(getNonFrameOutput($clientProcess->getOutput()))->toBe('close connection');
  95. $serverProcess->stop();
  96. $clientProcess->stop();
  97. });