/** * @test */ public function should_return_values_it_was_created_with() { $this->assertEquals($this->socketMessage->getMessageType(), $this->messageType); $this->assertSame($this->socketMessage->getCredentials(), $this->credentials); $this->assertEquals($this->socketMessage->getCorrelationID(), 'correlation'); $this->assertSame($this->socketMessage->getData(), $this->protobufMessage); }
/** * Composes message to binary data to send it via stream * * @param SocketMessage $socketMessage * * @return string * */ public function compose(SocketMessage $socketMessage) { //Correlation + flag length + command length $messageLength = MessageConfiguration::HEADER_LENGTH; $doAuthorization = $socketMessage->getCredentials() ? true : false; $authorizationLength = 0; if ($doAuthorization) { $authorizationLength = 1 + strlen($socketMessage->getCredentials()->getUsername()) + 1 + strlen($socketMessage->getCredentials()->getPassword()); } $dataToSend = $socketMessage->getData(); if ($dataToSend) { $dataToSend = $dataToSend->serializeToString(); $messageLength += strlen($dataToSend); } $wholeMessageLength = $messageLength + $authorizationLength + MessageConfiguration::INT_32_LENGTH; $buffer = new Buffer($wholeMessageLength); $buffer->writeInt32LE($messageLength + $authorizationLength, 0); $buffer->writeInt8($socketMessage->getMessageType()->getType(), MessageConfiguration::MESSAGE_TYPE_OFFSET); $buffer->writeInt8($doAuthorization ? MessageConfiguration::FLAG_AUTHORIZATION : MessageConfiguration::FLAGS_NONE, MessageConfiguration::FLAG_OFFSET); $buffer->write(hex2bin($this->createCorrelationID($socketMessage->getCorrelationID())), MessageConfiguration::CORRELATION_ID_OFFSET); if ($doAuthorization) { $usernameLength = strlen($socketMessage->getCredentials()->getUsername()); $passwordLength = strlen($socketMessage->getCredentials()->getPassword()); $buffer->writeInt8($usernameLength, MessageConfiguration::DATA_OFFSET); $buffer->write($socketMessage->getCredentials()->getUsername(), MessageConfiguration::DATA_OFFSET + 1); $buffer->writeInt8($passwordLength, MessageConfiguration::DATA_OFFSET + 1 + $usernameLength); $buffer->write($socketMessage->getCredentials()->getPassword(), MessageConfiguration::DATA_OFFSET + 1 + $usernameLength + 1); } if ($dataToSend) { $buffer->write($dataToSend, MessageConfiguration::DATA_OFFSET + $authorizationLength); } return (string) $buffer; }
/** * @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); } }
/** * @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)); }