PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Pheanstalk/ConnectionTest.php

http://github.com/pda/pheanstalk
PHP | 98 lines | 60 code | 18 blank | 20 comment | 0 complexity | 9f03bcd4182600162aa85555531a6bfb MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. namespace Pheanstalk;
  3. use Pheanstalk\Command\StatsCommand;
  4. use Pheanstalk\Contract\SocketFactoryInterface;
  5. use Pheanstalk\Contract\SocketInterface;
  6. use Pheanstalk\Exception\ConnectionException;
  7. use Pheanstalk\Exception\SocketException;
  8. use PHPUnit\Framework\MockObject\MockObject;
  9. use PHPUnit\Framework\TestCase;
  10. /**
  11. * Tests for the Connection.
  12. * Relies on a running beanstalkd server.
  13. */
  14. class ConnectionTest extends TestCase
  15. {
  16. const CONNECT_TIMEOUT = 2;
  17. public function connectionProvider($test, $host = SERVER_HOST, $port = SERVER_PORT)
  18. {
  19. return [
  20. 'stream' => [new Connection(new SocketFactory($host, $port, 1, SocketFactory::STREAM))],
  21. 'fsockopen' => [new Connection(new SocketFactory($host, $port, 1, SocketFactory::FSOCKOPEN))],
  22. 'socket' => [new Connection(new SocketFactory($host, $port, 1, SocketFactory::SOCKET))],
  23. 'autodetect' =>[new Connection(new SocketFactory($host, $port, 1, SocketFactory::AUTODETECT))]
  24. ];
  25. }
  26. public function badPortConnectionProvider($test)
  27. {
  28. return $this->connectionProvider($test, SERVER_HOST, SERVER_PORT + 1);
  29. }
  30. public function badHostConnectionProvider($test)
  31. {
  32. return $this->connectionProvider($test, SERVER_HOST . 'abc', SERVER_PORT);
  33. }
  34. /**
  35. * @dataProvider badPortConnectionProvider
  36. */
  37. public function testConnectionFailsToIncorrectPort(Connection $connection)
  38. {
  39. $this->expectException(ConnectionException::class);
  40. $command = new Command\UseCommand('test');
  41. $connection->dispatchCommand($command);
  42. }
  43. /**
  44. * @dataProvider badHostConnectionProvider
  45. */
  46. public function testConnectionFailsToIncorrectHost(Connection $connection)
  47. {
  48. $this->expectException(ConnectionException::class);
  49. $command = new Command\UseCommand('test');
  50. $connection->dispatchCommand($command);
  51. }
  52. /**
  53. * @throws Exception\ClientException
  54. * @dataProvider connectionProvider
  55. */
  56. public function testDispatchCommandSuccessful(Connection $connection)
  57. {
  58. $command = new Command\UseCommand('test');
  59. $response = $connection->dispatchCommand($command);
  60. $this->assertInstanceOf(Contract\ResponseInterface::class, $response);
  61. }
  62. /**
  63. * @dataProvider connectionProvider
  64. */
  65. public function testDisconnect(Connection $connection)
  66. {
  67. $pheanstalk = new Pheanstalk(new Connection(new SocketFactory(SERVER_HOST, SERVER_PORT)));
  68. $baseCount = $pheanstalk->stats()['current-connections'];
  69. $this->assertEquals($baseCount, $pheanstalk->stats()['current-connections']);
  70. // initial connection
  71. $connection->dispatchCommand(new Command\StatsCommand());
  72. $this->assertEquals($baseCount + 1, $pheanstalk->stats()['current-connections']);
  73. // disconnect
  74. $connection->disconnect();
  75. $this->assertEquals($baseCount, $pheanstalk->stats()['current-connections']);
  76. // auto-reconnect
  77. $connection->dispatchCommand(new Command\StatsCommand());
  78. $this->assertEquals($baseCount + 1, $pheanstalk->stats()['current-connections']);
  79. }
  80. }