Files
myep/tests/Service/PriceTimelineBuilderTest.php
T

262 lines
11 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\Entity\Groups\AccommodationPrice;
use App\Enum\Groups\PriceType;
use App\Service\PriceTimelineBuilder;
use PHPUnit\Framework\TestCase;
class PriceTimelineBuilderTest extends TestCase
{
private PriceTimelineBuilder $builder;
protected function setUp(): void
{
$this->builder = new PriceTimelineBuilder();
}
// --- buildTimeline ---
public function testReturnsEmptyArrayForNoPrices(): void
{
$result = $this->builder->buildTimeline([], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$this->assertSame([], $result);
}
public function testSingleBasePriceWithinYear(): void
{
$price = $this->makePrice('2026-01-01', '2026-03-31', pricePerNight: 15000, priceAdditionalPerson: 2000, includedPax: 2);
$result = $this->builder->buildTimeline([$price], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$this->assertCount(1, $result);
$this->assertSame('2026-01-01', $result[0]->dateFrom);
$this->assertSame('2026-03-31', $result[0]->dateTo);
$this->assertSame(150.0, $result[0]->pricePerNight);
$this->assertSame(20.0, $result[0]->priceAdditionalPerson);
$this->assertSame(2, $result[0]->includedPax);
$this->assertNull($result[0]->type);
$this->assertNull($result[0]->defaultPricePerNight);
$this->assertNull($result[0]->defaultPriceAdditionalPerson);
$this->assertSame('EUR', $result[0]->currency);
}
public function testBasePriceStartingBeforeYearIsClamped(): void
{
$price = $this->makePrice('2025-12-01', '2026-03-31', pricePerNight: 10000);
$result = $this->builder->buildTimeline([$price], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$this->assertCount(1, $result);
$this->assertSame('2026-01-01', $result[0]->dateFrom);
$this->assertSame('2026-03-31', $result[0]->dateTo);
}
public function testBasePriceEndingAfterYearIsClamped(): void
{
$price = $this->makePrice('2026-10-01', '2027-01-31', pricePerNight: 10000);
$result = $this->builder->buildTimeline([$price], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$this->assertCount(1, $result);
$this->assertSame('2026-10-01', $result[0]->dateFrom);
$this->assertSame('2026-12-31', $result[0]->dateTo);
}
public function testDiscountSplitsBasePeriodIntoThreeRows(): void
{
$base = $this->makePrice('2026-01-01', '2026-03-31', pricePerNight: 15000);
$discount = $this->makePrice('2026-01-15', '2026-01-31', pricePerNight: 12000, type: PriceType::DISCOUNT);
$result = $this->builder->buildTimeline([$base, $discount], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$this->assertCount(3, $result);
$this->assertSame('2026-01-01', $result[0]->dateFrom);
$this->assertSame('2026-01-14', $result[0]->dateTo);
$this->assertNull($result[0]->type);
$this->assertSame('2026-01-15', $result[1]->dateFrom);
$this->assertSame('2026-01-31', $result[1]->dateTo);
$this->assertSame('discount', $result[1]->type);
$this->assertSame('2026-02-01', $result[2]->dateFrom);
$this->assertSame('2026-03-31', $result[2]->dateTo);
$this->assertNull($result[2]->type);
}
public function testDiscountRowIncludesDefaultPrice(): void
{
$base = $this->makePrice('2026-01-01', '2026-03-31', pricePerNight: 15000, priceAdditionalPerson: 3000);
$discount = $this->makePrice('2026-01-15', '2026-01-31', pricePerNight: 12000, priceAdditionalPerson: 2000, type: PriceType::DISCOUNT);
$result = $this->builder->buildTimeline([$base, $discount], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$discountRow = $result[1];
$this->assertSame(120.0, $discountRow->pricePerNight);
$this->assertSame(150.0, $discountRow->defaultPricePerNight);
$this->assertSame(20.0, $discountRow->priceAdditionalPerson);
$this->assertSame(30.0, $discountRow->defaultPriceAdditionalPerson);
}
public function testBaseRowsHaveNullDefaultPriceFields(): void
{
$base = $this->makePrice('2026-01-01', '2026-03-31', pricePerNight: 15000);
$discount = $this->makePrice('2026-01-15', '2026-01-31', pricePerNight: 12000, type: PriceType::DISCOUNT);
$result = $this->builder->buildTimeline([$base, $discount], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$this->assertNull($result[0]->defaultPricePerNight);
$this->assertNull($result[0]->defaultPriceAdditionalPerson);
$this->assertNull($result[2]->defaultPricePerNight);
$this->assertNull($result[2]->defaultPriceAdditionalPerson);
}
public function testDiscountWithNoBasePriceHasNullDefaultPrice(): void
{
$discount = $this->makePrice('2026-01-15', '2026-01-31', pricePerNight: 12000, type: PriceType::DISCOUNT);
$result = $this->builder->buildTimeline([$discount], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$this->assertCount(1, $result);
$this->assertSame('discount', $result[0]->type);
$this->assertNull($result[0]->defaultPricePerNight);
}
public function testGapBetweenTwoPricesIsOmitted(): void
{
$first = $this->makePrice('2026-01-01', '2026-01-31', pricePerNight: 10000);
$second = $this->makePrice('2026-03-01', '2026-03-31', pricePerNight: 20000);
$result = $this->builder->buildTimeline([$first, $second], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$this->assertCount(2, $result);
$this->assertSame('2026-01-01', $result[0]->dateFrom);
$this->assertSame('2026-01-31', $result[0]->dateTo);
$this->assertSame('2026-03-01', $result[1]->dateFrom);
$this->assertSame('2026-03-31', $result[1]->dateTo);
}
public function testOverrideSplitsBasePeriod(): void
{
$base = $this->makePrice('2026-01-01', '2026-03-31', pricePerNight: 15000);
$override = $this->makePrice('2026-01-15', '2026-01-31', pricePerNight: 18000, type: PriceType::OVERRIDE);
$result = $this->builder->buildTimeline([$base, $override], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$this->assertCount(3, $result);
$this->assertNull($result[0]->type);
$this->assertSame('override', $result[1]->type);
$this->assertSame(180.0, $result[1]->pricePerNight);
$this->assertNull($result[2]->type);
}
public function testDiscountOverTwoDifferentBasePricesProducesTwoDiscountRows(): void
{
// The discount spans two different base price periods. Even though the discount entity
// is the same, the rows must NOT be merged because defaultPricePerNight differs.
$baseA = $this->makePrice('2026-01-01', '2026-01-20', pricePerNight: 10000);
$baseB = $this->makePrice('2026-01-21', '2026-03-31', pricePerNight: 12000);
$discount = $this->makePrice('2026-01-15', '2026-01-31', pricePerNight: 8000, type: PriceType::DISCOUNT);
$result = $this->builder->buildTimeline([$baseA, $baseB, $discount], new \DateTimeImmutable('2026-01-01'), new \DateTimeImmutable('2026-12-31'), 'EUR');
$this->assertCount(4, $result);
$this->assertSame('2026-01-01', $result[0]->dateFrom);
$this->assertSame('2026-01-14', $result[0]->dateTo);
$this->assertNull($result[0]->type);
$this->assertSame('2026-01-15', $result[1]->dateFrom);
$this->assertSame('2026-01-20', $result[1]->dateTo);
$this->assertSame('discount', $result[1]->type);
$this->assertSame(100.0, $result[1]->defaultPricePerNight); // baseA
$this->assertSame('2026-01-21', $result[2]->dateFrom);
$this->assertSame('2026-01-31', $result[2]->dateTo);
$this->assertSame('discount', $result[2]->type);
$this->assertSame(120.0, $result[2]->defaultPricePerNight); // baseB
$this->assertSame('2026-02-01', $result[3]->dateFrom);
$this->assertSame('2026-03-31', $result[3]->dateTo);
$this->assertNull($result[3]->type);
}
// --- resolveWinner ---
public function testResolveWinnerReturnsNullForEmptyArray(): void
{
$this->assertNull($this->builder->resolveWinner([]));
}
public function testResolveWinnerReturnsSingleCandidate(): void
{
$price = $this->makePrice('2026-01-01', '2026-03-31', pricePerNight: 10000);
$this->assertSame($price, $this->builder->resolveWinner([$price]));
}
public function testResolveWinnerPrefersDiscountOverBase(): void
{
$base = $this->makePrice('2026-01-01', '2026-03-31', pricePerNight: 15000);
$discount = $this->makePrice('2026-01-15', '2026-01-31', pricePerNight: 12000, type: PriceType::DISCOUNT);
$this->assertSame($discount, $this->builder->resolveWinner([$base, $discount]));
}
public function testResolveWinnerPrefersOverrideOverBase(): void
{
$base = $this->makePrice('2026-01-01', '2026-03-31', pricePerNight: 15000);
$override = $this->makePrice('2026-01-15', '2026-01-31', pricePerNight: 18000, type: PriceType::OVERRIDE);
$this->assertSame($override, $this->builder->resolveWinner([$base, $override]));
}
public function testResolveWinnerPrefersDiscountOverOverride(): void
{
$override = $this->makePrice('2026-01-01', '2026-01-31', pricePerNight: 18000, type: PriceType::OVERRIDE);
$discount = $this->makePrice('2026-01-01', '2026-01-31', pricePerNight: 12000, type: PriceType::DISCOUNT);
$this->assertSame($discount, $this->builder->resolveWinner([$override, $discount]));
}
public function testResolveWinnerPrefersShorterPeriodOnEqualTypeTie(): void
{
$wide = $this->makePrice('2026-01-01', '2026-03-31', pricePerNight: 15000);
$narrow = $this->makePrice('2026-01-15', '2026-01-31', pricePerNight: 12000);
$this->assertSame($narrow, $this->builder->resolveWinner([$wide, $narrow]));
}
public function testResolveWinnerPrefersLaterStartOnEqualLengthTie(): void
{
// Both cover 30 days; the one starting later should win.
$earlier = $this->makePrice('2026-01-01', '2026-01-30', pricePerNight: 15000);
$later = $this->makePrice('2026-02-01', '2026-03-02', pricePerNight: 12000);
$this->assertSame($later, $this->builder->resolveWinner([$earlier, $later]));
}
private function makePrice(
string $dateFrom,
string $dateTo,
int $pricePerNight,
int $priceAdditionalPerson = 0,
int $includedPax = 2,
?PriceType $type = null,
): AccommodationPrice {
$price = new AccommodationPrice();
$price->setDateFrom(new \DateTimeImmutable($dateFrom));
$price->setDateTo(new \DateTimeImmutable($dateTo));
$price->setPricePerNight($pricePerNight);
$price->setPriceAdditionalPerson($priceAdditionalPerson);
$price->setIncludedPax($includedPax);
$price->setType($type);
return $price;
}
}