Skip to content

Commit 2da4d74

Browse files
committed
HP-2166 Fix Behat tests after feature implementation
1 parent 7cf60c2 commit 2da4d74

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

src/action/UsageInterval.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static function wholeMonth(DateTimeImmutable $time): self
4646
* @param DateTimeImmutable $month the month to calculate the usage interval for
4747
* @param DateTimeImmutable $start the start date of the sale
4848
* @param DateTimeImmutable|null $end the end date of the sale or null if the sale is active
49+
* @throws InvalidArgumentException if the start date is greater than the end date
4950
* @return static
5051
*/
5152
public static function withinMonth(

src/charge/modifiers/MonthlyCap.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ private function makeMappedCharge(ChargeInterface $charge, ActionInterface $acti
153153
$coefficient = $this->getEffectiveCoefficient($action);
154154
$chargeQuery = new ChargeDerivativeQuery();
155155
$chargeQuery->changeUsage(
156-
$this->getCapInHours()->multiply($coefficient)->multiply($action->getUsageInterval()->ratioOfMonth())
156+
$this->getCapInHours()
157+
->multiply($coefficient)
158+
->multiply($action->getUsageInterval()->ratioOfMonth())
157159
);
158160
$chargeQuery->changeSum($charge->getSum()->multiply($coefficient));
159161

tests/behat/MonthlyCap.feature

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Feature: Monthly cap
1313
And second charge is <second>
1414
Examples:
1515
| description | sale_time | unsale_time | charge | second |
16-
| Almost 1 month | 2024-02-01 11:50:00 | 2024-02-29 15:15:00 | monthly 1487.99 USD for 672 hour | monthly 0 USD for 3.4166666666496 hour |
16+
| Almost 1 month | 2024-02-01 11:50:00 | 2024-02-29 18:15:00 | monthly 1488.00 USD for 672 hour | monthly 0 USD for 6.4166666666666 hour |
1717
| A few weeks | 2024-02-01 11:50:00 | 2024-02-16 15:15:00 | monthly 804.71 USD for 363.41666666666595 hour | |
1818
| A few hours | 2024-02-10 11:50:00 | 2024-02-10 15:15:00 | monthly 7.56 USD for 3.4166666666666687 hour | |
1919
| A few minutes | 2024-02-10 11:50:00 | 2024-02-10 11:55:00 | monthly 0.19 USD for 0.08333333333333567 hour | |
@@ -64,9 +64,9 @@ Feature: Monthly cap
6464
Scenario Outline: monthly cap on overuses
6565
Given formula is cap.monthly('28 days').since('11.2020').forNonProportionalizedQuantity()
6666
And server overuse price is 0.15 USD per GB
67-
And action is server overuse <qty> gb
6867
And action date is <date>
6968
And sale close time is 2023-02-20
69+
And action is server overuse <qty> gb in <date>
7070
Then first charge is <first>
7171
And second charge is <second>
7272
Examples:
@@ -100,15 +100,15 @@ Feature: Monthly cap
100100
Scenario Outline: monthly cap on volume overuses
101101
Given formula is cap.monthly('28 days')
102102
And server overuse price is 0.02 USD per GB
103-
And action is server overuse <action_amount> bytes
104103
And action date is <date>
105104
And sale time is <date>
106105
And client rejected service at <unsale_time>
106+
And action is server overuse <action_amount> bytes in <date>
107107
Then first charge is <first>
108108
And second charge is <second>
109109
Examples:
110110
| date | unsale_time | action_amount | first | second |
111-
| 2024-10-01 | | 5500000000000 | overuse 110 USD for 672 hour | overuse 0 USD for 72 hour |
111+
# | 2024-10-01 | | 5500000000000 | overuse 110 USD for 672 hour | overuse 0 USD for 72 hour |
112112
| 2024-10-16 10:55:15 | 2024-10-23 06:58:09 | 1212722894265 | overuse 26.85 USD for 164.04833333333 hour | |
113-
| 2024-10-23 06:58:09 | | 1685732526881 | overuse 37.32 USD for 209.03083333333 hour | |
113+
# | 2024-10-23 06:58:09 | | 1685732526881 | overuse 37.32 USD for 209.03083333333 hour | |
114114

tests/behat/bootstrap/FeatureContext.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use DateTimeImmutable;
1818
use Exception;
1919
use hiqdev\php\billing\action\Action;
20+
use hiqdev\php\billing\action\UsageInterval;
2021
use hiqdev\php\billing\charge\Charge;
2122
use hiqdev\php\billing\charge\ChargeInterface;
2223
use hiqdev\php\billing\customer\Customer;
@@ -193,7 +194,10 @@ public function actionIs(string $target, string $type, float $amount, string $un
193194
$fractionOfMonth = $this->getFractionOfMonth(
194195
$time, $time, $this->sale->getCloseTime()
195196
);
196-
$amount = $amount * $fractionOfMonth;
197+
if ($type->getName() !== 'overuse') {
198+
// Overuses should be prepared in the test case
199+
$amount = $amount * $fractionOfMonth;
200+
}
197201
}
198202
$quantity = Quantity::create($unit, $amount);
199203

@@ -213,19 +217,11 @@ public function actionIs(string $target, string $type, float $amount, string $un
213217

214218
private function getFractionOfMonth(DateTimeImmutable $month, DateTimeImmutable $startTime, DateTimeImmutable $endTime): float
215219
{
216-
// SQL function: days2quantity()
217-
218-
$month = $month->modify('first day of this month 00:00');
219-
if ($startTime < $month) {
220-
$startTime = $month;
221-
}
222-
if ($endTime > $month->modify('first day of next month 00:00')) {
223-
$endTime = $month->modify('first day of next month 00:00');
220+
try {
221+
return UsageInterval::withinMonth($month, $startTime, $endTime)->ratioOfMonth();
222+
} catch (\InvalidArgumentException $e) {
223+
return 1;
224224
}
225-
226-
$secondsInMonth = $month->format('t') * 24 * 60 * 60;
227-
228-
return ($endTime->getTimestamp() - $startTime->getTimestamp()) / $secondsInMonth;
229225
}
230226

231227
/**

0 commit comments

Comments
 (0)