/**
  * {@inheritdoc}
  */
 public function execute($command, callable $next)
 {
     try {
         $this->dispatch(CommandEvents::RECEIVED, new CommandReceived($command));
         $this->dispatch(CommandEvents::received($command), new CommandReceived($command));
         $result = $next($command);
         $this->dispatch(CommandEvents::HANDLED, new CommandHandled($command, $result));
         $this->dispatch(CommandEvents::handled($command), new CommandHandled($command, $result));
         return $result;
     } catch (\Exception $e) {
         $this->dispatch(CommandEvents::FAILED, new CommandFailed($command, $e));
         $this->dispatch(CommandEvents::failed($command), new CommandFailed($command, $e));
         throw $e;
     }
 }
 public function testEventMiddlewareFiresTheSpecificFailedEventWithTheException()
 {
     $eventCommand = null;
     $this->handlerWill($this->throwException($ex = new \LogicException('oops')));
     $this->dispatcher->addListener(CommandEvents::failed(Command::class), function ($e) use(&$eventCommand, $ex) {
         $this->assertSame($ex, $e->getException());
         $eventCommand = $e->getCommand();
     });
     // no exepcted exception here because we want to make sure
     // our listener is actually called
     $caught = false;
     try {
         $this->bus->handle($command = new Command());
     } catch (\LogicException $e) {
         $this->assertSame($ex, $e);
         $caught = true;
     }
     $this->assertSame($command, $eventCommand);
     $this->assertTrue($caught, 'should have caught the LogicException above');
 }