CompositeProtocolProcessorTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 PredisTestCase;
  12. /**
  13. *
  14. */
  15. class CompositeProtocolProcessorTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructor()
  21. {
  22. $protocol = new CompositeProtocolProcessor();
  23. $this->assertInstanceOf(
  24. 'Predis\Protocol\Text\RequestSerializer', $protocol->getRequestSerializer()
  25. );
  26. $this->assertInstanceOf(
  27. 'Predis\Protocol\Text\ResponseReader', $protocol->getResponseReader()
  28. );
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testConstructorWithArguments()
  34. {
  35. $serializer = $this->getMock('Predis\Protocol\RequestSerializerInterface');
  36. $reader = $this->getMock('Predis\Protocol\ResponseReaderInterface');
  37. $protocol = new CompositeProtocolProcessor($serializer, $reader);
  38. $this->assertSame($serializer, $protocol->getRequestSerializer());
  39. $this->assertSame($reader, $protocol->getResponseReader());
  40. }
  41. /**
  42. * @group disconnected
  43. */
  44. public function testCustomRequestSerializer()
  45. {
  46. $serializer = $this->getMock('Predis\Protocol\RequestSerializerInterface');
  47. $protocol = new CompositeProtocolProcessor();
  48. $protocol->setRequestSerializer($serializer);
  49. $this->assertSame($serializer, $protocol->getRequestSerializer());
  50. }
  51. /**
  52. * @group disconnected
  53. */
  54. public function testCustomResponseReader()
  55. {
  56. $reader = $this->getMock('Predis\Protocol\ResponseReaderInterface');
  57. $protocol = new CompositeProtocolProcessor();
  58. $protocol->setResponseReader($reader);
  59. $this->assertSame($reader, $protocol->getResponseReader());
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testConnectionWrite()
  65. {
  66. $serialized = "*1\r\n$4\r\nPING\r\n";
  67. $command = $this->getMock('Predis\Command\CommandInterface');
  68. $connection = $this->getMock('Predis\Connection\CompositeConnectionInterface');
  69. $serializer = $this->getMock('Predis\Protocol\RequestSerializerInterface');
  70. $protocol = new CompositeProtocolProcessor($serializer);
  71. $connection->expects($this->once())
  72. ->method('writeBuffer')
  73. ->with($this->equalTo($serialized));
  74. $serializer->expects($this->once())
  75. ->method('serialize')
  76. ->with($command)
  77. ->will($this->returnValue($serialized));
  78. $protocol->write($connection, $command);
  79. }
  80. /**
  81. * @group disconnected
  82. */
  83. public function testConnectionRead()
  84. {
  85. $connection = $this->getMock('Predis\Connection\CompositeConnectionInterface');
  86. $reader = $this->getMock('Predis\Protocol\ResponseReaderInterface');
  87. $protocol = new CompositeProtocolProcessor(null, $reader);
  88. $reader->expects($this->once())
  89. ->method('read')
  90. ->with($connection)
  91. ->will($this->returnValue('bulk'));
  92. $this->assertSame('bulk', $protocol->read($connection));
  93. }
  94. }