/**
  * {@inheritdoc}
  */
 public function execute($command, callable $next)
 {
     if (!$command instanceof Message) {
         return $next($command);
     }
     $this->capturer->publish($command);
     return null;
 }
 /**
  * @param $messages
  */
 private function storeMessages()
 {
     $messages = $this->capturer->fetchMessages();
     foreach ($messages as $message) {
         $this->transactionMessages[] = $message;
     }
 }
 /**
  * @test
  */
 public function it_should_clear_capture_messages()
 {
     $message1 = new MessageCommand('message', 'key');
     $message2 = new MessageCommand('message', 'key');
     $expectedMessage = [$message1, $message2];
     $this->publisher->publish($message1);
     $this->publisher->publish($message2);
     // Avoid fetch messages from clearing
     $this->assertSame($expectedMessage, $this->publisher->fetchMessages(false));
     $this->assertSame($expectedMessage, $this->publisher->fetchMessages(false));
     // Clear the messages
     $this->publisher->clear();
     $this->assertSame([], $this->publisher->fetchMessages());
 }
 /**
  * @test
  */
 public function it_publishes_message_once_internal_calls_where_handled()
 {
     $capturer = new MessageCapturer();
     $middleware = new InMemoryMiddleware($capturer, $this->locator);
     $message1 = Mockery::mock(Message::class);
     $message2 = Mockery::mock(Message::class);
     $message3 = Mockery::mock(Message::class);
     $message4 = Mockery::mock(Message::class);
     $message5 = Mockery::mock(Message::class);
     $push1 = function ($m) use($capturer, $message1) {
         $capturer->publish($message1);
     };
     $push2 = function ($m) use($capturer, $message2) {
         $capturer->publish($message2);
         throw new \RuntimeException('Failed');
     };
     $push3 = function ($m) use($middleware, $capturer, $message3, $message4, $message5) {
         $capturer->publish($message3);
         try {
             $middleware->execute($m, function () use($capturer, $message4) {
                 $capturer->publish($message4);
                 throw new \RuntimeException('Failed');
             });
         } catch (\RuntimeException $e) {
         }
         $middleware->execute($m, function () use($capturer, $message5) {
             $capturer->publish($message5);
         });
     };
     $chain = function ($m) use($middleware, $push1, $push2, $push3) {
         $middleware->execute($m, $push1);
         try {
             $middleware->execute($m, $push2);
         } catch (\RuntimeException $e) {
         }
         $middleware->execute($m, $push3);
     };
     $this->mockMessagePublish([$message1, $message3, $message5]);
     $middleware->execute('some_command', $chain);
 }
 /**
  * @test
  */
 public function it_should_continue_if_its_not_a_message()
 {
     $this->capturer->shouldReceive('publish');
     $command = new \stdClass();
     $this->execute($this->middleware, $command, $command);
 }