Skip to content

Commit 0b9b077

Browse files
ValeriyShnurovoyDrahma
andauthored
United tests (#89)
Co-authored-by: Drahma <[email protected]>
1 parent 2da4d74 commit 0b9b077

File tree

1 file changed

+35
-101
lines changed

1 file changed

+35
-101
lines changed

tests/unit/action/UsageIntervalTest.php

Lines changed: 35 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -44,107 +44,15 @@ public function testWithinMonth(array $constructor, array $expectations): void
4444
$this->assertSame($expectations['secondsInMonth'], $interval->secondsInMonth());
4545
}
4646

47-
public function provideWithinMonth()
48-
{
49-
yield 'For a start and end dates outside the month, the interval is the whole month' => [
50-
['month' => '2023-02-01 00:00:00', 'start' => '2023-01-01 00:00:00', 'end' => '2023-10-01 00:00:00'],
51-
[
52-
'start' => '2023-02-01 00:00:00',
53-
'end' => '2023-03-01 00:00:00',
54-
'ratioOfMonth' => 1.0,
55-
'seconds' => 2_419_200,
56-
'secondsInMonth' => 2_419_200,
57-
]
58-
];
59-
60-
yield 'When start date is greater than a month, the interval is a fraction of month' => [
61-
['month' => '2023-02-01 00:00:00', 'start' => '2023-02-15 00:00:00', 'end' => null],
62-
[
63-
'start' => '2023-02-15 00:00:00',
64-
'end' => '2023-03-01 00:00:00',
65-
'ratioOfMonth' => 0.5,
66-
'seconds' => 1_209_600,
67-
'secondsInMonth' => 2_419_200,
68-
]
69-
];
70-
71-
yield 'When end date is less than a month, the interval is a fraction of month' => [
72-
['month' => '2023-02-01 00:00:00', 'start' => '2021-10-02 19:01:10', 'end' => '2023-02-15 00:00:00'],
73-
[
74-
'start' => '2023-02-01 00:00:00',
75-
'end' => '2023-02-15 00:00:00',
76-
'ratioOfMonth' => 0.5,
77-
'seconds' => 1_209_600,
78-
'secondsInMonth' => 2_419_200,
79-
]
80-
];
81-
82-
yield 'When start and end dates are within a month, the interval is a fraction of month' => [
83-
['month' => '2023-02-01 00:00:00', 'start' => '2023-02-15 00:00:00', 'end' => '2023-02-20 00:00:00'],
84-
[
85-
'start' => '2023-02-15 00:00:00',
86-
'end' => '2023-02-20 00:00:00',
87-
'ratioOfMonth' => 0.17857142857142858,
88-
'seconds' => 432_000,
89-
'secondsInMonth' => 2_419_200,
90-
]
91-
];
92-
93-
yield 'When start date is greater than current month, the interval is zero' => [
94-
['month' => '2023-02-01 00:00:00', 'start' => '2023-03-15 00:00:00', 'end' => null],
95-
[
96-
'start' => '2023-02-01 00:00:00',
97-
'end' => '2023-02-01 00:00:00',
98-
'ratioOfMonth' => 0.0,
99-
'seconds' => 0,
100-
'secondsInMonth' => 2_419_200,
101-
]
102-
];
103-
104-
yield 'When end date is less than current month, the interval is zero' => [
105-
['month' => '2023-02-01 00:00:00', 'start' => '2021-10-02 19:01:10', 'end' => '2023-01-15 00:00:00'],
106-
[
107-
'start' => '2023-02-01 00:00:00',
108-
'end' => '2023-02-01 00:00:00',
109-
'ratioOfMonth' => 0.0,
110-
'seconds' => 0,
111-
'secondsInMonth' => 2_419_200,
112-
]
113-
];
114-
115-
yield 'When a start date is greater than the end an exception is thrown' => [
116-
['month' => '2023-02-01 00:00:00', 'start' => '2023-03-15 00:00:00', 'end' => '2023-02-15 00:00:00'],
117-
[
118-
'expectedException' => InvalidArgumentException::class,
119-
'expectedExceptionMessage' => 'Start date must be less than end date',
120-
]
121-
];
122-
123-
yield 'When start date is greater than start of month' => [
124-
['month' => '2024-02-01 00:00:00', 'start' => '2024-02-01 11:50:00', 'end' => '2024-02-29 18:15:00'],
125-
[
126-
'start' => '2024-02-01 11:50:00',
127-
'end' => '2024-02-29 18:15:00',
128-
'ratioOfMonth' => 0.9747365900383141,
129-
'seconds' => 2_442_300,
130-
'secondsInMonth' => 2_505_600,
131-
]
132-
];
133-
}
13447

13548
/**
136-
* @dataProvider provideWithMonthAndFraction
49+
* @dataProvider provideWithinMonth
13750
*/
13851
public function testWithMonthAndFraction(array $constructor, array $expectations): void
13952
{
14053
$month = new DateTimeImmutable($constructor['month']);
14154
$start = new DateTimeImmutable($constructor['start']);
14255

143-
if (isset($expectations['expectedException'])) {
144-
$this->expectException($expectations['expectedException']);
145-
$this->expectExceptionMessage($expectations['expectedExceptionMessage']);
146-
}
147-
14856
$interval = UsageInterval::withMonthAndFraction($month, $start, $constructor['fraction']);
14957

15058
$this->assertEquals($expectations['start'], $interval->start()->format("Y-m-d H:i:s"));
@@ -154,10 +62,10 @@ public function testWithMonthAndFraction(array $constructor, array $expectations
15462
$this->assertSame($expectations['secondsInMonth'], $interval->secondsInMonth());
15563
}
15664

157-
public function provideWithMonthAndFraction()
65+
public function provideWithinMonth()
15866
{
15967
yield 'For a start and end dates outside the month, the interval is the whole month' => [
160-
['month' => '2023-02-01 00:00:00', 'start' => '2023-01-01 00:00:00', 'fraction' => 1],
68+
['month' => '2023-02-01 00:00:00', 'start' => '2023-01-01 00:00:00', 'end' => '2023-10-01 00:00:00', 'fraction' => 1],
16169
[
16270
'start' => '2023-02-01 00:00:00',
16371
'end' => '2023-03-01 00:00:00',
@@ -168,7 +76,7 @@ public function provideWithMonthAndFraction()
16876
];
16977

17078
yield 'When start date is greater than a month, the interval is a fraction of month' => [
171-
['month' => '2023-02-01 00:00:00', 'start' => '2023-02-15 00:00:00', 'fraction' => 0.5],
79+
['month' => '2023-02-01 00:00:00', 'start' => '2023-02-15 00:00:00', 'end' => null, 'fraction' => 0.5],
17280
[
17381
'start' => '2023-02-15 00:00:00',
17482
'end' => '2023-03-01 00:00:00',
@@ -179,7 +87,7 @@ public function provideWithMonthAndFraction()
17987
];
18088

18189
yield 'When end date is less than a month, the interval is a fraction of month' => [
182-
['month' => '2023-02-01 00:00:00', 'start' => '2021-10-02 19:01:10', 'fraction' => 0.5],
90+
['month' => '2023-02-01 00:00:00', 'start' => '2021-10-02 19:01:10', 'end' => '2023-02-15 00:00:00', 'fraction' => 0.5],
18391
[
18492
'start' => '2023-02-01 00:00:00',
18593
'end' => '2023-02-15 00:00:00',
@@ -190,7 +98,7 @@ public function provideWithMonthAndFraction()
19098
];
19199

192100
yield 'When start and end dates are within a month, the interval is a fraction of month' => [
193-
['month' => '2023-02-01 00:00:00', 'start' => '2023-02-15 00:00:00', 'fraction' => 0.17857142857142858],
101+
['month' => '2023-02-01 00:00:00', 'start' => '2023-02-15 00:00:00', 'end' => '2023-02-20 00:00:00', 'fraction' => 0.17857142857142858],
194102
[
195103
'start' => '2023-02-15 00:00:00',
196104
'end' => '2023-02-20 00:00:00',
@@ -201,7 +109,7 @@ public function provideWithMonthAndFraction()
201109
];
202110

203111
yield 'When start date is greater than current month, the interval is zero' => [
204-
['month' => '2023-02-01 00:00:00', 'start' => '2023-03-15 00:00:00', 'fraction' => 0],
112+
['month' => '2023-02-01 00:00:00', 'start' => '2023-03-15 00:00:00', 'end' => null, 'fraction' => 0.0],
205113
[
206114
'start' => '2023-02-01 00:00:00',
207115
'end' => '2023-02-01 00:00:00',
@@ -212,7 +120,7 @@ public function provideWithMonthAndFraction()
212120
];
213121

214122
yield 'When end date is less than current month, the interval is zero' => [
215-
['month' => '2023-02-01 00:00:00', 'start' => '2021-10-02 19:01:10', 'fraction' => 0],
123+
['month' => '2023-02-01 00:00:00', 'start' => '2021-10-02 19:01:10', 'end' => '2023-01-15 00:00:00', 'fraction' => 0.0],
216124
[
217125
'start' => '2023-02-01 00:00:00',
218126
'end' => '2023-02-01 00:00:00',
@@ -223,7 +131,7 @@ public function provideWithMonthAndFraction()
223131
];
224132

225133
yield 'When start date is greater than start of month' => [
226-
['month' => '2024-02-01 00:00:00', 'start' => '2024-02-01 11:50:00', 'fraction' => 0.9747365900383141],
134+
['month' => '2024-02-01 00:00:00', 'start' => '2024-02-01 11:50:00', 'end' => '2024-02-29 18:15:00', 'fraction' => 0.9747365900383141],
227135
[
228136
'start' => '2024-02-01 11:50:00',
229137
'end' => '2024-02-29 18:15:00',
@@ -234,6 +142,32 @@ public function provideWithMonthAndFraction()
234142
];
235143
}
236144

145+
/**
146+
* @dataProvider provideWithinMonthFailed
147+
*/
148+
public function testWithinMonthFailed(array $constructor, array $expectations): void
149+
{
150+
$month = new DateTimeImmutable($constructor['month']);
151+
$start = new DateTimeImmutable($constructor['start']);
152+
$end = new DateTimeImmutable($constructor['end']);
153+
154+
$this->expectException($expectations['expectedException']);
155+
$this->expectExceptionMessage($expectations['expectedExceptionMessage']);
156+
157+
UsageInterval::withinMonth($month, $start, $end);
158+
}
159+
160+
public function provideWithinMonthFailed()
161+
{
162+
yield 'When a start date is greater than the end an exception is thrown' => [
163+
['month' => '2023-02-01 00:00:00', 'start' => '2023-03-15 00:00:00', 'end' => '2023-02-15 00:00:00'],
164+
[
165+
'expectedException' => InvalidArgumentException::class,
166+
'expectedExceptionMessage' => 'Start date must be less than end date',
167+
]
168+
];
169+
}
170+
237171
/**
238172
* @dataProvider provideInvalidFractionOfMonthValues
239173
*/

0 commit comments

Comments
 (0)