fix: correctly validate pickup selection
closes #869c94jqn
This commit is contained in:
@@ -28,14 +28,12 @@ class ParticipantValidator extends ConstraintValidator
|
||||
|
||||
public function assertPickupSelected(ParticipantDto $participant): void
|
||||
{
|
||||
// Check if either outbound or inbound transportation is bus
|
||||
// Pickup applies only to outbound bus journey
|
||||
$hasOutboundBus = null !== $participant->transportationOutbound
|
||||
&& 'BUS' === $participant->transportationOutbound->subType;
|
||||
$hasInboundBus = null !== $participant->transportationInbound
|
||||
&& 'BUS' === $participant->transportationInbound->subType;
|
||||
|
||||
// Require pickup if at least one direction has bus transport
|
||||
if (($hasOutboundBus || $hasInboundBus) && null === $participant->pickup) {
|
||||
// Require pickup only when outbound transportation is bus
|
||||
if (true === $hasOutboundBus && null === $participant->pickup) {
|
||||
$this->context->buildViolation('Bitte auswählen')
|
||||
->atPath('pickup')
|
||||
->addViolation()
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Validator\Constraints;
|
||||
|
||||
use App\BusProNet\Model\Pickup;
|
||||
use App\BusProNet\Model\Service;
|
||||
use App\Form\Model\ParticipantDto;
|
||||
use App\Validator\Constraints\Participant;
|
||||
@@ -107,6 +108,7 @@ class ParticipantValidatorTest extends ConstraintValidatorTestCase
|
||||
$busService = new Service();
|
||||
$busService->subType = 'BUS';
|
||||
$participant->transportationOutbound = $busService;
|
||||
$participant->transportationInbound = $this->createMockService();
|
||||
$participant->pickup = null;
|
||||
|
||||
$this->validator->validate($participant, new Participant());
|
||||
@@ -116,6 +118,46 @@ class ParticipantValidatorTest extends ConstraintValidatorTestCase
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testParticipantWithInboundBusOnlyAndNoPickupPassesValidation(): void
|
||||
{
|
||||
$participant = $this->createValidParticipant();
|
||||
$participant->transportationOutbound = $this->createMockService();
|
||||
|
||||
$busService = new Service();
|
||||
$busService->subType = 'BUS';
|
||||
$participant->transportationInbound = $busService;
|
||||
|
||||
$participant->pickup = null;
|
||||
$participant->dropOff = $this->createMockPickup();
|
||||
|
||||
$this->validator->validate($participant, new Participant());
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testParticipantWithPkwOutboundBusInboundRequiresDropOffButNotPickup(): void
|
||||
{
|
||||
$participant = $this->createValidParticipant();
|
||||
|
||||
$carService = new Service();
|
||||
$carService->subType = 'PKW';
|
||||
$participant->transportationOutbound = $carService;
|
||||
|
||||
$busService = new Service();
|
||||
$busService->subType = 'BUS';
|
||||
$participant->transportationInbound = $busService;
|
||||
|
||||
$participant->pickup = null;
|
||||
$participant->dropOff = null;
|
||||
|
||||
$this->validator->validate($participant, new Participant());
|
||||
|
||||
$this->buildViolation('Bitte auswählen')
|
||||
->atPath('property.path.dropOff')
|
||||
->assertRaised();
|
||||
$this->assertCount(1, $this->context->getViolations());
|
||||
}
|
||||
|
||||
public function testParticipantWithNonBusTransportationPassesPickupValidation(): void
|
||||
{
|
||||
$participant = $this->createValidParticipant();
|
||||
@@ -153,4 +195,13 @@ class ParticipantValidatorTest extends ConstraintValidatorTestCase
|
||||
|
||||
return $service;
|
||||
}
|
||||
|
||||
private function createMockPickup(): Pickup
|
||||
{
|
||||
$pickup = new Pickup();
|
||||
$pickup->id = 123;
|
||||
$pickup->city = 'Test City';
|
||||
|
||||
return $pickup;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user