173 lines
6.2 KiB
PHP
173 lines
6.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Service;
|
|
|
|
use App\Entity\Groups\AccommodationPrice;
|
|
use App\Service\GroupsPriceCalculator;
|
|
use App\Service\PriceTimelineBuilder;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class GroupsPriceCalculatorTest extends TestCase
|
|
{
|
|
private const int NIGHTS = 3;
|
|
|
|
public function testFreeBoardDoesNotTriggerUndersubscriptionSurcharge(): void
|
|
{
|
|
$result = $this->calculate(30, 0);
|
|
|
|
self::assertSame(0, $result['undersubscriptionSurcharge']);
|
|
self::assertNull($result['undersubscriptionThreshold']);
|
|
self::assertSame(0, $result['boardPrice']);
|
|
}
|
|
|
|
public function testNoBoardDoesNotTriggerUndersubscriptionSurcharge(): void
|
|
{
|
|
$result = $this->calculate(30, null);
|
|
|
|
self::assertSame(0, $result['undersubscriptionSurcharge']);
|
|
self::assertNull($result['undersubscriptionThreshold']);
|
|
self::assertSame(0, $result['boardPrice']);
|
|
}
|
|
|
|
public function testPaidBoardBelow40TriggersTheLowerThresholdSurcharge(): void
|
|
{
|
|
$result = $this->calculate(30, 1500);
|
|
|
|
self::assertSame(30 * 500 * self::NIGHTS, $result['undersubscriptionSurcharge']);
|
|
self::assertSame(40, $result['undersubscriptionThreshold']);
|
|
self::assertSame(1500 * 30 * self::NIGHTS, $result['boardPrice']);
|
|
}
|
|
|
|
public function testPaidBoardBelow50TriggersTheUpperThresholdSurcharge(): void
|
|
{
|
|
$result = $this->calculate(45, 1500);
|
|
|
|
self::assertSame(45 * 250 * self::NIGHTS, $result['undersubscriptionSurcharge']);
|
|
self::assertSame(50, $result['undersubscriptionThreshold']);
|
|
}
|
|
|
|
public function testTheShortTermSurchargeIsJudgedOnTheWholeStay(): void
|
|
{
|
|
// Three nights priced for the group, but one night split off a four-night stay: the
|
|
// stay is not short-term, so the three nights must not pick up the 10 % surcharge.
|
|
$price = $this->price('2026-06-01', '2026-08-31', 20);
|
|
|
|
$split = $this->calculateMain('2026-07-02', '2026-07-05', '2026-07-01', 4, [$price]);
|
|
$unsplit = $this->calculateMain('2026-07-02', '2026-07-05', '2026-07-02', 3, [$price]);
|
|
|
|
self::assertSame(0, $split['shortTermSurcharge']);
|
|
self::assertSame(3, $split['nights']);
|
|
self::assertSame((int) round(($unsplit['basePrice'] + $unsplit['additionalPersonsPrice']) * 10 / 100), $unsplit['shortTermSurcharge']);
|
|
}
|
|
|
|
public function testTheShortTermSurchargeOfAShortStayIsChargedOnThePricedNightsOnly(): void
|
|
{
|
|
// A three-night stay with one night split off: 10 % on the two nights the group pays
|
|
// for, not 20 % for a two-night stay and not on the split-off night's lines.
|
|
$price = $this->price('2026-06-01', '2026-08-31', 20);
|
|
|
|
$result = $this->calculateMain('2026-07-02', '2026-07-04', '2026-07-01', 3, [$price]);
|
|
|
|
self::assertSame(2 * 100_00, $result['basePrice']);
|
|
self::assertSame((int) round(($result['basePrice'] + $result['additionalPersonsPrice']) * 10 / 100), $result['shortTermSurcharge']);
|
|
}
|
|
|
|
public function testTheIncludedPaxFloorComesFromTheStaysFirstNight(): void
|
|
{
|
|
// The stay starts in a price period with a floor of 30; the main group arrives a day
|
|
// later, in one with a floor of 50. Splitting the first night off must not raise the
|
|
// floor the 20 people are billed at.
|
|
$prices = [
|
|
$this->price('2026-06-01', '2026-07-01', 30),
|
|
$this->price('2026-07-02', '2026-08-31', 50),
|
|
];
|
|
|
|
$result = $this->calculateMain('2026-07-02', '2026-07-08', '2026-07-01', 7, $prices, paxCount: 20, boardServicePrice: 1500);
|
|
|
|
self::assertSame(30, $result['includedPax']);
|
|
self::assertSame(30, $result['effectivePax']);
|
|
self::assertSame(1500 * 30 * 6, $result['boardPrice']);
|
|
}
|
|
|
|
/**
|
|
* @param list<AccommodationPrice> $prices
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function calculateMain(string $from, string $to, string $stayFrom, int $stayNights, array $prices, int $paxCount = 20, ?int $boardServicePrice = null): array
|
|
{
|
|
$dateFrom = new \DateTimeImmutable($from);
|
|
$dateTo = new \DateTimeImmutable($to);
|
|
|
|
return $this->calculator()->calculateFromSnapshots(
|
|
$paxCount,
|
|
0,
|
|
$dateFrom->diff($dateTo)->days,
|
|
$dateFrom,
|
|
$dateTo,
|
|
new \DateTimeImmutable($stayFrom),
|
|
$stayNights,
|
|
$prices,
|
|
$boardServicePrice,
|
|
[],
|
|
'EUR',
|
|
);
|
|
}
|
|
|
|
private function price(string $from, string $to, int $includedPax): AccommodationPrice
|
|
{
|
|
return (new AccommodationPrice())
|
|
->setDateFrom(new \DateTimeImmutable($from))
|
|
->setDateTo(new \DateTimeImmutable($to))
|
|
->setIncludedPax($includedPax)
|
|
->setPricePerNight(100_00)
|
|
->setPriceAdditionalPerson(10_00)
|
|
->setMinNights(1);
|
|
}
|
|
|
|
private function calculator(): GroupsPriceCalculator
|
|
{
|
|
return new GroupsPriceCalculator(new PriceTimelineBuilder(), [
|
|
'runningCostsEur' => 0,
|
|
'runningCostsChf' => 0,
|
|
'undersubscription30Eur' => 5.0,
|
|
'undersubscription30Chf' => 6.0,
|
|
'undersubscription40Eur' => 2.5,
|
|
'undersubscription40Chf' => 3.0,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function calculate(int $paxCount, ?int $boardServicePrice): array
|
|
{
|
|
$dateFrom = new \DateTimeImmutable('2026-07-01');
|
|
$dateTo = $dateFrom->modify(sprintf('+%d days', self::NIGHTS));
|
|
|
|
$price = (new AccommodationPrice())
|
|
->setDateFrom($dateFrom->modify('-1 month'))
|
|
->setDateTo($dateTo->modify('+1 month'))
|
|
->setIncludedPax(20)
|
|
->setPricePerNight(100_00)
|
|
->setPriceAdditionalPerson(10_00)
|
|
->setMinNights(1);
|
|
|
|
return $this->calculator()->calculateFromSnapshots(
|
|
$paxCount,
|
|
0,
|
|
self::NIGHTS,
|
|
$dateFrom,
|
|
$dateTo,
|
|
$dateFrom,
|
|
self::NIGHTS,
|
|
[$price],
|
|
$boardServicePrice,
|
|
[],
|
|
'EUR',
|
|
);
|
|
}
|
|
}
|