feat: groups price calculator admin crud, booking/offer flow and api
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use App\Repository\Groups\AccommodationPriceRepository;
|
||||
use App\Service\AccommodationBookingBreakdownCalculator;
|
||||
use App\Service\GroupsPriceCalculator;
|
||||
use App\Service\PriceTimelineBuilder;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AccommodationBookingBreakdownCalculatorTest extends TestCase
|
||||
{
|
||||
public function testComputeReturnsStoredSnapshotWithoutLoadingCurrentPrices(): void
|
||||
{
|
||||
$snapshot = ['total' => 12345, 'currency' => 'EUR'];
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setPriceSnapshot($snapshot, 12345, 'EUR', 1);
|
||||
|
||||
$priceRepository = $this->createMock(AccommodationPriceRepository::class);
|
||||
$priceRepository->expects(self::never())->method('findByHotelCodeAndDateRange');
|
||||
|
||||
$calculator = new AccommodationBookingBreakdownCalculator(
|
||||
new GroupsPriceCalculator(new PriceTimelineBuilder(), [
|
||||
'runningCostsEur' => 0,
|
||||
'runningCostsChf' => 0,
|
||||
'undersubscription30Eur' => 0,
|
||||
'undersubscription30Chf' => 0,
|
||||
'undersubscription40Eur' => 0,
|
||||
'undersubscription40Chf' => 0,
|
||||
]),
|
||||
$priceRepository,
|
||||
);
|
||||
|
||||
self::assertSame($snapshot, $calculator->compute($booking));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use App\Service\AccommodationBookingLinkSigner;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
class AccommodationBookingLinkSignerTest extends TestCase
|
||||
{
|
||||
public function testSignThenIsValidLinkRequestSucceeds(): void
|
||||
{
|
||||
$booking = $this->bookingWithAccessLink();
|
||||
$signer = $this->createSigner();
|
||||
|
||||
$signedUrl = $signer->sign($booking);
|
||||
$request = Request::create($signedUrl);
|
||||
|
||||
self::assertTrue($signer->isValidLinkRequest($request, $booking));
|
||||
}
|
||||
|
||||
public function testTamperedQueryParamFails(): void
|
||||
{
|
||||
$booking = $this->bookingWithAccessLink();
|
||||
$signer = $this->createSigner();
|
||||
|
||||
$signedUrl = $signer->sign($booking);
|
||||
self::assertTrue($signer->isValidLinkRequest(Request::create($signedUrl), $booking));
|
||||
|
||||
// Flip the `t` value while keeping the original _hash — signature no longer matches.
|
||||
$tamperedUrl = preg_replace('/(?<=[?&]t=)\d+/', '999999999', $signedUrl);
|
||||
self::assertNotNull($tamperedUrl);
|
||||
|
||||
self::assertFalse($signer->isValidLinkRequest(Request::create($tamperedUrl), $booking));
|
||||
}
|
||||
|
||||
public function testRegeneratedLinkInvalidatesThePreviousOne(): void
|
||||
{
|
||||
$booking = $this->bookingWithAccessLink();
|
||||
$signer = $this->createSigner();
|
||||
|
||||
$signedUrl = $signer->sign($booking);
|
||||
$request = Request::create($signedUrl);
|
||||
|
||||
// Regenerating overwrites accessLinkIssuedAt — the old signed `t` no longer matches.
|
||||
$booking->setAccessLinkIssuedAt(new \DateTimeImmutable('+1 minute'));
|
||||
|
||||
self::assertFalse($signer->isValidLinkRequest($request, $booking));
|
||||
}
|
||||
|
||||
public function testExpiredLinkFails(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setAccessLinkIssuedAt(new \DateTimeImmutable('-91 days'));
|
||||
$signer = $this->createSigner();
|
||||
|
||||
$signedUrl = $signer->sign($booking);
|
||||
$request = Request::create($signedUrl);
|
||||
|
||||
self::assertFalse($signer->isValidLinkRequest($request, $booking));
|
||||
}
|
||||
|
||||
public function testMissingAccessLinkIssuedAtFails(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$signer = $this->createSigner();
|
||||
|
||||
$request = Request::create('https://example.com/groups/booking/offer/'.$booking->getUuid().'?t=123');
|
||||
|
||||
self::assertFalse($signer->isValidLinkRequest($request, $booking));
|
||||
}
|
||||
|
||||
public function testSignThrowsWithoutAccessLinkIssuedAt(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$signer = $this->createSigner();
|
||||
|
||||
$this->expectException(\LogicException::class);
|
||||
|
||||
$signer->sign($booking);
|
||||
}
|
||||
|
||||
public function testExpiresAtIsNinetyDaysAfterIssuedAt(): void
|
||||
{
|
||||
$issuedAt = new \DateTimeImmutable('2026-01-01T00:00:00+00:00');
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setAccessLinkIssuedAt($issuedAt);
|
||||
$signer = $this->createSigner();
|
||||
|
||||
self::assertSame('2026-04-01T00:00:00+00:00', $signer->expiresAt($booking)?->format(\DATE_ATOM));
|
||||
}
|
||||
|
||||
public function testSessionIsAuthorizedAfterAuthorizeSession(): void
|
||||
{
|
||||
$booking = $this->bookingWithAccessLink();
|
||||
$signer = $this->createSigner();
|
||||
$request = $this->requestWithSession();
|
||||
|
||||
self::assertFalse($signer->isSessionAuthorized($request, $booking));
|
||||
|
||||
$signer->authorizeSession($request, $booking);
|
||||
|
||||
self::assertTrue($signer->isSessionAuthorized($request, $booking));
|
||||
}
|
||||
|
||||
public function testSessionAuthorizationIsPerBooking(): void
|
||||
{
|
||||
$booking = $this->bookingWithAccessLink();
|
||||
$otherBooking = $this->bookingWithAccessLink();
|
||||
$signer = $this->createSigner();
|
||||
$request = $this->requestWithSession();
|
||||
|
||||
$signer->authorizeSession($request, $booking);
|
||||
|
||||
self::assertTrue($signer->isSessionAuthorized($request, $booking));
|
||||
self::assertFalse($signer->isSessionAuthorized($request, $otherBooking));
|
||||
}
|
||||
|
||||
public function testSessionAuthorizationIsRevokedWhenLinkIsRegenerated(): void
|
||||
{
|
||||
$booking = $this->bookingWithAccessLink();
|
||||
$signer = $this->createSigner();
|
||||
$request = $this->requestWithSession();
|
||||
|
||||
$signer->authorizeSession($request, $booking);
|
||||
self::assertTrue($signer->isSessionAuthorized($request, $booking));
|
||||
|
||||
// Regenerating the access link overwrites accessLinkIssuedAt — the
|
||||
// previously-authorized session no longer matches.
|
||||
$booking->setAccessLinkIssuedAt(new \DateTimeImmutable('+1 minute'));
|
||||
|
||||
self::assertFalse($signer->isSessionAuthorized($request, $booking));
|
||||
}
|
||||
|
||||
public function testSessionAuthorizationFailsPastTtlEvenIfSessionMatches(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setAccessLinkIssuedAt(new \DateTimeImmutable('-91 days'));
|
||||
$signer = $this->createSigner();
|
||||
$request = $this->requestWithSession();
|
||||
|
||||
$signer->authorizeSession($request, $booking);
|
||||
|
||||
self::assertFalse($signer->isSessionAuthorized($request, $booking));
|
||||
}
|
||||
|
||||
public function testSessionAuthorizationFailsWithoutAccessLinkIssuedAt(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$signer = $this->createSigner();
|
||||
$request = $this->requestWithSession();
|
||||
|
||||
self::assertFalse($signer->isSessionAuthorized($request, $booking));
|
||||
}
|
||||
|
||||
private function requestWithSession(): Request
|
||||
{
|
||||
$request = Request::create('https://example.com/');
|
||||
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
private function bookingWithAccessLink(): AccommodationBooking
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setPaxCount(10);
|
||||
$booking->setAccessLinkIssuedAt(new \DateTimeImmutable());
|
||||
|
||||
return $booking;
|
||||
}
|
||||
|
||||
private function createSigner(): AccommodationBookingLinkSigner
|
||||
{
|
||||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
|
||||
$urlGenerator
|
||||
->method('generate')
|
||||
->willReturnCallback(static fn (string $name, array $parameters) => sprintf(
|
||||
'https://example.com/groups/booking/offer/%s?t=%s',
|
||||
$parameters['uuid'],
|
||||
$parameters['t'],
|
||||
));
|
||||
|
||||
return new AccommodationBookingLinkSigner($urlGenerator, 'test-secret');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,359 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\Email\Mailer;
|
||||
use App\Entity\Groups\Accommodation;
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use App\Entity\Groups\AccommodationPrice;
|
||||
use App\Model\AccommodationBookingQueryParams;
|
||||
use App\Repository\Groups\AccommodationPriceRepository;
|
||||
use App\Repository\Groups\AccommodationRepository;
|
||||
use App\Repository\Groups\AdditionalServiceRepository;
|
||||
use App\Repository\Groups\BoardServiceRepository;
|
||||
use App\Service\AccommodationBookingBreakdownCalculator;
|
||||
use App\Service\AccommodationBookingLinkSigner;
|
||||
use App\Service\AccommodationBookingService;
|
||||
use App\Service\CmsDataProvider;
|
||||
use App\Service\PriceTimelineBuilder;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class AccommodationBookingServiceTest extends TestCase
|
||||
{
|
||||
public function testInitFromParamsAcceptsStrictCalendarDates(): void
|
||||
{
|
||||
$service = $this->createServiceWithAccommodation();
|
||||
|
||||
$dto = $service->initFromParams(new AccommodationBookingQueryParams('HOTEL', '2026-02-28', '2026-03-01'));
|
||||
|
||||
self::assertSame('2026-02-28', $dto->dateFrom?->format('Y-m-d'));
|
||||
self::assertSame('2026-03-01', $dto->dateTo?->format('Y-m-d'));
|
||||
}
|
||||
|
||||
public function testInitFromParamsPrefillsPaxCountFromEffectiveMinPax(): void
|
||||
{
|
||||
$price = (new AccommodationPrice())
|
||||
->setDateFrom(new \DateTimeImmutable('2026-02-01'))
|
||||
->setDateTo(new \DateTimeImmutable('2026-03-31'))
|
||||
->setIncludedPax(4);
|
||||
$service = $this->createServiceWithAccommodation([$price]);
|
||||
|
||||
$dto = $service->initFromParams(new AccommodationBookingQueryParams('HOTEL', '2026-02-28', '2026-03-01'));
|
||||
|
||||
self::assertSame(4, $dto->paxCount);
|
||||
}
|
||||
|
||||
public function testInitFromParamsRejectsNormalizedInvalidCalendarDates(): void
|
||||
{
|
||||
$service = $this->createServiceWithAccommodation();
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Ungültiges Datumsformat. Erwartet: YYYY-MM-DD.');
|
||||
|
||||
$service->initFromParams(new AccommodationBookingQueryParams('HOTEL', '2026-02-31', '2026-03-05'));
|
||||
}
|
||||
|
||||
public function testIssueAccessLinkForDirectBookingSetsTimestampForDirectBooking(): void
|
||||
{
|
||||
$entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
$entityManager->expects(self::once())->method('flush');
|
||||
|
||||
$service = $this->createServiceWithAccommodation(entityManager: $entityManager);
|
||||
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(false);
|
||||
|
||||
$service->issueAccessLinkForDirectBooking($booking);
|
||||
|
||||
self::assertNotNull($booking->getAccessLinkIssuedAt());
|
||||
}
|
||||
|
||||
public function testIssueAccessLinkForDirectBookingNoOpsForInquiry(): void
|
||||
{
|
||||
$entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
$entityManager->expects(self::never())->method('flush');
|
||||
|
||||
$service = $this->createServiceWithAccommodation(entityManager: $entityManager);
|
||||
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(true);
|
||||
|
||||
$service->issueAccessLinkForDirectBooking($booking);
|
||||
|
||||
self::assertNull($booking->getAccessLinkIssuedAt());
|
||||
}
|
||||
|
||||
public function testIssueAccessLinkForDirectBookingNoOpsWhenAlreadySet(): void
|
||||
{
|
||||
$entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
$entityManager->expects(self::never())->method('flush');
|
||||
|
||||
$service = $this->createServiceWithAccommodation(entityManager: $entityManager);
|
||||
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(false);
|
||||
$issuedAt = new \DateTimeImmutable('2026-01-01');
|
||||
$booking->setAccessLinkIssuedAt($issuedAt);
|
||||
|
||||
$service->issueAccessLinkForDirectBooking($booking);
|
||||
|
||||
self::assertSame($issuedAt, $booking->getAccessLinkIssuedAt());
|
||||
}
|
||||
|
||||
public function testSendCustomerConfirmationEmailAlwaysSendsWithLinkWhenIssued(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setEmail('[email protected]');
|
||||
$booking->setIsInquiry(false);
|
||||
$booking->setAccessLinkIssuedAt(new \DateTimeImmutable());
|
||||
|
||||
$linkSigner = $this->createMock(AccommodationBookingLinkSigner::class);
|
||||
$linkSigner->method('sign')->with($booking)->willReturn('https://example.com/offer/signed-link');
|
||||
|
||||
$breakdownCalculator = $this->createMock(AccommodationBookingBreakdownCalculator::class);
|
||||
$breakdownCalculator->method('compute')->willReturn(['total' => 1000, 'currency' => 'EUR']);
|
||||
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
$mailer
|
||||
->expects(self::once())
|
||||
->method('createAndSendEmail')
|
||||
->with(
|
||||
self::callback(static fn (array $context) => 'https://example.com/offer/signed-link' === $context['accessLink']),
|
||||
self::callback(static fn (array $options) => '[email protected]' === $options['to']
|
||||
&& 'email/accommodation_booking_customer.html.twig' === $options['template']),
|
||||
);
|
||||
|
||||
$service = $this->createServiceWithAccommodation(mailer: $mailer, linkSigner: $linkSigner, breakdownCalculator: $breakdownCalculator);
|
||||
|
||||
$service->sendCustomerConfirmationEmail($booking);
|
||||
}
|
||||
|
||||
public function testSendCustomerConfirmationEmailSendsWithoutLinkForInquiry(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setEmail('[email protected]');
|
||||
$booking->setIsInquiry(true);
|
||||
|
||||
$breakdownCalculator = $this->createMock(AccommodationBookingBreakdownCalculator::class);
|
||||
$breakdownCalculator->method('compute')->willReturn(null);
|
||||
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
$mailer
|
||||
->expects(self::once())
|
||||
->method('createAndSendEmail')
|
||||
->with(
|
||||
self::callback(static fn (array $context) => null === $context['accessLink']),
|
||||
self::anything(),
|
||||
);
|
||||
|
||||
$service = $this->createServiceWithAccommodation(mailer: $mailer, breakdownCalculator: $breakdownCalculator);
|
||||
|
||||
$service->sendCustomerConfirmationEmail($booking);
|
||||
}
|
||||
|
||||
public function testSendCustomerConfirmationEmailLogsAndSwallowsMailerFailures(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setEmail('[email protected]');
|
||||
$booking->setIsInquiry(true);
|
||||
|
||||
$breakdownCalculator = $this->createMock(AccommodationBookingBreakdownCalculator::class);
|
||||
$breakdownCalculator->method('compute')->willReturn(null);
|
||||
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
$mailer->method('createAndSendEmail')->willThrowException(new \RuntimeException('SMTP down'));
|
||||
|
||||
$logger = $this->createMock(LoggerInterface::class);
|
||||
$logger->expects(self::once())->method('error');
|
||||
|
||||
$service = $this->createServiceWithAccommodation(mailer: $mailer, breakdownCalculator: $breakdownCalculator, logger: $logger);
|
||||
|
||||
$service->sendCustomerConfirmationEmail($booking);
|
||||
}
|
||||
|
||||
public function testRegenerateAccessLinkOverwritesAccessLinkIssuedAt(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setEmail('[email protected]');
|
||||
$booking->setIsInquiry(true);
|
||||
$previousIssuedAt = new \DateTimeImmutable('2026-01-01');
|
||||
$booking->setAccessLinkIssuedAt($previousIssuedAt);
|
||||
|
||||
$entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
$entityManager->expects(self::once())->method('flush');
|
||||
|
||||
$breakdownCalculator = $this->createMock(AccommodationBookingBreakdownCalculator::class);
|
||||
$breakdownCalculator->method('compute')->willReturn(null);
|
||||
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
$mailer->expects(self::never())->method('createAndSendEmail');
|
||||
|
||||
$service = $this->createServiceWithAccommodation(
|
||||
entityManager: $entityManager,
|
||||
mailer: $mailer,
|
||||
breakdownCalculator: $breakdownCalculator,
|
||||
);
|
||||
|
||||
$service->regenerateAccessLink($booking);
|
||||
|
||||
self::assertNotSame($previousIssuedAt, $booking->getAccessLinkIssuedAt());
|
||||
}
|
||||
|
||||
public function testAcceptBookingTransitionsInquiryToBookingAndSendsNotifications(): void
|
||||
{
|
||||
$entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
$entityManager->expects(self::once())->method('flush');
|
||||
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(true);
|
||||
$booking->setEmail('[email protected]');
|
||||
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
$mailer
|
||||
->expects(self::exactly(2))
|
||||
->method('createAndSendEmail')
|
||||
->with(
|
||||
self::anything(),
|
||||
self::callback(static fn (array $options) => in_array($options['to'], ['[email protected]', '[email protected]'], true)
|
||||
&& in_array($options['template'], ['email/offer_accepted.html.twig', 'email/offer_accepted_customer.html.twig'], true)),
|
||||
);
|
||||
|
||||
$service = $this->createServiceWithAccommodation(entityManager: $entityManager, mailer: $mailer);
|
||||
|
||||
$service->acceptBooking($booking);
|
||||
|
||||
self::assertFalse($booking->isInquiry());
|
||||
self::assertNotNull($booking->getAcceptedAt());
|
||||
}
|
||||
|
||||
public function testAcceptBookingIsIdempotentAndSendsNoNotifications(): void
|
||||
{
|
||||
$entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
$entityManager->expects(self::never())->method('flush');
|
||||
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
$mailer->expects(self::never())->method('createAndSendEmail');
|
||||
|
||||
$service = $this->createServiceWithAccommodation(entityManager: $entityManager, mailer: $mailer);
|
||||
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(false);
|
||||
|
||||
$service->acceptBooking($booking);
|
||||
|
||||
self::assertNull($booking->getAcceptedAt());
|
||||
}
|
||||
|
||||
public function testSendOfferAcceptedNotificationEmailLogsAndSwallowsMailerFailures(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setIsInquiry(false);
|
||||
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
$mailer->method('createAndSendEmail')->willThrowException(new \RuntimeException('SMTP down'));
|
||||
|
||||
$logger = $this->createMock(LoggerInterface::class);
|
||||
$logger->expects(self::once())->method('error');
|
||||
|
||||
$service = $this->createServiceWithAccommodation(mailer: $mailer, logger: $logger);
|
||||
|
||||
$service->sendOfferAcceptedNotificationEmail($booking);
|
||||
}
|
||||
|
||||
public function testSendOfferAcceptedCustomerEmailLogsAndSwallowsMailerFailures(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setEmail('[email protected]');
|
||||
$booking->setIsInquiry(false);
|
||||
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
$mailer->method('createAndSendEmail')->willThrowException(new \RuntimeException('SMTP down'));
|
||||
|
||||
$logger = $this->createMock(LoggerInterface::class);
|
||||
$logger->expects(self::once())->method('error');
|
||||
|
||||
$service = $this->createServiceWithAccommodation(mailer: $mailer, logger: $logger);
|
||||
|
||||
$service->sendOfferAcceptedCustomerEmail($booking);
|
||||
}
|
||||
|
||||
public function testRefreshPriceSnapshotStoresDiscountedFinalTotal(): void
|
||||
{
|
||||
$booking = new AccommodationBooking();
|
||||
$booking->setAccommodationDiscount(10);
|
||||
$booking->setBoardServiceDiscount(20);
|
||||
$booking->setAdditionalServicesDiscount(50);
|
||||
|
||||
$breakdown = [
|
||||
'total' => 12345,
|
||||
'currency' => 'CHF',
|
||||
'basePrice' => 8000,
|
||||
'additionalPersonsPrice' => 2000,
|
||||
'boardPrice' => 1000,
|
||||
'servicesPrice' => 500,
|
||||
];
|
||||
|
||||
$breakdownCalculator = $this->createMock(AccommodationBookingBreakdownCalculator::class);
|
||||
$breakdownCalculator
|
||||
->expects(self::once())
|
||||
->method('computeCurrent')
|
||||
->with($booking)
|
||||
->willReturn($breakdown);
|
||||
|
||||
$service = $this->createServiceWithAccommodation(breakdownCalculator: $breakdownCalculator);
|
||||
$service->refreshPriceSnapshot($booking);
|
||||
|
||||
// accommodation: (8000+2000)*10% = 1000, board: 1000*20% = 200, services: 500*50% = 250
|
||||
self::assertSame($breakdown, $booking->getPriceBreakdown());
|
||||
self::assertSame(12345 - 1000 - 200 - 250, $booking->getTotalPrice());
|
||||
self::assertSame('CHF', $booking->getPricingCurrency());
|
||||
self::assertSame(1, $booking->getPricingVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AccommodationPrice[] $prices
|
||||
*/
|
||||
private function createServiceWithAccommodation(
|
||||
array $prices = [],
|
||||
?EntityManagerInterface $entityManager = null,
|
||||
?Mailer $mailer = null,
|
||||
?LoggerInterface $logger = null,
|
||||
?AccommodationBookingLinkSigner $linkSigner = null,
|
||||
?AccommodationBookingBreakdownCalculator $breakdownCalculator = null,
|
||||
): AccommodationBookingService {
|
||||
$accommodation = (new Accommodation())
|
||||
->setName('Hotel')
|
||||
->setCalendarCode('HOTEL')
|
||||
->setMaxAdolescentAge(17);
|
||||
|
||||
$accommodationRepo = $this->createMock(AccommodationRepository::class);
|
||||
$accommodationRepo
|
||||
->method('findOneBy')
|
||||
->with(['calendarCode' => 'HOTEL'])
|
||||
->willReturn($accommodation);
|
||||
|
||||
$priceRepo = $this->createMock(AccommodationPriceRepository::class);
|
||||
$priceRepo
|
||||
->method('findByHotelCodeAndDateRange')
|
||||
->willReturn($prices);
|
||||
|
||||
return new AccommodationBookingService(
|
||||
$accommodationRepo,
|
||||
$priceRepo,
|
||||
$this->createMock(AdditionalServiceRepository::class),
|
||||
$this->createMock(BoardServiceRepository::class),
|
||||
new PriceTimelineBuilder(),
|
||||
$entityManager ?? $this->createMock(EntityManagerInterface::class),
|
||||
$mailer ?? $this->createMock(Mailer::class),
|
||||
$logger ?? $this->createMock(LoggerInterface::class),
|
||||
$this->createMock(CmsDataProvider::class),
|
||||
$linkSigner ?? $this->createMock(AccommodationBookingLinkSigner::class),
|
||||
$breakdownCalculator ?? $this->createMock(AccommodationBookingBreakdownCalculator::class),
|
||||
'[email protected]',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\Service\CmsDataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||
use Symfony\Component\HttpClient\MockHttpClient;
|
||||
use Symfony\Component\HttpClient\Response\MockResponse;
|
||||
|
||||
class CmsDataProviderTest extends TestCase
|
||||
{
|
||||
public function testGetHotelDetailsMapsSnakeCaseFieldsToDto(): void
|
||||
{
|
||||
$client = new MockHttpClient(fn () => new MockResponse(json_encode([
|
||||
'success' => true,
|
||||
'name' => 'Hotel Alpin',
|
||||
'address' => "Musterstraße 1\n1234 Musterort",
|
||||
'description' => '<p>Beschreibung</p>',
|
||||
'features' => '<p>Ausstattung</p>',
|
||||
'room_types' => '<p>Zimmer</p>',
|
||||
'additional_information' => '<p>Weitere Infos</p>',
|
||||
'images' => ['resized' => ['l' => [['url' => 'l.jpg', 'alt' => 'Alt']]]],
|
||||
'icons' => ['sauna' => ['label' => 'Sauna', 'value' => true]],
|
||||
'region' => [
|
||||
'name' => 'Alpenregion',
|
||||
'latitude' => 47.1,
|
||||
'longitude' => 11.2,
|
||||
'webcam' => 'https://webcam.test',
|
||||
'ski_area' => '<p>Skigebiet</p>',
|
||||
'ski_area_extended' => '<p>Mehr Skigebiet</p>',
|
||||
'description' => '<p>Region</p>',
|
||||
'news' => '<p>News</p>',
|
||||
'length' => '42',
|
||||
'altitude' => '1800',
|
||||
'lifts' => '5',
|
||||
'images' => ['resized' => ['l' => []]],
|
||||
'region_maps' => ['map1.jpg', 'map2.jpg'],
|
||||
],
|
||||
], JSON_THROW_ON_ERROR)));
|
||||
|
||||
$provider = new CmsDataProvider($client, new ArrayAdapter(), 'test-key');
|
||||
|
||||
$data = $provider->getHotelDetails('HOTEL1');
|
||||
|
||||
self::assertNotNull($data);
|
||||
self::assertSame('Hotel Alpin', $data->name);
|
||||
self::assertSame("Musterstraße 1\n1234 Musterort", $data->address);
|
||||
self::assertSame('<p>Zimmer</p>', $data->roomTypes);
|
||||
self::assertSame('<p>Weitere Infos</p>', $data->additionalInformation);
|
||||
self::assertSame(['sauna' => ['label' => 'Sauna', 'value' => true]], $data->icons);
|
||||
self::assertNotNull($data->region);
|
||||
self::assertSame('Alpenregion', $data->region->name);
|
||||
self::assertSame('<p>Skigebiet</p>', $data->region->skiArea);
|
||||
self::assertSame('<p>Mehr Skigebiet</p>', $data->region->skiAreaExtended);
|
||||
self::assertSame(['map1.jpg', 'map2.jpg'], $data->region->regionMaps);
|
||||
self::assertSame(42, $data->region->length);
|
||||
self::assertSame(1800, $data->region->altitude);
|
||||
self::assertSame(5, $data->region->lifts);
|
||||
self::assertSame(47.1, $data->region->latitude);
|
||||
self::assertSame(11.2, $data->region->longitude);
|
||||
}
|
||||
|
||||
public function testGetHotelDetailsReturnsNullOnFailure(): void
|
||||
{
|
||||
$client = new MockHttpClient(fn () => new MockResponse('', ['http_code' => 500]));
|
||||
|
||||
$provider = new CmsDataProvider($client, new ArrayAdapter(), 'test-key');
|
||||
|
||||
self::assertNull($provider->getHotelDetails('UNKNOWN'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user