From 4c5cf0ba035b7faff7e6970f9cb3f0e2d1b3bc3c Mon Sep 17 00:00:00 2001 From: Gregor Harlan Date: Fri, 9 Feb 2024 22:44:52 +0100 Subject: [PATCH] use phpunit v10 --- .gitignore | 1 - .php-cs-fixer.dist.php | 2 +- composer.json | 6 +- phpunit.xml.dist | 20 ++- tests/Base/IntlFormatterTest.php | 8 +- tests/DateTest.php | 171 +++++++++++----------- tests/DateTimeTest.php | 243 ++++++++++++++++--------------- tests/DurationTest.php | 96 ++++++------ tests/MonthTest.php | 17 ++- tests/TimeTest.php | 97 ++++++------ tests/TimeZoneTest.php | 28 ++-- tests/WeekdayTest.php | 19 ++- 12 files changed, 370 insertions(+), 338 deletions(-) diff --git a/.gitignore b/.gitignore index 519f5d3..4c6a9bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /.build/ /vendor/ /.php-cs-fixer.cache -/.phpunit.result.cache /composer.lock diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 2123878..aad21c1 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -12,7 +12,7 @@ '@Symfony:risky' => true, '@PHP81Migration' => true, '@PHP80Migration:risky' => true, - '@PHPUnit84Migration:risky' => true, + '@PHPUnit100Migration:risky' => true, 'array_indentation' => true, 'declare_strict_types' => true, diff --git a/composer.json b/composer.json index 039e79e..23d25bc 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require-dev": { "ext-intl": "*", "friendsofphp/php-cs-fixer": "^3.49", - "phpunit/phpunit": "^9.6", + "phpunit/phpunit": "^10.5", "vimeo/psalm": "^5.21" }, @@ -45,8 +45,8 @@ "scripts": { "cs": "php-cs-fixer fix -v", "phpunit": "phpunit", - "phpunit-coverage": "phpdbg -qrr vendor/bin/phpunit --coverage-text", - "phpunit-coverage-html": "phpdbg -qrr vendor/bin/phpunit --coverage-html .build/coverage-html", + "phpunit-coverage": "phpunit --coverage-text", + "phpunit-coverage-html": "phpunit --coverage-html .build/coverage-html", "psalm": "psalm", "check": [ diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7ca3489..0e30a66 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,11 +1,21 @@ - @@ -16,12 +26,12 @@ tests/ - + src/ src/Exception/ - + diff --git a/tests/Base/IntlFormatterTest.php b/tests/Base/IntlFormatterTest.php index d74c1fc..f17bb96 100644 --- a/tests/Base/IntlFormatterTest.php +++ b/tests/Base/IntlFormatterTest.php @@ -5,12 +5,14 @@ namespace HillValley\Fluxcap\Tests\Base; use HillValley\Fluxcap\Base\IntlFormatter; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** * @internal - * @covers \HillValley\Fluxcap\Base\IntlFormatter */ +#[CoversClass(IntlFormatter::class)] final class IntlFormatterTest extends TestCase { protected function tearDown(): void @@ -18,7 +20,7 @@ protected function tearDown(): void \Locale::setDefault('de-DE'); } - /** @dataProvider dataFormatDateTime */ + #[DataProvider('dataFormatDateTime')] public function testFormatDateTime(string $expected, string $dateTime, string $locale, int $dateFormat, int $timeFormat = \IntlDateFormatter::NONE): void { \Locale::setDefault($locale); @@ -27,7 +29,7 @@ public function testFormatDateTime(string $expected, string $dateTime, string $l self::assertSame($expected, IntlFormatter::formatDateTime($dateTime, $dateFormat, $timeFormat)); } - public function dataFormatDateTime(): iterable + public static function dataFormatDateTime(): iterable { return [ ['03/09/2020', '2020-09-03', 'en-GB', \IntlDateFormatter::SHORT], diff --git a/tests/DateTest.php b/tests/DateTest.php index 6f2d67a..b9357cd 100644 --- a/tests/DateTest.php +++ b/tests/DateTest.php @@ -4,6 +4,7 @@ namespace HillValley\Fluxcap\Tests; +use HillValley\Fluxcap\Base\IntlFormatter; use HillValley\Fluxcap\Date; use HillValley\Fluxcap\DateTime; use HillValley\Fluxcap\Duration; @@ -13,13 +14,15 @@ use HillValley\Fluxcap\Month; use HillValley\Fluxcap\TimeZone; use HillValley\Fluxcap\Weekday; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** * @internal - * @covers \HillValley\Fluxcap\Base\IntlFormatter - * @covers \HillValley\Fluxcap\Date */ +#[CoversClass(IntlFormatter::class)] +#[CoversClass(Date::class)] final class DateTest extends TestCase { public function testConstruct(): void @@ -29,7 +32,7 @@ public function testConstruct(): void self::assertTrue($method->isPrivate()); } - /** @dataProvider dataTimeZones */ + #[DataProvider('dataTimeZones')] public function testToday(string $expectedTimezone, ...$parameters): void { $expected = new \DateTimeImmutable('today', new \DateTimeZone($expectedTimezone)); @@ -37,7 +40,7 @@ public function testToday(string $expectedTimezone, ...$parameters): void self::assertDate($expected->format('Y-m-d'), Date::today(...$parameters)); } - /** @dataProvider dataTimeZones */ + #[DataProvider('dataTimeZones')] public function testYesterday(string $expectedTimezone, ...$parameters): void { $expected = new \DateTimeImmutable('yesterday', new \DateTimeZone($expectedTimezone)); @@ -45,7 +48,7 @@ public function testYesterday(string $expectedTimezone, ...$parameters): void self::assertDate($expected->format('Y-m-d'), Date::yesterday(...$parameters)); } - /** @dataProvider dataTimeZones */ + #[DataProvider('dataTimeZones')] public function testTomorrow(string $expectedTimezone, ...$parameters): void { $expected = new \DateTimeImmutable('tomorrow', new \DateTimeZone($expectedTimezone)); @@ -53,7 +56,7 @@ public function testTomorrow(string $expectedTimezone, ...$parameters): void self::assertDate($expected->format('Y-m-d'), Date::tomorrow(...$parameters)); } - public function dataTimeZones(): iterable + public static function dataTimeZones(): iterable { return [ ['Europe/Berlin'], @@ -63,13 +66,13 @@ public function dataTimeZones(): iterable ]; } - /** @dataProvider dataFromString */ + #[DataProvider('dataFromString')] public function testFromString(string $expected, string $date): void { self::assertDate($expected, Date::fromString($date)); } - public function dataFromString(): iterable + public static function dataFromString(): iterable { return [ [date('Y-m-d'), 'now'], @@ -81,7 +84,7 @@ public function dataFromString(): iterable ]; } - /** @dataProvider dataFromStringInvalid */ + #[DataProvider('dataFromStringInvalid')] public function testFromStringInvalid(string $expected, string $date): void { $this->expectException(InvalidStringException::class); @@ -90,7 +93,7 @@ public function testFromStringInvalid(string $expected, string $date): void Date::fromString($date); } - public function dataFromStringInvalid(): iterable + public static function dataFromStringInvalid(): iterable { return [ ['The date string can not be empty (use "today" for current date).', ''], @@ -116,7 +119,7 @@ public function testFromParts(): void self::assertDate('2018-06-17', Date::fromParts(2018, 6, 17)); } - /** @dataProvider dataFromPartsInvalid */ + #[DataProvider('dataFromPartsInvalid')] public function testFromPartsInvalid(string $expected, int $year, int $month, int $day): void { $this->expectException(InvalidPartException::class); @@ -125,7 +128,7 @@ public function testFromPartsInvalid(string $expected, int $year, int $month, in Date::fromParts($year, $month, $day); } - public function dataFromPartsInvalid(): iterable + public static function dataFromPartsInvalid(): iterable { return [ ['Month part must be between 1 and 12, but -1 given.', 2019, -1, 1], @@ -143,13 +146,13 @@ public function testFromTimestamp(): void self::assertDate('2018-06-17', Date::fromTimestamp($timestamp)); } - /** @dataProvider dataFromNative */ + #[DataProvider('dataFromNative')] public function testFromNative(string $expected, \DateTimeInterface $date): void { self::assertDate($expected, Date::fromNative($date)); } - public function dataFromNative(): iterable + public static function dataFromNative(): iterable { return [ ['2018-09-13', new \DateTime('2018-09-13 23:00:00')], @@ -159,13 +162,13 @@ public function dataFromNative(): iterable ]; } - /** @dataProvider dataCast */ + #[DataProvider('dataCast')] public function testCast(string $expected, $date): void { self::assertDate($expected, Date::cast($date)); } - public function dataCast(): iterable + public static function dataCast(): iterable { return [ ['2018-06-17', strtotime('2018-06-17 23:35:00')], @@ -194,13 +197,13 @@ public function testFormatIntl(): void self::assertSame('Samstag, 1.12.2018 0:00:00.0000 UTC', $date->formatIntl(null, 'EEEE, d.MM.yyyy H:mm:ss.SSSS VV')); } - /** @dataProvider dataIsPast */ + #[DataProvider('dataIsPast')] public function testIsPast(bool $expected, Date $date): void { self::assertSame($expected, $date->isPast()); } - public function dataIsPast(): iterable + public static function dataIsPast(): iterable { return [ [false, Date::today()], @@ -210,13 +213,13 @@ public function dataIsPast(): iterable ]; } - /** @dataProvider dataIsFuture */ + #[DataProvider('dataIsFuture')] public function testIsFuture(bool $expected, Date $date): void { self::assertSame($expected, $date->isFuture()); } - public function dataIsFuture(): iterable + public static function dataIsFuture(): iterable { return [ [false, Date::today()], @@ -226,13 +229,13 @@ public function dataIsFuture(): iterable ]; } - /** @dataProvider dataIsToday */ + #[DataProvider('dataIsToday')] public function testIsToday(bool $expected, Date $date): void { self::assertSame($expected, $date->isToday()); } - public function dataIsToday(): iterable + public static function dataIsToday(): iterable { return [ [false, Date::yesterday()], @@ -243,7 +246,7 @@ public function dataIsToday(): iterable ]; } - /** @dataProvider dataTimeZones */ + #[DataProvider('dataTimeZones')] public function testToDateTime(string $expectedTimezone, ...$parameters): void { $dateTime = Date::fromString('2018-12-01')->toDateTime(...$parameters); @@ -316,7 +319,7 @@ public function testGetter(): void self::assertSame(3, $date->getWeekday()); } - /** @dataProvider dataGetQuarter */ + #[DataProvider('dataGetQuarter')] public function testGetQuarter(int $expected, string $date): void { $date = Date::fromString($date); @@ -324,7 +327,7 @@ public function testGetQuarter(int $expected, string $date): void self::assertSame($expected, $date->getQuarter()); } - public function dataGetQuarter(): iterable + public static function dataGetQuarter(): iterable { return [ [1, '2020-01-01'], @@ -338,13 +341,13 @@ public function dataGetQuarter(): iterable ]; } - /** @dataProvider dataIsFirstDayOfYear */ + #[DataProvider('dataIsFirstDayOfYear')] public function testIsFirstDayOfYear(bool $expected, string $date): void { self::assertSame($expected, Date::fromString($date)->isFirstDayOfYear()); } - public function dataIsFirstDayOfYear(): iterable + public static function dataIsFirstDayOfYear(): iterable { return [ [true, '2020-01-01'], @@ -355,13 +358,13 @@ public function dataIsFirstDayOfYear(): iterable ]; } - /** @dataProvider dataIsLastDayOfYear */ + #[DataProvider('dataIsLastDayOfYear')] public function testIsLastDayOfYear(bool $expected, string $date): void { self::assertSame($expected, Date::fromString($date)->isLastDayOfYear()); } - public function dataIsLastDayOfYear(): iterable + public static function dataIsLastDayOfYear(): iterable { return [ [true, '2020-12-31'], @@ -372,13 +375,13 @@ public function dataIsLastDayOfYear(): iterable ]; } - /** @dataProvider dataIsFirstDayOfMonth */ + #[DataProvider('dataIsFirstDayOfMonth')] public function testIsFirstDayOfMonth(bool $expected, string $date): void { self::assertSame($expected, Date::fromString($date)->isFirstDayOfMonth()); } - public function dataIsFirstDayOfMonth(): iterable + public static function dataIsFirstDayOfMonth(): iterable { return [ [true, '2020-01-01'], @@ -388,13 +391,13 @@ public function dataIsFirstDayOfMonth(): iterable ]; } - /** @dataProvider dataIsLastDayOfMonth */ + #[DataProvider('dataIsLastDayOfMonth')] public function testIsLastDayOfMonth(bool $expected, string $date): void { self::assertSame($expected, Date::fromString($date)->isLastDayOfMonth()); } - public function dataIsLastDayOfMonth(): iterable + public static function dataIsLastDayOfMonth(): iterable { return [ [true, '2020-01-31'], @@ -404,13 +407,13 @@ public function dataIsLastDayOfMonth(): iterable ]; } - /** @dataProvider dataAddYears */ + #[DataProvider('dataAddYears')] public function testAddYears(string $expected, Date $date, int $years): void { self::assertDate($expected, $date->addYears($years)); } - public function dataAddYears(): iterable + public static function dataAddYears(): iterable { return [ ['2022-11-28', Date::fromString('2020-11-28'), 2], @@ -419,13 +422,13 @@ public function dataAddYears(): iterable ]; } - /** @dataProvider dataSubYears */ + #[DataProvider('dataSubYears')] public function testSubYears(string $expected, Date $date, int $years): void { self::assertDate($expected, $date->subYears($years)); } - public function dataSubYears(): iterable + public static function dataSubYears(): iterable { return [ ['2018-11-28', Date::fromString('2020-11-28'), 2], @@ -434,13 +437,13 @@ public function dataSubYears(): iterable ]; } - /** @dataProvider dataAddMonths */ + #[DataProvider('dataAddMonths')] public function testAddMonths(string $expected, Date $date, int $months): void { self::assertDate($expected, $date->addMonths($months)); } - public function dataAddMonths(): iterable + public static function dataAddMonths(): iterable { return [ ['2021-01-28', Date::fromString('2020-11-28'), 2], @@ -449,13 +452,13 @@ public function dataAddMonths(): iterable ]; } - /** @dataProvider dataSubMonths */ + #[DataProvider('dataSubMonths')] public function testSubMonths(string $expected, Date $date, int $months): void { self::assertDate($expected, $date->subMonths($months)); } - public function dataSubMonths(): iterable + public static function dataSubMonths(): iterable { return [ ['2019-11-28', Date::fromString('2020-01-28'), 2], @@ -464,13 +467,13 @@ public function dataSubMonths(): iterable ]; } - /** @dataProvider dataAddWeeks */ + #[DataProvider('dataAddWeeks')] public function testAddWeeks(string $expected, Date $date, int $weeks): void { self::assertDate($expected, $date->addWeeks($weeks)); } - public function dataAddWeeks(): iterable + public static function dataAddWeeks(): iterable { return [ ['2020-12-12', Date::fromString('2020-11-28'), 2], @@ -479,13 +482,13 @@ public function dataAddWeeks(): iterable ]; } - /** @dataProvider dataSubWeeks */ + #[DataProvider('dataSubWeeks')] public function testSubWeeks(string $expected, Date $date, int $weeks): void { self::assertDate($expected, $date->subWeeks($weeks)); } - public function dataSubWeeks(): iterable + public static function dataSubWeeks(): iterable { return [ ['2020-11-14', Date::fromString('2020-11-28'), 2], @@ -494,13 +497,13 @@ public function dataSubWeeks(): iterable ]; } - /** @dataProvider dataAddDays */ + #[DataProvider('dataAddDays')] public function testAddDays(string $expected, Date $date, int $days): void { self::assertDate($expected, $date->addDays($days)); } - public function dataAddDays(): iterable + public static function dataAddDays(): iterable { return [ ['2020-12-01', Date::fromString('2020-11-29'), 2], @@ -509,13 +512,13 @@ public function dataAddDays(): iterable ]; } - /** @dataProvider dataSubDays */ + #[DataProvider('dataSubDays')] public function testSubDays(string $expected, Date $date, int $days): void { self::assertDate($expected, $date->subDays($days)); } - public function dataSubDays(): iterable + public static function dataSubDays(): iterable { return [ ['2020-11-27', Date::fromString('2020-11-29'), 2], @@ -524,13 +527,13 @@ public function dataSubDays(): iterable ]; } - /** @dataProvider dataToFirstDayOfYear */ + #[DataProvider('dataToFirstDayOfYear')] public function testToFirstDayOfYear(string $expected, Date $date): void { self::assertDate($expected, $date->toFirstDayOfYear()); } - public function dataToFirstDayOfYear(): iterable + public static function dataToFirstDayOfYear(): iterable { return [ ['2019-01-01', Date::fromString('2019-01-01')], @@ -539,13 +542,13 @@ public function dataToFirstDayOfYear(): iterable ]; } - /** @dataProvider dataToLastDayOfYear */ + #[DataProvider('dataToLastDayOfYear')] public function testToLastDayOfYear(string $expected, Date $date): void { self::assertDate($expected, $date->toLastDayOfYear()); } - public function dataToLastDayOfYear(): iterable + public static function dataToLastDayOfYear(): iterable { return [ ['2019-12-31', Date::fromString('2019-01-01')], @@ -554,13 +557,13 @@ public function dataToLastDayOfYear(): iterable ]; } - /** @dataProvider dataToFirstDayOfQuarter */ + #[DataProvider('dataToFirstDayOfQuarter')] public function testToFirstDayOfQuarter(string $expected, Date $date): void { self::assertDate($expected, $date->toFirstDayOfQuarter()); } - public function dataToFirstDayOfQuarter(): iterable + public static function dataToFirstDayOfQuarter(): iterable { return [ ['2019-01-01', Date::fromString('2019-01-01')], @@ -570,13 +573,13 @@ public function dataToFirstDayOfQuarter(): iterable ]; } - /** @dataProvider dataToLastDayOfQuarter */ + #[DataProvider('dataToLastDayOfQuarter')] public function testToLastDayOfQuarter(string $expected, Date $date): void { self::assertDate($expected, $date->toLastDayOfQuarter()); } - public function dataToLastDayOfQuarter(): iterable + public static function dataToLastDayOfQuarter(): iterable { return [ ['2019-03-31', Date::fromString('2019-01-01')], @@ -586,13 +589,13 @@ public function dataToLastDayOfQuarter(): iterable ]; } - /** @dataProvider dataToFirstDayOfMonth */ + #[DataProvider('dataToFirstDayOfMonth')] public function testToFirstDayOfMonth(string $expected, Date $date): void { self::assertDate($expected, $date->toFirstDayOfMonth()); } - public function dataToFirstDayOfMonth(): iterable + public static function dataToFirstDayOfMonth(): iterable { return [ ['2019-01-01', Date::fromString('2019-01-01')], @@ -601,13 +604,13 @@ public function dataToFirstDayOfMonth(): iterable ]; } - /** @dataProvider dataToLastDayOfMonth */ + #[DataProvider('dataToLastDayOfMonth')] public function testToLastDayOfMonth(string $expected, Date $date): void { self::assertDate($expected, $date->toLastDayOfMonth()); } - public function dataToLastDayOfMonth(): iterable + public static function dataToLastDayOfMonth(): iterable { return [ ['2019-01-31', Date::fromString('2019-01-01')], @@ -616,7 +619,7 @@ public function dataToLastDayOfMonth(): iterable ]; } - /** @dataProvider dataToMonth */ + #[DataProvider('dataToMonth')] public function testToMonth(Month $expected, string $date): void { $date = Date::fromString($date); @@ -624,7 +627,7 @@ public function testToMonth(Month $expected, string $date): void self::assertSame($expected, $date->toMonth()); } - public function dataToMonth(): iterable + public static function dataToMonth(): iterable { return [ [Month::March, '2019-03-12'], @@ -633,7 +636,7 @@ public function dataToMonth(): iterable ]; } - /** @dataProvider dataToWeekday */ + #[DataProvider('dataToWeekday')] public function testToWeekday(Weekday $expected, string $date): void { $date = Date::fromString($date); @@ -641,7 +644,7 @@ public function testToWeekday(Weekday $expected, string $date): void self::assertSame($expected, $date->toWeekday()); } - public function dataToWeekday(): iterable + public static function dataToWeekday(): iterable { return [ [Weekday::Monday, '2019-07-15'], @@ -650,7 +653,7 @@ public function dataToWeekday(): iterable ]; } - /** @dataProvider dataToPrevWeekday */ + #[DataProvider('dataToPrevWeekday')] public function testToPrevWeekday(string $expected, string $date, $weekday): void { $date = Date::fromString($date); @@ -658,7 +661,7 @@ public function testToPrevWeekday(string $expected, string $date, $weekday): voi self::assertDate($expected, $date->toPrevWeekday($weekday)); } - public function dataToPrevWeekday(): iterable + public static function dataToPrevWeekday(): iterable { return [ ['2019-07-15', '2019-07-15', Weekday::Monday], @@ -667,7 +670,7 @@ public function dataToPrevWeekday(): iterable ]; } - /** @dataProvider dataToNextWeekday */ + #[DataProvider('dataToNextWeekday')] public function testToNextWeekday(string $expected, string $date, $weekday): void { $date = Date::fromString($date); @@ -675,7 +678,7 @@ public function testToNextWeekday(string $expected, string $date, $weekday): voi self::assertDate($expected, $date->toNextWeekday($weekday)); } - public function dataToNextWeekday(): iterable + public static function dataToNextWeekday(): iterable { return [ ['2019-07-14', '2019-07-14', Weekday::Sunday], @@ -686,13 +689,13 @@ public function dataToNextWeekday(): iterable // === CompareTrait === - /** @dataProvider dataMin */ + #[DataProvider('dataMin')] public function testMin(int $expectedIndex, Date ...$dates): void { self::assertSame($dates[$expectedIndex], Date::min(...$dates)); } - public function dataMin(): iterable + public static function dataMin(): iterable { return [ [0, Date::fromString('2020-07-19')], @@ -701,13 +704,13 @@ public function dataMin(): iterable ]; } - /** @dataProvider dataMax */ + #[DataProvider('dataMax')] public function testMax(int $expectedIndex, Date ...$dates): void { self::assertSame($dates[$expectedIndex], Date::max(...$dates)); } - public function dataMax(): iterable + public static function dataMax(): iterable { return [ [0, Date::fromString('2020-07-19')], @@ -716,7 +719,7 @@ public function dataMax(): iterable ]; } - /** @dataProvider dataDiff */ + #[DataProvider('dataDiff')] public function testDiff(string $expected, Date $dateTime, Date $other, ...$parameters): void { $duration = $dateTime->diff($other, ...$parameters); @@ -725,7 +728,7 @@ public function testDiff(string $expected, Date $dateTime, Date $other, ...$para self::assertSame($expected, $duration->toIso()); } - public function dataDiff(): iterable + public static function dataDiff(): iterable { return [ ['PT0S', Date::fromString('2020-07-19'), Date::fromString('2020-07-19')], @@ -736,7 +739,7 @@ public function dataDiff(): iterable ]; } - /** @dataProvider dataCompareTo */ + #[DataProvider('dataCompareTo')] public function testCompareTo(int $expected, Date $dateTime, Date $other): void { self::assertSame($expected, $dateTime->compareTo($other)); @@ -747,7 +750,7 @@ public function testCompareTo(int $expected, Date $dateTime, Date $other): void self::assertSame(1 !== $expected, $dateTime->lowerEquals($other)); } - public function dataCompareTo(): iterable + public static function dataCompareTo(): iterable { return [ [0, Date::fromString('2020-07-19'), Date::fromString('2020-07-19')], @@ -767,7 +770,7 @@ public function testJsonSerialize(): void // === ModifyTrait === - /** @dataProvider dataModify */ + #[DataProvider('dataModify')] public function testModify(string $expected, string $modify): void { $date = Date::fromString('2020-07-26'); @@ -775,7 +778,7 @@ public function testModify(string $expected, string $modify): void self::assertDate($expected, $date->modify($modify)); } - public function dataModify(): iterable + public static function dataModify(): iterable { return [ ['2021-06-26', '+1year -1month'], @@ -787,12 +790,12 @@ public function dataModify(): iterable public function testModifyInvalid(): void { $this->expectException(InvalidStringException::class); - $this->expectExceptionMessage('Failed to parse time string (foo) at position 0 (f): The timezone could not be found in the database.'); + $this->expectExceptionMessageMatches('/^Failed to parse time string \(foo\)/'); Date::fromString('2024-02-09')->modify('foo'); } - /** @dataProvider dataAdd */ + #[DataProvider('dataAdd')] public function testAdd(string $expected, string $duration): void { $date = Date::fromString('2020-07-26'); @@ -800,7 +803,7 @@ public function testAdd(string $expected, string $duration): void self::assertDate($expected, $date->add(Duration::fromString($duration))); } - public function dataAdd(): iterable + public static function dataAdd(): iterable { return [ ['2020-08-01', 'P1WT-1H'], @@ -808,7 +811,7 @@ public function dataAdd(): iterable ]; } - /** @dataProvider dataSub */ + #[DataProvider('dataSub')] public function testSub(string $expected, string $duration): void { $date = Date::fromString('2020-07-26'); @@ -816,7 +819,7 @@ public function testSub(string $expected, string $duration): void self::assertDate($expected, $date->sub(Duration::fromString($duration))); } - public function dataSub(): iterable + public static function dataSub(): iterable { return [ ['2020-07-19', 'P1WT-2H'], diff --git a/tests/DateTimeTest.php b/tests/DateTimeTest.php index bc4efd3..4761f80 100644 --- a/tests/DateTimeTest.php +++ b/tests/DateTimeTest.php @@ -4,6 +4,7 @@ namespace HillValley\Fluxcap\Tests; +use HillValley\Fluxcap\Base\IntlFormatter; use HillValley\Fluxcap\Date; use HillValley\Fluxcap\DateTime; use HillValley\Fluxcap\Duration; @@ -14,13 +15,15 @@ use HillValley\Fluxcap\Time; use HillValley\Fluxcap\TimeZone; use HillValley\Fluxcap\Weekday; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** * @internal - * @covers \HillValley\Fluxcap\Base\IntlFormatter - * @covers \HillValley\Fluxcap\DateTime */ +#[CoversClass(IntlFormatter::class)] +#[CoversClass(DateTime::class)] final class DateTimeTest extends TestCase { public function testConstruct(): void @@ -30,7 +33,7 @@ public function testConstruct(): void self::assertTrue($method->isPrivate()); } - /** @dataProvider dataTimeZones */ + #[DataProvider('dataTimeZones')] public function testNow(string $expectedTimezone, ...$parameters): void { self::assertDateTimeNow(DateTime::now(...$parameters), $expectedTimezone); @@ -41,7 +44,7 @@ public function testUtcNow(): void self::assertDateTimeNow(DateTime::utcNow(), 'UTC'); } - /** @dataProvider dataTimeZones */ + #[DataProvider('dataTimeZones')] public function testToday(string $expectedTimezone, ...$parameters): void { $expected = new \DateTimeImmutable('today', new \DateTimeZone($expectedTimezone)); @@ -50,7 +53,7 @@ public function testToday(string $expectedTimezone, ...$parameters): void self::assertDateTime($expected, DateTime::today(...$parameters)); } - /** @dataProvider dataTimeZones */ + #[DataProvider('dataTimeZones')] public function testYesterday(string $expectedTimezone, ...$parameters): void { $expected = new \DateTimeImmutable('yesterday', new \DateTimeZone($expectedTimezone)); @@ -59,7 +62,7 @@ public function testYesterday(string $expectedTimezone, ...$parameters): void self::assertDateTime($expected, DateTime::yesterday(...$parameters)); } - /** @dataProvider dataTimeZones */ + #[DataProvider('dataTimeZones')] public function testTomorrow(string $expectedTimezone, ...$parameters): void { $expected = new \DateTimeImmutable('tomorrow', new \DateTimeZone($expectedTimezone)); @@ -68,7 +71,7 @@ public function testTomorrow(string $expectedTimezone, ...$parameters): void self::assertDateTime($expected, DateTime::tomorrow(...$parameters)); } - public function dataTimeZones(): iterable + public static function dataTimeZones(): iterable { return [ ['Europe/Berlin'], @@ -78,19 +81,19 @@ public function dataTimeZones(): iterable ]; } - /** @dataProvider dataTimeZones */ + #[DataProvider('dataTimeZones')] public function testFromStringNow(string $expectedTimezone, ...$parameters): void { self::assertDateTimeNow(DateTime::fromString('now', ...$parameters), $expectedTimezone); } - /** @dataProvider dataFromString */ + #[DataProvider('dataFromString')] public function testFromString(string $expected, string $dateTime, ?TimeZone $timeZone = null): void { self::assertDateTime($expected, DateTime::fromString($dateTime, $timeZone)); } - public function dataFromString(): iterable + public static function dataFromString(): iterable { return [ ['2018-12-08 00:00:00.000000 Europe/Berlin', '2018-12-08'], @@ -106,7 +109,7 @@ public function dataFromString(): iterable ]; } - /** @dataProvider dataFromStringInvalid */ + #[DataProvider('dataFromStringInvalid')] public function testFromStringInvalid(string $expected, string $dateTime): void { $this->expectException(InvalidStringException::class); @@ -115,7 +118,7 @@ public function testFromStringInvalid(string $expected, string $dateTime): void DateTime::fromString($dateTime); } - public function dataFromStringInvalid(): iterable + public static function dataFromStringInvalid(): iterable { return [ ['The date-time string can not be empty (use "now" for current time).', ''], @@ -129,13 +132,13 @@ public function testUtcFromStringNow(): void self::assertDateTimeNow(DateTime::utcFromString('now'), 'UTC'); } - /** @dataProvider dataUtcFromString */ + #[DataProvider('dataUtcFromString')] public function testUtcFromString(string $expected, string $dateTime): void { self::assertDateTime($expected, DateTime::utcFromString($dateTime)); } - public function dataUtcFromString(): iterable + public static function dataUtcFromString(): iterable { return [ ['2018-12-08 00:00:00.000000 UTC', '2018-12-08'], @@ -149,19 +152,19 @@ public function dataUtcFromString(): iterable ]; } - /** @dataProvider dataTimeZones */ + #[DataProvider('dataTimeZones')] public function testFromUtcStringNow(string $expectedTimezone, ...$parameters): void { self::assertDateTimeNow(DateTime::fromUtcString('now', ...$parameters), $expectedTimezone); } - /** @dataProvider dataFromUtcString */ + #[DataProvider('dataFromUtcString')] public function testFromUtcString(string $expected, string $dateTime, ?TimeZone $timeZone = null): void { self::assertDateTime($expected, DateTime::fromUtcString($dateTime, $timeZone)); } - public function dataFromUtcString(): iterable + public static function dataFromUtcString(): iterable { return [ ['2018-12-08 01:00:00.000000 Europe/Berlin', '2018-12-08'], @@ -177,13 +180,13 @@ public function dataFromUtcString(): iterable ]; } - /** @dataProvider dataFromFormat */ + #[DataProvider('dataFromFormat')] public function testFromFormat(string $expected, string $format, string $dateTime, ?TimeZone $timeZone = null): void { self::assertDateTime($expected, DateTime::fromFormat($format, $dateTime, $timeZone)); } - public function dataFromFormat(): iterable + public static function dataFromFormat(): iterable { return [ ['2018-12-08 15:32:00.000000 Europe/Berlin', 'j.n.Y, i.H', '8.12.2018, 32.15'], @@ -201,13 +204,13 @@ public function testFromFormatInvalid(): void DateTime::fromFormat('d.m.Y, H:i:s', '2018-10-03 20:10:00'); } - /** @dataProvider dataFromParts */ + #[DataProvider('dataFromParts')] public function testFromParts(string $expected, ...$parts): void { self::assertDateTime($expected, DateTime::fromParts(...$parts)); } - public function dataFromParts(): iterable + public static function dataFromParts(): iterable { return [ ['2018-05-04 00:00:00.000000 Europe/Berlin', 2018, 5, 4], @@ -220,7 +223,7 @@ public function dataFromParts(): iterable ]; } - /** @dataProvider dataFromPartsInvalid */ + #[DataProvider('dataFromPartsInvalid')] public function testFromPartsInvalid(string $expected, ...$parts): void { $this->expectException(InvalidPartException::class); @@ -229,7 +232,7 @@ public function testFromPartsInvalid(string $expected, ...$parts): void DateTime::fromParts(...$parts); } - public function dataFromPartsInvalid(): iterable + public static function dataFromPartsInvalid(): iterable { return [ ['Month part must be between 1 and 12, but -1 given.', 2019, -1, 1], @@ -249,13 +252,13 @@ public function dataFromPartsInvalid(): iterable ]; } - /** @dataProvider dataFromTimestamp */ + #[DataProvider('dataFromTimestamp')] public function testFromTimestamp(string $expected, int $timestamp, ?TimeZone $timeZone = null): void { self::assertDateTime($expected, DateTime::fromTimestamp($timestamp, $timeZone)); } - public function dataFromTimestamp(): iterable + public static function dataFromTimestamp(): iterable { return [ ['2018-12-01 01:05:20.000000 Europe/Berlin', strtotime('2018-12-01 01:05:20')], @@ -263,13 +266,13 @@ public function dataFromTimestamp(): iterable ]; } - /** @dataProvider dataFromNative */ + #[DataProvider('dataFromNative')] public function testFromNative(string $expected, \DateTimeInterface $dateTime): void { self::assertDateTime($expected, DateTime::fromNative($dateTime)); } - public function dataFromNative(): iterable + public static function dataFromNative(): iterable { return [ ['2018-09-13 23:00:00.000000 Europe/Berlin', new \DateTime('2018-09-13 23:00:00')], @@ -279,13 +282,13 @@ public function dataFromNative(): iterable ]; } - /** @dataProvider dataCombine */ + #[DataProvider('dataCombine')] public function testCombine(string $expected, Date $date, Time $time, ?TimeZone $timeZone = null): void { self::assertDateTime($expected, DateTime::combine($date, $time, $timeZone)); } - public function dataCombine(): iterable + public static function dataCombine(): iterable { return [ ['2018-10-12 04:30:15.123456 Europe/Berlin', Date::fromString('2018-10-12'), Time::fromString('04:30:15.123456')], @@ -293,13 +296,13 @@ public function dataCombine(): iterable ]; } - /** @dataProvider dataCast */ + #[DataProvider('dataCast')] public function testCast(string $expected, $dateTime): void { self::assertDateTime($expected, DateTime::cast($dateTime)); } - public function dataCast(): iterable + public static function dataCast(): iterable { return [ ['2018-06-17 23:35:00.000000 Europe/Berlin', strtotime('2018-06-17 23:35:00')], @@ -329,14 +332,14 @@ public function testGetOffset(): void self::assertSame(-25200, $dateTime->getOffset()); } - /** @dataProvider dataToIso */ + #[DataProvider('dataToIso')] public function testToIso(string $expected, DateTime $dateTime): void { self::assertSame($expected, $dateTime->toIso()); self::assertSame($expected, (string) $dateTime); } - public function dataToIso(): iterable + public static function dataToIso(): iterable { return [ ['2018-12-08T12:45:00.000000Z', DateTime::utcFromString('2018-12-08 12:45')], @@ -347,7 +350,7 @@ public function dataToIso(): iterable ]; } - /** @dataProvider dataFormatIntl */ + #[DataProvider('dataFormatIntl')] public function testFormatIntl(string $expected, ...$parameters): void { $dateTime = DateTime::fromString('2020-07-02 03:20:50.123456'); @@ -355,7 +358,7 @@ public function testFormatIntl(string $expected, ...$parameters): void self::assertSame($expected, $dateTime->formatIntl(...$parameters)); } - public function dataFormatIntl(): iterable + public static function dataFormatIntl(): iterable { return [ ['2. Juli 2020 um 03:20:50'], @@ -380,13 +383,13 @@ public function testFormatIntlTime(): void self::assertSame('03:20', $dateTime->formatIntlTime(\IntlDateFormatter::SHORT)); } - /** @dataProvider dataIsPast */ + #[DataProvider('dataIsPast')] public function testIsPast(bool $expected, DateTime $dateTime): void { self::assertSame($expected, $dateTime->isPast()); } - public function dataIsPast(): iterable + public static function dataIsPast(): iterable { return [ [false, DateTime::now()->addSeconds(2)], @@ -398,13 +401,13 @@ public function dataIsPast(): iterable ]; } - /** @dataProvider dataIsFuture */ + #[DataProvider('dataIsFuture')] public function testIsFuture(bool $expected, DateTime $dateTime): void { self::assertSame($expected, $dateTime->isFuture()); } - public function dataIsFuture(): iterable + public static function dataIsFuture(): iterable { return [ [false, DateTime::now()], @@ -416,13 +419,13 @@ public function dataIsFuture(): iterable ]; } - /** @dataProvider dataIsToday */ + #[DataProvider('dataIsToday')] public function testIsToday(bool $expected, DateTime $dateTime): void { self::assertSame($expected, $dateTime->isToday()); } - public function dataIsToday(): iterable + public static function dataIsToday(): iterable { return [ [false, DateTime::fromString('yesterday 23:59:59.999999')], @@ -468,13 +471,13 @@ public function testToUtc(): void self::assertDateTime('2020-07-02 01:20:50.123456 UTC', $dateTime->toUtc()); } - /** @dataProvider dataToMidnight */ + #[DataProvider('dataToMidnight')] public function testToMidnight(string $expected, DateTime $dateTime): void { self::assertDateTime($expected, $dateTime->toMidnight()); } - public function dataToMidnight(): iterable + public static function dataToMidnight(): iterable { return [ ['2020-11-15 00:00:00.000000 Europe/Berlin', DateTime::fromString('2020-11-15 00:00:00.000000')], @@ -515,7 +518,7 @@ public function testToMutable(): void self::assertSame('2018-12-08 12:45:00.123456 EET', $dateTime->format('Y-m-d H:i:s.u e')); } - /** @dataProvider dataToTimestamp */ + #[DataProvider('dataToTimestamp')] public function testToTimestamp(?TimeZone $timeZone): void { $timestamp = time(); @@ -524,7 +527,7 @@ public function testToTimestamp(?TimeZone $timeZone): void self::assertSame($timestamp, $dateTime->toTimestamp()); } - public function dataToTimestamp(): iterable + public static function dataToTimestamp(): iterable { return [ [null], @@ -579,7 +582,7 @@ public function testGetter(): void self::assertSame(12340, $dateTime->getMicrosecond()); } - /** @dataProvider dataGetQuarter */ + #[DataProvider('dataGetQuarter')] public function testGetQuarter(int $expected, string $dateTime): void { $dateTime = DateTime::fromString($dateTime); @@ -587,7 +590,7 @@ public function testGetQuarter(int $expected, string $dateTime): void self::assertSame($expected, $dateTime->getQuarter()); } - public function dataGetQuarter(): iterable + public static function dataGetQuarter(): iterable { return [ [1, '2020-01-01 00:00:00'], @@ -601,13 +604,13 @@ public function dataGetQuarter(): iterable ]; } - /** @dataProvider dataIsFirstDayOfYear */ + #[DataProvider('dataIsFirstDayOfYear')] public function testIsFirstDayOfYear(bool $expected, string $dateTime): void { self::assertSame($expected, DateTime::fromString($dateTime)->isFirstDayOfYear()); } - public function dataIsFirstDayOfYear(): iterable + public static function dataIsFirstDayOfYear(): iterable { return [ [true, '2020-01-01 00:00:00'], @@ -618,13 +621,13 @@ public function dataIsFirstDayOfYear(): iterable ]; } - /** @dataProvider dataIsLastDayOfYear */ + #[DataProvider('dataIsLastDayOfYear')] public function testIsLastDayOfYear(bool $expected, string $dateTime): void { self::assertSame($expected, DateTime::fromString($dateTime)->isLastDayOfYear()); } - public function dataIsLastDayOfYear(): iterable + public static function dataIsLastDayOfYear(): iterable { return [ [true, '2020-12-31 00:00:00'], @@ -635,13 +638,13 @@ public function dataIsLastDayOfYear(): iterable ]; } - /** @dataProvider dataIsFirstDayOfMonth */ + #[DataProvider('dataIsFirstDayOfMonth')] public function testIsFirstDayOfMonth(bool $expected, string $dateTime): void { self::assertSame($expected, DateTime::fromString($dateTime)->isFirstDayOfMonth()); } - public function dataIsFirstDayOfMonth(): iterable + public static function dataIsFirstDayOfMonth(): iterable { return [ [true, '2020-01-01 00:00:00'], @@ -651,13 +654,13 @@ public function dataIsFirstDayOfMonth(): iterable ]; } - /** @dataProvider dataIsLastDayOfMonth */ + #[DataProvider('dataIsLastDayOfMonth')] public function testIsLastDayOfMonth(bool $expected, string $dateTime): void { self::assertSame($expected, DateTime::fromString($dateTime)->isLastDayOfMonth()); } - public function dataIsLastDayOfMonth(): iterable + public static function dataIsLastDayOfMonth(): iterable { return [ [true, '2020-01-31 00:00:00'], @@ -667,13 +670,13 @@ public function dataIsLastDayOfMonth(): iterable ]; } - /** @dataProvider dataIsMidnight */ + #[DataProvider('dataIsMidnight')] public function testIsMidnight(bool $expected, string $dateTime): void { self::assertSame($expected, DateTime::fromString($dateTime)->isMidnight()); } - public function dataIsMidnight(): iterable + public static function dataIsMidnight(): iterable { return [ [true, '2020-08-10 00:00'], @@ -683,13 +686,13 @@ public function dataIsMidnight(): iterable ]; } - /** @dataProvider dataAddYears */ + #[DataProvider('dataAddYears')] public function testAddYears(string $expected, DateTime $dateTime, int $years): void { self::assertDateTime($expected, $dateTime->addYears($years)); } - public function dataAddYears(): iterable + public static function dataAddYears(): iterable { return [ ['2022-11-28 17:25:00.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:00'), 2], @@ -698,13 +701,13 @@ public function dataAddYears(): iterable ]; } - /** @dataProvider dataSubYears */ + #[DataProvider('dataSubYears')] public function testSubYears(string $expected, DateTime $dateTime, int $years): void { self::assertDateTime($expected, $dateTime->subYears($years)); } - public function dataSubYears(): iterable + public static function dataSubYears(): iterable { return [ ['2018-11-28 17:25:00.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:00'), 2], @@ -713,13 +716,13 @@ public function dataSubYears(): iterable ]; } - /** @dataProvider dataAddMonths */ + #[DataProvider('dataAddMonths')] public function testAddMonths(string $expected, DateTime $dateTime, int $months): void { self::assertDateTime($expected, $dateTime->addMonths($months)); } - public function dataAddMonths(): iterable + public static function dataAddMonths(): iterable { return [ ['2021-01-28 17:25:00.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:00'), 2], @@ -728,13 +731,13 @@ public function dataAddMonths(): iterable ]; } - /** @dataProvider dataSubMonths */ + #[DataProvider('dataSubMonths')] public function testSubMonths(string $expected, DateTime $dateTime, int $months): void { self::assertDateTime($expected, $dateTime->subMonths($months)); } - public function dataSubMonths(): iterable + public static function dataSubMonths(): iterable { return [ ['2019-11-28 17:25:00.000000 Europe/Berlin', DateTime::fromString('2020-01-28 17:25:00'), 2], @@ -743,13 +746,13 @@ public function dataSubMonths(): iterable ]; } - /** @dataProvider dataAddWeeks */ + #[DataProvider('dataAddWeeks')] public function testAddWeeks(string $expected, DateTime $dateTime, int $weeks): void { self::assertDateTime($expected, $dateTime->addWeeks($weeks)); } - public function dataAddWeeks(): iterable + public static function dataAddWeeks(): iterable { return [ ['2020-12-12 17:25:00.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:00'), 2], @@ -758,13 +761,13 @@ public function dataAddWeeks(): iterable ]; } - /** @dataProvider dataSubWeeks */ + #[DataProvider('dataSubWeeks')] public function testSubWeeks(string $expected, DateTime $dateTime, int $weeks): void { self::assertDateTime($expected, $dateTime->subWeeks($weeks)); } - public function dataSubWeeks(): iterable + public static function dataSubWeeks(): iterable { return [ ['2020-11-14 17:25:00.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:00'), 2], @@ -773,13 +776,13 @@ public function dataSubWeeks(): iterable ]; } - /** @dataProvider dataAddDays */ + #[DataProvider('dataAddDays')] public function testAddDays(string $expected, DateTime $dateTime, int $days): void { self::assertDateTime($expected, $dateTime->addDays($days)); } - public function dataAddDays(): iterable + public static function dataAddDays(): iterable { return [ ['2020-12-01 17:25:00.000000 Europe/Berlin', DateTime::fromString('2020-11-29 17:25:00'), 2], @@ -788,13 +791,13 @@ public function dataAddDays(): iterable ]; } - /** @dataProvider dataSubDays */ + #[DataProvider('dataSubDays')] public function testSubDays(string $expected, DateTime $dateTime, int $days): void { self::assertDateTime($expected, $dateTime->subDays($days)); } - public function dataSubDays(): iterable + public static function dataSubDays(): iterable { return [ ['2020-11-27 17:25:00.000000 Europe/Berlin', DateTime::fromString('2020-11-29 17:25:00'), 2], @@ -803,13 +806,13 @@ public function dataSubDays(): iterable ]; } - /** @dataProvider dataAddHours */ + #[DataProvider('dataAddHours')] public function testAddHours(string $expected, DateTime $dateTime, int $hours): void { self::assertDateTime($expected, $dateTime->addHours($hours)); } - public function dataAddHours(): iterable + public static function dataAddHours(): iterable { return [ ['2020-11-28 19:25:00.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:00'), 2], @@ -818,13 +821,13 @@ public function dataAddHours(): iterable ]; } - /** @dataProvider dataSubHours */ + #[DataProvider('dataSubHours')] public function testSubHours(string $expected, DateTime $dateTime, int $hours): void { self::assertDateTime($expected, $dateTime->subHours($hours)); } - public function dataSubHours(): iterable + public static function dataSubHours(): iterable { return [ ['2020-11-28 15:25:00.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:00'), 2], @@ -833,13 +836,13 @@ public function dataSubHours(): iterable ]; } - /** @dataProvider dataAddMinutes */ + #[DataProvider('dataAddMinutes')] public function testAddMinutes(string $expected, DateTime $dateTime, int $minutes): void { self::assertDateTime($expected, $dateTime->addMinutes($minutes)); } - public function dataAddMinutes(): iterable + public static function dataAddMinutes(): iterable { return [ ['2020-11-28 17:27:00.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:00'), 2], @@ -848,13 +851,13 @@ public function dataAddMinutes(): iterable ]; } - /** @dataProvider dataSubMinutes */ + #[DataProvider('dataSubMinutes')] public function testSubMinutes(string $expected, DateTime $dateTime, int $minutes): void { self::assertDateTime($expected, $dateTime->subMinutes($minutes)); } - public function dataSubMinutes(): iterable + public static function dataSubMinutes(): iterable { return [ ['2020-11-28 17:23:00.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:00'), 2], @@ -863,13 +866,13 @@ public function dataSubMinutes(): iterable ]; } - /** @dataProvider dataAddSeconds */ + #[DataProvider('dataAddSeconds')] public function testAddSeconds(string $expected, DateTime $dateTime, int $seconds): void { self::assertDateTime($expected, $dateTime->addSeconds($seconds)); } - public function dataAddSeconds(): iterable + public static function dataAddSeconds(): iterable { return [ ['2020-11-28 17:25:07.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:05'), 2], @@ -878,13 +881,13 @@ public function dataAddSeconds(): iterable ]; } - /** @dataProvider dataSubSeconds */ + #[DataProvider('dataSubSeconds')] public function testSubSeconds(string $expected, DateTime $dateTime, int $seconds): void { self::assertDateTime($expected, $dateTime->subSeconds($seconds)); } - public function dataSubSeconds(): iterable + public static function dataSubSeconds(): iterable { return [ ['2020-11-28 17:25:03.000000 Europe/Berlin', DateTime::fromString('2020-11-28 17:25:05'), 2], @@ -893,13 +896,13 @@ public function dataSubSeconds(): iterable ]; } - /** @dataProvider dataToFirstDayOfYear */ + #[DataProvider('dataToFirstDayOfYear')] public function testToFirstDayOfYear(string $expected, DateTime $dateTime): void { self::assertDateTime($expected, $dateTime->toFirstDayOfYear()); } - public function dataToFirstDayOfYear(): iterable + public static function dataToFirstDayOfYear(): iterable { return [ ['2019-01-01 00:00:00.000000 Europe/Berlin', DateTime::fromString('2019-01-01 00:00:00')], @@ -908,13 +911,13 @@ public function dataToFirstDayOfYear(): iterable ]; } - /** @dataProvider dataToLastDayOfYear */ + #[DataProvider('dataToLastDayOfYear')] public function testToLastDayOfYear(string $expected, DateTime $dateTime): void { self::assertDateTime($expected, $dateTime->toLastDayOfYear()); } - public function dataToLastDayOfYear(): iterable + public static function dataToLastDayOfYear(): iterable { return [ ['2019-12-31 00:00:00.000000 Europe/Berlin', DateTime::fromString('2019-01-01 00:00:00')], @@ -923,13 +926,13 @@ public function dataToLastDayOfYear(): iterable ]; } - /** @dataProvider dataToFirstDayOfQuarter */ + #[DataProvider('dataToFirstDayOfQuarter')] public function testToFirstDayOfQuarter(string $expected, DateTime $dateTime): void { self::assertDateTime($expected, $dateTime->toFirstDayOfQuarter()); } - public function dataToFirstDayOfQuarter(): iterable + public static function dataToFirstDayOfQuarter(): iterable { return [ ['2019-01-01 00:00:00.000000 Europe/Berlin', DateTime::fromString('2019-01-01 00:00:00')], @@ -939,13 +942,13 @@ public function dataToFirstDayOfQuarter(): iterable ]; } - /** @dataProvider dataToLastDayOfQuarter */ + #[DataProvider('dataToLastDayOfQuarter')] public function testToLastDayOfQuarter(string $expected, DateTime $dateTime): void { self::assertDateTime($expected, $dateTime->toLastDayOfQuarter()); } - public function dataToLastDayOfQuarter(): iterable + public static function dataToLastDayOfQuarter(): iterable { return [ ['2019-03-31 00:00:00.000000 Europe/Berlin', DateTime::fromString('2019-01-01 00:00:00')], @@ -955,13 +958,13 @@ public function dataToLastDayOfQuarter(): iterable ]; } - /** @dataProvider dataToFirstDayOfMonth */ + #[DataProvider('dataToFirstDayOfMonth')] public function testToFirstDayOfMonth(string $expected, DateTime $dateTime): void { self::assertDateTime($expected, $dateTime->toFirstDayOfMonth()); } - public function dataToFirstDayOfMonth(): iterable + public static function dataToFirstDayOfMonth(): iterable { return [ ['2019-01-01 00:00:00.000000 Europe/Berlin', DateTime::fromString('2019-01-01 00:00:00')], @@ -970,13 +973,13 @@ public function dataToFirstDayOfMonth(): iterable ]; } - /** @dataProvider dataToLastDayOfMonth */ + #[DataProvider('dataToLastDayOfMonth')] public function testToLastDayOfMonth(string $expected, DateTime $dateTime): void { self::assertDateTime($expected, $dateTime->toLastDayOfMonth()); } - public function dataToLastDayOfMonth(): iterable + public static function dataToLastDayOfMonth(): iterable { return [ ['2019-01-31 00:00:00.000000 Europe/Berlin', DateTime::fromString('2019-01-01 00:00:00')], @@ -985,7 +988,7 @@ public function dataToLastDayOfMonth(): iterable ]; } - /** @dataProvider dataToMonth */ + #[DataProvider('dataToMonth')] public function testToMonth(Month $expected, string $dateTime): void { $dateTime = DateTime::fromString($dateTime); @@ -993,7 +996,7 @@ public function testToMonth(Month $expected, string $dateTime): void self::assertSame($expected, $dateTime->toMonth()); } - public function dataToMonth(): iterable + public static function dataToMonth(): iterable { return [ [Month::March, '2019-03-12 12:05:23'], @@ -1002,7 +1005,7 @@ public function dataToMonth(): iterable ]; } - /** @dataProvider dataToWeekday */ + #[DataProvider('dataToWeekday')] public function testToWeekday(Weekday $expected, string $dateTime): void { $dateTime = DateTime::fromString($dateTime); @@ -1010,7 +1013,7 @@ public function testToWeekday(Weekday $expected, string $dateTime): void self::assertSame($expected, $dateTime->toWeekday()); } - public function dataToWeekday(): iterable + public static function dataToWeekday(): iterable { return [ [Weekday::Monday, '2019-07-15 12:05:23'], @@ -1019,7 +1022,7 @@ public function dataToWeekday(): iterable ]; } - /** @dataProvider dataToPrevWeekday */ + #[DataProvider('dataToPrevWeekday')] public function testToPrevWeekday(string $expected, string $dateTime, $weekday): void { $dateTime = DateTime::fromString($dateTime); @@ -1027,7 +1030,7 @@ public function testToPrevWeekday(string $expected, string $dateTime, $weekday): self::assertDateTime($expected, $dateTime->toPrevWeekday($weekday)); } - public function dataToPrevWeekday(): iterable + public static function dataToPrevWeekday(): iterable { return [ ['2019-07-15 12:05:23.000000 Europe/Berlin', '2019-07-15 12:05:23', Weekday::Monday], @@ -1036,7 +1039,7 @@ public function dataToPrevWeekday(): iterable ]; } - /** @dataProvider dataToNextWeekday */ + #[DataProvider('dataToNextWeekday')] public function testToNextWeekday(string $expected, string $dateTime, $weekday): void { $dateTime = DateTime::fromString($dateTime); @@ -1044,7 +1047,7 @@ public function testToNextWeekday(string $expected, string $dateTime, $weekday): self::assertDateTime($expected, $dateTime->toNextWeekday($weekday)); } - public function dataToNextWeekday(): iterable + public static function dataToNextWeekday(): iterable { return [ ['2019-07-14 12:05:23.000000 Europe/Berlin', '2019-07-14 12:05:23', Weekday::Sunday], @@ -1055,13 +1058,13 @@ public function dataToNextWeekday(): iterable // === CompareTrait === - /** @dataProvider dataMin */ + #[DataProvider('dataMin')] public function testMin(int $expectedIndex, DateTime ...$dateTimes): void { self::assertSame($dateTimes[$expectedIndex], DateTime::min(...$dateTimes)); } - public function dataMin(): iterable + public static function dataMin(): iterable { return [ [0, DateTime::fromString('2020-07-19 18:30:00')], @@ -1071,13 +1074,13 @@ public function dataMin(): iterable ]; } - /** @dataProvider dataMax */ + #[DataProvider('dataMax')] public function testMax(int $expectedIndex, DateTime ...$dateTimes): void { self::assertSame($dateTimes[$expectedIndex], DateTime::max(...$dateTimes)); } - public function dataMax(): iterable + public static function dataMax(): iterable { return [ [0, DateTime::fromString('2020-07-19 18:30:00')], @@ -1087,7 +1090,7 @@ public function dataMax(): iterable ]; } - /** @dataProvider dataDiff */ + #[DataProvider('dataDiff')] public function testDiff(string $expected, DateTime $dateTime, DateTime $other, ...$parameters): void { $duration = $dateTime->diff($other, ...$parameters); @@ -1096,7 +1099,7 @@ public function testDiff(string $expected, DateTime $dateTime, DateTime $other, self::assertSame($expected, $duration->toIso()); } - public function dataDiff(): iterable + public static function dataDiff(): iterable { return [ ['PT0S', DateTime::fromString('2020-07-19 18:30:00'), DateTime::fromString('2020-07-19 18:30:00')], @@ -1108,7 +1111,7 @@ public function dataDiff(): iterable ]; } - /** @dataProvider dataCompareTo */ + #[DataProvider('dataCompareTo')] public function testCompareTo(int $expected, DateTime $dateTime, DateTime $other): void { self::assertSame($expected, $dateTime->compareTo($other)); @@ -1119,7 +1122,7 @@ public function testCompareTo(int $expected, DateTime $dateTime, DateTime $other self::assertSame(1 !== $expected, $dateTime->lowerEquals($other)); } - public function dataCompareTo(): iterable + public static function dataCompareTo(): iterable { return [ [0, DateTime::fromString('2020-07-19 18:30:00'), DateTime::fromString('2020-07-19 18:30:00')], @@ -1141,7 +1144,7 @@ public function testJsonSerialize(): void // === ModifyTrait === - /** @dataProvider dataModify */ + #[DataProvider('dataModify')] public function testModify(string $expected, string $modify): void { $dateTime = DateTime::fromString('2020-07-26 18:30:00.123456'); @@ -1149,7 +1152,7 @@ public function testModify(string $expected, string $modify): void self::assertDateTime($expected, $dateTime->modify($modify)); } - public function dataModify(): iterable + public static function dataModify(): iterable { return [ ['2020-07-27 13:30:00.123456 Europe/Berlin', '+1day -5hours'], @@ -1161,12 +1164,12 @@ public function dataModify(): iterable public function testModifyInvalid(): void { $this->expectException(InvalidStringException::class); - $this->expectExceptionMessage('Failed to parse time string (foo) at position 0 (f): The timezone could not be found in the database.'); + $this->expectExceptionMessageMatches('/^Failed to parse time string \(foo\)/'); DateTime::fromString('2024-02-09')->modify('foo'); } - /** @dataProvider dataAdd */ + #[DataProvider('dataAdd')] public function testAdd(string $expected, string $duration): void { $dateTime = DateTime::fromString('2020-07-26 18:30:00.123456'); @@ -1174,7 +1177,7 @@ public function testAdd(string $expected, string $duration): void self::assertDateTime($expected, $dateTime->add(Duration::fromString($duration))); } - public function dataAdd(): iterable + public static function dataAdd(): iterable { return [ ['2020-08-02 16:30:05.456456 Europe/Berlin', 'P1WT-2H5.333S'], @@ -1182,7 +1185,7 @@ public function dataAdd(): iterable ]; } - /** @dataProvider dataSub */ + #[DataProvider('dataSub')] public function testSub(string $expected, string $duration): void { $dateTime = DateTime::fromString('2020-07-26 18:30:00.123456'); @@ -1190,7 +1193,7 @@ public function testSub(string $expected, string $duration): void self::assertDateTime($expected, $dateTime->sub(Duration::fromString($duration))); } - public function dataSub(): iterable + public static function dataSub(): iterable { return [ ['2020-07-19 20:29:55.000456 Europe/Berlin', 'P1WT-2H5.123S'], diff --git a/tests/DurationTest.php b/tests/DurationTest.php index db14a00..d749234 100644 --- a/tests/DurationTest.php +++ b/tests/DurationTest.php @@ -9,12 +9,14 @@ use HillValley\Fluxcap\Duration; use HillValley\Fluxcap\Exception\InvalidStringException; use HillValley\Fluxcap\TimeZone; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** * @internal - * @covers \HillValley\Fluxcap\Duration */ +#[CoversClass(Duration::class)] final class DurationTest extends TestCase { public function testConstruct(): void @@ -24,13 +26,13 @@ public function testConstruct(): void self::assertTrue($method->isPrivate()); } - /** @dataProvider dataFromString */ + #[DataProvider('dataFromString')] public function testFromString(string $expected, ?string $duration = null): void { static::assertDuration($expected, Duration::fromString($duration ?? $expected)); } - public function dataFromString(): iterable + public static function dataFromString(): iterable { return [ ['PT0S', 'P0D'], @@ -59,13 +61,13 @@ public function testFromParts(): void self::assertDuration('-P2M10DT-2H1.0234S', $duration); } - /** @dataProvider dataFromNative */ + #[DataProvider('dataFromNative')] public function testFromNative(string $expected, \DateInterval $interval): void { static::assertDuration($expected, Duration::fromNative($interval)); } - public function dataFromNative(): iterable + public static function dataFromNative(): iterable { yield ['P1DT20M', new \DateInterval('P1DT20M')]; @@ -88,13 +90,13 @@ public function testFromNativeClone(): void static::assertDuration('P1D', $duration); } - /** @dataProvider dataCast */ + #[DataProvider('dataCast')] public function testCast(string $expected, $duration): void { static::assertDuration($expected, Duration::cast($duration)); } - public function dataCast(): iterable + public static function dataCast(): iterable { return [ ['P1DT20M', Duration::fromString('P1DT20M')], @@ -103,13 +105,13 @@ public function dataCast(): iterable ]; } - /** @dataProvider dataYears */ + #[DataProvider('dataYears')] public function testYears(string $expected, int $years): void { static::assertDuration($expected, Duration::years($years)); } - public function dataYears(): iterable + public static function dataYears(): iterable { return [ ['P-2Y', -2], @@ -118,13 +120,13 @@ public function dataYears(): iterable ]; } - /** @dataProvider dataMonths */ + #[DataProvider('dataMonths')] public function testMonths(string $expected, int $months): void { static::assertDuration($expected, Duration::months($months)); } - public function dataMonths(): iterable + public static function dataMonths(): iterable { return [ ['P-2M', -2], @@ -133,13 +135,13 @@ public function dataMonths(): iterable ]; } - /** @dataProvider dataWeeks */ + #[DataProvider('dataWeeks')] public function testWeeks(string $expected, int $weeks): void { static::assertDuration($expected, Duration::weeks($weeks)); } - public function dataWeeks(): iterable + public static function dataWeeks(): iterable { return [ ['P-14D', -2], @@ -148,13 +150,13 @@ public function dataWeeks(): iterable ]; } - /** @dataProvider dataDays */ + #[DataProvider('dataDays')] public function testDays(string $expected, int $days): void { static::assertDuration($expected, Duration::days($days)); } - public function dataDays(): iterable + public static function dataDays(): iterable { return [ ['P-2D', -2], @@ -163,13 +165,13 @@ public function dataDays(): iterable ]; } - /** @dataProvider dataHours */ + #[DataProvider('dataHours')] public function testHours(string $expected, int $hours): void { static::assertDuration($expected, Duration::hours($hours)); } - public function dataHours(): iterable + public static function dataHours(): iterable { return [ ['PT-2H', -2], @@ -178,13 +180,13 @@ public function dataHours(): iterable ]; } - /** @dataProvider dataMinutes */ + #[DataProvider('dataMinutes')] public function testMinutes(string $expected, int $minutes): void { static::assertDuration($expected, Duration::minutes($minutes)); } - public function dataMinutes(): iterable + public static function dataMinutes(): iterable { return [ ['PT-2M', -2], @@ -193,13 +195,13 @@ public function dataMinutes(): iterable ]; } - /** @dataProvider dataSeconds */ + #[DataProvider('dataSeconds')] public function testSeconds(string $expected, int $seconds): void { static::assertDuration($expected, Duration::seconds($seconds)); } - public function dataSeconds(): iterable + public static function dataSeconds(): iterable { return [ ['PT-2S', -2], @@ -208,7 +210,7 @@ public function dataSeconds(): iterable ]; } - /** @dataProvider dataGetYears */ + #[DataProvider('dataGetYears')] public function testGetYears(int $expected, string $duration): void { $duration = Duration::fromString($duration); @@ -216,7 +218,7 @@ public function testGetYears(int $expected, string $duration): void self::assertSame($expected, $duration->getYears()); } - public function dataGetYears(): iterable + public static function dataGetYears(): iterable { return [ [0, 'P6MT3M'], @@ -226,7 +228,7 @@ public function dataGetYears(): iterable ]; } - /** @dataProvider dataGetMonths */ + #[DataProvider('dataGetMonths')] public function testGetMonths(int $expected, string $duration): void { $duration = Duration::fromString($duration); @@ -234,7 +236,7 @@ public function testGetMonths(int $expected, string $duration): void self::assertSame($expected, $duration->getMonths()); } - public function dataGetMonths(): iterable + public static function dataGetMonths(): iterable { return [ [0, 'P6YT3M'], @@ -244,7 +246,7 @@ public function dataGetMonths(): iterable ]; } - /** @dataProvider dataGetDays */ + #[DataProvider('dataGetDays')] public function testGetDays(int $expected, string $duration): void { $duration = Duration::fromString($duration); @@ -252,7 +254,7 @@ public function testGetDays(int $expected, string $duration): void self::assertSame($expected, $duration->getDays()); } - public function dataGetDays(): iterable + public static function dataGetDays(): iterable { return [ [0, 'P6YT3M'], @@ -262,7 +264,7 @@ public function dataGetDays(): iterable ]; } - /** @dataProvider dataGetHours */ + #[DataProvider('dataGetHours')] public function testGetHours(int $expected, string $duration): void { $duration = Duration::fromString($duration); @@ -270,7 +272,7 @@ public function testGetHours(int $expected, string $duration): void self::assertSame($expected, $duration->getHours()); } - public function dataGetHours(): iterable + public static function dataGetHours(): iterable { return [ [0, 'P2YT3M'], @@ -280,7 +282,7 @@ public function dataGetHours(): iterable ]; } - /** @dataProvider dataGetMinutes */ + #[DataProvider('dataGetMinutes')] public function testGetMinutes(int $expected, string $duration): void { $duration = Duration::fromString($duration); @@ -288,7 +290,7 @@ public function testGetMinutes(int $expected, string $duration): void self::assertSame($expected, $duration->getMinutes()); } - public function dataGetMinutes(): iterable + public static function dataGetMinutes(): iterable { return [ [0, 'P2YT3H'], @@ -298,7 +300,7 @@ public function dataGetMinutes(): iterable ]; } - /** @dataProvider dataGetSeconds */ + #[DataProvider('dataGetSeconds')] public function testGetSeconds(int $expected, string $duration): void { $duration = Duration::fromString($duration); @@ -306,7 +308,7 @@ public function testGetSeconds(int $expected, string $duration): void self::assertSame($expected, $duration->getSeconds()); } - public function dataGetSeconds(): iterable + public static function dataGetSeconds(): iterable { return [ [0, 'P2YT3H'], @@ -317,7 +319,7 @@ public function dataGetSeconds(): iterable ]; } - /** @dataProvider dataGetMicroseconds */ + #[DataProvider('dataGetMicroseconds')] public function testGetMicroseconds(int $expected, string $duration): void { $duration = Duration::fromString($duration); @@ -325,7 +327,7 @@ public function testGetMicroseconds(int $expected, string $duration): void self::assertSame($expected, $duration->getMicroseconds()); } - public function dataGetMicroseconds(): iterable + public static function dataGetMicroseconds(): iterable { return [ [0, 'P2YT3H'], @@ -337,7 +339,7 @@ public function dataGetMicroseconds(): iterable ]; } - /** @dataProvider dataGetTotalMonths */ + #[DataProvider('dataGetTotalMonths')] public function testGetTotalMonths(int $expected, string $duration): void { $duration = Duration::fromString($duration); @@ -345,7 +347,7 @@ public function testGetTotalMonths(int $expected, string $duration): void self::assertSame($expected, $duration->getTotalMonths()); } - public function dataGetTotalMonths(): iterable + public static function dataGetTotalMonths(): iterable { return [ [0, 'P40DT3H'], @@ -357,13 +359,13 @@ public function dataGetTotalMonths(): iterable ]; } - /** @dataProvider dataGetTotalDays */ + #[DataProvider('dataGetTotalDays')] public function testGetTotalDays(?int $expected, Duration $duration): void { self::assertSame($expected, $duration->getTotalDays()); } - public function dataGetTotalDays(): iterable + public static function dataGetTotalDays(): iterable { return [ [null, Duration::fromString('P4D')], @@ -372,13 +374,13 @@ public function dataGetTotalDays(): iterable ]; } - /** @dataProvider dataIsInverted */ + #[DataProvider('dataIsInverted')] public function testIsInverted(bool $expected, Duration $duration): void { self::assertSame($expected, $duration->isInverted()); } - public function dataIsInverted(): iterable + public static function dataIsInverted(): iterable { return [ [false, Duration::fromString('P4D')], @@ -389,13 +391,13 @@ public function dataIsInverted(): iterable ]; } - /** @dataProvider dataIsZero */ + #[DataProvider('dataIsZero')] public function testIsZero(bool $expected, Duration $duration): void { self::assertSame($expected, $duration->isZero()); } - public function dataIsZero(): iterable + public static function dataIsZero(): iterable { return [ [false, Duration::fromString('P4D')], @@ -408,7 +410,7 @@ public function dataIsZero(): iterable ]; } - /** @dataProvider dataFormat */ + #[DataProvider('dataFormat')] public function testFormat(string $expected, string $duration, string $format): void { $duration = Duration::fromString($duration); @@ -416,7 +418,7 @@ public function testFormat(string $expected, string $duration, string $format): self::assertSame($expected, $duration->format($format)); } - public function dataFormat(): iterable + public static function dataFormat(): iterable { return [ ['+ 2 months, 02h, 0min, 1sec, 123ms', 'P1Y2M3DT2H1.000123S', '%R %m months, %Hh, %imin, %ssec, %fms'], @@ -455,7 +457,7 @@ public function testJsonSerialize(): void self::assertJsonStringEqualsJsonString('"P1DT2H"', json_encode($duration)); } - /** @dataProvider dataSerialization */ + #[DataProvider('dataSerialization')] public function testSerialization(Duration $duration): void { $duration2 = unserialize(serialize($duration)); @@ -464,7 +466,7 @@ public function testSerialization(Duration $duration): void self::assertSame($duration->getTotalDays(), $duration2->getTotalDays()); } - public function dataSerialization(): iterable + public static function dataSerialization(): iterable { return [ [Duration::fromString('P2YT3H')], diff --git a/tests/MonthTest.php b/tests/MonthTest.php index 633c67d..2f4f9d1 100644 --- a/tests/MonthTest.php +++ b/tests/MonthTest.php @@ -4,9 +4,12 @@ namespace HillValley\Fluxcap\Tests; +use HillValley\Fluxcap\Base\IntlFormatter; use HillValley\Fluxcap\Date; use HillValley\Fluxcap\DateTime; use HillValley\Fluxcap\Month; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -14,15 +17,17 @@ * @covers \HillValley\Fluxcap\Base\IntlFormatter * @covers \HillValley\Fluxcap\Month */ +#[CoversClass(IntlFormatter::class)] +#[CoversClass(Month::class)] final class MonthTest extends TestCase { - /** @dataProvider dataCast */ + #[DataProvider('dataCast')] public function testCast(Month $exptected, $month): void { self::assertSame($exptected, Month::cast($month)); } - public function dataCast(): iterable + public static function dataCast(): iterable { return [ [Month::April, 4], @@ -79,13 +84,13 @@ public function testGetIntlAbbreviation(): void self::assertSame('Okt', Month::October->getIntlAbbreviation()); } - /** @dataProvider dataDiffToPrev */ + #[DataProvider('dataDiffToPrev')] public function testDiffToPrev(int $expected, Month $month, Month $prev): void { self::assertSame($expected, $month->diffToPrev($prev)); } - public function dataDiffToPrev(): iterable + public static function dataDiffToPrev(): iterable { return [ [1, Month::April, Month::March], @@ -95,13 +100,13 @@ public function dataDiffToPrev(): iterable ]; } - /** @dataProvider dataDiffToNext */ + #[DataProvider('dataDiffToNext')] public function testDiffToNext(int $expected, Month $month, Month $next): void { self::assertSame($expected, $month->diffToNext($next)); } - public function dataDiffToNext(): iterable + public static function dataDiffToNext(): iterable { return [ [1, Month::March, Month::April], diff --git a/tests/TimeTest.php b/tests/TimeTest.php index f36fedd..269493c 100644 --- a/tests/TimeTest.php +++ b/tests/TimeTest.php @@ -4,19 +4,22 @@ namespace HillValley\Fluxcap\Tests; +use HillValley\Fluxcap\Base\IntlFormatter; use HillValley\Fluxcap\DateTime; use HillValley\Fluxcap\Duration; use HillValley\Fluxcap\Exception\FormatMismatchException; use HillValley\Fluxcap\Exception\InvalidPartException; use HillValley\Fluxcap\Exception\InvalidStringException; use HillValley\Fluxcap\Time; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** * @internal - * @covers \HillValley\Fluxcap\Base\IntlFormatter - * @covers \HillValley\Fluxcap\Time */ +#[CoversClass(IntlFormatter::class)] +#[CoversClass(Time::class)] final class TimeTest extends TestCase { public function testConstruct(): void @@ -36,13 +39,13 @@ public function testFromStringNow(): void self::assertTimeNow(Time::fromString('now')); } - /** @dataProvider dataFromString */ + #[DataProvider('dataFromString')] public function testFromString(string $expected, string $time): void { self::assertTime($expected, Time::fromString($time)); } - public function dataFromString(): iterable + public static function dataFromString(): iterable { return [ ['15:32:00.000000', '15:32'], @@ -54,7 +57,7 @@ public function dataFromString(): iterable ]; } - /** @dataProvider dataFromStringInvalid */ + #[DataProvider('dataFromStringInvalid')] public function testFromStringInvalid(string $expected, string $time): void { $this->expectException(InvalidStringException::class); @@ -63,7 +66,7 @@ public function testFromStringInvalid(string $expected, string $time): void Time::fromString($time); } - public function dataFromStringInvalid(): iterable + public static function dataFromStringInvalid(): iterable { return [ ['The time string can not be empty (use "now" for current time).', ''], @@ -72,13 +75,13 @@ public function dataFromStringInvalid(): iterable ]; } - /** @dataProvider dataFromFormat */ + #[DataProvider('dataFromFormat')] public function testFromFormat(string $expected, string $format, string $time): void { self::assertTime($expected, Time::fromFormat($format, $time)); } - public function dataFromFormat(): iterable + public static function dataFromFormat(): iterable { return [ ['15:32:00.000000', 'i.H', '32.15'], @@ -93,13 +96,13 @@ public function testFromFormatInvalid(): void Time::fromFormat('H.i', '20:10'); } - /** @dataProvider dataFromParts */ + #[DataProvider('dataFromParts')] public function testFromParts(string $expected, ...$parts): void { self::assertTime($expected, Time::fromParts(...$parts)); } - public function dataFromParts(): iterable + public static function dataFromParts(): iterable { return [ ['05:00:00.000000', 5], @@ -109,7 +112,7 @@ public function dataFromParts(): iterable ]; } - /** @dataProvider dataFromPartsInvalid */ + #[DataProvider('dataFromPartsInvalid')] public function testFromPartsInvalid(string $expected, ...$parts): void { $this->expectException(InvalidPartException::class); @@ -118,7 +121,7 @@ public function testFromPartsInvalid(string $expected, ...$parts): void Time::fromParts(...$parts); } - public function dataFromPartsInvalid(): iterable + public static function dataFromPartsInvalid(): iterable { return [ ['Hour part must be between 0 and 23, but -1 given.', -1], @@ -138,13 +141,13 @@ public function testFromTimestamp(): void self::assertTime('01:05:20.000000', Time::fromTimestamp($timestamp)); } - /** @dataProvider dataFromNative */ + #[DataProvider('dataFromNative')] public function testFromNative(string $expected, \DateTimeInterface $time): void { self::assertTime($expected, Time::fromNative($time)); } - public function dataFromNative(): iterable + public static function dataFromNative(): iterable { return [ ['23:00:00.000000', new \DateTime('2018-09-13 23:00:00')], @@ -153,13 +156,13 @@ public function dataFromNative(): iterable ]; } - /** @dataProvider dataCast */ + #[DataProvider('dataCast')] public function testCast(string $expected, $time): void { self::assertTime($expected, Time::cast($time)); } - public function dataCast(): iterable + public static function dataCast(): iterable { return [ ['23:35:00.000000', strtotime('2018-06-17 23:35:00')], @@ -170,7 +173,7 @@ public function dataCast(): iterable ]; } - /** @dataProvider dataToIso */ + #[DataProvider('dataToIso')] public function testToIso(string $expected, string $time): void { $time = Time::fromString($time); @@ -179,7 +182,7 @@ public function testToIso(string $expected, string $time): void self::assertSame($expected, (string) $time); } - public function dataToIso(): iterable + public static function dataToIso(): iterable { return [ ['10:00:00.000000', '10:00'], @@ -268,13 +271,13 @@ public function testGetter(): void self::assertSame(12340, $time->getMicrosecond()); } - /** @dataProvider dataIsMidnight */ + #[DataProvider('dataIsMidnight')] public function testIsMidnight(bool $expected, string $time): void { self::assertSame($expected, Time::fromString($time)->isMidnight()); } - public function dataIsMidnight(): iterable + public static function dataIsMidnight(): iterable { return [ [true, '00:00'], @@ -284,13 +287,13 @@ public function dataIsMidnight(): iterable ]; } - /** @dataProvider dataAddHours */ + #[DataProvider('dataAddHours')] public function testAddHours(string $expected, Time $time, int $hours): void { self::assertTime($expected, $time->addHours($hours)); } - public function dataAddHours(): iterable + public static function dataAddHours(): iterable { return [ ['19:25:00.000000', Time::fromString('17:25:00'), 2], @@ -299,13 +302,13 @@ public function dataAddHours(): iterable ]; } - /** @dataProvider dataSubHours */ + #[DataProvider('dataSubHours')] public function testSubHours(string $expected, Time $time, int $hours): void { self::assertTime($expected, $time->subHours($hours)); } - public function dataSubHours(): iterable + public static function dataSubHours(): iterable { return [ ['15:25:00.000000', Time::fromString('17:25:00'), 2], @@ -314,13 +317,13 @@ public function dataSubHours(): iterable ]; } - /** @dataProvider dataAddMinutes */ + #[DataProvider('dataAddMinutes')] public function testAddMinutes(string $expected, Time $time, int $minutes): void { self::assertTime($expected, $time->addMinutes($minutes)); } - public function dataAddMinutes(): iterable + public static function dataAddMinutes(): iterable { return [ ['17:27:00.000000', Time::fromString('17:25:00'), 2], @@ -329,13 +332,13 @@ public function dataAddMinutes(): iterable ]; } - /** @dataProvider dataSubMinutes */ + #[DataProvider('dataSubMinutes')] public function testSubMinutes(string $expected, Time $time, int $minutes): void { self::assertTime($expected, $time->subMinutes($minutes)); } - public function dataSubMinutes(): iterable + public static function dataSubMinutes(): iterable { return [ ['17:23:00.000000', Time::fromString('17:25:00'), 2], @@ -344,13 +347,13 @@ public function dataSubMinutes(): iterable ]; } - /** @dataProvider dataAddSeconds */ + #[DataProvider('dataAddSeconds')] public function testAddSeconds(string $expected, Time $time, int $seconds): void { self::assertTime($expected, $time->addSeconds($seconds)); } - public function dataAddSeconds(): iterable + public static function dataAddSeconds(): iterable { return [ ['17:25:07.000000', Time::fromString('17:25:05'), 2], @@ -359,13 +362,13 @@ public function dataAddSeconds(): iterable ]; } - /** @dataProvider dataSubSeconds */ + #[DataProvider('dataSubSeconds')] public function testSubSeconds(string $expected, Time $time, int $seconds): void { self::assertTime($expected, $time->subSeconds($seconds)); } - public function dataSubSeconds(): iterable + public static function dataSubSeconds(): iterable { return [ ['17:25:03.000000', Time::fromString('17:25:05'), 2], @@ -376,13 +379,13 @@ public function dataSubSeconds(): iterable // === CompareTrait === - /** @dataProvider dataMin */ + #[DataProvider('dataMin')] public function testMin(int $expectedIndex, Time ...$times): void { self::assertSame($times[$expectedIndex], Time::min(...$times)); } - public function dataMin(): iterable + public static function dataMin(): iterable { return [ [0, Time::fromString('18:30:00')], @@ -391,13 +394,13 @@ public function dataMin(): iterable ]; } - /** @dataProvider dataMax */ + #[DataProvider('dataMax')] public function testMax(int $expectedIndex, Time ...$times): void { self::assertSame($times[$expectedIndex], Time::max(...$times)); } - public function dataMax(): iterable + public static function dataMax(): iterable { return [ [0, Time::fromString('18:30:00')], @@ -406,7 +409,7 @@ public function dataMax(): iterable ]; } - /** @dataProvider dataDiff */ + #[DataProvider('dataDiff')] public function testDiff(string $expected, Time $dateTime, Time $other, ...$parameters): void { $duration = $dateTime->diff($other, ...$parameters); @@ -415,7 +418,7 @@ public function testDiff(string $expected, Time $dateTime, Time $other, ...$para self::assertSame($expected, $duration->toIso()); } - public function dataDiff(): iterable + public static function dataDiff(): iterable { return [ ['PT0S', Time::fromString('18:30:00'), Time::fromString('18:30:00')], @@ -426,7 +429,7 @@ public function dataDiff(): iterable ]; } - /** @dataProvider dataCompareTo */ + #[DataProvider('dataCompareTo')] public function testCompareTo(int $expected, Time $dateTime, Time $other): void { self::assertSame($expected, $dateTime->compareTo($other)); @@ -437,7 +440,7 @@ public function testCompareTo(int $expected, Time $dateTime, Time $other): void self::assertSame(1 !== $expected, $dateTime->lowerEquals($other)); } - public function dataCompareTo(): iterable + public static function dataCompareTo(): iterable { return [ [0, Time::fromString('18:30:00'), Time::fromString('18:30:00')], @@ -458,7 +461,7 @@ public function testJsonSerialize(): void // === ModifyTrait === - /** @dataProvider dataModify */ + #[DataProvider('dataModify')] public function testModify(string $expected, string $modify): void { $time = Time::fromString('18:30:00.123456'); @@ -466,7 +469,7 @@ public function testModify(string $expected, string $modify): void self::assertTime($expected, $time->modify($modify)); } - public function dataModify(): iterable + public static function dataModify(): iterable { return [ ['19:25:00.123456', '+1hour -5min'], @@ -478,12 +481,12 @@ public function dataModify(): iterable public function testModifyInvalid(): void { $this->expectException(InvalidStringException::class); - $this->expectExceptionMessage('Failed to parse time string (foo) at position 0 (f): The timezone could not be found in the database.'); + $this->expectExceptionMessageMatches('/^Failed to parse time string \(foo\)/'); Time::fromString('13:00')->modify('foo'); } - /** @dataProvider dataAdd */ + #[DataProvider('dataAdd')] public function testAdd(string $expected, string $duration): void { $dateTime = Time::fromString('18:30:00.123456'); @@ -491,7 +494,7 @@ public function testAdd(string $expected, string $duration): void self::assertTime($expected, $dateTime->add(Duration::fromString($duration))); } - public function dataAdd(): iterable + public static function dataAdd(): iterable { return [ ['16:30:05.456456', 'P1WT-2H5.333S'], @@ -499,7 +502,7 @@ public function dataAdd(): iterable ]; } - /** @dataProvider dataSub */ + #[DataProvider('dataSub')] public function testSub(string $expected, string $duration): void { $dateTime = Time::fromString('18:30:00.123456'); @@ -507,7 +510,7 @@ public function testSub(string $expected, string $duration): void self::assertTime($expected, $dateTime->sub(Duration::fromString($duration))); } - public function dataSub(): iterable + public static function dataSub(): iterable { return [ ['20:29:55.000456', 'P1WT-2H5.123S'], diff --git a/tests/TimeZoneTest.php b/tests/TimeZoneTest.php index 00023a6..ab4054a 100644 --- a/tests/TimeZoneTest.php +++ b/tests/TimeZoneTest.php @@ -6,14 +6,16 @@ use HillValley\Fluxcap\Exception\InvalidStringException; use HillValley\Fluxcap\TimeZone; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use function ini_get; /** * @internal - * @covers \HillValley\Fluxcap\TimeZone */ +#[CoversClass(TimeZone::class)] final class TimeZoneTest extends TestCase { protected function tearDown(): void @@ -50,13 +52,13 @@ public function testUtc(): void self::assertSame($timeZone, TimeZone::utc()); } - /** @dataProvider dataFromString */ + #[DataProvider('dataFromString')] public function testFromString(string $timeZone): void { $this->assertTimeZone($timeZone, TimeZone::fromString($timeZone)); } - public function dataFromString(): iterable + public static function dataFromString(): iterable { return [ ['UTC'], @@ -65,7 +67,7 @@ public function dataFromString(): iterable ]; } - /** @dataProvider dataFromStringInvalid */ + #[DataProvider('dataFromStringInvalid')] public function testFromStringInvalid(string $expected, string $timeZone): void { $this->expectException(InvalidStringException::class); @@ -74,7 +76,7 @@ public function testFromStringInvalid(string $expected, string $timeZone): void TimeZone::fromString($timeZone); } - public function dataFromStringInvalid(): iterable + public static function dataFromStringInvalid(): iterable { return [ ['The time zone string can not be empty.', ''], @@ -82,13 +84,13 @@ public function dataFromStringInvalid(): iterable ]; } - /** @dataProvider dataFromNative */ + #[DataProvider('dataFromNative')] public function testFromNative(string $expected, \DateTimeZone $timeZone): void { $this->assertTimeZone($expected, TimeZone::fromNative($timeZone)); } - public function dataFromNative(): iterable + public static function dataFromNative(): iterable { return [ ['UTC', new \DateTimeZone('UTC')], @@ -97,13 +99,13 @@ public function dataFromNative(): iterable ]; } - /** @dataProvider dataCast */ + #[DataProvider('dataCast')] public function testCast(string $expected, $timeZone): void { $this->assertTimeZone($expected, TimeZone::cast($timeZone)); } - public function dataCast(): iterable + public static function dataCast(): iterable { return [ ['Europe/Paris', 'Europe/Paris'], @@ -112,7 +114,7 @@ public function dataCast(): iterable ]; } - /** @dataProvider dataEquals */ + #[DataProvider('dataEquals')] public function testEquals(bool $expected, string $timeZone1, string $timeZone2): void { $timeZone1 = TimeZone::fromString($timeZone1); @@ -121,7 +123,7 @@ public function testEquals(bool $expected, string $timeZone1, string $timeZone2) self::assertSame($expected, $timeZone1->equals($timeZone2)); } - public function dataEquals(): iterable + public static function dataEquals(): iterable { return [ [false, 'UTC', 'Europe/Berlin'], @@ -149,7 +151,7 @@ public function testIsDefault(): void self::assertTrue($timeZone2->isDefault()); } - /** @dataProvider dataIsUtc */ + #[DataProvider('dataIsUtc')] public function testIsUtc(bool $expected, string $timeZone): void { $timeZone = TimeZone::fromString($timeZone); @@ -157,7 +159,7 @@ public function testIsUtc(bool $expected, string $timeZone): void self::assertSame($expected, $timeZone->isUtc()); } - public function dataIsUtc(): iterable + public static function dataIsUtc(): iterable { return [ [true, 'UTC'], diff --git a/tests/WeekdayTest.php b/tests/WeekdayTest.php index 267bac9..349b007 100644 --- a/tests/WeekdayTest.php +++ b/tests/WeekdayTest.php @@ -4,25 +4,28 @@ namespace HillValley\Fluxcap\Tests; +use HillValley\Fluxcap\Base\IntlFormatter; use HillValley\Fluxcap\Date; use HillValley\Fluxcap\DateTime; use HillValley\Fluxcap\Weekday; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** * @internal - * @covers \HillValley\Fluxcap\Base\IntlFormatter - * @covers \HillValley\Fluxcap\Weekday */ +#[CoversClass(IntlFormatter::class)] +#[CoversClass(Weekday::class)] final class WeekdayTest extends TestCase { - /** @dataProvider dataCast */ + #[DataProvider('dataCast')] public function testCast(Weekday $expected, $weekday): void { self::assertSame($expected, Weekday::cast($weekday)); } - public function dataCast(): iterable + public static function dataCast(): iterable { return [ [Weekday::Wednesday, 3], @@ -74,13 +77,13 @@ public function testGetIntlAbbreviation(): void self::assertSame('Sa', Weekday::Saturday->getIntlAbbreviation()); } - /** @dataProvider dataDiffToPrev */ + #[DataProvider('dataDiffToPrev')] public function testDiffToPrev(int $expected, Weekday $weekday, Weekday $prev): void { self::assertSame($expected, $weekday->diffToPrev($prev)); } - public function dataDiffToPrev(): iterable + public static function dataDiffToPrev(): iterable { return [ [1, Weekday::Wednesday, Weekday::Tuesday], @@ -90,13 +93,13 @@ public function dataDiffToPrev(): iterable ]; } - /** @dataProvider dataDiffToNext */ + #[DataProvider('dataDiffToNext')] public function testDiffToNext(int $expected, Weekday $weekday, Weekday $next): void { self::assertSame($expected, $weekday->diffToNext($next)); } - public function dataDiffToNext(): iterable + public static function dataDiffToNext(): iterable { return [ [1, Weekday::Tuesday, Weekday::Wednesday],