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