PhpiredisConnectionTest.php 4.2 KB

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