Skip to content

Commit

Permalink
[general] Upgraded date time factory dueto testing issues (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 29, 2024
1 parent 28f4d2d commit ab866bf
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 53 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"guzzlehttp/guzzle": "^7.5",
"ipub/doctrine-crud": "^5.0",
"nette/di": "^3.0",
"orisai/nette-object-mapper": "^0.1",
"orisai/object-mapper": "^0.2",
"psr/log": "^3.0",
"ramsey/uuid": "^4.5",
"react/async": "^4.0",
Expand Down
4 changes: 2 additions & 2 deletions src/API/TelevisionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function __construct(
private readonly EventLoop\LoopInterface $eventLoop,
private readonly Helpers\MessageBuilder $messageBuilder,
private readonly Viera\Logger $logger,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
)
{
$this->isEncrypted = $this->appId !== null && $this->encryptionKey !== null;
Expand All @@ -161,7 +161,7 @@ public function connect(bool $subscribe = false): void
}

$this->isConnected = true;
$this->connectedAt = $this->dateTimeFactory->getNow();
$this->connectedAt = $this->clock->getNow();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Clients/Television.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function __construct(
private readonly DevicesModels\Configuration\Channels\Repository $channelsConfigurationRepository,
private readonly DevicesModels\Configuration\Channels\Properties\Repository $channelsPropertiesConfigurationRepository,
private readonly DevicesUtilities\DeviceConnection $deviceConnectionManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
private readonly EventLoop\LoopInterface $eventLoop,
)
{
Expand Down Expand Up @@ -265,7 +265,7 @@ private function processDevice(Documents\Devices\Device $device): bool
$client->getLastConnectAttempt() === null
|| (
// phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
$this->dateTimeFactory->getNow()->getTimestamp() - $client->getLastConnectAttempt()->getTimestamp() >= self::RECONNECT_COOL_DOWN_TIME
$this->clock->getNow()->getTimestamp() - $client->getLastConnectAttempt()->getTimestamp() >= self::RECONNECT_COOL_DOWN_TIME
)
) {
try {
Expand Down Expand Up @@ -392,15 +392,15 @@ private function processDevice(Documents\Devices\Device $device): bool
if (
$cmdResult instanceof DateTimeInterface
&& (
$this->dateTimeFactory->getNow()->getTimestamp() - $cmdResult->getTimestamp()
$this->clock->getNow()->getTimestamp() - $cmdResult->getTimestamp()
< $this->deviceHelper->getStateReadingDelay($device)
)
) {
return false;
}
}

$this->processedChannelsProperties[$device->getId()->toString()][$property->getId()->toString()] = $this->dateTimeFactory->getNow();
$this->processedChannelsProperties[$device->getId()->toString()][$property->getId()->toString()] = $this->clock->getNow();

$deviceState = $this->deviceConnectionManager->getState($device);

Expand Down Expand Up @@ -515,7 +515,7 @@ private function processDevice(Documents\Devices\Device $device): bool
$result
->then(function (int|bool $value) use ($device, $property): void {
// phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
$this->processedChannelsProperties[$device->getId()->toString()][$property->getId()->toString()] = $this->dateTimeFactory->getNow();
$this->processedChannelsProperties[$device->getId()->toString()][$property->getId()->toString()] = $this->clock->getNow();

$this->queue->append(
$this->messageBuilder->create(
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Discover.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct(
private readonly DevicesModels\Entities\Devices\DevicesRepository $devicesRepository,
private readonly DevicesModels\Configuration\Connectors\Repository $connectorsConfigurationRepository,
private readonly DevicesModels\Configuration\Devices\Repository $devicesConfigurationRepository,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
private readonly Localization\Translator $translator,
string|null $name = null,
)
Expand Down Expand Up @@ -304,7 +304,7 @@ function (string|int|null $answer) use ($connectors): Documents\Connectors\Conne

$io->info((string) $this->translator->translate('//viera-connector.cmd.discover.messages.starting'));

$this->executedTime = $this->dateTimeFactory->getNow();
$this->executedTime = $this->clock->getNow();

$serviceCmd = $symfonyApp->find(DevicesCommands\Connector::NAME);

Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function __construct(
private readonly DevicesModels\Entities\Channels\ChannelsManager $channelsManager,
private readonly DevicesModels\Entities\Channels\Properties\PropertiesRepository $channelsPropertiesRepository,
private readonly ApplicationHelpers\Database $databaseHelper,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
private readonly Localization\Translator $translator,
string|null $name = null,
)
Expand Down Expand Up @@ -537,7 +537,7 @@ private function listConnectors(Style\SymfonyStyle $io): void
*/
private function createDevice(Style\SymfonyStyle $io, Entities\Connectors\Connector $connector): void
{
$tempIdentifier = 'new-device-' . $this->dateTimeFactory->getNow()->format(DateTimeInterface::ATOM);
$tempIdentifier = 'new-device-' . $this->clock->getNow()->format(DateTimeInterface::ATOM);

$ipAddress = $this->askDeviceIpAddress($io);

Expand Down Expand Up @@ -1712,7 +1712,7 @@ private function discoverDevices(Style\SymfonyStyle $io, Entities\Connectors\Con
throw new Exceptions\InvalidState('Something went wrong, console output is not configured');
}

$executedTime = $this->dateTimeFactory->getNow();
$executedTime = $this->clock->getNow();

$symfonyApp = $this->getApplication();

Expand Down
4 changes: 2 additions & 2 deletions src/Queue/Consumers/WriteChannelPropertyState.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct(
private readonly DevicesModels\Configuration\Channels\Repository $channelsConfigurationRepository,
private readonly DevicesModels\Configuration\Channels\Properties\Repository $channelsPropertiesConfigurationRepository,
private readonly DevicesModels\States\Async\ChannelPropertiesManager $channelPropertiesStatesManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
)
{
}
Expand Down Expand Up @@ -310,7 +310,7 @@ public function consume(Queue\Messages\Message $message): bool
return true;
}

$now = $this->dateTimeFactory->getNow();
$now = $this->clock->getNow();
$pending = $state->getPending();

if (
Expand Down
4 changes: 2 additions & 2 deletions src/Writers/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(
DevicesModels\Configuration\Channels\Repository $channelsConfigurationRepository,
DevicesModels\Configuration\Channels\Properties\Repository $channelsPropertiesConfigurationRepository,
DevicesModels\States\Async\ChannelPropertiesManager $channelPropertiesStatesManager,
DateTimeFactory\Factory $dateTimeFactory,
DateTimeFactory\Clock $clock,
EventLoop\LoopInterface $eventLoop,
private readonly ExchangeConsumers\Container $consumer,
)
Expand All @@ -67,7 +67,7 @@ public function __construct(
$channelsConfigurationRepository,
$channelsPropertiesConfigurationRepository,
$channelPropertiesStatesManager,
$dateTimeFactory,
$clock,
$eventLoop,
);

Expand Down
4 changes: 2 additions & 2 deletions src/Writers/Periodic.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct(
protected readonly DevicesModels\Configuration\Channels\Repository $channelsConfigurationRepository,
private readonly DevicesModels\Configuration\Channels\Properties\Repository $channelsPropertiesConfigurationRepository,
private readonly DevicesModels\States\Async\ChannelPropertiesManager $channelPropertiesStatesManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
private readonly EventLoop\LoopInterface $eventLoop,
)
{
Expand Down Expand Up @@ -196,7 +196,7 @@ private function handleCommunication(): void
*/
private function writeProperty(Documents\Devices\Device $device): bool
{
$now = $this->dateTimeFactory->getNow();
$now = $this->clock->getNow();

foreach ($this->properties[$device->getId()->toString()] as $property) {
$debounce = array_key_exists($property->getId()->toString(), $this->processedProperties)
Expand Down
33 changes: 0 additions & 33 deletions tests/cases/unit/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace FastyBird\Connector\Viera\Tests\Cases\Unit;

use DateTimeImmutable;
use Doctrine\DBAL;
use Doctrine\ORM;
use Error;
use FastyBird\Connector\Viera\DI;
use FastyBird\Connector\Viera\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;
Expand Down Expand Up @@ -49,41 +46,11 @@ abstract class DbTestCase extends TestCase
/** @var array<string> */
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
Expand Down
5 changes: 4 additions & 1 deletion tests/common.neon
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,7 @@ fbSimpleAuth:

fbJsonApi:
meta:
copyright: FastyBird s.r.o
copyright: FastyBird s.r.o

fbDateTimeFactory:
frozen: DateTimeImmutable('2020-04-01T12:00:00+00:00')

0 comments on commit ab866bf

Please sign in to comment.