|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Pitchart\Phlunit\Checks; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\Assert; |
|
7
|
|
|
use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck; |
|
8
|
|
|
use Pitchart\Phlunit\Checks\Mixin\FluentChecks; |
|
9
|
|
|
use Pitchart\Phlunit\Checks\Mixin\TypeCheck; |
|
|
|
|
|
|
10
|
|
|
use Pitchart\Phlunit\Checks\Mixin\WithMessage; |
|
11
|
|
|
use Pitchart\Phlunit\Constraint\DateTime\IsSameDayAs; |
|
12
|
|
|
use Pitchart\Phlunit\Constraint\DateTime\IsSameIgnoringMillis; |
|
13
|
|
|
use Pitchart\Phlunit\Constraint\DateTime\IsSameTimeAs; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class DateTimeCheck |
|
17
|
|
|
* |
|
18
|
|
|
* @package Pitchart\Phlunit\Checks |
|
19
|
|
|
* |
|
20
|
|
|
* @author Julien VITTE <[email protected]> |
|
21
|
|
|
* |
|
22
|
|
|
* @implements FluentCheck<\DateTimeInterface> |
|
23
|
|
|
*/ |
|
24
|
|
|
class DateTimeCheck implements FluentCheck |
|
25
|
|
|
{ |
|
26
|
1 |
|
use TypeCheck, FluentChecks, ConstraintCheck, WithMessage; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \DateTimeInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $value; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* DateTimeCheck constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \DateTimeInterface $date |
|
37
|
|
|
*/ |
|
38
|
15 |
|
public function __construct(\DateTimeInterface $date) |
|
39
|
|
|
{ |
|
40
|
15 |
|
$this->value = $date; |
|
41
|
15 |
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
public function hasYear(int $year): self |
|
44
|
|
|
{ |
|
45
|
1 |
|
Assert::assertSame($year, (int) $this->value->format('Y'), $this->message); |
|
46
|
1 |
|
$this->resetMessage(); |
|
47
|
1 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function hasMonth(int $month): self |
|
51
|
|
|
{ |
|
52
|
1 |
|
Assert::assertSame($month, (int) $this->value->format('m'), $this->message); |
|
53
|
1 |
|
$this->resetMessage(); |
|
54
|
1 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function hasDay(int $day): self |
|
58
|
|
|
{ |
|
59
|
1 |
|
Assert::assertSame($day, (int) $this->value->format('d'), $this->message); |
|
60
|
1 |
|
$this->resetMessage(); |
|
61
|
1 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function hasHours(int $hours): self |
|
65
|
|
|
{ |
|
66
|
1 |
|
Assert::assertSame($hours, (int) $this->value->format('H'), $this->message); |
|
67
|
1 |
|
$this->resetMessage(); |
|
68
|
1 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function hasMinutes(int $minutes): self |
|
72
|
|
|
{ |
|
73
|
1 |
|
Assert::assertSame($minutes, (int) $this->value->format('i'), $this->message); |
|
74
|
1 |
|
$this->resetMessage(); |
|
75
|
1 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
public function hasSeconds(int $seconds): self |
|
79
|
|
|
{ |
|
80
|
1 |
|
Assert::assertSame($seconds, (int) $this->value->format('s'), $this->message); |
|
81
|
1 |
|
$this->resetMessage(); |
|
82
|
1 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
5 |
|
public function isSameDayAs(\DateTimeInterface $expected): self |
|
86
|
|
|
{ |
|
87
|
5 |
|
Assert::assertThat($this->value, new IsSameDayAs($expected), $this->message); |
|
88
|
3 |
|
$this->resetMessage(); |
|
89
|
3 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
public function isSameTimeAs(\DateTimeInterface $expected): self |
|
93
|
|
|
{ |
|
94
|
2 |
|
Assert::assertThat($this->value, new IsSameTimeAs($expected), $this->message); |
|
95
|
2 |
|
$this->resetMessage(); |
|
96
|
2 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
public function isSameIgnoringMillis(\DateTimeInterface $expected): self |
|
100
|
|
|
{ |
|
101
|
1 |
|
Assert::assertThat($this->value, new IsSameIgnoringMillis($expected), $this->message); |
|
102
|
1 |
|
$this->resetMessage(); |
|
103
|
1 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
public function isAfter(\DateTimeInterface $date): self |
|
107
|
|
|
{ |
|
108
|
1 |
|
Assert::assertGreaterThan($date, $this->value, $this->message); |
|
109
|
1 |
|
$this->resetMessage(); |
|
110
|
1 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
public function isBefore(\DateTimeInterface $date): self |
|
114
|
|
|
{ |
|
115
|
1 |
|
Assert::assertLessThan($date, $this->value, $this->message); |
|
116
|
1 |
|
$this->resetMessage(); |
|
117
|
1 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
public function isBetween(\DateTimeInterface $from, \DateTimeInterface $to): self |
|
121
|
|
|
{ |
|
122
|
1 |
|
Assert::assertGreaterThanOrEqual($from, $this->value, $this->message); |
|
123
|
1 |
|
Assert::assertLessThanOrEqual($to, $this->value, $this->message); |
|
124
|
1 |
|
$this->resetMessage(); |
|
125
|
1 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: