cleanup(); // create newsletter $newsletterFactory = new NewsletterFactory(); $newsletter = $newsletterFactory->withType('type')->create(); // create subscriber $subscriberFactory = new SubscriberFactory(); $this->subscriber = $subscriberFactory ->withEmail('test@example.com') ->withFirstName('First') ->withLastName('Last') ->create(); // create queue $queue = SendingTask::create(); $queue->newsletterId = $newsletter->getId(); $queue->setSubscribers([$this->subscriber->getId()]); $queue->updateProcessedSubscribers([$this->subscriber->getId()]); $this->queue = $queue->save(); // instantiate class $this->unsubscribes = $this->diContainer->get(Unsubscribes::class); $this->statisticsUnsubscribesRepository = $this->diContainer->get(StatisticsUnsubscribesRepository::class); } public function testItTracksUnsubscribeEvent() { $subscriberId = $this->subscriber->getId(); $this->assertIsInt($subscriberId); $this->unsubscribes->track( $subscriberId, 'source', $this->queue->id ); expect(count($this->statisticsUnsubscribesRepository->findAll()))->equals(1); } public function testItDoesNotTrackRepeatedUnsubscribeEvents() { $subscriberId = $this->subscriber->getId(); $this->assertIsInt($subscriberId); for ($count = 0; $count <= 2; $count++) { $this->unsubscribes->track( $subscriberId, 'source', $this->queue->id ); } expect(count($this->statisticsUnsubscribesRepository->findAll()))->equals(1); } private function cleanup() { $this->truncateEntity(NewsletterEntity::class); $this->truncateEntity(SubscriberEntity::class); $this->truncateEntity(SendingQueueEntity::class); $this->truncateEntity(StatisticsUnsubscribeEntity::class); } public function _after() { $this->cleanup(); } }