feat: centralize create/edit flow contexts, extract edit submit service
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\BusProNet\Model\BaseData;
|
||||
use App\BusProNet\Model\Booking;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\BookingEditContext;
|
||||
use App\Form\Model\BookingSummaryDto;
|
||||
use App\Service\BookingEditContextFactory;
|
||||
use App\Service\BookingSummaryDataService;
|
||||
use App\Service\ParticipantCardDataService;
|
||||
use App\Service\TravelDataService;
|
||||
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(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('enrichWithFreshAvailabilities')
|
||||
->with($travel);
|
||||
|
||||
$service = new BookingEditContextFactory(
|
||||
$this->createMock(ParticipantCardDataService::class),
|
||||
$this->createMock(BookingSummaryDataService::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([]);
|
||||
$summaryData = $this->createMock(BookingSummaryDto::class);
|
||||
|
||||
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
|
||||
$summaryDataService->expects($this->once())
|
||||
->method('getSummaryData')
|
||||
->with($bookingDto)
|
||||
->willReturn($summaryData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->once())
|
||||
->method('getMutabilityData')
|
||||
->with(1234)
|
||||
->willReturn($mutableData);
|
||||
|
||||
$service = new BookingEditContextFactory(
|
||||
$this->createMock(ParticipantCardDataService::class),
|
||||
$summaryDataService,
|
||||
$travelDataService,
|
||||
);
|
||||
|
||||
$context = $service->createParticipantContext($bookingDto, $bookingData);
|
||||
|
||||
$this->assertInstanceOf(BookingEditContext::class, $context);
|
||||
$this->assertSame($bookingDto, $context->bookingDto);
|
||||
$this->assertSame($bookingData, $context->bookingData);
|
||||
$this->assertSame($mutableData, $context->mutableData);
|
||||
$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(BookingSummaryDataService::class);
|
||||
$summaryDataService->expects($this->once())
|
||||
->method('getSummaryData')
|
||||
->with($bookingDto)
|
||||
->willReturn($summaryData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::class);
|
||||
$travelDataService->expects($this->never())
|
||||
->method('getMutabilityData');
|
||||
|
||||
$service = new BookingEditContextFactory(
|
||||
$this->createMock(ParticipantCardDataService::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(ParticipantCardDataService::class);
|
||||
$participantCardDataService->expects($this->once())
|
||||
->method('getAllCardsDataWithValidation')
|
||||
->with($bookingDto)
|
||||
->willReturn($cardsData);
|
||||
|
||||
$summaryDataService = $this->createMock(BookingSummaryDataService::class);
|
||||
$summaryDataService->expects($this->once())
|
||||
->method('getSummaryData')
|
||||
->with($bookingDto)
|
||||
->willReturn($summaryData);
|
||||
|
||||
$travelDataService = $this->createMock(TravelDataService::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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user