StreamConnectionTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. *
  14. */
  15. class StreamConnectionTest extends ConnectionTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructorDoesNotOpenConnection()
  21. {
  22. $connection = new StreamConnection($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 StreamConnection($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 StreamConnection($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 StreamConnection($parameters);
  51. $unserialized = unserialize(serialize($connection));
  52. $this->assertEquals($connection, $unserialized);
  53. }
  54. // ******************************************************************** //
  55. // ---- INTEGRATION TESTS --------------------------------------------- //
  56. // ******************************************************************** //
  57. /**
  58. * @group connected
  59. */
  60. public function testAcceptsTcpNodelayParameter()
  61. {
  62. if (!version_compare(PHP_VERSION, '5.4.0', '>=')) {
  63. $this->markTestSkipped('Setting TCP_NODELAY on PHP socket streams works on PHP >= 5.4.0');
  64. }
  65. $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => false)));
  66. $connection->connect();
  67. $this->assertTrue($connection->isConnected());
  68. $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => true)));
  69. $connection->connect();
  70. $this->assertTrue($connection->isConnected());
  71. }
  72. /**
  73. * @group connected
  74. * @expectedException Predis\Protocol\ProtocolException
  75. * @expectedExceptionMessage Unknown prefix: 'P'
  76. */
  77. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  78. {
  79. $connection = $this->getConnection($profile);
  80. $stream = $connection->getResource();
  81. $connection->writeCommand($profile->createCommand('ping'));
  82. fread($stream, 1);
  83. $connection->read();
  84. }
  85. // ******************************************************************** //
  86. // ---- HELPER METHODS ------------------------------------------------ //
  87. // ******************************************************************** //
  88. /**
  89. * {@inheritdoc}
  90. */
  91. protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
  92. {
  93. $parameters = $this->getParameters($parameters);
  94. $profile = $this->getProfile();
  95. $connection = new StreamConnection($parameters);
  96. if ($initialize) {
  97. $connection->pushInitCommand($profile->createCommand('select', array($parameters->database)));
  98. $connection->pushInitCommand($profile->createCommand('flushdb'));
  99. }
  100. return $connection;
  101. }
  102. }