/
TestTimeService.php
73 lines (62 loc) · 1.57 KB
/
TestTimeService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace DobroSite\TimeService\Test;
use DobroSite\TimeService\TimeService;
/**
* Тестовая версия {@see TimeService}.
*
* @since 1.0
*/
final class TestTimeService implements TimeService
{
/**
* Время, используемое {@see resetCurrentTime()} при сбросе.
*/
private const DEFAULT_CURRENT_TIME = '2001-02-03T04:05:06+07:00';
/**
* Время, возвращаемое {@see getCurrentTime()}.
*/
private \DateTimeImmutable $currentTime;
/**
* @throws \Throwable
*
* @since 1.0
*/
public function __construct()
{
$this->resetCurrentTime();
}
public function getCurrentTime(): \DateTimeImmutable
{
return $this->currentTime;
}
public function getTimeZone(): \DateTimeZone
{
return $this->currentTime->getTimezone();
}
/**
* Устанавливает время по умолчанию.
*
* @throws \Throwable
*
* @since 1.0
*/
public function resetCurrentTime(): void
{
$this->currentTime = new \DateTimeImmutable(self::DEFAULT_CURRENT_TIME);
}
/**
* Задаёт время, возвращаемое TimeService.
*
* @throws \Throwable
*
* @since 1.0
*/
public function setCurrentTime(\DateTimeInterface|string $time): void
{
$time = is_string($time)
? new \DateTimeImmutable($time)
: \DateTimeImmutable::createFromInterface($time);
$this->currentTime = $time;
}
}