From 6b6ae5b20ce0dabb638d549d3ac794681b2ba446 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 7 Jul 2022 09:03:31 +0200 Subject: [PATCH] Do not break tests once a certain date is passed The code used native date functions, this made tests not robust. We switch to TYPO3 API to fetch the current date and time. Test is adjusted to define the date and time to verify code works as expected. --- .../DatesFactory.php | 20 +++++- ...rtDoesntEndUpInEndlessDateCreationTest.php | 14 +++++ .../DatesFactoryTest.php | 63 ++++++++++++++++--- 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/Classes/Service/DestinationDataImportService/DatesFactory.php b/Classes/Service/DestinationDataImportService/DatesFactory.php index 9baf86f..b3abcc7 100644 --- a/Classes/Service/DestinationDataImportService/DatesFactory.php +++ b/Classes/Service/DestinationDataImportService/DatesFactory.php @@ -2,10 +2,22 @@ namespace Wrm\Events\Service\DestinationDataImportService; +use TYPO3\CMS\Core\Context\Context; use Wrm\Events\Domain\Model\Date; class DatesFactory { + /** + * @var Context + */ + private $context; + + public function __construct( + Context $context + ) { + $this->context = $context; + } + /** * @return \Generator */ @@ -178,6 +190,12 @@ class DatesFactory private function getToday(): int { - return (int) date('U', strtotime('midnight')); + $today = $this->context->getPropertyFromAspect('date', 'full', new \DateTimeImmutable()); + if (!$today instanceof \DateTimeImmutable) { + $today = new \DateTimeImmutable(); + } + + $midnight = $today->modify('midnight'); + return (int) $midnight->format('U'); } } diff --git a/Tests/Functional/Import/DestinationDataTest/ImportDoesntEndUpInEndlessDateCreationTest.php b/Tests/Functional/Import/DestinationDataTest/ImportDoesntEndUpInEndlessDateCreationTest.php index acc4c67..99814ed 100644 --- a/Tests/Functional/Import/DestinationDataTest/ImportDoesntEndUpInEndlessDateCreationTest.php +++ b/Tests/Functional/Import/DestinationDataTest/ImportDoesntEndUpInEndlessDateCreationTest.php @@ -3,6 +3,8 @@ namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; use GuzzleHttp\Psr7\Response; +use TYPO3\CMS\Core\Context\Context; +use TYPO3\CMS\Core\Context\DateTimeAspect; use TYPO3\CMS\Core\Utility\GeneralUtility; class ImportDoesntEndUpInEndlessDateCreationTest extends AbstractTest @@ -16,6 +18,7 @@ class ImportDoesntEndUpInEndlessDateCreationTest extends AbstractTest $fileImportPath = $this->getInstancePath() . '/fileadmin/' . $fileImportPathConfiguration; GeneralUtility::mkdir_deep($fileImportPath); + $this->setDateAspect(new \DateTimeImmutable('2022-07-01')); $this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleRegion.xml'); $this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleCategory.xml'); $this->setUpConfiguration([ @@ -49,4 +52,15 @@ class ImportDoesntEndUpInEndlessDateCreationTest extends AbstractTest 'Logfile was not empty.' ); } + + private function setDateAspect(\DateTimeImmutable $dateTime): void + { + $context = $this->getContainer()->get(Context::class); + if (!$context instanceof Context) { + throw new \TypeError('Retrieved context was of unexpected type.', 1638182021); + } + + $aspect = new DateTimeAspect($dateTime); + $context->setAspect('date', $aspect); + } } diff --git a/Tests/Unit/Service/DestinationDataImportService/DatesFactoryTest.php b/Tests/Unit/Service/DestinationDataImportService/DatesFactoryTest.php index bf3454f..7dc295e 100644 --- a/Tests/Unit/Service/DestinationDataImportService/DatesFactoryTest.php +++ b/Tests/Unit/Service/DestinationDataImportService/DatesFactoryTest.php @@ -5,20 +5,28 @@ declare(strict_types=1); namespace Wrm\Events\Tests\Unit\Service\DestinationDataImportService; use PHPUnit\Framework\TestCase; +use TYPO3\CMS\Core\Context\Context; use Wrm\Events\Domain\Model\Date; use Wrm\Events\Service\DestinationDataImportService\DatesFactory; +use Wrm\Events\Tests\ProphecyTrait; /** * @covers \Wrm\Events\Service\DestinationDataImportService\DatesFactory */ class DatesFactoryTest extends TestCase { + use ProphecyTrait; + /** * @test */ public function canBeCreated(): void { - $subject = new DatesFactory(); + $context = $this->prophesize(Context::class); + + $subject = new DatesFactory( + $context->reveal() + ); self::assertInstanceOf( DatesFactory::class, @@ -32,7 +40,11 @@ class DatesFactoryTest extends TestCase */ public function returnsNoResultOnUnkownInput(array $unkownInput): void { - $subject = new DatesFactory(); + $context = $this->prophesize(Context::class); + + $subject = new DatesFactory( + $context->reveal() + ); $result = $subject->createDates($unkownInput, false); @@ -60,7 +72,11 @@ class DatesFactoryTest extends TestCase */ public function returnsSingleNotCanceledDate(): void { - $subject = new DatesFactory(); + $context = $this->prophesize(Context::class); + + $subject = new DatesFactory( + $context->reveal() + ); $result = $subject->createDates([[ 'start' => '2099-06-21T16:00:00+02:00', @@ -86,7 +102,11 @@ class DatesFactoryTest extends TestCase */ public function returnsSingleCanceledDate(): void { - $subject = new DatesFactory(); + $context = $this->prophesize(Context::class); + + $subject = new DatesFactory( + $context->reveal() + ); $result = $subject->createDates([[ 'start' => '2099-06-21T16:00:00+02:00', @@ -111,7 +131,11 @@ class DatesFactoryTest extends TestCase */ public function returnsCanceledDatesOnDailyBasis(): void { - $subject = new DatesFactory(); + $context = $this->prophesize(Context::class); + + $subject = new DatesFactory( + $context->reveal() + ); $result = $subject->createDates([[ 'start' => '2099-04-01T16:00:00+02:00', @@ -142,8 +166,11 @@ class DatesFactoryTest extends TestCase */ public function returnsNotCanceledDatesOnDailyBasis(): void { + $context = $this->prophesize(Context::class); - $subject = new DatesFactory(); + $subject = new DatesFactory( + $context->reveal() + ); $result = $subject->createDates([[ 'start' => '2099-04-01T16:00:00+02:00', @@ -174,7 +201,11 @@ class DatesFactoryTest extends TestCase */ public function returnsCanceledDatesOnWeeklyBasis(): void { - $subject = new DatesFactory(); + $context = $this->prophesize(Context::class); + + $subject = new DatesFactory( + $context->reveal() + ); $result = $subject->createDates([[ 'weekdays' => [ @@ -209,7 +240,11 @@ class DatesFactoryTest extends TestCase */ public function returnsNotCanceledDatesOnWeeklyBasis(): void { - $subject = new DatesFactory(); + $context = $this->prophesize(Context::class); + + $subject = new DatesFactory( + $context->reveal() + ); $result = $subject->createDates([[ 'weekdays' => [ @@ -244,7 +279,11 @@ class DatesFactoryTest extends TestCase */ public function returnsCanceledDatesOnMixedIntervals(): void { - $subject = new DatesFactory(); + $context = $this->prophesize(Context::class); + + $subject = new DatesFactory( + $context->reveal() + ); $result = $subject->createDates([ [ @@ -291,7 +330,11 @@ class DatesFactoryTest extends TestCase */ public function returnsNotCanceledDatesOnMixedIntervals(): void { - $subject = new DatesFactory(); + $context = $this->prophesize(Context::class); + + $subject = new DatesFactory( + $context->reveal() + ); $result = $subject->createDates([ [