diff --git a/module/CLI/src/Command/Visit/AbstractVisitsListCommand.php b/module/CLI/src/Command/Visit/AbstractVisitsListCommand.php index a3e8a43c..d3a49c53 100644 --- a/module/CLI/src/Command/Visit/AbstractVisitsListCommand.php +++ b/module/CLI/src/Command/Visit/AbstractVisitsListCommand.php @@ -55,7 +55,7 @@ abstract class AbstractVisitsListCommand extends Command $rowData = [ 'referer' => $visit->referer, - 'date' => $visit->getDate()->toAtomString(), + 'date' => $visit->date->toAtomString(), 'userAgent' => $visit->userAgent, 'potentialBot' => $visit->potentialBot, 'country' => $visit->getVisitLocation()?->countryName ?? 'Unknown', diff --git a/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php b/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php index 7f4bd076..6563abc0 100644 --- a/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php +++ b/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php @@ -60,7 +60,7 @@ class GetDomainVisitsCommandTest extends TestCase +---------+---------------------------+------------+---------+--------+---------------+ | Referer | Date | User agent | Country | City | Short Url | +---------+---------------------------+------------+---------+--------+---------------+ - | foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | the_short_url | + | foo | {$visit->date->toAtomString()} | bar | Spain | Madrid | the_short_url | +---------+---------------------------+------------+---------+--------+---------------+ OUTPUT, diff --git a/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php b/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php index f93ab5ec..ba6735ba 100644 --- a/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php @@ -110,7 +110,7 @@ class GetShortUrlVisitsCommandTest extends TestCase +---------+---------------------------+------------+---------+--------+ | Referer | Date | User agent | Country | City | +---------+---------------------------+------------+---------+--------+ - | foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | + | foo | {$visit->date->toAtomString()} | bar | Spain | Madrid | +---------+---------------------------+------------+---------+--------+ OUTPUT, diff --git a/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php b/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php index a2dc059f..9b79f509 100644 --- a/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php +++ b/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php @@ -57,7 +57,7 @@ class GetTagVisitsCommandTest extends TestCase +---------+---------------------------+------------+---------+--------+---------------+ | Referer | Date | User agent | Country | City | Short Url | +---------+---------------------------+------------+---------+--------+---------------+ - | foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | the_short_url | + | foo | {$visit->date->toAtomString()} | bar | Spain | Madrid | the_short_url | +---------+---------------------------+------------+---------+--------+---------------+ OUTPUT, diff --git a/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php b/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php index 439b33bd..0462c2c0 100644 --- a/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php @@ -56,7 +56,7 @@ class GetNonOrphanVisitsCommandTest extends TestCase +---------+---------------------------+------------+---------+--------+---------------+ | Referer | Date | User agent | Country | City | Short Url | +---------+---------------------------+------------+---------+--------+---------------+ - | foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | the_short_url | + | foo | {$visit->date->toAtomString()} | bar | Spain | Madrid | the_short_url | +---------+---------------------------+------------+---------+--------+---------------+ OUTPUT, diff --git a/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php b/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php index a9e2a50c..29914b61 100644 --- a/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php @@ -54,7 +54,7 @@ class GetOrphanVisitsCommandTest extends TestCase +---------+---------------------------+------------+---------+--------+----------+ | Referer | Date | User agent | Country | City | Type | +---------+---------------------------+------------+---------+--------+----------+ - | foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | base_url | + | foo | {$visit->date->toAtomString()} | bar | Spain | Madrid | base_url | +---------+---------------------------+------------+---------+--------+----------+ OUTPUT, diff --git a/module/Core/src/Importer/ImportedLinksProcessor.php b/module/Core/src/Importer/ImportedLinksProcessor.php index 7a9c3b92..16da0a09 100644 --- a/module/Core/src/Importer/ImportedLinksProcessor.php +++ b/module/Core/src/Importer/ImportedLinksProcessor.php @@ -139,7 +139,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface $importedVisits = 0; foreach ($iterable as $importedOrphanVisit) { // Skip visits which are older than the most recent already imported visit's date - if ($mostRecentOrphanVisit?->getDate()->greaterThanOrEquals(normalizeDate($importedOrphanVisit->date))) { + if ($mostRecentOrphanVisit?->date->greaterThanOrEquals(normalizeDate($importedOrphanVisit->date))) { continue; } diff --git a/module/Core/src/ShortUrl/Entity/ShortUrl.php b/module/Core/src/ShortUrl/Entity/ShortUrl.php index ed97f9f5..084acabd 100644 --- a/module/Core/src/ShortUrl/Entity/ShortUrl.php +++ b/module/Core/src/ShortUrl/Entity/ShortUrl.php @@ -209,7 +209,7 @@ class ShortUrl extends AbstractEntity ->setMaxResults(1); $visit = $this->visits->matching($criteria)->last(); - return $visit instanceof Visit ? $visit->getDate() : null; + return $visit instanceof Visit ? $visit->date : null; } /** diff --git a/module/Core/src/Visit/Entity/Visit.php b/module/Core/src/Visit/Entity/Visit.php index 86854945..be8400dc 100644 --- a/module/Core/src/Visit/Entity/Visit.php +++ b/module/Core/src/Visit/Entity/Visit.php @@ -29,8 +29,7 @@ class Visit extends AbstractEntity implements JsonSerializable public readonly ?string $remoteAddr = null, public readonly ?string $visitedUrl = null, private ?VisitLocation $visitLocation = null, - // TODO Make public readonly once VisitRepositoryTest does not try to set it - private Chronos $date = new Chronos(), + public readonly Chronos $date = new Chronos(), ) { } @@ -147,14 +146,6 @@ class Visit extends AbstractEntity implements JsonSerializable return $this->type; } - /** - * @internal - */ - public function getDate(): Chronos - { - return $this->date; - } - public function jsonSerialize(): array { $base = [ diff --git a/module/Core/test-db/Visit/Repository/VisitRepositoryTest.php b/module/Core/test-db/Visit/Repository/VisitRepositoryTest.php index 1506dd1a..9dc18390 100644 --- a/module/Core/test-db/Visit/Repository/VisitRepositoryTest.php +++ b/module/Core/test-db/Visit/Repository/VisitRepositoryTest.php @@ -6,7 +6,6 @@ namespace ShlinkioDbTest\Shlink\Core\Visit\Repository; use Cake\Chronos\Chronos; use PHPUnit\Framework\Attributes\Test; -use ReflectionObject; use Shlinkio\Shlink\Common\Util\DateRange; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; @@ -371,15 +370,15 @@ class VisitRepositoryTest extends DatabaseTestCase $botsCount = 3; for ($i = 0; $i < 6; $i++) { $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forBasePath($botsCount < 1 ? Visitor::emptyInstance() : Visitor::botInstance()), + fn () => Visit::forBasePath($botsCount < 1 ? Visitor::emptyInstance() : Visitor::botInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forInvalidShortUrl(Visitor::emptyInstance()), + fn () => Visit::forInvalidShortUrl(Visitor::emptyInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forRegularNotFound(Visitor::emptyInstance()), + fn () => Visit::forRegularNotFound(Visitor::emptyInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); @@ -429,15 +428,15 @@ class VisitRepositoryTest extends DatabaseTestCase for ($i = 0; $i < 6; $i++) { $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forBasePath(Visitor::emptyInstance()), + fn () => Visit::forBasePath(Visitor::emptyInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forInvalidShortUrl(Visitor::emptyInstance()), + fn () => Visit::forInvalidShortUrl(Visitor::emptyInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); $this->getEntityManager()->persist($this->setDateOnVisit( - Visit::forRegularNotFound(Visitor::emptyInstance()), + fn () => Visit::forRegularNotFound(Visitor::emptyInstance()), Chronos::parse(sprintf('2020-01-0%s', $i + 1)), )); } @@ -566,7 +565,7 @@ class VisitRepositoryTest extends DatabaseTestCase { for ($i = 0; $i < $amount; $i++) { $visit = $this->setDateOnVisit( - Visit::forValidShortUrl( + fn () => Visit::forValidShortUrl( $shortUrl, $botsAmount < 1 ? Visitor::emptyInstance() : Visitor::botInstance(), ), @@ -578,12 +577,14 @@ class VisitRepositoryTest extends DatabaseTestCase } } - private function setDateOnVisit(Visit $visit, Chronos $date): Visit + /** + * @param callable(): Visit $createVisit + */ + private function setDateOnVisit(callable $createVisit, Chronos $date): Visit { - $ref = new ReflectionObject($visit); - $dateProp = $ref->getProperty('date'); - $dateProp->setAccessible(true); - $dateProp->setValue($visit, $date); + Chronos::setTestNow($date); + $visit = $createVisit(); + Chronos::setTestNow(); return $visit; } diff --git a/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php b/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php index 2dd1c0c7..bd3c82c8 100644 --- a/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php +++ b/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php @@ -76,7 +76,7 @@ class PublishingUpdatesGeneratorTest extends TestCase 'referer' => '', 'userAgent' => '', 'visitLocation' => null, - 'date' => $visit->getDate()->toAtomString(), + 'date' => $visit->date->toAtomString(), 'potentialBot' => false, 'visitedUrl' => '', ], @@ -100,7 +100,7 @@ class PublishingUpdatesGeneratorTest extends TestCase 'referer' => '', 'userAgent' => '', 'visitLocation' => null, - 'date' => $orphanVisit->getDate()->toAtomString(), + 'date' => $orphanVisit->date->toAtomString(), 'potentialBot' => false, 'visitedUrl' => $orphanVisit->visitedUrl, 'type' => $orphanVisit->type->value, diff --git a/module/Core/test/Visit/Entity/VisitTest.php b/module/Core/test/Visit/Entity/VisitTest.php index d9c50af6..923b2e6b 100644 --- a/module/Core/test/Visit/Entity/VisitTest.php +++ b/module/Core/test/Visit/Entity/VisitTest.php @@ -26,7 +26,7 @@ class VisitTest extends TestCase self::assertEquals([ 'referer' => 'some site', - 'date' => $visit->getDate()->toAtomString(), + 'date' => $visit->date->toAtomString(), 'userAgent' => $userAgent, 'visitLocation' => null, 'potentialBot' => $expectedToBePotentialBot, @@ -58,7 +58,7 @@ class VisitTest extends TestCase $visit = Visit::forBasePath(Visitor::emptyInstance()), [ 'referer' => '', - 'date' => $visit->getDate()->toAtomString(), + 'date' => $visit->date->toAtomString(), 'userAgent' => '', 'visitLocation' => null, 'potentialBot' => false, @@ -74,7 +74,7 @@ class VisitTest extends TestCase )), [ 'referer' => 'bar', - 'date' => $visit->getDate()->toAtomString(), + 'date' => $visit->date->toAtomString(), 'userAgent' => 'foo', 'visitLocation' => null, 'potentialBot' => false, @@ -92,7 +92,7 @@ class VisitTest extends TestCase )->locate($location = VisitLocation::fromGeolocation(Location::emptyInstance())), [ 'referer' => 'referer', - 'date' => $visit->getDate()->toAtomString(), + 'date' => $visit->date->toAtomString(), 'userAgent' => 'user-agent', 'visitLocation' => $location, 'potentialBot' => false, diff --git a/module/Rest/test-api/Fixtures/VisitsFixture.php b/module/Rest/test-api/Fixtures/VisitsFixture.php index 6076f95e..9972e3a8 100644 --- a/module/Rest/test-api/Fixtures/VisitsFixture.php +++ b/module/Rest/test-api/Fixtures/VisitsFixture.php @@ -8,7 +8,6 @@ use Cake\Chronos\Chronos; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\DependentFixtureInterface; use Doctrine\Persistence\ObjectManager; -use ReflectionObject; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Model\Visitor; @@ -50,27 +49,31 @@ class VisitsFixture extends AbstractFixture implements DependentFixtureInterface ); $manager->persist($this->setVisitDate( - Visit::forBasePath(new Visitor('shlink-tests-agent', 'https://s.test', '1.2.3.4', '')), + fn () => Visit::forBasePath(new Visitor('shlink-tests-agent', 'https://s.test', '1.2.3.4', '')), '2020-01-01', )); $manager->persist($this->setVisitDate( - Visit::forRegularNotFound(new Visitor('shlink-tests-agent', 'https://s.test/foo/bar', '1.2.3.4', '')), + fn () => Visit::forRegularNotFound( + new Visitor('shlink-tests-agent', 'https://s.test/foo/bar', '1.2.3.4', ''), + ), '2020-02-01', )); $manager->persist($this->setVisitDate( - Visit::forInvalidShortUrl(new Visitor('cf-facebook', 'https://s.test/foo', '1.2.3.4', 'foo.com')), + fn () => Visit::forInvalidShortUrl(new Visitor('cf-facebook', 'https://s.test/foo', '1.2.3.4', 'foo.com')), '2020-03-01', )); $manager->flush(); } - private function setVisitDate(Visit $visit, string $date): Visit + /** + * @param callable(): Visit $createVisit + */ + private function setVisitDate(callable $createVisit, string $date): Visit { - $ref = new ReflectionObject($visit); - $dateProp = $ref->getProperty('date'); - $dateProp->setAccessible(true); - $dateProp->setValue($visit, Chronos::parse($date)); + Chronos::setTestNow($date); + $visit = $createVisit(); + Chronos::setTestNow(); return $visit; }