From 51a4ef70056267d0104c3bcb3f4b0b65dcf40f39 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Thu, 28 Apr 2022 21:50:27 +0200 Subject: [PATCH] More tests for locations. --- composer.json | 9 +++- tests/LocaleFormatterTestTrait.php | 18 +++++++ tests/LocaleTests/Locale_en_EN_TestTrait.php | 52 ++++++++++++++++++++ tests/LocaleTests/Locale_es_ES_TestTrait.php | 52 ++++++++++++++++++++ tests/LocaleTests/Locale_eu_TestTrait.php | 52 ++++++++++++++++++++ tests/LocaleTests/Locale_it_CH_TestTrait.php | 52 ++++++++++++++++++++ tests/LocaleTests/Locale_it_IT_TestTrait.php | 52 ++++++++++++++++++++ tests/strftimeTest.php | 37 +++++--------- 8 files changed, 299 insertions(+), 25 deletions(-) create mode 100644 tests/LocaleFormatterTestTrait.php create mode 100644 tests/LocaleTests/Locale_en_EN_TestTrait.php create mode 100644 tests/LocaleTests/Locale_es_ES_TestTrait.php create mode 100644 tests/LocaleTests/Locale_eu_TestTrait.php create mode 100644 tests/LocaleTests/Locale_it_CH_TestTrait.php create mode 100644 tests/LocaleTests/Locale_it_IT_TestTrait.php diff --git a/composer.json b/composer.json index 0e570c8..98cbb62 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,13 @@ "test": "Run PHPUnit tests" }, "autoload": { - "files": ["src/php-8.1-strftime.php"] + "files": [ + "src/php-8.1-strftime.php" + ] + }, + "autoload-dev": { + "psr-4": { + "PHP81_BC\\Tests\\": "tests/" + } } } diff --git a/tests/LocaleFormatterTestTrait.php b/tests/LocaleFormatterTestTrait.php new file mode 100644 index 0000000..4870a42 --- /dev/null +++ b/tests/LocaleFormatterTestTrait.php @@ -0,0 +1,18 @@ +assertEquals('Sun', $result, '%a: An abbreviated textual representation of the day'); + + $result = strftime('%A', '20220306 13:02:03', $locale); + $this->assertEquals('Sunday', $result, '%A: A full textual representation of the day'); + + $result = strftime('%b', '20220306 13:02:03', $locale); + $this->assertEquals('Mar', $result, '%b: Abbreviated month name, based on the locale'); + + $result = strftime('%B', '20220306 13:02:03', $locale); + $this->assertEquals('March', $result, '%B: Full month name, based on the locale'); + + $result = strftime('%h', '20220306 13:02:03', $locale); + $this->assertEquals('Mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); + + $result = strftime('%X', '20220306 13:02:03', $locale); + $this->assertEquals('1:02:03 PM', $result, '%X: Preferred time representation based on locale, without the date'); + + $result = strftime('%c', '20220306 13:02:03', $locale); + $this->assertEquals('March 6, 2022 at 1:02 PM', $result, '%c: Preferred date and time stamp based on locale'); + + $result = strftime('%x', '20220306 13:02:03', $locale); + $this->assertEquals('3/6/22', $result, '%x: Preferred date representation based on locale, without the time'); + + // 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-01: 10/1/82', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian'); + + // In much of Europe, the 10th October 1582 never existed + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-10: 10/10/82', $result, 'In much of Europe, the 10th October 1582 never existed'); + + // The 15th October was the first day after the cutover, after which both systems agree + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-15: 10/15/82', $result, 'The 15th October was the first day after the cutover, after which both systems agree'); + } + } diff --git a/tests/LocaleTests/Locale_es_ES_TestTrait.php b/tests/LocaleTests/Locale_es_ES_TestTrait.php new file mode 100644 index 0000000..c810e53 --- /dev/null +++ b/tests/LocaleTests/Locale_es_ES_TestTrait.php @@ -0,0 +1,52 @@ +assertEquals('dom.', $result, '%a: An abbreviated textual representation of the day'); + + $result = strftime('%A', '20220306 13:02:03', $locale); + $this->assertEquals('domingo', $result, '%A: A full textual representation of the day'); + + $result = strftime('%b', '20220306 13:02:03', $locale); + $this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale'); + + $result = strftime('%B', '20220306 13:02:03', $locale); + $this->assertEquals('marzo', $result, '%B: Full month name, based on the locale'); + + $result = strftime('%h', '20220306 13:02:03', $locale); + $this->assertEquals('mar.', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); + + $result = strftime('%X', '20220306 13:02:03', $locale); + $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); + + $result = strftime('%c', '20220306 13:02:03', $locale); + $this->assertEquals('6 de marzo de 2022, 13:02', $result, '%c: Preferred date and time stamp based on locale'); + + $result = strftime('%x', '20220306 13:02:03', $locale); + $this->assertEquals('6/3/22', $result, '%x: Preferred date representation based on locale, without the time'); + + // 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-01: 1/10/82', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian'); + + // In much of Europe, the 10th October 1582 never existed + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-10: 10/10/82', $result, 'In much of Europe, the 10th October 1582 never existed'); + + // The 15th October was the first day after the cutover, after which both systems agree + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-15: 15/10/82', $result, 'The 15th October was the first day after the cutover, after which both systems agree'); + } + } diff --git a/tests/LocaleTests/Locale_eu_TestTrait.php b/tests/LocaleTests/Locale_eu_TestTrait.php new file mode 100644 index 0000000..fabeb0a --- /dev/null +++ b/tests/LocaleTests/Locale_eu_TestTrait.php @@ -0,0 +1,52 @@ +assertEquals('ig.', $result, '%a: An abbreviated textual representation of the day'); + + $result = strftime('%A', '20220306 13:02:03', $locale); + $this->assertEquals('igandea', $result, '%A: A full textual representation of the day'); + + $result = strftime('%b', '20220306 13:02:03', $locale); + $this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale'); + + $result = strftime('%B', '20220306 13:02:03', $locale); + $this->assertEquals('martxoa', $result, '%B: Full month name, based on the locale'); + + $result = strftime('%h', '20220306 13:02:03', $locale); + $this->assertEquals('mar.', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); + + $result = strftime('%X', '20220306 13:02:03', $locale); + $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); + + $result = strftime('%c', '20220306 13:02:03', $locale); + $this->assertEquals('2022(e)ko martxoaren 6(a) 13:02', $result, '%c: Preferred date and time stamp based on locale'); + + $result = strftime('%x', '20220306 13:02:03', $locale); + $this->assertEquals('22/3/6', $result, '%x: Preferred date representation based on locale, without the time'); + + // 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-01: 82/10/1', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian'); + + // In much of Europe, the 10th October 1582 never existed + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-10: 82/10/10', $result, 'In much of Europe, the 10th October 1582 never existed'); + + // The 15th October was the first day after the cutover, after which both systems agree + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-15: 82/10/15', $result, 'The 15th October was the first day after the cutover, after which both systems agree'); + } + } diff --git a/tests/LocaleTests/Locale_it_CH_TestTrait.php b/tests/LocaleTests/Locale_it_CH_TestTrait.php new file mode 100644 index 0000000..9400b85 --- /dev/null +++ b/tests/LocaleTests/Locale_it_CH_TestTrait.php @@ -0,0 +1,52 @@ +assertEquals('dom', $result, '%a: An abbreviated textual representation of the day'); + + $result = strftime('%A', '20220306 13:02:03', $locale); + $this->assertEquals('domenica', $result, '%A: A full textual representation of the day'); + + $result = strftime('%b', '20220306 13:02:03', $locale); + $this->assertEquals('mar', $result, '%b: Abbreviated month name, based on the locale'); + + $result = strftime('%B', '20220306 13:02:03', $locale); + $this->assertEquals('marzo', $result, '%B: Full month name, based on the locale'); + + $result = strftime('%h', '20220306 13:02:03', $locale); + $this->assertEquals('mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); + + $result = strftime('%X', '20220306 13:02:03', $locale); + $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); + + $result = strftime('%c', '20220306 13:02:03', $locale); + $this->assertEquals('6 marzo 2022 13:02', $result, '%c: Preferred date and time stamp based on locale'); + + $result = strftime('%x', '20220306 13:02:03', $locale); + $this->assertEquals('06.03.22', $result, '%x: Preferred date representation based on locale, without the time'); + + // 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-01: 01.10.82', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian'); + + // In much of Europe, the 10th October 1582 never existed + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-10: 10.10.82', $result, 'In much of Europe, the 10th October 1582 never existed'); + + // The 15th October was the first day after the cutover, after which both systems agree + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-15: 15.10.82', $result, 'The 15th October was the first day after the cutover, after which both systems agree'); + } + } diff --git a/tests/LocaleTests/Locale_it_IT_TestTrait.php b/tests/LocaleTests/Locale_it_IT_TestTrait.php new file mode 100644 index 0000000..b0dd9b0 --- /dev/null +++ b/tests/LocaleTests/Locale_it_IT_TestTrait.php @@ -0,0 +1,52 @@ +assertEquals('dom', $result, '%a: An abbreviated textual representation of the day'); + + $result = strftime('%A', '20220306 13:02:03', $locale); + $this->assertEquals('domenica', $result, '%A: A full textual representation of the day'); + + $result = strftime('%b', '20220306 13:02:03', $locale); + $this->assertEquals('mar', $result, '%b: Abbreviated month name, based on the locale'); + + $result = strftime('%B', '20220306 13:02:03', $locale); + $this->assertEquals('marzo', $result, '%B: Full month name, based on the locale'); + + $result = strftime('%h', '20220306 13:02:03', $locale); + $this->assertEquals('mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); + + $result = strftime('%X', '20220306 13:02:03', $locale); + $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); + + $result = strftime('%c', '20220306 13:02:03', $locale); + $this->assertEquals('6 marzo 2022 13:02', $result, '%c: Preferred date and time stamp based on locale'); + + $result = strftime('%x', '20220306 13:02:03', $locale); + $this->assertEquals('06/03/22', $result, '%x: Preferred date representation based on locale, without the time'); + + // 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-01: 01/10/82', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian'); + + // In much of Europe, the 10th October 1582 never existed + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-10: 10/10/82', $result, 'In much of Europe, the 10th October 1582 never existed'); + + // The 15th October was the first day after the cutover, after which both systems agree + $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp(); + $result = strftime('%F: %x', $prolepticTimestamp, $locale); + $this->assertEquals('1582-10-15: 15/10/82', $result, 'The 15th October was the first day after the cutover, after which both systems agree'); + } + } diff --git a/tests/strftimeTest.php b/tests/strftimeTest.php index 4d29a59..04f7a4e 100644 --- a/tests/strftimeTest.php +++ b/tests/strftimeTest.php @@ -1,12 +1,18 @@ assertEquals('%', $result, '%%: A literal percentage character ("%")'); } - public function testLocale () { - $result = strftime('%x %X', '20220306 13:02:03', 'it_IT'); - $this->assertEquals('06/03/22 13:02:03', $result, 'Locale test for it_IT'); - - $result = strftime('%x %X', '20220306 13:02:03', 'it_CH'); - $this->assertEquals('06.03.22 13:02:03', $result, 'Locale test for it_CH'); - - $result = strftime('%c', '20220306 13:02:03', 'eu'); - $this->assertEquals('2022(e)ko martxoaren 6(a) 13:02', $result, '%c: Preferred date and time stamp based on locale'); - - $result = strftime('%b', '20220306 13:02:03', 'eu'); - $this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale'); - - $result = strftime('%B', '20220306 13:02:03', 'eu'); - $this->assertEquals('martxoa', $result, '%B: Full month name, based on the locale'); - } - /** * In October 1582, the Gregorian calendar replaced the Julian in much of Europe, and * the 4th October was followed by the 15th October. @@ -195,17 +184,17 @@ public function testLocale () { public function testJulianCutover () { // 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp(); - $result = strftime('%x', $prolepticTimestamp, 'eu'); - $this->assertEquals('82/10/1', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian'); + $result = strftime('%F: %x', $prolepticTimestamp, 'en-EN'); + $this->assertEquals('1582-10-01: 10/1/82', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian'); // In much of Europe, the 10th October 1582 never existed $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp(); - $result = strftime('%x', $prolepticTimestamp, 'eu'); - $this->assertEquals('82/10/10', $result, 'In much of Europe, the 10th October 1582 never existed'); + $result = strftime('%F: %x', $prolepticTimestamp, 'en-EN'); + $this->assertEquals('1582-10-10: 10/10/82', $result, 'In much of Europe, the 10th October 1582 never existed'); // The 15th October was the first day after the cutover, after which both systems agree $prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp(); - $result = strftime('%x', $prolepticTimestamp, 'eu'); - $this->assertEquals('82/10/15', $result, 'The 15th October was the first day after the cutover, after which both systems agree'); + $result = strftime('%F: %x', $prolepticTimestamp, 'en-EN'); + $this->assertEquals('1582-10-15: 10/15/82', $result, 'The 15th October was the first day after the cutover, after which both systems agree'); } }