/**
  * @test
  */
 public function it_should_handle_multiple_messages()
 {
     // Create 2 messages
     $wholeMessageLength = 0 + \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::INT_32_LENGTH + \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::HEADER_LENGTH;
     $buffer1 = new \TrafficCophp\ByteBuffer\Buffer($wholeMessageLength);
     $buffer1->writeInt32LE(18, 0);
     $buffer1->writeInt8(\Madkom\EventStore\Client\Domain\Socket\Message\MessageType::PING, \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::MESSAGE_TYPE_OFFSET);
     $buffer1->writeInt8(\Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::FLAGS_NONE, \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::FLAG_OFFSET);
     $buffer1->write(hex2bin('12350000000000000000000000000000'), \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::CORRELATION_ID_OFFSET);
     $buffer2 = new \TrafficCophp\ByteBuffer\Buffer($wholeMessageLength);
     $buffer2->writeInt32LE(18, 0);
     $buffer2->writeInt8(\Madkom\EventStore\Client\Domain\Socket\Message\MessageType::PING, \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::MESSAGE_TYPE_OFFSET);
     $buffer2->writeInt8(\Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::FLAGS_NONE, \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::FLAG_OFFSET);
     $buffer2->write(hex2bin('12350000000000000000000000000000'), \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::CORRELATION_ID_OFFSET);
     $messageType = $this->internalProphet->prophesize('Madkom\\EventStore\\Client\\Domain\\Socket\\Message\\MessageType');
     $messageType->getType()->willReturn(\Madkom\EventStore\Client\Domain\Socket\Message\MessageType::PING);
     $this->socketMessage->getMessageType()->willReturn($messageType->reveal());
     $this->socketMessage->reveal();
     $buffer = (string) $buffer1 . (string) $buffer2;
     $this->messageDecomposer->decomposeMessage((string) $buffer1)->willReturn($this->socketMessage);
     $socketMessages = $this->streamHandler->handle($buffer);
     PHPUnit_Framework_Assert::assertEquals(2, sizeof($socketMessages));
     foreach ($socketMessages as $socketMessage) {
         PHPUnit_Framework_Assert::assertInstanceOf('Madkom\\EventStore\\Client\\Domain\\Socket\\Message\\SocketMessage', $socketMessage);
     }
 }
Ejemplo n.º 2
0
 /**
  * @param $data
  *
  * @return SocketMessage
  */
 private function decomposeMessage($data)
 {
     $socketMessage = $this->messageDecomposer->decomposeMessage($data);
     //If heartbeat then response
     if ($socketMessage->getMessageType()->getType() === MessageType::HEARTBEAT_REQUEST) {
         $this->sendMessage(new SocketMessage(new MessageType(MessageType::HEARTBEAT_RESPONSE), $socketMessage->getCorrelationID()));
     }
     return $socketMessage;
 }
 /**
  * @test
  */
 public function it_should_handle_with_response()
 {
     $wholeMessageLength = 0 + \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::INT_32_LENGTH + \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::HEADER_LENGTH;
     $buffer = new \TrafficCophp\ByteBuffer\Buffer($wholeMessageLength);
     $buffer->writeInt32LE(18, 0);
     $buffer->writeInt8(\Madkom\EventStore\Client\Domain\Socket\Message\MessageType::PING, \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::MESSAGE_TYPE_OFFSET);
     $buffer->writeInt8(\Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::FLAGS_NONE, \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::FLAG_OFFSET);
     $buffer->write(hex2bin('12350000000000000000000000000000'), \Madkom\EventStore\Client\Domain\Socket\Message\MessageConfiguration::CORRELATION_ID_OFFSET);
     $messageTypeChanged = $this->internalProphet->prophesize('Madkom\\EventStore\\Client\\Domain\\Socket\\Message\\MessageType');
     $messageTypeChanged->getType()->willReturn(\Madkom\EventStore\Client\Domain\Socket\Message\MessageType::HEARTBEAT_REQUEST);
     $messageTypeChanged = $messageTypeChanged->reveal();
     $this->socketMessage->getMessageType()->willReturn($messageTypeChanged);
     $this->socketMessage->getCorrelationID()->willReturn('some');
     $this->messageDecomposer->decomposeMessage((string) $buffer)->willReturn($this->socketMessage->reveal());
     $this->messageComposer->compose(\Prophecy\Argument::type('Madkom\\EventStore\\Client\\Domain\\Socket\\Message\\SocketMessage'))->willReturn('someBinary');
     $this->stream->write('someBinary')->shouldBeCalledTimes(1);
     $this->stream->reveal();
     PHPUnit_Framework_Assert::assertInstanceOf('Madkom\\EventStore\\Client\\Domain\\Socket\\Message\\SocketMessage', $this->streamHandler->handle((string) $buffer));
 }