PhpiredisConnectionTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /**
  12. * @group ext-phpiredis
  13. */
  14. class PhpiredisConnectionTest extends PredisConnectionTestCase
  15. {
  16. /**
  17. * @group disconnected
  18. */
  19. public function testConstructorDoesNotOpenConnection()
  20. {
  21. $connection = new PhpiredisConnection($this->getParameters());
  22. $this->assertFalse($connection->isConnected());
  23. }
  24. /**
  25. * @group disconnected
  26. */
  27. public function testExposesParameters()
  28. {
  29. $parameters = $this->getParameters();
  30. $connection = new PhpiredisConnection($parameters);
  31. $this->assertSame($parameters, $connection->getParameters());
  32. }
  33. /**
  34. * @group disconnected
  35. * @expectedException InvalidArgumentException
  36. * @expectedExceptionMessage Invalid scheme: udp
  37. */
  38. public function testThrowsExceptionOnInvalidScheme()
  39. {
  40. $parameters = $this->getParameters(array('scheme' => 'udp'));
  41. $connection = new PhpiredisConnection($parameters);
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testCanBeSerialized()
  47. {
  48. $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
  49. $connection = new PhpiredisConnection($parameters);
  50. $unserialized = unserialize(serialize($connection));
  51. $this->assertInstanceOf('Predis\Connection\PhpiredisConnection', $unserialized);
  52. $this->assertEquals($parameters, $unserialized->getParameters());
  53. }
  54. // ******************************************************************** //
  55. // ---- INTEGRATION TESTS --------------------------------------------- //
  56. // ******************************************************************** //
  57. /**
  58. * @group connected
  59. */
  60. public function testExecutesCommandsOnServer()
  61. {
  62. $connection = $this->getConnection($profile, true);
  63. $cmdPing = $profile->createCommand('ping');
  64. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  65. $cmdGet = $profile->createCommand('get', array('foobar'));
  66. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  67. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  68. $this->assertSame('PONG', $connection->executeCommand($cmdPing));
  69. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  70. $this->assertNull($connection->executeCommand($cmdGet));
  71. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  72. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  73. }
  74. /**
  75. * @group connected
  76. * @expectedException Predis\Connection\ConnectionException
  77. * @expectedExceptionMessage Cannot resolve the address of 'bogus.tld'.
  78. */
  79. public function testThrowsExceptionOnUnresolvableHostname()
  80. {
  81. $parameters = $this->getParameters(array('host' => 'bogus.tld'));
  82. $connection = new PhpiredisConnection($parameters);
  83. $connection->connect();
  84. }
  85. /**
  86. * @group connected
  87. * @expectedException Predis\Protocol\ProtocolException
  88. * @expectedExceptionMessage Protocol error, got "P" as reply type byte
  89. */
  90. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  91. {
  92. $connection = $this->getConnection($profile);
  93. $socket = $connection->getResource();
  94. $connection->writeCommand($profile->createCommand('ping'));
  95. socket_read($socket, 1);
  96. $connection->read();
  97. }
  98. // ******************************************************************** //
  99. // ---- HELPER METHODS ------------------------------------------------ //
  100. // ******************************************************************** //
  101. /**
  102. * {@inheritdoc}
  103. */
  104. protected function getConnection(&$profile = null, $initialize = false, Array $parameters = array())
  105. {
  106. $parameters = $this->getParameters($parameters);
  107. $profile = $this->getProfile();
  108. $connection = new PhpiredisConnection($parameters);
  109. if ($initialize) {
  110. $connection->pushInitCommand($profile->createCommand('select', array($parameters->database)));
  111. $connection->pushInitCommand($profile->createCommand('flushdb'));
  112. }
  113. return $connection;
  114. }
  115. }