diff --git a/src/Duration.php b/src/Duration.php index 9a2d03a..6f6d8d8 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -754,15 +754,15 @@ public function toNanosPart(): int } /** - * Serializes as a string using {@see Duration::__toString()}. + * Serializes as a string using {@see Duration::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } /** - * Returns an ISO-8601 string representation of this duration. + * Returns the ISO 8601 representation of this duration. * * The format of the returned string will be PTnHnMn.nS, where n is * the relevant hours, minutes, seconds or nanoseconds part of the duration. @@ -772,7 +772,7 @@ public function jsonSerialize(): string * * Note that multiples of 24 hours are not output as days to avoid confusion with Period. */ - public function __toString(): string + public function toISOString(): string { $seconds = $this->seconds; $nanos = $this->nanos; @@ -813,4 +813,12 @@ public function __toString(): string return $string . 'S'; } + + /** + * {@see Duration::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/Instant.php b/src/Instant.php index 49238bd..81b2db2 100644 --- a/src/Instant.php +++ b/src/Instant.php @@ -366,15 +366,26 @@ public function toDecimal(): string } /** - * Serializes as a string using {@see Instant::__toString()}. + * Serializes as a string using {@see Instant::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } - public function __toString(): string + /** + * Returns the ISO 8601 representation of this instant. + */ + public function toISOString(): string { return (string) ZonedDateTime::ofInstant($this, TimeZone::utc()); } + + /** + * {@see Instant::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/Interval.php b/src/Interval.php index 2f566f9..6e6df7d 100644 --- a/src/Interval.php +++ b/src/Interval.php @@ -138,15 +138,26 @@ public function isEqualTo(Interval $that): bool } /** - * Serializes as a string using {@see Interval::__toString()}. + * Serializes as a string using {@see Interval::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } - public function __toString(): string + /** + * Returns the ISO 8601 representation of this interval. + */ + public function toISOString(): string { return $this->start . '/' . $this->end; } + + /** + * {@see Interval::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/LocalDate.php b/src/LocalDate.php index f0939c1..335fed5 100644 --- a/src/LocalDate.php +++ b/src/LocalDate.php @@ -747,23 +747,31 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable } /** - * Serializes as a string using {@see LocalDate::__toString()}. + * Serializes as a string using {@see LocalDate::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } /** - * Returns the ISO 8601 representation of this LocalDate. + * Returns the ISO 8601 representation of this date. */ - public function __toString(): string + public function toISOString(): string { $pattern = ($this->year < 0 ? '%05d' : '%04d') . '-%02d-%02d'; return sprintf($pattern, $this->year, $this->month, $this->day); } + /** + * {@see LocalDate::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } + /** * Resolves the date, resolving days past the end of month. * diff --git a/src/LocalDateRange.php b/src/LocalDateRange.php index 58ca387..6ed5fbd 100644 --- a/src/LocalDateRange.php +++ b/src/LocalDateRange.php @@ -237,11 +237,11 @@ public function count(): int } /** - * Serializes as a string using {@see LocalDateRange::__toString()}. + * Serializes as a string using {@see LocalDateRange::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } /** @@ -260,10 +260,18 @@ public function toNativeDatePeriod(): DatePeriod } /** - * Returns an ISO 8601 string representation of this date range. + * Returns the ISO 8601 representation of this date range. */ - public function __toString(): string + public function toISOString(): string { return $this->start . '/' . $this->end; } + + /** + * {@see LocalDateRange::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/LocalDateTime.php b/src/LocalDateTime.php index 5c2387e..f44afba 100644 --- a/src/LocalDateTime.php +++ b/src/LocalDateTime.php @@ -716,18 +716,29 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable } /** - * Serializes as a string using {@see LocalDateTime::__toString()}. + * Serializes as a string using {@see LocalDateTime::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } - public function __toString(): string + /** + * Returns the ISO 8601 representation of this date time. + */ + public function toISOString(): string { return $this->date . 'T' . $this->time; } + /** + * {@see LocalDateTime::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } + /** * Returns a copy of this `LocalDateTime` with the specified period added. * diff --git a/src/LocalTime.php b/src/LocalTime.php index d1376d4..663c248 100644 --- a/src/LocalTime.php +++ b/src/LocalTime.php @@ -640,17 +640,17 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable } /** - * Serializes as a string using {@see LocalTime::__toString()}. + * Serializes as a string using {@see LocalTime::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } /** - * Returns this time as a string, such as 10:15. + * Returns the ISO 8601 representation of this time. * - * The output will be one of the following ISO-8601 formats: + * The output will be one of the following formats: * * * `HH:mm` * * `HH:mm:ss` @@ -660,7 +660,7 @@ public function jsonSerialize(): string * the time where the omitted parts are implied to be zero. * The nanoseconds value, if present, can be 0 to 9 digits. */ - public function __toString(): string + public function toISOString(): string { if ($this->nano === 0) { if ($this->second === 0) { @@ -674,4 +674,12 @@ public function __toString(): string return sprintf('%02u:%02u:%02u.%s', $this->hour, $this->minute, $this->second, $nanos); } + + /** + * {@see LocalTime::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/MonthDay.php b/src/MonthDay.php index 7b1ddb4..52e5102 100644 --- a/src/MonthDay.php +++ b/src/MonthDay.php @@ -226,15 +226,26 @@ public function atYear(int $year): LocalDate } /** - * Serializes as a string using {@see MonthDay::__toString()}. + * Serializes as a string using {@see MonthDay::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } - public function __toString(): string + /** + * Returns the ISO 8601 representation of this month-day. + */ + public function toISOString(): string { return sprintf('--%02d-%02d', $this->month, $this->day); } + + /** + * {@see MonthDay::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/Period.php b/src/Period.php index aee2a22..4709762 100644 --- a/src/Period.php +++ b/src/Period.php @@ -377,14 +377,17 @@ public function toNativeDateInterval(): DateInterval } /** - * Serializes as a string using {@see Period::__toString()}. + * Serializes as a string using {@see Period::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } - public function __toString(): string + /** + * Returns the ISO 8601 representation of this period. + */ + public function toISOString(): string { if ($this->isZero()) { return 'P0D'; @@ -404,4 +407,12 @@ public function __toString(): string return $string; } + + /** + * {@see Period::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/Year.php b/src/Year.php index 10a56f3..ca6a29c 100644 --- a/src/Year.php +++ b/src/Year.php @@ -269,15 +269,26 @@ public function toLocalDateRange(): LocalDateRange } /** - * Serializes as a string using {@see Year::__toString()}. + * Serializes as a string using {@see Year::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } - public function __toString(): string + /** + * Returns the ISO 8601 representation of this year. + */ + public function toISOString(): string { return (string) $this->year; } + + /** + * {@see Year::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/YearMonth.php b/src/YearMonth.php index 032fbd4..c3d10d3 100644 --- a/src/YearMonth.php +++ b/src/YearMonth.php @@ -288,20 +288,28 @@ public function toLocalDateRange(): LocalDateRange } /** - * Serializes as a string using {@see YearMonth::__toString()}. + * Serializes as a string using {@see YearMonth::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } /** - * Returns the ISO 8601 representation of this YearMonth. + * Returns the ISO 8601 representation of this year-month. */ - public function __toString(): string + public function toISOString(): string { $pattern = ($this->year < 0 ? '%05d' : '%04d') . '-%02d'; return sprintf($pattern, $this->year, $this->month); } + + /** + * {@see YearMonth::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/YearMonthRange.php b/src/YearMonthRange.php index d7a6f6b..0515411 100644 --- a/src/YearMonthRange.php +++ b/src/YearMonthRange.php @@ -180,11 +180,11 @@ public function toLocalDateRange(): LocalDateRange } /** - * Serializes as a string using {@see YearMonthRange::__toString()}. + * Serializes as a string using {@see YearMonthRange::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } /** @@ -193,8 +193,16 @@ public function jsonSerialize(): string * ISO 8601 does not seem to provide a standard notation for year-month ranges, but we're using the same format as * date ranges. */ - public function __toString(): string + public function toISOString(): string { return $this->start . '/' . $this->end; } + + /** + * {@see YearMonthRange::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/YearWeek.php b/src/YearWeek.php index e261f1e..e9c93ed 100644 --- a/src/YearWeek.php +++ b/src/YearWeek.php @@ -296,17 +296,28 @@ public function toLocalDateRange(): LocalDateRange } /** - * Serializes as a string using {@see YearWeek::__toString()}. + * Serializes as a string using {@see YearWeek::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } - public function __toString(): string + /** + * Returns the ISO 8601 representation of this year-week. + */ + public function toISOString(): string { $pattern = ($this->year < 0 ? '%05d' : '%04d') . '-W%02d'; return sprintf($pattern, $this->year, $this->week); } + + /** + * {@see YearWeek::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index 2ced2b0..bb620f6 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -710,14 +710,17 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable } /** - * Serializes as a string using {@see ZonedDateTime::__toString()}. + * Serializes as a string using {@see ZonedDateTime::toISOString()}. */ public function jsonSerialize(): string { - return (string) $this; + return $this->toISOString(); } - public function __toString(): string + /** + * Returns the ISO 8601 representation of this zoned date time. + */ + public function toISOString(): string { $string = $this->localDateTime . $this->timeZoneOffset; @@ -727,4 +730,12 @@ public function __toString(): string return $string; } + + /** + * {@see ZonedDateTime::toISOString()}. + */ + public function __toString(): string + { + return $this->toISOString(); + } } diff --git a/tests/DurationTest.php b/tests/DurationTest.php index 8fe4611..fa1347b 100644 --- a/tests/DurationTest.php +++ b/tests/DurationTest.php @@ -1282,6 +1282,14 @@ public function testJsonSerialize(int $seconds, int $nanos, string $expected): v self::assertSame(json_encode($expected), json_encode(Duration::ofSeconds($seconds, $nanos))); } + /** + * @dataProvider providerToString + */ + public function testToISOString(int $seconds, int $nanos, string $expected): void + { + self::assertSame($expected, Duration::ofSeconds($seconds, $nanos)->toISOString()); + } + /** * @dataProvider providerToString */ diff --git a/tests/InstantTest.php b/tests/InstantTest.php index 5357957..4ce072b 100644 --- a/tests/InstantTest.php +++ b/tests/InstantTest.php @@ -645,6 +645,18 @@ public function testJsonSerialize(int $epochSecond, int $nano, string $expectedS self::assertSame(json_encode($expectedString), json_encode(Instant::of($epochSecond, $nano))); } + /** + * @dataProvider providerToString + * + * @param int $epochSecond The epoch second to test. + * @param int $nano The nano adjustment to the epoch second. + * @param string $expectedString The expected string output. + */ + public function testToISOString(int $epochSecond, int $nano, string $expectedString): void + { + self::assertSame($expectedString, Instant::of($epochSecond, $nano)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/IntervalTest.php b/tests/IntervalTest.php index 01a5733..2dcbc53 100644 --- a/tests/IntervalTest.php +++ b/tests/IntervalTest.php @@ -260,23 +260,44 @@ public function providerIsEqualTo(): array ]; } - public function testJsonSerialize(): void + /** @dataProvider providerToString */ + public function testJsonSerialize(int $epochSecondStart, int $epochSecondEnd, string $expectedString): void { $interval = Interval::of( - Instant::of(1000000000), - Instant::of(2000000000) + Instant::of($epochSecondStart), + Instant::of($epochSecondEnd) ); - self::assertSame(json_encode('2001-09-09T01:46:40Z/2033-05-18T03:33:20Z'), json_encode($interval)); + self::assertSame(json_encode($expectedString), json_encode($interval)); } - public function testToString(): void + /** @dataProvider providerToString */ + public function testToISOString(int $epochSecondStart, int $epochSecondEnd, string $expectedString): void { $interval = Interval::of( - Instant::of(1000000000), - Instant::of(2000000000) + Instant::of($epochSecondStart), + Instant::of($epochSecondEnd) ); - self::assertSame('2001-09-09T01:46:40Z/2033-05-18T03:33:20Z', (string) $interval); + self::assertSame($expectedString, $interval->toISOString()); + } + + /** @dataProvider providerToString */ + public function testToString(int $epochSecondStart, int $epochSecondEnd, string $expectedString): void + { + $interval = Interval::of( + Instant::of($epochSecondStart), + Instant::of($epochSecondEnd) + ); + + self::assertSame($expectedString, (string) $interval); + } + + public function providerToString(): array + { + return [ + [1000000000, 1000000000, '2001-09-09T01:46:40Z/2001-09-09T01:46:40Z'], + [1000000000, 2000000000, '2001-09-09T01:46:40Z/2033-05-18T03:33:20Z'], + ]; } } diff --git a/tests/LocalDateRangeTest.php b/tests/LocalDateRangeTest.php index 0bb6421..aa77ba1 100644 --- a/tests/LocalDateRangeTest.php +++ b/tests/LocalDateRangeTest.php @@ -189,20 +189,66 @@ public function providerCount(): array ]; } - public function testJsonSerialize(): void - { - self::assertSame(json_encode('2008-12-31/2011-01-01'), json_encode(LocalDateRange::of( - LocalDate::of(2008, 12, 31), - LocalDate::of(2011, 1, 1) - ))); + /** @dataProvider providerToString */ + public function testJsonSerialize( + int $yearStart, + int $monthStart, + int $dayStart, + int $yearEnd, + int $monthEnd, + int $dayEnd, + string $expectedString + ): void { + $dateRange = LocalDateRange::of( + LocalDate::of($yearStart, $monthStart, $dayStart), + LocalDate::of($yearEnd, $monthEnd, $dayEnd) + ); + + self::assertSame(json_encode($expectedString), json_encode($dateRange)); + } + + /** @dataProvider providerToString */ + public function testToISOString( + int $yearStart, + int $monthStart, + int $dayStart, + int $yearEnd, + int $monthEnd, + int $dayEnd, + string $expectedString + ): void { + $dateRange = LocalDateRange::of( + LocalDate::of($yearStart, $monthStart, $dayStart), + LocalDate::of($yearEnd, $monthEnd, $dayEnd) + ); + + self::assertSame($expectedString, $dateRange->toISOString()); } - public function testToString(): void + /** @dataProvider providerToString */ + public function testToString( + int $yearStart, + int $monthStart, + int $dayStart, + int $yearEnd, + int $monthEnd, + int $dayEnd, + string $expectedString + ): void { + $dateRange = LocalDateRange::of( + LocalDate::of($yearStart, $monthStart, $dayStart), + LocalDate::of($yearEnd, $monthEnd, $dayEnd) + ); + + self::assertSame($expectedString, (string) $dateRange); + } + + public function providerToString(): array { - self::assertSame('2008-12-31/2011-01-01', (string) LocalDateRange::of( - LocalDate::of(2008, 12, 31), - LocalDate::of(2011, 1, 1) - )); + return [ + [2008, 12, 31, 2008, 12, 31, '2008-12-31/2008-12-31'], + [2008, 12, 31, 2011, 1, 1, '2008-12-31/2011-01-01'], + ]; } /** diff --git a/tests/LocalDateTest.php b/tests/LocalDateTest.php index f46db60..190c9fc 100644 --- a/tests/LocalDateTest.php +++ b/tests/LocalDateTest.php @@ -1373,6 +1373,19 @@ public function testJsonSerialize(int $year, int $month, int $day, string $expec self::assertSame(json_encode($expected), json_encode(LocalDate::of($year, $month, $day))); } + /** + * @dataProvider providerToString + * + * @param int $year The year. + * @param int $month The month. + * @param int $day The day-of-month. + * @param string $expected The expected result string. + */ + public function testToISOString(int $year, int $month, int $day, string $expected): void + { + self::assertSame($expected, LocalDate::of($year, $month, $day)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/LocalDateTimeTest.php b/tests/LocalDateTimeTest.php index 3cd94d6..b176f8a 100644 --- a/tests/LocalDateTimeTest.php +++ b/tests/LocalDateTimeTest.php @@ -1220,6 +1220,23 @@ public function testJsonSerialize(int $year, int $month, int $day, int $hour, in self::assertSame(json_encode($expected), json_encode(LocalDateTime::of($year, $month, $day, $hour, $minute, $second, $nano))); } + /** + * @dataProvider providerToString + * + * @param int $year The year. + * @param int $month The month. + * @param int $day The day-of-month. + * @param int $hour The hour-of-day. + * @param int $minute The minute-of-hour. + * @param int $second The second-of-minute. + * @param int $nano The nano-of-second. + * @param string $expected The expected result string. + */ + public function testToISOString(int $year, int $month, int $day, int $hour, int $minute, int $second, int $nano, string $expected): void + { + self::assertSame($expected, LocalDateTime::of($year, $month, $day, $hour, $minute, $second, $nano)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/LocalTimeTest.php b/tests/LocalTimeTest.php index dd28983..c0ad9e7 100644 --- a/tests/LocalTimeTest.php +++ b/tests/LocalTimeTest.php @@ -1045,6 +1045,20 @@ public function testJsonSerialize(int $h, int $m, int $s, int $n, string $r): vo self::assertSame(json_encode($r), json_encode(LocalTime::of($h, $m, $s, $n))); } + /** + * @dataProvider providerToString + * + * @param int $h The hour. + * @param int $m The minute. + * @param int $s The second. + * @param int $n The nanosecond. + * @param string $r The expected result. + */ + public function testToISOString(int $h, int $m, int $s, int $n, string $r): void + { + self::assertSame($r, LocalTime::of($h, $m, $s, $n)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/MonthDayTest.php b/tests/MonthDayTest.php index d54fc14..54b4324 100644 --- a/tests/MonthDayTest.php +++ b/tests/MonthDayTest.php @@ -476,6 +476,18 @@ public function testJsonSerialize(int $month, int $day, string $string): void self::assertSame(json_encode($string), json_encode(MonthDay::of($month, $day))); } + /** + * @dataProvider providerToString + * + * @param int $month The month of the month-day to test. + * @param int $day The day of the month-day to test. + * @param string $string The expected result string. + */ + public function testToISOString(int $month, int $day, string $string): void + { + self::assertSame($string, MonthDay::of($month, $day)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/PeriodTest.php b/tests/PeriodTest.php index b43b1c1..1c6a1a8 100644 --- a/tests/PeriodTest.php +++ b/tests/PeriodTest.php @@ -409,6 +409,19 @@ public function testJsonSerialize(int $years, int $months, int $days, string $ex self::assertSame(json_encode($expected), json_encode(Period::of($years, $months, $days))); } + /** + * @dataProvider providerToString + * + * @param int $years The number of years in the period. + * @param int $months The number of months in the period. + * @param int $days The number of days in the period. + * @param string $expected The expected string output. + */ + public function testToISOString(int $years, int $months, int $days, string $expected): void + { + self::assertSame($expected, Period::of($years, $months, $days)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/YearMonthRangeTest.php b/tests/YearMonthRangeTest.php index f451346..309252a 100644 --- a/tests/YearMonthRangeTest.php +++ b/tests/YearMonthRangeTest.php @@ -197,19 +197,44 @@ public function providerToLocalDateRange(): array ]; } - public function testJsonSerialize(): void + /** @dataProvider providerToString */ + public function testJsonSerialize(int $yearStart, int $monthStart, int $yearEnd, int $monthEnd, string $expectedString): void { - self::assertSame(json_encode('2008-12/2011-01'), json_encode(YearMonthRange::of( - YearMonth::of(2008, 12), - YearMonth::of(2011, 1) - ))); + $yearMonthRange = YearMonthRange::of( + YearMonth::of($yearStart, $monthStart), + YearMonth::of($yearEnd, $monthEnd) + ); + + self::assertSame(json_encode($expectedString), json_encode($yearMonthRange)); } - public function testToString(): void + /** @dataProvider providerToString */ + public function testToISOString(int $yearStart, int $monthStart, int $yearEnd, int $monthEnd, string $expectedString): void { - self::assertSame('2008-12/2011-01', (string) YearMonthRange::of( - YearMonth::of(2008, 12), - YearMonth::of(2011, 1) - )); + $yearMonthRange = YearMonthRange::of( + YearMonth::of($yearStart, $monthStart), + YearMonth::of($yearEnd, $monthEnd) + ); + + self::assertSame($expectedString, $yearMonthRange->toISOString()); + } + + /** @dataProvider providerToString */ + public function testToString(int $yearStart, int $monthStart, int $yearEnd, int $monthEnd, string $expectedString): void + { + $yearMonthRange = YearMonthRange::of( + YearMonth::of($yearStart, $monthStart), + YearMonth::of($yearEnd, $monthEnd) + ); + + self::assertSame($expectedString, (string) $yearMonthRange); + } + + public function providerToString(): array + { + return [ + [2008, 12, 2008, 12, '2008-12/2008-12'], + [2008, 12, 2011, 1, '2008-12/2011-01'], + ]; } } diff --git a/tests/YearMonthTest.php b/tests/YearMonthTest.php index d758741..1464211 100644 --- a/tests/YearMonthTest.php +++ b/tests/YearMonthTest.php @@ -459,13 +459,53 @@ public function providerToLocalDateRange(): array ]; } - public function testJsonSerialize(): void + /** + * @dataProvider providerToString + */ + public function testJsonSerialize(int $year, int $week, string $expectedString): void { - self::assertSame(json_encode('2013-09'), json_encode(YearMonth::of(2013, 9))); + self::assertSame(json_encode($expectedString), json_encode(YearMonth::of($year, $week))); } - public function testToString(): void + /** + * @dataProvider providerToString + */ + public function testToISOString(int $year, int $month, string $expectedString): void { - self::assertSame('2013-09', (string) YearMonth::of(2013, 9)); + self::assertSame($expectedString, YearMonth::of($year, $month)->toISOString()); + } + + /** + * @dataProvider providerToString + * + * @param int $year The year. + * @param int $month The month. + * @param string $expected The expected result string. + */ + public function testToString(int $year, int $month, string $expected): void + { + self::assertSame($expected, (string) YearMonth::of($year, $month)); + } + + public function providerToString(): array + { + return [ + [-999999, 12, '-999999-12'], + [-185321, 11, '-185321-11'], + [-18532, 11, '-18532-11'], + [-2023, 11, '-2023-11'], + [-2023, 11, '-2023-11'], + [-2023, 1, '-2023-01'], + [-999, 1, '-0999-01'], + [-2, 1, '-0002-01'], + [2, 1, '0002-01'], + [999, 1, '0999-01'], + [2023, 1, '2023-01'], + [2023, 11, '2023-11'], + [2023, 11, '2023-11'], + [18532, 11, '18532-11'], + [185321, 11, '185321-11'], + [999999, 12, '999999-12'], + ]; } } diff --git a/tests/YearTest.php b/tests/YearTest.php index 3b7a318..59b8f0e 100644 --- a/tests/YearTest.php +++ b/tests/YearTest.php @@ -495,13 +495,30 @@ public function providerToLocalDateRange(): array ]; } - public function testJsonSerialize(): void + /** @dataProvider providerToString */ + public function testJsonSerialize(int $year, string $expectedString): void { - self::assertSame(json_encode('1987'), json_encode(Year::of(1987))); + self::assertSame(json_encode($expectedString), json_encode(Year::of($year))); } - public function testToString(): void + /** @dataProvider providerToString */ + public function testToISOString(int $year, string $expectedString): void { - self::assertSame('1987', (string) Year::of(1987)); + self::assertSame($expectedString, Year::of($year)->toISOString()); + } + + /** @dataProvider providerToString */ + public function testToString(int $year, string $expectedString): void + { + self::assertSame($expectedString, (string) Year::of($year)); + } + + public function providerToString(): array + { + return [ + [-100, '-100'], + [1987, '1987'], + [105781, '105781'], + ]; } } diff --git a/tests/YearWeekTest.php b/tests/YearWeekTest.php index f60e201..18c1aa4 100644 --- a/tests/YearWeekTest.php +++ b/tests/YearWeekTest.php @@ -327,6 +327,15 @@ public function testJsonSerialize(int $year, int $week, string $expected): void self::assertSame(json_encode($expected), json_encode($yearWeek)); } + /** + * @dataProvider providerToString + */ + public function testToISOString(int $year, int $week, string $expected): void + { + $yearWeek = YearWeek::of($year, $week); + self::assertSame($expected, $yearWeek->toISOString()); + } + /** * @dataProvider providerToString */ diff --git a/tests/ZonedDateTimeTest.php b/tests/ZonedDateTimeTest.php index 40fa510..fdb59bf 100644 --- a/tests/ZonedDateTimeTest.php +++ b/tests/ZonedDateTimeTest.php @@ -814,24 +814,39 @@ public function providerToNativeDateTime(): array ]; } - public function testJsonSerialize(): void + /** @dataProvider providerToString */ + public function testJsonSerialize(string $localDateTime, string $timeZone, string $expectedString): void { - $timeZone = TimeZone::parse('America/Los_Angeles'); - $localDateTime = '2000-01-20T12:34:56.123456789'; $localDateTime = LocalDateTime::parse($localDateTime); - $zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone); + $zonedDateTime = ZonedDateTime::of($localDateTime, TimeZone::parse($timeZone)); - self::assertSame(json_encode('2000-01-20T12:34:56.123456789-08:00[America/Los_Angeles]'), json_encode($zonedDateTime)); + self::assertSame(json_encode($expectedString), json_encode($zonedDateTime)); } - public function testToString(): void + /** @dataProvider providerToString */ + public function testToISOString(string $localDateTime, string $timeZone, string $expectedString): void { - $timeZone = TimeZone::parse('America/Los_Angeles'); - $localDateTime = '2000-01-20T12:34:56.123456789'; $localDateTime = LocalDateTime::parse($localDateTime); - $zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone); + $zonedDateTime = ZonedDateTime::of($localDateTime, TimeZone::parse($timeZone)); + + self::assertSame($expectedString, $zonedDateTime->toISOString()); + } - self::assertSame('2000-01-20T12:34:56.123456789-08:00[America/Los_Angeles]', (string) $zonedDateTime); + /** @dataProvider providerToString */ + public function testToString(string $localDateTime, string $timeZone, string $expectedString): void + { + $localDateTime = LocalDateTime::parse($localDateTime); + $zonedDateTime = ZonedDateTime::of($localDateTime, TimeZone::parse($timeZone)); + + self::assertSame($expectedString, (string) $zonedDateTime); + } + + public function providerToString(): array + { + return [ + ['2000-01-20T12:34:56.123456789', 'America/Los_Angeles', '2000-01-20T12:34:56.123456789-08:00[America/Los_Angeles]'], + ['2000-01-20T12:34:56.123456789', '-07:00', '2000-01-20T12:34:56.123456789-07:00'], + ]; } /**