em = $this->createMock(EntityManager::class); $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); } /** * @test * @dataProvider provideTrackingMethodNames */ public function trackPersistsVisitAndDispatchesEvent(string $method, array $args): void { $this->em->expects($this->once())->method('persist')->with( $this->callback(fn (Visit $visit) => $visit->setId('1') !== null), ); $this->em->expects($this->once())->method('flush'); $this->eventDispatcher->expects($this->once())->method('dispatch')->with( $this->isInstanceOf(UrlVisited::class), ); $this->visitsTracker()->{$method}(...$args); } /** * @test * @dataProvider provideTrackingMethodNames */ public function trackingIsSkippedCompletelyWhenDisabledFromOptions(string $method, array $args): void { $this->em->expects($this->never())->method('persist'); $this->em->expects($this->never())->method('flush'); $this->eventDispatcher->expects($this->never())->method('dispatch'); $this->visitsTracker(new TrackingOptions(disableTracking: true))->{$method}(...$args); } public function provideTrackingMethodNames(): iterable { yield 'track' => ['track', [ShortUrl::createEmpty(), Visitor::emptyInstance()]]; yield 'trackInvalidShortUrlVisit' => ['trackInvalidShortUrlVisit', [Visitor::emptyInstance()]]; yield 'trackBaseUrlVisit' => ['trackBaseUrlVisit', [Visitor::emptyInstance()]]; yield 'trackRegularNotFoundVisit' => ['trackRegularNotFoundVisit', [Visitor::emptyInstance()]]; } /** * @test * @dataProvider provideOrphanTrackingMethodNames */ public function orphanVisitsAreNotTrackedWhenDisabled(string $method): void { $this->em->expects($this->never())->method('persist'); $this->em->expects($this->never())->method('flush'); $this->eventDispatcher->expects($this->never())->method('dispatch'); $this->visitsTracker(new TrackingOptions(trackOrphanVisits: false))->{$method}(Visitor::emptyInstance()); } public function provideOrphanTrackingMethodNames(): iterable { yield 'trackInvalidShortUrlVisit' => ['trackInvalidShortUrlVisit']; yield 'trackBaseUrlVisit' => ['trackBaseUrlVisit']; yield 'trackRegularNotFoundVisit' => ['trackRegularNotFoundVisit']; } private function visitsTracker(?TrackingOptions $options = null): VisitsTracker { return new VisitsTracker($this->em, $this->eventDispatcher, $options ?? new TrackingOptions()); } }