/**
  * @test
  */
 public function it_sends_message_as_a_json_encoded_string()
 {
     $zmqMessageProducer = $this->zmqMessageProducer;
     $doSomething = new DoSomething(['data' => 'test command']);
     $this->zmqClient->send($this->validate_message_body($doSomething), \ZMQ::MODE_NOBLOCK)->willReturn(null)->shouldBeCalled();
     $zmqMessageProducer($doSomething);
 }
 /**
  * Message producers need to be invokable.
  *
  * A producer MUST be able to handle a message async without returning a response.
  * A producer MAY also support future response by resolving the passed $deferred.
  *
  * Note: A $deferred is only passed by a QueryBus but in this case the $deferred
  *       MUST either be resolved/rejected OR the message producer
  *       MUST throw a Prooph\ServiceBus\Exception\RuntimeException if it cannot
  *       handle the $deferred
  *
  * @param Message $message
  * @param null|Deferred $deferred
  * @throws RuntimeException If a $deferred is passed but producer can not handle it
  */
 public function __invoke(Message $message, Deferred $deferred = null)
 {
     if (null !== $deferred) {
         throw new RuntimeException(__CLASS__ . ' cannot handle query messages which require future responses.');
     }
     $data = $this->arrayFromMessage($message);
     $this->zmqClient->send(json_encode($data), ZMQ::MODE_NOBLOCK);
 }
 /**
  * @test
  */
 public function it_sends_message_and_gets_response()
 {
     $socket = new \ZMQSocket(new \ZMQContext(), \ZMQ::SOCKET_REQ);
     $zmqClient = new ZeroMQSocket($socket, 'tcp://localhost:5556');
     $zmqClient->send($message = 'testing-123');
     $this->assertEquals($message, $zmqClient->receive());
     $socket->disconnect('tcp://localhost:5556');
 }
 /**
  * @param bool $usingDeferred
  * @throws RuntimeException
  */
 private function assertDeferred($usingDeferred)
 {
     if ($usingDeferred and !$this->zmqClient->handlesDeferred()) {
         throw new RuntimeException(__CLASS__ . ' cannot handle query messages which require future responses.');
     }
     if ($this->zmqClient->handlesDeferred() and !$usingDeferred) {
         throw new RuntimeException(__CLASS__ . ' cannot handle push and forget messages.');
     }
 }
 /**
  * @test
  */
 public function it_can_handle_rpc()
 {
     $this->zmqClient->handlesDeferred()->willReturn(true)->shouldBeCalled();
     $this->zmqClient->receive()->willReturn($response = 'Hello World')->shouldBeCalled();
     $deferred = $this->prophesize(Deferred::class);
     $deferred->resolve($response)->shouldBeCalled();
     $zmqMessageProducer = $this->zmqMessageProducer;
     $doSomething = new DoSomething(['data' => 'test command']);
     $this->zmqClient->send($this->validate_message_body($doSomething), \ZMQ::MODE_NOBLOCK)->willReturn(null)->shouldBeCalled();
     $zmqMessageProducer($doSomething, $deferred->reveal());
 }