PhpiredisConnectionTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // ---- INTEGRATION TESTS --------------------------------------------- //
  48. // ******************************************************************** //
  49. /**
  50. * @group connected
  51. */
  52. public function testExecutesCommandsOnServer()
  53. {
  54. $connection = $this->getConnection($profile, true);
  55. $cmdPing = $profile->createCommand('ping');
  56. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  57. $cmdGet = $profile->createCommand('get', array('foobar'));
  58. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  59. $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
  60. $this->assertTrue($connection->executeCommand($cmdPing));
  61. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  62. $this->assertNull($connection->executeCommand($cmdGet));
  63. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  64. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  65. }
  66. /**
  67. * @group connected
  68. * @expectedException Predis\Protocol\ProtocolException
  69. * @expectedExceptionMessage Protocol error, got "P" as reply type byte
  70. */
  71. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  72. {
  73. $connection = $this->getConnection($profile);
  74. $socket = $connection->getResource();
  75. $connection->writeCommand($profile->createCommand('ping'));
  76. socket_read($socket, 1);
  77. $connection->read();
  78. }
  79. // ******************************************************************** //
  80. // ---- HELPER METHODS ------------------------------------------------ //
  81. // ******************************************************************** //
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function getConnection(&$profile = null, $initialize = false, Array $parameters = array())
  86. {
  87. $parameters = $this->getParameters($parameters);
  88. $profile = $this->getProfile();
  89. $connection = new PhpiredisConnection($parameters);
  90. if ($initialize) {
  91. $connection->pushInitCommand($profile->createCommand('select', array($parameters->database)));
  92. $connection->pushInitCommand($profile->createCommand('flushdb'));
  93. }
  94. return $connection;
  95. }
  96. }