Migrated TrackingOptions to immutable object

This commit is contained in:
Alejandro Celaya
2022-09-17 12:57:04 +02:00
parent 5f87bb13f8
commit fe4b2c4ae4
14 changed files with 77 additions and 162 deletions

View File

@@ -82,11 +82,11 @@ class VisitorTest extends TestCase
$this->generateRandomString(2000),
$this->generateRandomString(2000),
);
$normalizedVisitor = $visitor->normalizeForTrackingOptions(new TrackingOptions([
'disableIpTracking' => true,
'disableReferrerTracking' => true,
'disableUaTracking' => true,
]));
$normalizedVisitor = $visitor->normalizeForTrackingOptions(new TrackingOptions(
disableIpTracking: true,
disableReferrerTracking: true,
disableUaTracking: true,
));
self::assertNotSame($visitor, $normalizedVisitor);
self::assertEmpty($normalizedVisitor->userAgent);

View File

@@ -16,7 +16,7 @@ class ShortUrlRedirectionBuilderTest extends TestCase
protected function setUp(): void
{
$trackingOptions = new TrackingOptions(['disable_track_param' => 'foobar']);
$trackingOptions = new TrackingOptions(disableTrackParam: 'foobar');
$this->redirectionBuilder = new ShortUrlRedirectionBuilder($trackingOptions);
}

View File

@@ -38,10 +38,10 @@ class RequestTrackerTest extends TestCase
$this->requestTracker = new RequestTracker(
$this->visitsTracker->reveal(),
new TrackingOptions([
'disable_track_param' => 'foobar',
'disable_tracking_from' => ['80.90.100.110', '192.168.10.0/24', '1.2.*.*'],
]),
new TrackingOptions(
disableTrackParam: 'foobar',
disableTrackingFrom: ['80.90.100.110', '192.168.10.0/24', '1.2.*.*'],
),
);
$this->request = ServerRequestFactory::fromGlobals()->withAttribute(

View File

@@ -24,16 +24,11 @@ class VisitsTrackerTest extends TestCase
private VisitsTracker $visitsTracker;
private ObjectProphecy $em;
private ObjectProphecy $eventDispatcher;
private TrackingOptions $options;
protected function setUp(): void
{
$this->em = $this->prophesize(EntityManager::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$this->options = new TrackingOptions();
$this->visitsTracker = new VisitsTracker($this->em->reveal(), $this->eventDispatcher->reveal(), $this->options);
}
/**
@@ -45,7 +40,7 @@ class VisitsTrackerTest extends TestCase
$persist = $this->em->persist(Argument::that(fn (Visit $visit) => $visit->setId('1')))->will(function (): void {
});
$this->visitsTracker->{$method}(...$args);
$this->visitsTracker()->{$method}(...$args);
$persist->shouldHaveBeenCalledOnce();
$this->em->flush()->shouldHaveBeenCalledOnce();
@@ -58,9 +53,7 @@ class VisitsTrackerTest extends TestCase
*/
public function trackingIsSkippedCompletelyWhenDisabledFromOptions(string $method, array $args): void
{
$this->options->disableTracking = true;
$this->visitsTracker->{$method}(...$args);
$this->visitsTracker(new TrackingOptions(disableTracking: true))->{$method}(...$args);
$this->eventDispatcher->dispatch(Argument::cetera())->shouldNotHaveBeenCalled();
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
@@ -81,9 +74,7 @@ class VisitsTrackerTest extends TestCase
*/
public function orphanVisitsAreNotTrackedWhenDisabled(string $method): void
{
$this->options->trackOrphanVisits = false;
$this->visitsTracker->{$method}(Visitor::emptyInstance());
$this->visitsTracker(new TrackingOptions(trackOrphanVisits: false))->{$method}(Visitor::emptyInstance());
$this->eventDispatcher->dispatch(Argument::cetera())->shouldNotHaveBeenCalled();
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
@@ -96,4 +87,13 @@ class VisitsTrackerTest extends TestCase
yield 'trackBaseUrlVisit' => ['trackBaseUrlVisit'];
yield 'trackRegularNotFoundVisit' => ['trackRegularNotFoundVisit'];
}
private function visitsTracker(?TrackingOptions $options = null): VisitsTracker
{
return new VisitsTracker(
$this->em->reveal(),
$this->eventDispatcher->reveal(),
$options ?? new TrackingOptions(),
);
}
}