PhpiredisSocketConnectionTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Connection;
  11. use Predis\Command\RawCommand;
  12. use Predis\Response\Error as ErrorResponse;
  13. /**
  14. * @group ext-phpiredis
  15. * @requires extension phpiredis
  16. */
  17. class PhpiredisSocketConnectionTest extends PredisConnectionTestCase
  18. {
  19. const CONNECTION_CLASS = 'Predis\Connection\PhpiredisSocketConnection';
  20. /**
  21. * @group disconnected
  22. * @expectedException \InvalidArgumentException
  23. * @expectedExceptionMessage Invalid scheme: 'tls'.
  24. */
  25. public function testSupportsSchemeTls()
  26. {
  27. $connection = $this->createConnectionWithParams(array('scheme' => 'tls'));
  28. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  29. }
  30. /**
  31. * @group disconnected
  32. * @expectedException \InvalidArgumentException
  33. * @expectedExceptionMessage Invalid scheme: 'rediss'.
  34. */
  35. public function testSupportsSchemeRediss()
  36. {
  37. $connection = $this->createConnectionWithParams(array('scheme' => 'rediss'));
  38. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  39. }
  40. /**
  41. * @group disconnected
  42. * @expectedException \Predis\Connection\ConnectionException
  43. * @expectedExceptionMessage `SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379]
  44. */
  45. public function testThrowsExceptionOnInitializationCommandFailure()
  46. {
  47. $cmdSelect = RawCommand::create('SELECT', '1000');
  48. $connection = $this->getMockBuilder(static::CONNECTION_CLASS)
  49. ->setMethods(array('executeCommand', 'createResource'))
  50. ->setConstructorArgs(array(new Parameters()))
  51. ->getMock();
  52. $connection->method('executeCommand')
  53. ->with($cmdSelect)
  54. ->will($this->returnValue(
  55. new ErrorResponse('ERR invalid DB index')
  56. ));
  57. $connection->method('createResource');
  58. $connection->addConnectCommand($cmdSelect);
  59. $connection->connect();
  60. }
  61. // ******************************************************************** //
  62. // ---- INTEGRATION TESTS --------------------------------------------- //
  63. // ******************************************************************** //
  64. /**
  65. * @group connected
  66. * @expectedException \Predis\Connection\ConnectionException
  67. * @expectedExceptionMessage Cannot resolve the address of 'bogus.tld'.
  68. */
  69. public function testThrowsExceptionOnUnresolvableHostname()
  70. {
  71. $connection = $this->createConnectionWithParams(array('host' => 'bogus.tld'));
  72. $connection->connect();
  73. }
  74. /**
  75. * @medium
  76. * @group connected
  77. * @expectedException \Predis\Protocol\ProtocolException
  78. */
  79. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  80. {
  81. $connection = $this->createConnection();
  82. $socket = $connection->getResource();
  83. $connection->writeRequest($this->getCurrentProfile()->createCommand('ping'));
  84. socket_read($socket, 1);
  85. $connection->read();
  86. }
  87. }