Skip to content

Commit

Permalink
More tests for locations.
Browse files Browse the repository at this point in the history
  • Loading branch information
alphp committed Apr 28, 2022
1 parent e27834d commit 51a4ef7
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 25 deletions.
9 changes: 8 additions & 1 deletion composer.json
Expand Up @@ -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/"
}
}
}
18 changes: 18 additions & 0 deletions tests/LocaleFormatterTestTrait.php
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace PHP81_BC\Tests;

use PHP81_BC\Tests\LocaleTests\Locale_en_EN_TestTrait;
use PHP81_BC\Tests\LocaleTests\Locale_es_ES_TestTrait;
use PHP81_BC\Tests\LocaleTests\Locale_eu_TestTrait;
use PHP81_BC\Tests\LocaleTests\Locale_it_CH_TestTrait;
use PHP81_BC\Tests\LocaleTests\Locale_it_IT_TestTrait;

trait LocaleFormatterTestTrait {
use Locale_en_EN_TestTrait;
use Locale_es_ES_TestTrait;
use Locale_eu_TestTrait;
use Locale_it_CH_TestTrait;
use Locale_it_IT_TestTrait;
}
52 changes: 52 additions & 0 deletions tests/LocaleTests/Locale_en_EN_TestTrait.php
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

namespace PHP81_BC\Tests\LocaleTests;

use DateTimeImmutable;
use function PHP81_BC\strftime;

trait Locale_en_EN_TestTrait {
public function testLocale_en_EN () {
$locale = 'en-EN';

$result = strftime('%a', '20220306 13:02:03', $locale);
$this->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');
}
}
52 changes: 52 additions & 0 deletions tests/LocaleTests/Locale_es_ES_TestTrait.php
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

namespace PHP81_BC\Tests\LocaleTests;

use DateTimeImmutable;
use function PHP81_BC\strftime;

trait Locale_es_ES_TestTrait {
public function testLocale_es_ES () {
$locale = 'es-ES';

$result = strftime('%a', '20220306 13:02:03', $locale);
$this->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');
}
}
52 changes: 52 additions & 0 deletions tests/LocaleTests/Locale_eu_TestTrait.php
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

namespace PHP81_BC\Tests\LocaleTests;

use DateTimeImmutable;
use function PHP81_BC\strftime;

trait Locale_eu_TestTrait {
public function testLocale_eu () {
$locale = 'eu';

$result = strftime('%a', '20220306 13:02:03', $locale);
$this->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');
}
}
52 changes: 52 additions & 0 deletions tests/LocaleTests/Locale_it_CH_TestTrait.php
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

namespace PHP81_BC\Tests\LocaleTests;

use DateTimeImmutable;
use function PHP81_BC\strftime;

trait Locale_it_CH_TestTrait {
public function testLocale_it_CH () {
$locale = 'it-CH';

$result = strftime('%a', '20220306 13:02:03', $locale);
$this->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');
}
}
52 changes: 52 additions & 0 deletions tests/LocaleTests/Locale_it_IT_TestTrait.php
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

namespace PHP81_BC\Tests\LocaleTests;

use DateTimeImmutable;
use function PHP81_BC\strftime;

trait Locale_it_IT_TestTrait {
public function testLocale_it_IT () {
$locale = 'it-IT';

$result = strftime('%a', '20220306 13:02:03', $locale);
$this->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');
}
}
37 changes: 13 additions & 24 deletions tests/strftimeTest.php
@@ -1,12 +1,18 @@
<?php
declare(strict_types=1);

namespace PHP81_BC\Tests;

use DateTime;
use DateTimeImmutable;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use function PHP81_BC\strftime;

class strftimeTest extends TestCase {
use LocaleFormatterTestTrait;

public function setUp () : void {
public static function setUpBeforeClass () : void {
date_default_timezone_set('Europe/Madrid');
}

Expand Down Expand Up @@ -166,23 +172,6 @@ public function testMiscellaneousFormats () {
$this->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.
Expand All @@ -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');
}
}

0 comments on commit 51a4ef7

Please sign in to comment.