chore: major code cleanup and type safety using phpstan

This commit is contained in:
Björn Fromme committed 2026-04-16 17:11:16 +02:00
1 parent 060cbea9da
commit 3da73d626b
103 files changed
+976 -1192

No files matched your search

@@ -11,11 +11,14 @@ use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\ParticipantTransportationDiscountReplacementFieldHandler;
use App\Service\ServiceAvailabilityCalculator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestCase
{
private ParticipantTransportationDiscountReplacementFieldHandler $handler;
/** @var ServiceAvailabilityCalculator&MockObject */
private ServiceAvailabilityCalculator $serviceAvailabilityCalculator;
protected function setUp(): void
@@ -28,18 +31,18 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
public function testGetFieldName(): void
{
$this->assertSame('transportationDiscountReplacement', $this->handler->getFieldName());
self::assertSame('transportationDiscountReplacement', $this->handler->getFieldName());
}
public function testGetDependencies(): void
{
$this->assertSame(['transportationOutbound', 'transportationInbound'], $this->handler->getDependencies());
self::assertSame(['transportationOutbound', 'transportationInbound'], $this->handler->getDependencies());
}
public function testShouldProcessAlwaysReturnsTrue(): void
{
$this->assertTrue($this->handler->shouldProcess([], BookingDto::MODE_CREATE, 0));
$this->assertTrue($this->handler->shouldProcess(['some' => 'data'], BookingDto::MODE_EDIT, 5));
self::assertTrue($this->handler->shouldProcess([], BookingDto::MODE_CREATE, 0));
self::assertTrue($this->handler->shouldProcess(['some' => 'data'], BookingDto::MODE_EDIT, 5));
}
public function testProcessFieldReplacesDiscountedWithRegularWhenInboundIsBus(): void
@@ -65,16 +68,16 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert outbound was replaced with regular PKW
$this->assertSame($regularPkw->id, $participant->transportationOutbound->id);
$this->assertSame($regularPkw->label, $participant->transportationOutbound->label);
self::assertSame($regularPkw->id, $participant->transportationOutbound->id);
self::assertSame($regularPkw->label, $participant->transportationOutbound->label);
// Assert notification was generated
$this->assertCount(1, $participant->notifications);
self::assertCount(1, $participant->notifications);
$notification = reset($participant->notifications); // Get first notification (keyed by MD5 hash)
$this->assertSame('warning', $notification['type']);
$this->assertStringContainsString('Hinfahrt automatisch angepasst', $notification['message']);
$this->assertStringContainsString('Selbstorganisiert (-15€ Rabatt)', $notification['message']);
$this->assertStringContainsString('Selbstorganisiert', $notification['message']);
self::assertSame('warning', $notification['type']);
self::assertStringContainsString('Hinfahrt automatisch angepasst', $notification['message']);
self::assertStringContainsString('Selbstorganisiert (-15€ Rabatt)', $notification['message']);
self::assertStringContainsString('Selbstorganisiert', $notification['message']);
}
public function testProcessFieldRestoresDiscountedPkwWhenInboundChangesToPkw(): void
@@ -96,7 +99,7 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
// Mock availability: discounted PKW is available
$this->serviceAvailabilityCalculator
->expects($this->once())
->expects(self::once())
->method('isServiceUnavailable')
->with($discountedPkw->id, $bookingDto, 0)
->willReturn(false);
@@ -107,15 +110,15 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert outbound was replaced with discounted PKW
$this->assertSame($discountedPkw->id, $participant->transportationOutbound->id);
$this->assertSame($discountedPkw->label, $participant->transportationOutbound->label);
self::assertSame($discountedPkw->id, $participant->transportationOutbound->id);
self::assertSame($discountedPkw->label, $participant->transportationOutbound->label);
// Assert notification was generated
$this->assertCount(1, $participant->notifications);
self::assertCount(1, $participant->notifications);
$notification = reset($participant->notifications); // Get first notification (keyed by MD5 hash)
$this->assertSame('success', $notification['type']);
$this->assertStringContainsString('Hinfahrt automatisch angepasst', $notification['message']);
$this->assertStringContainsString('wieder verfügbar', $notification['message']);
self::assertSame('success', $notification['type']);
self::assertStringContainsString('Hinfahrt automatisch angepasst', $notification['message']);
self::assertStringContainsString('wieder verfügbar', $notification['message']);
}
public function testProcessFieldDoesNotRestoreDiscountWhenUnavailable(): void
@@ -137,7 +140,7 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
// Mock availability: discounted PKW is sold out
$this->serviceAvailabilityCalculator
->expects($this->once())
->expects(self::once())
->method('isServiceUnavailable')
->with($discountedPkw->id, $bookingDto, 0)
->willReturn(true);
@@ -148,10 +151,10 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert outbound remains regular PKW
$this->assertSame($regularPkw, $participant->transportationOutbound);
self::assertSame($regularPkw, $participant->transportationOutbound);
// Assert no notification was generated
$this->assertEmpty($participant->notifications);
self::assertEmpty($participant->notifications);
}
public function testProcessFieldNoReplacementWhenOutboundIsBus(): void
@@ -177,10 +180,10 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert outbound remains BUS (no replacement)
$this->assertSame($busOutbound, $participant->transportationOutbound);
self::assertSame($busOutbound, $participant->transportationOutbound);
// Assert no notification was generated
$this->assertEmpty($participant->notifications);
self::assertEmpty($participant->notifications);
}
public function testProcessFieldNoReplacementWhenOutboundIsRegularPkwAndInboundIsBus(): void
@@ -206,10 +209,10 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert outbound remains regular PKW (already correct)
$this->assertSame($regularPkw, $participant->transportationOutbound);
self::assertSame($regularPkw, $participant->transportationOutbound);
// Assert no notification was generated
$this->assertEmpty($participant->notifications);
self::assertEmpty($participant->notifications);
}
public function testProcessFieldNoReplacementWhenOutboundIsDiscountedAndInboundIsPkw(): void
@@ -235,10 +238,10 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert outbound remains discounted PKW (already optimal)
$this->assertSame($discountedPkw, $participant->transportationOutbound);
self::assertSame($discountedPkw, $participant->transportationOutbound);
// Assert no notification was generated
$this->assertEmpty($participant->notifications);
self::assertEmpty($participant->notifications);
}
public function testProcessFieldHandlesNullOutbound(): void
@@ -259,8 +262,8 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert no error and no notification
$this->assertNull($participant->transportationOutbound);
$this->assertEmpty($participant->notifications);
self::assertNull($participant->transportationOutbound);
self::assertEmpty($participant->notifications);
}
public function testProcessFieldHandlesNullInbound(): void
@@ -281,7 +284,7 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
// Mock availability: discounted PKW is available
$this->serviceAvailabilityCalculator
->expects($this->once())
->expects(self::once())
->method('isServiceUnavailable')
->with($discountedPkw->id, $bookingDto, 0)
->willReturn(false);
@@ -292,8 +295,8 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert outbound was replaced with discounted PKW
$this->assertSame($discountedPkw->id, $participant->transportationOutbound->id);
$this->assertSame($discountedPkw->label, $participant->transportationOutbound->label);
self::assertSame($discountedPkw->id, $participant->transportationOutbound->id);
self::assertSame($discountedPkw->label, $participant->transportationOutbound->label);
}
public function testProcessFieldHandlesNullParticipant(): void
@@ -333,7 +336,7 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
// Availability calculator should NOT be called for babies
$this->serviceAvailabilityCalculator
->expects($this->never())
->expects(self::never())
->method('isServiceUnavailable');
$submittedData = [];
@@ -342,10 +345,10 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert outbound remains regular PKW (no discount restoration for babies)
$this->assertSame($regularPkw->id, $participant->transportationOutbound->id);
self::assertSame($regularPkw->id, $participant->transportationOutbound->id);
// Assert no notification was generated
$this->assertEmpty($participant->notifications);
self::assertEmpty($participant->notifications);
}
public function testProcessFieldSkipsDiscountRestorationForTwoYearOldParticipant(): void
@@ -369,7 +372,7 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
// Availability calculator should NOT be called for babies
$this->serviceAvailabilityCalculator
->expects($this->never())
->expects(self::never())
->method('isServiceUnavailable');
$submittedData = [];
@@ -378,10 +381,10 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert outbound remains regular PKW
$this->assertSame($regularPkw->id, $participant->transportationOutbound->id);
self::assertSame($regularPkw->id, $participant->transportationOutbound->id);
// Assert no notification was generated
$this->assertEmpty($participant->notifications);
self::assertEmpty($participant->notifications);
}
public function testProcessFieldRestoresDiscountForThreeYearOldParticipant(): void
@@ -406,7 +409,7 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
// Mock availability: discounted PKW is available
$this->serviceAvailabilityCalculator
->expects($this->once())
->expects(self::once())
->method('isServiceUnavailable')
->with($discountedPkw->id, $bookingDto, 0)
->willReturn(false);
@@ -417,10 +420,10 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
$this->handler->processField($submittedData, $bookingDto, 0);
// Assert outbound was replaced with discounted PKW (3-year-old is eligible)
$this->assertSame($discountedPkw->id, $participant->transportationOutbound->id);
self::assertSame($discountedPkw->id, $participant->transportationOutbound->id);
// Assert notification was generated
$this->assertCount(1, $participant->notifications);
self::assertCount(1, $participant->notifications);
}
private function createPkwService(int $id, string $label, float $price, string $direction = DirectionMapper::OUTBOUND_TRAVEL): Service
@@ -447,6 +450,9 @@ class ParticipantTransportationDiscountReplacementFieldHandlerTest extends TestC
return $service;
}
/**
* @param array<int, Service> $services
*/
private function createTravelWithServices(array $services): Travel
{
$travel = new Travel();