|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use PhpTypedValues\DateTime\DateTimeRFC3339Extended; |
| 6 | + |
| 7 | +it('fromDateTime returns same instant and toString is ISO 8601', function (): void { |
| 8 | + $dt = new DateTimeImmutable('2025-01-02T03:04:05+00:00'); |
| 9 | + $vo = DateTimeRFC3339Extended::fromDateTime($dt); |
| 10 | + |
| 11 | + expect($vo->value()->format(\DATE_RFC3339_EXTENDED))->toBe('2025-01-02T03:04:05.000+00:00') |
| 12 | + ->and($vo->toString())->toBe('2025-01-02T03:04:05.000+00:00'); |
| 13 | +}); |
| 14 | + |
| 15 | +it('fromString parses valid RFC3339 and preserves timezone offset', function (): void { |
| 16 | + $vo = DateTimeRFC3339Extended::fromString('2030-12-31T23:59:59.000+03:00'); |
| 17 | + |
| 18 | + expect($vo->toString())->toBe('2030-12-31T23:59:59.000+03:00') |
| 19 | + ->and($vo->value()->format(\DATE_RFC3339_EXTENDED))->toBe('2030-12-31T23:59:59.000+03:00'); |
| 20 | +}); |
| 21 | + |
| 22 | +it('fromString throws on invalid date parts (errors path)', function (): void { |
| 23 | + // invalid month 13 produces errors |
| 24 | + $call = fn() => DateTimeRFC3339Extended::fromString('2025-13-02T03:04:05.000+00:00'); |
| 25 | + expect($call)->toThrow(PhpTypedValues\Code\Exception\DateTimeTypeException::class); |
| 26 | +}); |
| 27 | + |
| 28 | +it('fromString throws on trailing data (warnings path)', function (): void { |
| 29 | + // trailing space should trigger a warning from DateTime::getLastErrors() |
| 30 | + try { |
| 31 | + DateTimeRFC3339Extended::fromString('2025-01-02T03:04:05.000+00:00 '); |
| 32 | + expect()->fail('Exception was not thrown'); |
| 33 | + } catch (Throwable $e) { |
| 34 | + expect($e)->toBeInstanceOf(PhpTypedValues\Code\Exception\DateTimeTypeException::class) |
| 35 | + ->and($e->getMessage())->toContain('Invalid date time value'); |
| 36 | + } |
| 37 | +}); |
| 38 | + |
| 39 | +it('getFormat returns RFC3339 format', function (): void { |
| 40 | + expect(DateTimeRFC3339Extended::getFormat())->toBe(\DATE_RFC3339_EXTENDED); |
| 41 | +}); |
| 42 | + |
| 43 | +it('fromString with both parse error and warning includes both details in the exception message', function (): void { |
| 44 | + // invalid month (error) + trailing space (warning) |
| 45 | + $input = '2025-13-02T03:04:05.000+00:00 '; |
| 46 | + try { |
| 47 | + DateTimeRFC3339Extended::fromString($input); |
| 48 | + expect()->fail('Exception was not thrown'); |
| 49 | + } catch (Throwable $e) { |
| 50 | + expect($e)->toBeInstanceOf(PhpTypedValues\Code\Exception\DateTimeTypeException::class) |
| 51 | + ->and($e->getMessage())->toContain('Invalid date time value') |
| 52 | + ->and($e->getMessage())->toContain('Error at') |
| 53 | + ->and($e->getMessage())->toContain('Warning at'); |
| 54 | + } |
| 55 | +}); |
| 56 | + |
| 57 | +it('fromString aggregates multiple parse warnings with proper concatenation and line breaks', function (): void { |
| 58 | + // Multiple invalid components to force several distinct parse errors |
| 59 | + $input = '2025-13-40T25:61:61.000+00:00'; |
| 60 | + try { |
| 61 | + DateTimeRFC3339Extended::fromString($input); |
| 62 | + expect()->fail('Exception was not thrown'); |
| 63 | + } catch (Throwable $e) { |
| 64 | + $msg = $e->getMessage(); |
| 65 | + // Header should be present and terminated with a newline |
| 66 | + $expectedHeader = \sprintf( |
| 67 | + 'Invalid date time value "%s", use format "%s"', |
| 68 | + $input, |
| 69 | + \DATE_RFC3339_EXTENDED |
| 70 | + ) . \PHP_EOL; |
| 71 | + |
| 72 | + expect($e)->toBeInstanceOf(PhpTypedValues\Code\Exception\DateTimeTypeException::class) |
| 73 | + ->and($msg)->toContain($expectedHeader) |
| 74 | + ->and($msg)->toContain('Invalid date time value "2025-13-40T25:61:61.000+00:00", use format "Y-m-d\TH:i:s.vP" |
| 75 | +Warning at 29: The parsed date was invalid |
| 76 | +') |
| 77 | + // No injected garbage from the mutation should appear |
| 78 | + ->and($msg)->not->toContain('PEST Mutator was here!'); |
| 79 | + } |
| 80 | +}); |
| 81 | + |
| 82 | +it('errors-only path keeps newline after header and after error line', function (): void { |
| 83 | + $input = '2025-01-02T03:04:05.000+00:00 '; |
| 84 | + try { |
| 85 | + DateTimeRFC3339Extended::fromString($input); |
| 86 | + expect()->fail('Exception was not thrown'); |
| 87 | + } catch (Throwable $e) { |
| 88 | + $msg = $e->getMessage(); |
| 89 | + // must contain the header + newline and at least one warning line ending with newline |
| 90 | + expect($e)->toBeInstanceOf(PhpTypedValues\Code\Exception\DateTimeTypeException::class) |
| 91 | + ->and($msg)->toContain('Invalid date time value "2025-01-02T03:04:05.000+00:00 ", use format "Y-m-d\TH:i:s.vP" |
| 92 | +Error at 29: Trailing data |
| 93 | +'); |
| 94 | + |
| 95 | + // Count total newlines: one after header + one after the warning line => at least 2 |
| 96 | + expect(substr_count($msg, \PHP_EOL))->toBeGreaterThanOrEqual(2); |
| 97 | + } |
| 98 | +}); |
| 99 | + |
| 100 | +it('double error message', function (): void { |
| 101 | + $input = '2025-12-02T03:04:05.000+ 00:00'; |
| 102 | + try { |
| 103 | + DateTimeRFC3339Extended::fromString($input); |
| 104 | + expect()->fail('Exception was not thrown'); |
| 105 | + } catch (Throwable $e) { |
| 106 | + $msg = $e->getMessage(); |
| 107 | + // must contain the header + newline and at least one warning line ending with newline |
| 108 | + expect($e)->toBeInstanceOf(PhpTypedValues\Code\Exception\DateTimeTypeException::class) |
| 109 | + ->and($msg)->toContain('Invalid date time value "2025-12-02T03:04:05.000+ 00:00", use format "Y-m-d\TH:i:s.vP" |
| 110 | +Error at 23: The timezone could not be found in the database |
| 111 | +Error at 24: Trailing data |
| 112 | +'); |
| 113 | + } |
| 114 | +}); |
0 commit comments