public function testShouldReturnTrueIfThereIsNoExternalDispatcher() { // Arrange $dispatcher = m::mock(EventTriggerInterface::class); $service = new EventTriggerService(); // Act $dispatcher->shouldReceive('fire')->never(); // Assertion /* without calling registerEventDispatcher */ $this->assertTrue($service->fire('foobar', ['answer' => 23], true)); }
/** * Register MongoDbConnector within the application. * * @return void */ public function registerConnector() { $config = $this->app->make('config'); MongolidIoc::setContainer($this->app); $connectionString = $this->buildConnectionString(); $connection = new Connection($connectionString); $pool = new Pool(); $eventService = new EventTriggerService(); $eventService->registerEventDispatcher($this->app->make(LaravelEventTrigger::class)); $pool->addConnection($connection); $this->app->instance(Pool::class, $pool); $this->app->instance(EventTriggerService::class, $eventService); $this->app->bind(CacheComponentInterface::class, function ($app) { return new LaravelCacheComponent($app[CacheRepository::class], $app[Serializer::class]); }); $connection->defaultDatabase = $config->get('database.mongodb.default.database', 'mongolid'); }
/** * Sets the event trigger for Mongolid events. * * @param EventTriggerInterface $eventTrigger External event trigger. * * @return void */ public function setEventTrigger(EventTriggerInterface $eventTrigger) { $this->init(); $eventService = new EventTriggerService(); $eventService->registerEventDispatcher($eventTrigger); $this->container->instance(EventTriggerService::class, $eventService); }
/** * Triggers an event. May return if that event had success. * * @param string $event Identification of the event. * @param mixed $entity Event payload. * @param bool $halt True if the return of the event handler will be used in a conditional. * * @return mixed Event handler return. */ protected function fireEvent(string $event, $entity, bool $halt = false) { $event = "mongolid.{$event}: " . get_class($entity); $this->eventService ? $this->eventService : ($this->eventService = Ioc::make(EventTriggerService::class)); return $this->eventService->fire($event, $entity, $halt); }