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.
This commit is contained in:
Daniel Siepmann 2022-07-07 09:03:31 +02:00
parent 3d98475a6c
commit 6b6ae5b20c
3 changed files with 86 additions and 11 deletions

View file

@ -2,10 +2,22 @@
namespace Wrm\Events\Service\DestinationDataImportService; namespace Wrm\Events\Service\DestinationDataImportService;
use TYPO3\CMS\Core\Context\Context;
use Wrm\Events\Domain\Model\Date; use Wrm\Events\Domain\Model\Date;
class DatesFactory class DatesFactory
{ {
/**
* @var Context
*/
private $context;
public function __construct(
Context $context
) {
$this->context = $context;
}
/** /**
* @return \Generator<Date> * @return \Generator<Date>
*/ */
@ -178,6 +190,12 @@ class DatesFactory
private function getToday(): int 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');
} }
} }

View file

@ -3,6 +3,8 @@
namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\DateTimeAspect;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
class ImportDoesntEndUpInEndlessDateCreationTest extends AbstractTest class ImportDoesntEndUpInEndlessDateCreationTest extends AbstractTest
@ -16,6 +18,7 @@ class ImportDoesntEndUpInEndlessDateCreationTest extends AbstractTest
$fileImportPath = $this->getInstancePath() . '/fileadmin/' . $fileImportPathConfiguration; $fileImportPath = $this->getInstancePath() . '/fileadmin/' . $fileImportPathConfiguration;
GeneralUtility::mkdir_deep($fileImportPath); 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/SingleRegion.xml');
$this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleCategory.xml'); $this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleCategory.xml');
$this->setUpConfiguration([ $this->setUpConfiguration([
@ -49,4 +52,15 @@ class ImportDoesntEndUpInEndlessDateCreationTest extends AbstractTest
'Logfile was not empty.' '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);
}
} }

View file

@ -5,20 +5,28 @@ declare(strict_types=1);
namespace Wrm\Events\Tests\Unit\Service\DestinationDataImportService; namespace Wrm\Events\Tests\Unit\Service\DestinationDataImportService;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use TYPO3\CMS\Core\Context\Context;
use Wrm\Events\Domain\Model\Date; use Wrm\Events\Domain\Model\Date;
use Wrm\Events\Service\DestinationDataImportService\DatesFactory; use Wrm\Events\Service\DestinationDataImportService\DatesFactory;
use Wrm\Events\Tests\ProphecyTrait;
/** /**
* @covers \Wrm\Events\Service\DestinationDataImportService\DatesFactory * @covers \Wrm\Events\Service\DestinationDataImportService\DatesFactory
*/ */
class DatesFactoryTest extends TestCase class DatesFactoryTest extends TestCase
{ {
use ProphecyTrait;
/** /**
* @test * @test
*/ */
public function canBeCreated(): void public function canBeCreated(): void
{ {
$subject = new DatesFactory(); $context = $this->prophesize(Context::class);
$subject = new DatesFactory(
$context->reveal()
);
self::assertInstanceOf( self::assertInstanceOf(
DatesFactory::class, DatesFactory::class,
@ -32,7 +40,11 @@ class DatesFactoryTest extends TestCase
*/ */
public function returnsNoResultOnUnkownInput(array $unkownInput): void public function returnsNoResultOnUnkownInput(array $unkownInput): void
{ {
$subject = new DatesFactory(); $context = $this->prophesize(Context::class);
$subject = new DatesFactory(
$context->reveal()
);
$result = $subject->createDates($unkownInput, false); $result = $subject->createDates($unkownInput, false);
@ -60,7 +72,11 @@ class DatesFactoryTest extends TestCase
*/ */
public function returnsSingleNotCanceledDate(): void public function returnsSingleNotCanceledDate(): void
{ {
$subject = new DatesFactory(); $context = $this->prophesize(Context::class);
$subject = new DatesFactory(
$context->reveal()
);
$result = $subject->createDates([[ $result = $subject->createDates([[
'start' => '2099-06-21T16:00:00+02:00', 'start' => '2099-06-21T16:00:00+02:00',
@ -86,7 +102,11 @@ class DatesFactoryTest extends TestCase
*/ */
public function returnsSingleCanceledDate(): void public function returnsSingleCanceledDate(): void
{ {
$subject = new DatesFactory(); $context = $this->prophesize(Context::class);
$subject = new DatesFactory(
$context->reveal()
);
$result = $subject->createDates([[ $result = $subject->createDates([[
'start' => '2099-06-21T16:00:00+02:00', 'start' => '2099-06-21T16:00:00+02:00',
@ -111,7 +131,11 @@ class DatesFactoryTest extends TestCase
*/ */
public function returnsCanceledDatesOnDailyBasis(): void public function returnsCanceledDatesOnDailyBasis(): void
{ {
$subject = new DatesFactory(); $context = $this->prophesize(Context::class);
$subject = new DatesFactory(
$context->reveal()
);
$result = $subject->createDates([[ $result = $subject->createDates([[
'start' => '2099-04-01T16:00:00+02:00', 'start' => '2099-04-01T16:00:00+02:00',
@ -142,8 +166,11 @@ class DatesFactoryTest extends TestCase
*/ */
public function returnsNotCanceledDatesOnDailyBasis(): void public function returnsNotCanceledDatesOnDailyBasis(): void
{ {
$context = $this->prophesize(Context::class);
$subject = new DatesFactory(); $subject = new DatesFactory(
$context->reveal()
);
$result = $subject->createDates([[ $result = $subject->createDates([[
'start' => '2099-04-01T16:00:00+02:00', 'start' => '2099-04-01T16:00:00+02:00',
@ -174,7 +201,11 @@ class DatesFactoryTest extends TestCase
*/ */
public function returnsCanceledDatesOnWeeklyBasis(): void public function returnsCanceledDatesOnWeeklyBasis(): void
{ {
$subject = new DatesFactory(); $context = $this->prophesize(Context::class);
$subject = new DatesFactory(
$context->reveal()
);
$result = $subject->createDates([[ $result = $subject->createDates([[
'weekdays' => [ 'weekdays' => [
@ -209,7 +240,11 @@ class DatesFactoryTest extends TestCase
*/ */
public function returnsNotCanceledDatesOnWeeklyBasis(): void public function returnsNotCanceledDatesOnWeeklyBasis(): void
{ {
$subject = new DatesFactory(); $context = $this->prophesize(Context::class);
$subject = new DatesFactory(
$context->reveal()
);
$result = $subject->createDates([[ $result = $subject->createDates([[
'weekdays' => [ 'weekdays' => [
@ -244,7 +279,11 @@ class DatesFactoryTest extends TestCase
*/ */
public function returnsCanceledDatesOnMixedIntervals(): void public function returnsCanceledDatesOnMixedIntervals(): void
{ {
$subject = new DatesFactory(); $context = $this->prophesize(Context::class);
$subject = new DatesFactory(
$context->reveal()
);
$result = $subject->createDates([ $result = $subject->createDates([
[ [
@ -291,7 +330,11 @@ class DatesFactoryTest extends TestCase
*/ */
public function returnsNotCanceledDatesOnMixedIntervals(): void public function returnsNotCanceledDatesOnMixedIntervals(): void
{ {
$subject = new DatesFactory(); $context = $this->prophesize(Context::class);
$subject = new DatesFactory(
$context->reveal()
);
$result = $subject->createDates([ $result = $subject->createDates([
[ [