-
Notifications
You must be signed in to change notification settings - Fork 12
HP-2166/Store_Action_Usage_Interval_in_the_action_table #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,4 +120,118 @@ public function provideWithinMonth() | |
| ] | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provideWithMonthAndFraction | ||
| */ | ||
| public function testWithMonthAndFraction(array $constructor, array $expectations): void | ||
| { | ||
| $month = new DateTimeImmutable($constructor['month']); | ||
| $start = new DateTimeImmutable($constructor['start']); | ||
|
|
||
| if (isset($expectations['expectedException'])) { | ||
| $this->expectException($expectations['expectedException']); | ||
| $this->expectExceptionMessage($expectations['expectedExceptionMessage']); | ||
| } | ||
|
|
||
| $interval = UsageInterval::withMonthAndFraction($month, $start, $constructor['fraction']); | ||
|
|
||
| $this->assertEquals($expectations['start'], $interval->start()->format("Y-m-d H:i:s")); | ||
| $this->assertEquals($expectations['end'], $interval->end()->format("Y-m-d H:i:s")); | ||
| $this->assertSame($expectations['ratioOfMonth'], $interval->ratioOfMonth()); | ||
| $this->assertSame($expectations['seconds'], $interval->seconds()); | ||
| $this->assertSame($expectations['secondsInMonth'], $interval->secondsInMonth()); | ||
| } | ||
|
|
||
| public function provideWithMonthAndFraction() | ||
| { | ||
| yield 'For a start and end dates outside the month, the interval is the whole month' => [ | ||
| ['month' => '2023-02-01 00:00:00', 'start' => '2023-01-01 00:00:00', 'fraction' => 1], | ||
| [ | ||
| 'start' => '2023-02-01 00:00:00', | ||
| 'end' => '2023-03-01 00:00:00', | ||
| 'ratioOfMonth' => 1.0, | ||
| 'seconds' => 2_419_200, | ||
| 'secondsInMonth' => 2_419_200, | ||
| ] | ||
| ]; | ||
|
|
||
| yield 'When start date is greater than a month, the interval is a fraction of month' => [ | ||
| ['month' => '2023-02-01 00:00:00', 'start' => '2023-02-15 00:00:00', 'fraction' => 0.5], | ||
| [ | ||
| 'start' => '2023-02-15 00:00:00', | ||
| 'end' => '2023-03-01 00:00:00', | ||
| 'ratioOfMonth' => 0.5, | ||
| 'seconds' => 1_209_600, | ||
| 'secondsInMonth' => 2_419_200, | ||
| ] | ||
| ]; | ||
|
|
||
| yield 'When end date is less than a month, the interval is a fraction of month' => [ | ||
| ['month' => '2023-02-01 00:00:00', 'start' => '2021-10-02 19:01:10', 'fraction' => 0.5], | ||
| [ | ||
| 'start' => '2023-02-01 00:00:00', | ||
| 'end' => '2023-02-15 00:00:00', | ||
| 'ratioOfMonth' => 0.5, | ||
| 'seconds' => 1_209_600, | ||
| 'secondsInMonth' => 2_419_200, | ||
| ] | ||
| ]; | ||
|
|
||
| yield 'When start and end dates are within a month, the interval is a fraction of month' => [ | ||
| ['month' => '2023-02-01 00:00:00', 'start' => '2023-02-15 00:00:00', 'fraction' => 0.17857142857142858], | ||
| [ | ||
| 'start' => '2023-02-15 00:00:00', | ||
| 'end' => '2023-02-20 00:00:00', | ||
| 'ratioOfMonth' => 0.17857142857142858, | ||
| 'seconds' => 432_000, | ||
| 'secondsInMonth' => 2_419_200, | ||
| ] | ||
| ]; | ||
|
|
||
| yield 'When start date is greater than current month, the interval is zero' => [ | ||
| ['month' => '2023-02-01 00:00:00', 'start' => '2023-03-15 00:00:00', 'fraction' => 0], | ||
| [ | ||
| 'start' => '2023-02-01 00:00:00', | ||
| 'end' => '2023-02-01 00:00:00', | ||
| 'ratioOfMonth' => 0.0, | ||
| 'seconds' => 0, | ||
| 'secondsInMonth' => 2_419_200, | ||
| ] | ||
| ]; | ||
|
|
||
| yield 'When end date is less than current month, the interval is zero' => [ | ||
| ['month' => '2023-02-01 00:00:00', 'start' => '2021-10-02 19:01:10', 'fraction' => 0], | ||
| [ | ||
| 'start' => '2023-02-01 00:00:00', | ||
| 'end' => '2023-02-01 00:00:00', | ||
| 'ratioOfMonth' => 0.0, | ||
| 'seconds' => 0, | ||
| 'secondsInMonth' => 2_419_200, | ||
| ] | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provideInvalidFractionOfMonthValues | ||
| */ | ||
| public function testWithMonthAndFractionInvalidValues(float $fractionOfMonth): void | ||
| { | ||
| $month = new DateTimeImmutable('2023-01-01'); | ||
| $start = new DateTimeImmutable('2023-01-15'); | ||
|
|
||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Fraction of month must be between 0 and 1'); | ||
|
|
||
| UsageInterval::withMonthAndFraction($month, $start, $fractionOfMonth); | ||
| } | ||
|
|
||
| public function provideInvalidFractionOfMonthValues(): array | ||
| { | ||
| return [ | ||
| [-0.1], | ||
| [1.1], | ||
| [2.0], | ||
| ]; | ||
| } | ||
|
Comment on lines
+215
to
+236
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance invalid value testing. Good implementation of invalid value testing as requested. Consider these additions:
Add these test cases to public function provideInvalidFractionOfMonthValues(): array
{
return [
[-0.1],
[1.1],
[2.0],
+ [NAN],
+ [INF],
+ [-INF],
+ [PHP_FLOAT_MAX],
+ [PHP_FLOAT_MIN],
];
}Also, consider testing the exact error message format: - $this->expectExceptionMessage('Fraction of month must be between 0 and 1');
+ $this->expectExceptionMessage(sprintf(
+ 'Fraction of month must be between 0 and 1, got: %f',
+ $fractionOfMonth
+ ));
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Refactor Action constructor to use builder pattern or named arguments
The constructor call has several issues:
Consider these improvements: