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 71efce0 commit 1845335
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 43 deletions.
18 changes: 9 additions & 9 deletions src/Drivers/Thermostat.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function __construct(
private readonly VirtualThermostat\Logger $logger,
private readonly DevicesModels\Configuration\Channels\Repository $channelsConfigurationRepository,
private readonly DevicesModels\States\ChannelPropertiesManager $channelPropertiesStatesManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
)
{
$this->presetMode = Types\Preset::MANUAL;
Expand Down Expand Up @@ -395,7 +395,7 @@ public function connect(): Promise\PromiseInterface
}

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

return Promise\resolve(true);
}
Expand Down Expand Up @@ -458,7 +458,7 @@ public function process(): Promise\PromiseInterface
return Promise\reject(new Exceptions\InvalidState('Thermostat device is not connected'));
}

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

if ($this->hvacMode === null || $this->presetMode === null) {
$this->stop('Thermostat mode is not configured');
Expand Down Expand Up @@ -888,7 +888,7 @@ public function notifyState(
if (
$this->lastProcessedTime instanceof DateTimeInterface
&& (
$this->dateTimeFactory->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
$this->clock->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
< self::PROCESSING_DEBOUNCE_DELAY
)
) {
Expand All @@ -914,7 +914,7 @@ public function notifyState(
if (
$this->lastProcessedTime instanceof DateTimeInterface
&& (
$this->dateTimeFactory->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
$this->clock->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
< self::PROCESSING_DEBOUNCE_DELAY
)
) {
Expand Down Expand Up @@ -947,7 +947,7 @@ public function notifyState(
if (
$this->lastProcessedTime instanceof DateTimeInterface
&& (
$this->dateTimeFactory->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
$this->clock->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
< self::PROCESSING_DEBOUNCE_DELAY
)
) {
Expand All @@ -974,7 +974,7 @@ public function notifyState(
if (
$this->lastProcessedTime instanceof DateTimeInterface
&& (
$this->dateTimeFactory->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
$this->clock->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
< self::PROCESSING_DEBOUNCE_DELAY
)
) {
Expand Down Expand Up @@ -1006,7 +1006,7 @@ public function notifyState(
if (
$this->lastProcessedTime instanceof DateTimeInterface
&& (
$this->dateTimeFactory->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
$this->clock->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
< self::PROCESSING_DEBOUNCE_DELAY
)
) {
Expand Down Expand Up @@ -1038,7 +1038,7 @@ public function notifyState(
if (
$this->lastProcessedTime instanceof DateTimeInterface
&& (
$this->dateTimeFactory->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
$this->clock->getNow()->getTimestamp() - $this->lastProcessedTime->getTimestamp()
< self::PROCESSING_DEBOUNCE_DELAY
)
) {
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\Addon\VirtualThermostat\Tests\Cases\Unit;

use DateTimeImmutable;
use Doctrine\DBAL;
use Doctrine\ORM;
use Error;
use FastyBird\Addon\VirtualThermostat\DI;
use FastyBird\Addon\VirtualThermostat\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 @@ -121,4 +121,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 1845335

Please sign in to comment.