Files
myep/tests/Service/BookingEditContextFactoryTest.php
T

171 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\BusProNet\Model\BaseData;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\MutableData;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\BookingEditContext;
use App\Form\Model\BookingMutabilityDto;
use App\Form\Model\BookingSummaryDto;
use App\Service\BookingEditContextFactory;
use App\Service\BookingSummaryAssembler;
use App\Service\ParticipantCardAssembler;
use App\Service\TravelDataProvider;
use PHPUnit\Framework\TestCase;
class BookingEditContextFactoryTest extends TestCase
{
public function testPrepareBookingDtoRefreshesTravelAvailability(): void
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
$bookingDto = new BookingDto($travel, 157047);
$travelDataService = $this->createMock(TravelDataProvider::class);
$travelDataService->expects($this->once())
->method('enrichWithFreshAvailabilities')
->with($travel);
$service = new BookingEditContextFactory(
$this->createMock(ParticipantCardAssembler::class),
$this->createMock(BookingSummaryAssembler::class),
$travelDataService,
);
$service->prepareBookingDto($bookingDto);
}
public function testCreateBuildsEditContext(): void
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
$bookingDto = new BookingDto($travel, 157047);
$bookingDto->booking = new Booking();
$bookingData = new Booking();
$bookingData->dateId = 1234;
$mutableData = new BaseData([
MutableData::CATEGORY_ADDITIONAL_SERVICES => new MutableData(MutableData::CATEGORY_ADDITIONAL_SERVICES, true, new \DateTimeImmutable('2030-01-02')),
]);
$summaryData = $this->createMock(BookingSummaryDto::class);
$summaryDataService = $this->createMock(BookingSummaryAssembler::class);
$summaryDataService->expects($this->once())
->method('getSummaryData')
->with($bookingDto)
->willReturn($summaryData);
$travelDataService = $this->createMock(TravelDataProvider::class);
$travelDataService->expects($this->once())
->method('getMutabilityData')
->with(1234)
->willReturn($mutableData);
$service = new BookingEditContextFactory(
$this->createMock(ParticipantCardAssembler::class),
$summaryDataService,
$travelDataService,
);
$context = $service->createParticipantContext($bookingDto, $bookingData);
$this->assertInstanceOf(BookingEditContext::class, $context);
$this->assertSame($bookingDto, $context->bookingDto);
$this->assertSame($bookingData, $context->bookingData);
$this->assertInstanceOf(BookingMutabilityDto::class, $context->mutableData);
$this->assertSame($mutableData->getItemByKey(MutableData::CATEGORY_ADDITIONAL_SERVICES), $context->mutableData->additionalServices);
$this->assertNull($context->mutableData->transportation);
$this->assertSame($summaryData, $context->summaryData);
}
public function testCreateParticipantContextWithoutBookingDataFallsBackToSummaryOnly(): void
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
$bookingDto = new BookingDto($travel, 157047);
$summaryData = $this->createMock(BookingSummaryDto::class);
$summaryDataService = $this->createMock(BookingSummaryAssembler::class);
$summaryDataService->expects($this->once())
->method('getSummaryData')
->with($bookingDto)
->willReturn($summaryData);
$travelDataService = $this->createMock(TravelDataProvider::class);
$travelDataService->expects($this->never())
->method('getMutabilityData');
$service = new BookingEditContextFactory(
$this->createMock(ParticipantCardAssembler::class),
$summaryDataService,
$travelDataService,
);
$context = $service->createParticipantContext($bookingDto, null);
$this->assertInstanceOf(BookingEditContext::class, $context);
$this->assertSame($bookingDto, $context->bookingDto);
$this->assertNull($context->bookingData);
$this->assertNull($context->mutableData);
$this->assertSame($summaryData, $context->summaryData);
}
public function testCreateOverviewContextBuildsEditOverviewPayload(): void
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
$bookingDto = new BookingDto($travel, 157047);
$bookingData = new Booking();
$bookingData->dateId = 1234;
$mutableData = new BaseData([]);
$summaryData = $this->createMock(BookingSummaryDto::class);
$cardsData = [];
$participantCardDataService = $this->createMock(ParticipantCardAssembler::class);
$participantCardDataService->expects($this->once())
->method('getAllCardsDataWithValidation')
->with($bookingDto)
->willReturn($cardsData);
$summaryDataService = $this->createMock(BookingSummaryAssembler::class);
$summaryDataService->expects($this->once())
->method('getSummaryData')
->with($bookingDto)
->willReturn($summaryData);
$travelDataService = $this->createMock(TravelDataProvider::class);
$travelDataService->expects($this->once())
->method('getMutabilityData')
->with(1234)
->willReturn($mutableData);
$service = new BookingEditContextFactory(
$participantCardDataService,
$summaryDataService,
$travelDataService,
);
$context = $service->createOverviewContext($bookingDto, $bookingData, true, false, true);
$this->assertInstanceOf(BookingEditContext::class, $context);
$this->assertSame($cardsData, $context->cardsData);
$this->assertTrue($context->isDirty);
$this->assertFalse($context->isSubmitted);
$this->assertTrue($context->hasValidationErrors);
}
}