|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Termyn\Timekeeper\Test; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use Termyn\Timekeeper; |
|
11
|
|
|
use Termyn\Timekeeper\FixedTimeService; |
|
12
|
|
|
|
|
13
|
|
|
final class FixedTimeServiceTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testItMeasuresTime(): void |
|
16
|
|
|
{ |
|
17
|
|
|
$dateTime = new DateTimeImmutable('@' . Timekeeper\time()); |
|
18
|
|
|
$timeService = new FixedTimeService($dateTime); |
|
19
|
|
|
|
|
20
|
|
|
$this->assertSame($dateTime, $timeService->measure()); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testItCreatesByTimeStamp(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$timeStamp = Timekeeper\time(); |
|
26
|
|
|
$timeService = FixedTimeService::fromStamp($timeStamp); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertEquals($timeStamp, $timeService->measure()->getTimestamp()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @dataProvider provideTimeStrings |
|
33
|
|
|
*/ |
|
34
|
|
|
public function testItCreatesByTimeString(string $value, string $format): void |
|
35
|
|
|
{ |
|
36
|
|
|
$dateTime = DateTimeImmutable::createFromFormat($format, $value); |
|
37
|
|
|
$timeService = FixedTimeService::fromString($value, $format); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertEquals($dateTime, $timeService->measure()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function provideTimeStrings(): array |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
|
|
'iso' => ['2021-04-07 12:30:24', 'Y-m-d H:i:s'], |
|
46
|
|
|
'local' => ['07.04.2021 12:30:24', 'd.m.Y H:i:s'], |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testItThrowsExceptionIfTimeStringIsInvalid(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
53
|
|
|
|
|
54
|
|
|
FixedTimeService::fromString('07.04.2021', 'Y-m-d H:i:s'); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|