ComposableTextProtocolTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Protocol\Text;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. *
  14. */
  15. class ComposableTextProtocolTest extends StandardTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testCustomSerializer()
  21. {
  22. $serializer = $this->getMock('Predis\Protocol\CommandSerializerInterface');
  23. $protocol = new ComposableTextProtocol();
  24. $protocol->setSerializer($serializer);
  25. $this->assertSame($serializer, $protocol->getSerializer());
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testCustomReader()
  31. {
  32. $reader = $this->getMock('Predis\Protocol\ResponseReaderInterface');
  33. $protocol = new ComposableTextProtocol();
  34. $protocol->setReader($reader);
  35. $this->assertSame($reader, $protocol->getReader());
  36. }
  37. /**
  38. * @group disconnected
  39. */
  40. public function testConnectionWrite()
  41. {
  42. $serialized = "*1\r\n$4\r\nPING\r\n";
  43. $command = $this->getMock('Predis\Command\CommandInterface');
  44. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  45. $serializer = $this->getMock('Predis\Protocol\CommandSerializerInterface');
  46. $protocol = new ComposableTextProtocol();
  47. $protocol->setSerializer($serializer);
  48. $connection->expects($this->once())
  49. ->method('writeBytes')
  50. ->with($this->equalTo($serialized));
  51. $serializer->expects($this->once())
  52. ->method('serialize')
  53. ->with($command)
  54. ->will($this->returnValue($serialized));
  55. $protocol->write($connection, $command);
  56. }
  57. /**
  58. * @group disconnected
  59. */
  60. public function testConnectionRead()
  61. {
  62. $serialized = "*1\r\n$4\r\nPING\r\n";
  63. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  64. $reader = $this->getMock('Predis\Protocol\ResponseReaderInterface');
  65. $protocol = new ComposableTextProtocol();
  66. $protocol->setReader($reader);
  67. $reader->expects($this->once())
  68. ->method('read')
  69. ->with($connection)
  70. ->will($this->returnValue('bulk'));
  71. $this->assertSame('bulk', $protocol->read($connection));
  72. }
  73. /**
  74. * @group disconnected
  75. */
  76. public function testSetMultibulkOption()
  77. {
  78. $protocol = new ComposableTextProtocol();
  79. $reader = $protocol->getReader();
  80. $protocol->setOption('iterable_multibulk', true);
  81. $this->assertInstanceOf('Predis\Protocol\Text\ResponseMultiBulkStreamHandler', $reader->getHandler('*'));
  82. $protocol->setOption('iterable_multibulk', false);
  83. $this->assertInstanceOf('Predis\Protocol\Text\ResponseMultiBulkHandler', $reader->getHandler('*'));
  84. }
  85. /**
  86. * @group disconnected
  87. * @expectedException InvalidArgumentException
  88. * @expectedExceptionMessage The option unknown_option is not supported by the current protocol
  89. */
  90. public function testSetInvalidOption()
  91. {
  92. $protocol = new ComposableTextProtocol();
  93. $protocol->setOption('unknown_option', true);
  94. }
  95. }