public static function eventRegistration() { $thingHappened = ClassName::underscore(ThingHappenedEvent::class); $taskCreated = ClassName::underscore(TaskCreatedEvent::class); $descriptionChanged = ClassName::underscore(DescriptionChangedEvent::class); return [$thingHappened => 'onThingHappened', $taskCreated => ['onTaskCreated', 10], $descriptionChanged => [['onDescriptionChangedSecond', 10], ['onDescriptionChangedThird', 5], ['onDescriptionChangedFirst', 100]]]; }
public function test_that_has_handlers_returns_true_with_handlers_added_in_memory() { $dispatcher = $this->container->get('event.dispatcher'); $eventType = ClassName::underscore(ThingHappenedEvent::class); $dispatcher->addHandler($eventType, function () { }); $this->assertTrue($dispatcher->hasHandlers($eventType)); }
/** * Handles event if the apply method is available * * This method delegates to a protected method based on the domain event * class name: 'apply'.$className * * @param DomainEvent $domainEvent The domain event * * @return void */ protected function handle(DomainEvent $domainEvent) { $method = 'apply' . ClassName::short($domainEvent); if (!method_exists($this, $method)) { return; } $this->{$method}($domainEvent); }
public function test_that_has_handlers_returns_true_with_handlers_added_in_memory() { $dispatcher = new ServiceAwareEventDispatcher($this->container); $type = ClassName::underscore(UserRegisteredEvent::class); $dispatcher->addHandler($type, function () { }); $this->assertTrue($dispatcher->hasHandlers($type)); }
public function test_that_query_is_handled_by_pipeline() { $handler = new UserByEmailHandler(); $this->queryMap->registerHandlers([UserByEmailQuery::class => $handler]); $query = new UserByEmailQuery('*****@*****.**'); $user = $this->pipeline->fetch($query); $this->assertTrue($this->queryMap->hasHandler(UserByEmailQuery::class) && $this->logHandler->hasInfoThatContains(sprintf('Query received {%s}', ClassName::canonical(UserByEmailQuery::class))) && $this->logHandler->hasInfoThatContains(sprintf('Query handled {%s}', ClassName::canonical(UserByEmailQuery::class))) && $user['email'] === '*****@*****.**'); }
public function test_that_command_is_executed_by_pipeline() { $handler = new RegisterUserHandler(); $this->commandMap->registerHandlers([RegisterUserCommand::class => $handler]); $command = new RegisterUserCommand(); $command->setFirstName('James')->setMiddleName('D')->setLastName('Smith')->setEmail('*****@*****.**')->setPassword('secret'); $this->pipeline->execute($command); $this->assertTrue($this->commandMap->hasHandler(RegisterUserCommand::class) && $this->logHandler->hasInfoThatContains(sprintf('Command received {%s}', ClassName::canonical(RegisterUserCommand::class))) && $this->logHandler->hasInfoThatContains(sprintf('Command handled {%s}', ClassName::canonical(RegisterUserCommand::class))) && $handler->isHandled()); }
/** * {@inheritdoc} */ public function dispatch(EventMessage $message) { $eventType = ClassName::underscore((string) $message->payloadType()); foreach ($this->getHandlers($eventType) as $handler) { call_user_func($handler, $message); } foreach ($this->getHandlers(Subscriber::ALL_EVENTS) as $handler) { call_user_func($handler, $message); } }
/** * {@inheritdoc} */ public function dispatch(Event $event) { $message = EventMessage::create($event); $eventType = ClassName::underscore($event); foreach ($this->getHandlers($eventType) as $handler) { call_user_func($handler, $message); } foreach ($this->getHandlers(EventSubscriber::ALL_EVENTS) as $handler) { call_user_func($handler, $message); } }
/** * {@inheritdoc} */ public function process(CommandMessage $message, callable $next) { $command = ClassName::short($message->payloadType()->toString()); try { $this->logger->debug(sprintf('Command (%s) received: %s', $command, date(DATE_ATOM)), ['message' => $message->toString()]); $next($message); $this->logger->debug(sprintf('Command (%s) handled: %s', $command, date(DATE_ATOM)), ['message' => $message->toString()]); } catch (Exception $exception) { $this->logger->error(sprintf('Command (%s) failed: %s', $command, date(DATE_ATOM)), ['message' => $message->toString(), 'exception' => $exception]); throw $exception; } }
/** * {@inheritdoc} */ public function process(QueryMessage $message, callable $next) { $query = ClassName::short($message->payloadType()->toString()); try { $this->logger->debug(sprintf('Query (%s) received: %s', $query, date(DATE_ATOM)), ['message' => $message->serialize()]); $data = $next($message); $this->logger->debug(sprintf('Query (%s) handled: %s', $query, date(DATE_ATOM)), ['message' => $message->serialize()]); } catch (Exception $exception) { $this->logger->error(sprintf('Query (%s) failed: %s', $query, date(DATE_ATOM)), ['message' => $message->serialize(), 'exception' => $exception]); throw $exception; } return $data; }
public function test_that_event_is_logged_by_subscriber() { $event = new UserRegisteredEvent('*****@*****.**', 'James', 'Smith', 'D'); $this->dispatcher->dispatch($event); $this->assertTrue($this->logHandler->hasInfoThatContains(sprintf('Event dispatched {%s}', ClassName::canonical(UserRegisteredEvent::class)))); }
public static function eventRegistration() : array { return [ClassName::underscore(UserRegisteredEvent::class) => 'onUserRegistered', 'other.event' => ['onOtherEvent', 10], 'some.event' => [['onSomeEventSecond', 10], ['onSomeEventThird', 5], ['onSomeEventFirst', 100]]]; }
/** * @expectedException Novuso\Common\Application\Messaging\Event\Exception\FrozenDispatcherException */ public function test_that_remove_handler_throws_exception_when_called() { $dispatcher = $this->container->get('event.dispatcher'); $dispatcher->attachService('test.subscriber', TestSubscriber::class); $dispatcher = new FrozenDispatcher($dispatcher); $eventType = ClassName::underscore(ThingHappenedEvent::class); $dispatcher->removeHandler($eventType, function () { }); }
/** * {@inheritdoc} */ public function serialize(Serializable $object) : string { $data = ['@' => ClassName::canonical($object), '$' => $object->serialize()]; return json_encode($data, JSON_UNESCAPED_SLASHES); }
/** * {@inheritdoc} */ public function dispatch(EventMessage $message) { $this->lazyLoad(ClassName::underscore((string) $message->payloadType())); return parent::dispatch($message); }
/** * Retrieves a string representation * * @return string */ public final function toString() : string { return sprintf('%s.%s', ClassName::short(static::class), $this->name()); }
/** * @expectedException \Novuso\System\Exception\TypeException */ public function test_that_full_throws_exception_for_invalid_argument_type() { ClassName::full(null); }
/** * {@inheritdoc} */ public function dispatch(Event $event) { $this->lazyLoad(ClassName::underscore($event)); parent::dispatch($event); }
/** * Retrieves the full class name * * @return string */ public function toClassName() : string { return ClassName::full($this->name); }