Skip to content

Commit

Permalink
🚑 When a user joins a connection, existing chekins points might be se…
Browse files Browse the repository at this point in the history
…t to 1 (#2161)
  • Loading branch information
HerrLevin authored Nov 25, 2023
1 parent 5990031 commit ca1f013
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ public static function refreshDistanceAndPoints(Status $status, bool $resetPolyl
distanceInMeter: $distance,
hafasTravelType: $trainCheckin->HafasTrip->category,
departure: $firstStop->departure,
arrival: $lastStop->arrival
arrival: $lastStop->arrival,
timestampOfView: $status->created_at
);
$payload = [
'distance' => $distance,
Expand Down
101 changes: 0 additions & 101 deletions tests/Feature/Transport/PointsCalculation/ReasonTest.php

This file was deleted.

26 changes: 0 additions & 26 deletions tests/Unit/Transport/PointsCalculation/FactorTest.php

This file was deleted.

106 changes: 106 additions & 0 deletions tests/Unit/Transport/PointsCalculationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Unit\Transport;

use App\Enum\HafasTravelType;
use App\Enum\PointReason;
use App\Http\Controllers\Backend\Transport\PointsCalculationController;
use Carbon\Carbon;
use Tests\Unit\UnitTestCase;

class PointsCalculationTest extends UnitTestCase
{
public static function reasonDataProvider(): array {
return [
'5 Minutes before' => [now()->addMinutes(5), now()->addHour(), false, now(), PointReason::IN_TIME],
'25 Minutes before' => [now()->addMinutes(25), now()->addHour(), false, now(), PointReason::GOOD_ENOUGH],
'65 Minutes before' => [now()->addMinutes(65), now()->addHour(), false, now(), PointReason::NOT_SUFFICIENT],
'during trip' => [now()->subHour(), now()->addHour(), false, now(), PointReason::IN_TIME],
'11 Minutes after' => [now()->subHour(), now(), false, now()->addMinutes(11), PointReason::GOOD_ENOUGH],
'61 Minutes after' => [now()->subHours(2), now()->subMinutes(61), false, now(), PointReason::NOT_SUFFICIENT],
'forced' => [now()->subHour(), now()->addHour(), true, now(), PointReason::FORCED],
];
}

/**
* @dataProvider reasonDataProvider
*/
public function testReason(
Carbon $departure,
Carbon $arrival,
bool $forceCheckin,
Carbon $timestampOfView,
PointReason $expectedReason
): void {
$pointReason = PointsCalculationController::getReason(
departure: $departure,
arrival: $arrival,
forceCheckin: $forceCheckin,
timestampOfView: $timestampOfView,
);

$this->assertEquals($expectedReason, $pointReason);
}

public static function factorDataProvider(): array {
return [
[PointReason::IN_TIME, 1],
[PointReason::GOOD_ENOUGH, 0.25],
[PointReason::NOT_SUFFICIENT, 0],
[PointReason::FORCED, 0],
];
}

/**
* @dataProvider factorDataProvider
*/
public function testFactor(PointReason $reason, float $expectedFactor): void {
$this->assertEquals($expectedFactor, PointsCalculationController::getFactorByReason($reason));
}


public static function calculatePointsDataProvider() {
return [
'50km in an IC/ICE => 50/10 + 10 = 15 points' => [
15, HafasTravelType::NATIONAL_EXPRESS, now()->subMinutes(2), now()->addMinutes(10)
],
'50km in an RB => 50/10 + 5 = 10 points' => [
10, HafasTravelType::REGIONAL, now()->subMinutes(2), now()->addMinutes(10)
],
'18km in a Bus => 20/10 + 2 = 4 points' => [
7, HafasTravelType::BUS, now()->subMinutes(2), now()->addMinutes(10)
],
'< 20min before 50/10 + 10 = 15' => [
15, HafasTravelType::NATIONAL_EXPRESS, now()->addMinutes(18), now()->addMinutes(40)
],
'< 60min before, but > 20min (50/10 + 10) * 0.25 = 4' => [
4, HafasTravelType::NATIONAL_EXPRESS, now()->addMinutes(40), now()->addMinutes(100)
],
'> 60min before Only returns one fun-point 0*(50/10) + 10 = 1' => [
1, HafasTravelType::NATIONAL_EXPRESS, now()->addMinutes(62), now()->addMinutes(100)
],
'just before the Arrival 50/10 + 10 = 15' => [
15, HafasTravelType::NATIONAL_EXPRESS, now()->subMinutes(62), now()->addMinute()
],
'upto 60min after the Arrival (50/10 + 10) * 0.25 = 4' => [
4, HafasTravelType::NATIONAL_EXPRESS, now()->subMinutes(92), now()->subMinutes(35)
],
'longer in the past Only returns one fun-point 0*(50/10) + 10 = 1' => [
1, HafasTravelType::NATIONAL_EXPRESS, now()->subMinutes(62), now()->subMinutes(61)
],

];
}

/**
* @dataProvider calculatePointsDataProvider
*/
public function testCalculateTrainPoints(int $expectedPoints, HafasTravelType $hafasTravelType, Carbon $departure, Carbon $arrival): void {
$this->assertEquals($expectedPoints, PointsCalculationController::calculatePoints(
distanceInMeter: 50000,
hafasTravelType: $hafasTravelType,
departure: $departure,
arrival: $arrival,
)->points);
}
}
104 changes: 0 additions & 104 deletions tests/Unit/TransportControllerTest.php

This file was deleted.

0 comments on commit ca1f013

Please sign in to comment.