From f967f670a7aaf54d0a5c89aed28c88883c7131ec Mon Sep 17 00:00:00 2001 From: GitHub Date: Thu, 29 Aug 2024 19:52:01 +0000 Subject: [PATCH] [general] Upgraded date time factory dueto testing issues (#311) --- composer.json | 2 +- src/Controllers/CharacteristicsController.php | 6 ++-- src/Writers/Exchange.php | 4 +-- src/Writers/Periodic.php | 4 +-- tests/cases/unit/DbTestCase.php | 33 ------------------- tests/common.neon | 5 ++- 6 files changed, 12 insertions(+), 42 deletions(-) diff --git a/composer.json b/composer.json index c5acdeb..2c5512f 100644 --- a/composer.json +++ b/composer.json @@ -52,7 +52,7 @@ "ipub/slim-router": "^0.2", "leigh/curve25519": "^0.1", "nette/di": "^3.0", - "orisai/nette-object-mapper": "^0.1", + "orisai/object-mapper": "^0.2", "psr/event-dispatcher": "^1.0", "psr/log": "^3.0", "endroid/qr-code": "4.5", diff --git a/src/Controllers/CharacteristicsController.php b/src/Controllers/CharacteristicsController.php index 19d4ea9..e0b1ea2 100644 --- a/src/Controllers/CharacteristicsController.php +++ b/src/Controllers/CharacteristicsController.php @@ -65,7 +65,7 @@ public function __construct( private readonly Queue\Queue $queue, private readonly Protocol\Driver $accessoryDriver, private readonly Clients\Subscriber $subscriber, - private readonly DateTimeFactory\Factory $dateTimeFactory, + private readonly DateTimeFactory\Clock $clock, ) { } @@ -269,7 +269,7 @@ public function update( || !array_key_exists($pid, $this->preparedWrites[strval($requestParams['REMOTE_ADDR'])]) || $this->preparedWrites[strval( $requestParams['REMOTE_ADDR'], - )][$pid] < $this->dateTimeFactory->getNow()->getTimestamp() + )][$pid] < $this->clock->getNow()->getTimestamp() ) { $timedWriteError = true; } @@ -403,7 +403,7 @@ public function prepare( } $this->preparedWrites[$clientAddress][intval($body[Types\Representation::PID->value])] - = $this->dateTimeFactory->getNow()->getTimestamp() + (intval( + = $this->clock->getNow()->getTimestamp() + (intval( $body[Types\Representation::TTL->value], ) / 1_000); diff --git a/src/Writers/Exchange.php b/src/Writers/Exchange.php index 8bdba0d..60d84a5 100644 --- a/src/Writers/Exchange.php +++ b/src/Writers/Exchange.php @@ -65,7 +65,7 @@ public function __construct( DevicesModels\Configuration\Channels\Properties\Repository $channelsPropertiesConfigurationRepository, DevicesModels\States\Async\DevicePropertiesManager $devicePropertiesStatesManager, DevicesModels\States\Async\ChannelPropertiesManager $channelPropertiesStatesManager, - DateTimeFactory\Factory $dateTimeFactory, + DateTimeFactory\Clock $clock, EventLoop\LoopInterface $eventLoop, private readonly ExchangeConsumers\Container $consumer, private readonly PsrEventDispatcher\EventDispatcherInterface|null $dispatcher = null, @@ -82,7 +82,7 @@ public function __construct( $accessoryDriver, $devicePropertiesStatesManager, $channelPropertiesStatesManager, - $dateTimeFactory, + $clock, $eventLoop, ); diff --git a/src/Writers/Periodic.php b/src/Writers/Periodic.php index 533f750..934545b 100644 --- a/src/Writers/Periodic.php +++ b/src/Writers/Periodic.php @@ -83,7 +83,7 @@ public function __construct( private readonly Protocol\Driver $accessoryDriver, private readonly DevicesModels\States\Async\DevicePropertiesManager $devicePropertiesStatesManager, private readonly DevicesModels\States\Async\ChannelPropertiesManager $channelPropertiesStatesManager, - private readonly DateTimeFactory\Factory $dateTimeFactory, + private readonly DateTimeFactory\Clock $clock, private readonly EventLoop\LoopInterface $eventLoop, ) { @@ -209,7 +209,7 @@ private function handleCommunication(): void */ private function writeProperty(Documents\Devices\Device $device): bool { - $now = $this->dateTimeFactory->getNow(); + $now = $this->clock->getNow(); $accessory = $this->accessoryDriver->findAccessory($device->getId()); diff --git a/tests/cases/unit/DbTestCase.php b/tests/cases/unit/DbTestCase.php index 96cbc6d..e813c61 100644 --- a/tests/cases/unit/DbTestCase.php +++ b/tests/cases/unit/DbTestCase.php @@ -2,16 +2,13 @@ namespace FastyBird\Connector\HomeKit\Tests\Cases\Unit; -use DateTimeImmutable; use Doctrine\DBAL; use Doctrine\ORM; use Error; use FastyBird\Connector\HomeKit\DI; use FastyBird\Connector\HomeKit\Exceptions; -use FastyBird\DateTimeFactory; use FastyBird\Library\Application\Boot as ApplicationBoot; use FastyBird\Library\Application\Exceptions as ApplicationExceptions; -use FastyBird\Library\Application\Utilities as ApplicationUtilities; use IPub\DoctrineCrud; use Nette; use Nettrine\ORM as NettrineORM; @@ -49,41 +46,11 @@ abstract class DbTestCase extends TestCase /** @var array */ private array $neonFiles = []; - /** - * @throws ApplicationExceptions\InvalidArgument - * @throws Exceptions\InvalidArgument - * @throws Nette\DI\MissingServiceException - * @throws RuntimeException - * @throws Error - */ public function setUp(): void { $this->registerDatabaseSchemaFile(__DIR__ . '/../../sql/dummy.data.sql'); parent::setUp(); - - $dateTimeFactory = $this->createMock(DateTimeFactory\Factory::class); - $dateTimeFactory - ->method('getNow') - ->willReturn(new DateTimeImmutable('2020-04-01T12:00:00+00:00')); - - $this->mockContainerService( - DateTimeFactory\Factory::class, - $dateTimeFactory, - ); - - $dateTimeProvider = $this->createMock(ApplicationUtilities\DateTimeProvider::class); - $dateTimeProvider - ->method('getDate') - ->willReturn($dateTimeFactory->getNow()); - $dateTimeProvider - ->method('getTimestamp') - ->willReturn($dateTimeFactory->getNow()->getTimestamp()); - - $this->mockContainerService( - ApplicationUtilities\DateTimeProvider::class, - $dateTimeProvider, - ); } protected function registerDatabaseSchemaFile(string $file): void diff --git a/tests/common.neon b/tests/common.neon index 869653f..06ed8b3 100644 --- a/tests/common.neon +++ b/tests/common.neon @@ -101,4 +101,7 @@ fbSimpleAuth: fbJsonApi: meta: - copyright: FastyBird s.r.o \ No newline at end of file + copyright: FastyBird s.r.o + +fbDateTimeFactory: + frozen: DateTimeImmutable('2020-04-01T12:00:00+00:00') \ No newline at end of file