diff --git a/src/Shared/Helpers/DatesHelper.php b/src/Shared/Helpers/DatesHelper.php index 49acb36..feda33c 100644 --- a/src/Shared/Helpers/DatesHelper.php +++ b/src/Shared/Helpers/DatesHelper.php @@ -6,6 +6,7 @@ namespace Shared\Helpers; use Carbon\CarbonImmutable; use Carbon\CarbonInterface; +use Shared\Helpers\Exceptions\InvalidDateFormatException; final class DatesHelper { @@ -68,7 +69,7 @@ final class DatesHelper try { $output = new \DateTimeImmutable($datetime); } catch (\Exception) { - throw new \InvalidArgumentException('Unrecognized date/time format: ' . $datetime); + throw InvalidDateFormatException::forDateString($datetime); } } @@ -113,7 +114,7 @@ final class DatesHelper try { return new \DateTimeImmutable($datetime); } catch (\Throwable) { - throw new \InvalidArgumentException("Unrecognized date/time format: {$datetime}"); + throw InvalidDateFormatException::forDateString($datetime); } } } diff --git a/src/Shared/Helpers/Exceptions/InvalidDateFormatException.php b/src/Shared/Helpers/Exceptions/InvalidDateFormatException.php new file mode 100644 index 0000000..ede1c07 --- /dev/null +++ b/src/Shared/Helpers/Exceptions/InvalidDateFormatException.php @@ -0,0 +1,13 @@ + DatesHelper::parseDateTime($invalidDate)) - ->toThrow(\InvalidArgumentException::class, 'Unrecognized date/time format: not-a-real-date-string'); + ->toThrow(InvalidDateFormatException::class, 'Unrecognized date/time format: not-a-real-date-string'); }); it('falls back to DateTimeImmutable for valid formats not fully handled by createFromFormat', function (): void { @@ -65,13 +66,13 @@ describe('Date Helper Integration Tests', function (): void { ->and($result->format('Y-m-d'))->toBe('2025-12-25'); }); - it('throws InvalidArgumentException in parseFallback when fallback date is completely invalid logically', function (): void { + it('throws InvalidDateFormatException in parseFallback when fallback date is completely invalid logically', function (): void { // GIVEN $invalidFallbackDate = '2025-12-25T25:10:23.1234567Z'; // Invalid hour 25, matches regex but fails both createFromFormat and new DateTimeImmutable // WHEN & THEN expect(fn () => DatesHelper::parseDateTime($invalidFallbackDate)) - ->toThrow(\InvalidArgumentException::class, "Unrecognized date/time format: {$invalidFallbackDate}"); + ->toThrow(InvalidDateFormatException::class, "Unrecognized date/time format: {$invalidFallbackDate}"); }); it('handles isIn date boundary checks correctly', function (): void {