public function testHandleThrowAExceptionWhenAHandlerHasNoEventMethod()
 {
     $this->setExpectedException(CanNotInvokeHandlerException::class);
     $event = new \stdClass();
     $domainMessage = new DomainMessage(12, 1, new Metadata(), $event, DateTime::now());
     $badHandler = new \stdClass();
     $this->nameExtractor->expects(self::atLeastOnce())->method('extract')->with($event)->willReturn('Finished');
     $this->handlerLocator->expects(self::atLeastOnce())->method('getHandlersForEvent')->with('Finished')->willReturn([$badHandler]);
     $this->methodNameInflector->expects(self::any())->method('inflect')->with($event, $badHandler)->willReturn('aEventMethod');
     $this->eventListener->handle($domainMessage);
 }
 /**
  * @param DomainMessage $domainMessage
  * @return void
  */
 public function handle(DomainMessage $domainMessage)
 {
     $event = $domainMessage->getPayload();
     $eventName = $this->nameExtractor->extract($event);
     $eventHandlers = $this->handlerLocator->getHandlersForEvent($eventName);
     foreach ($eventHandlers as $handler) {
         $methodName = $this->methodNameInflector->inflect($event, $handler);
         // is_callable is used here instead of method_exists, as method_exists
         // will fail when given a handler that relies on __call.
         if (!is_callable([$handler, $methodName])) {
             throw CanNotInvokeHandlerException::forEvent($event, sprintf("Method '%s' does not exist on handler", is_object($methodName) ? get_class($methodName) : gettype($methodName)));
         }
         $handler->{$methodName}($event, $domainMessage);
     }
 }