fix: validation error indication on cards

This commit is contained in:
Björn Fromme
2025-10-23 10:19:18 +02:00
parent 3e54a58b9b
commit 2b272ef789
6 changed files with 208 additions and 17 deletions
@@ -11,16 +11,19 @@ use App\Form\Model\ParticipantDto;
use App\Service\BookingPriceCalculatorService;
use App\Service\ParticipantCardDataService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ParticipantCardDataServiceTest extends TestCase
{
private ParticipantCardDataService $service;
private BookingPriceCalculatorService $priceCalculator;
private ValidatorInterface $validator;
protected function setUp(): void
{
$this->priceCalculator = $this->createMock(BookingPriceCalculatorService::class);
$this->service = new ParticipantCardDataService($this->priceCalculator);
$this->validator = $this->createMock(ValidatorInterface::class);
$this->service = new ParticipantCardDataService($this->priceCalculator, $this->validator);
}
public function testGetCardDataWithFullParticipantData(): void
@@ -347,4 +350,134 @@ class ParticipantCardDataServiceTest extends TestCase
$this->assertEquals('Teilnehmer:in 1', $result2['name']);
$this->assertEquals('Teilnehmer:in 2', $result3['name']);
}
public function testGetCardDataWithValidationReturnsValidCard(): void
{
$room = new Room();
$room->id = 1;
$room->label = 'Doppelzimmer';
$travel = new Travel();
$travel->rooms = [$room];
$participant = new ParticipantDto();
$participant->firstName = 'Max';
$participant->lastName = 'Mustermann';
$participant->assignedRoomId = 1;
$participant->email = '[email protected]';
$bookingDto = new BookingDto($travel, 1);
$bookingDto->participants = [$participant];
$this->priceCalculator
->method('calculateAllParticipantIndividualPrices')
->willReturn([450.50]);
// Mock validator to return no violations (valid)
$violations = $this->createMock(\Symfony\Component\Validator\ConstraintViolationListInterface::class);
$violations->expects($this->once())
->method('count')
->willReturn(0);
$this->validator
->expects($this->once())
->method('validate')
->willReturn($violations);
$result = $this->service->getCardDataWithValidation($bookingDto, 0, ['booking_create']);
$this->assertEquals('Max Mustermann', $result['name']);
$this->assertEquals('Doppelzimmer', $result['roomName']);
$this->assertEquals('450,50 €', $result['price']);
$this->assertTrue($result['isValid']);
$this->assertEmpty($result['errorMessages']);
}
public function testGetCardDataWithValidationReturnsInvalidCard(): void
{
$travel = new Travel();
$travel->rooms = [];
$participant = new ParticipantDto();
$participant->firstName = 'Max';
$participant->lastName = 'Mustermann';
$participant->email = '[email protected]';
$bookingDto = new BookingDto($travel, 1);
$bookingDto->participants = [$participant];
$this->priceCalculator
->method('calculateAllParticipantIndividualPrices')
->willReturn([0.0]);
// Mock validator to return violations (invalid)
$violation = $this->createMock(\Symfony\Component\Validator\ConstraintViolationInterface::class);
$violation->expects($this->once())
->method('getMessage')
->willReturn('Diese E-Mail Adresse wird bereits von einem anderen Teilnehmer verwendet');
// Use ConstraintViolationList directly instead of mocking
$violations = new \Symfony\Component\Validator\ConstraintViolationList([$violation]);
$this->validator
->expects($this->once())
->method('validate')
->willReturn($violations);
$result = $this->service->getCardDataWithValidation($bookingDto, 0, ['booking_create']);
$this->assertEquals('Max Mustermann', $result['name']);
$this->assertFalse($result['isValid']);
$this->assertCount(1, $result['errorMessages']);
$this->assertEquals('Diese E-Mail Adresse wird bereits von einem anderen Teilnehmer verwendet', $result['errorMessages'][0]);
}
public function testGetAllCardsDataWithValidationReturnsAllCards(): void
{
$room = new Room();
$room->id = 1;
$room->label = 'Doppelzimmer';
$travel = new Travel();
$travel->rooms = [$room];
$participant1 = new ParticipantDto();
$participant1->firstName = 'Max';
$participant1->lastName = 'Mustermann';
$participant1->assignedRoomId = 1;
$participant1->email = '[email protected]';
$participant2 = new ParticipantDto();
$participant2->firstName = 'Anna';
$participant2->lastName = 'Schmidt';
$participant2->assignedRoomId = 1;
$participant2->email = '[email protected]';
$bookingDto = new BookingDto($travel, 1);
$bookingDto->participants = [$participant1, $participant2];
$this->priceCalculator
->expects($this->exactly(2))
->method('calculateAllParticipantIndividualPrices')
->willReturn([450.0, 500.0]);
// Mock validator to return no violations for both participants
$violations = $this->createMock(\Symfony\Component\Validator\ConstraintViolationListInterface::class);
$violations->expects($this->exactly(2))
->method('count')
->willReturn(0);
$this->validator
->expects($this->exactly(2))
->method('validate')
->willReturn($violations);
$result = $this->service->getAllCardsDataWithValidation($bookingDto, ['booking_create']);
$this->assertCount(2, $result);
$this->assertTrue($result[0]['isValid']);
$this->assertTrue($result[1]['isValid']);
$this->assertEquals('Max Mustermann', $result[0]['name']);
$this->assertEquals('Anna Schmidt', $result[1]['name']);
}
}