PhpiredisConnectionTest.php 4.1 KB

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