Skip to content

Commit

Permalink
Give testing some love
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Sep 10, 2024
1 parent c5ce28b commit 0ef7d4e
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 36 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:

env:
COMPOSER_ROOT_VERSION: 1.x-dev
SYMFONY_PHPUNIT_VERSION: 8.5

strategy:
matrix:
Expand Down Expand Up @@ -41,10 +40,12 @@ jobs:
tools: "composer:v2"

- name: Install dependencies
run: composer --prefer-source --no-progress --ansi install
run: |
composer --prefer-source --no-progress --ansi install
./phpunit install
- name: Run tests
run: |
ok=0
./vendor/bin/simple-phpunit || ok=1
./phpunit || ok=1
[[ "${{ matrix.mode }}" = experimental ]] || (exit $ok)
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
.phpunit.result.cache
composer.lock
phpunit.xml
.phpunit
vendor/
/tests/unicode
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ cache:
init:
- SET PATH=c:\php;%PATH%
- SET COMPOSER_NO_INTERACTION=1
- SET COMPOSER_ROOT_VERSION=1.x-dev
- SET PHP=1
- SET SYMFONY_PHPUNIT_VERSION=8.5

install:
- cinst wget
Expand Down Expand Up @@ -41,8 +41,8 @@ install:
- appveyor DownloadFile https://github.com/composer/composer/releases/download/2.7.9/composer.phar
- cd c:\projects\polyfill
- mkdir %APPDATA%\Composer && copy /Y .github\composer-config.json %APPDATA%\Composer\config.json
- SET COMPOSER_ROOT_VERSION=1.x-dev
- composer update --prefer-source --no-progress --ansi
- php -d allow_url_fopen=0 ./phpunit install

test_script:
- php -d allow_url_fopen=0 ./vendor/symfony/phpunit-bridge/bin/simple-phpunit
- php -d allow_url_fopen=0 ./phpunit
13 changes: 13 additions & 0 deletions phpunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env php
<?php

if (!file_exists(__DIR__.'/vendor/symfony/phpunit-bridge/bin/simple-phpunit')) {
echo "Unable to find the `simple-phpunit` script in `vendor/symfony/phpunit-bridge/bin/`.\nPlease run `composer update` before running this command.\n";
exit(1);
}
if (!getenv('SYMFONY_PHPUNIT_VERSION')) {
putenv('SYMFONY_PHPUNIT_VERSION=8.5');
}

putenv('SYMFONY_PHPUNIT_DIR='.__DIR__.'/.phpunit');
require __DIR__.'/vendor/symfony/phpunit-bridge/bin/simple-phpunit';
2 changes: 1 addition & 1 deletion src/Apcu/bootstrap80.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function apcu_delete($key): array|bool { return p\Apcu::apcu_delete($key); }
function apcu_exists($key): array|bool { return p\Apcu::apcu_exists($key); }
}
if (!function_exists('apcu_fetch')) {
function apcu_fetch($key, &$success = null): mixed { return p\Apcu::apcu_fetch($key, $success); }
function apcu_fetch($key, &$success = null) { return p\Apcu::apcu_fetch($key, $success); }
}
if (!function_exists('apcu_store')) {
function apcu_store($key, mixed $value, ?int $ttl = 0): array|bool { return p\Apcu::apcu_store($key, $value, (int) $ttl); }
Expand Down
8 changes: 8 additions & 0 deletions src/Intl/Idn/Idn.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ final class Idn
*/
public static function idn_to_ascii($domainName, $options = self::IDNA_DEFAULT, $variant = self::INTL_IDNA_VARIANT_UTS46, &$idna_info = [])
{
if (\PHP_VERSION_ID > 80400 && '' === $domainName) {
throw new \ValueError('idn_to_ascii(): Argument #1 ($domain) cannot be empty');
}

if (self::INTL_IDNA_VARIANT_2003 === $variant) {
@trigger_error('idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated', \E_USER_DEPRECATED);
}
Expand Down Expand Up @@ -198,6 +202,10 @@ public static function idn_to_ascii($domainName, $options = self::IDNA_DEFAULT,
*/
public static function idn_to_utf8($domainName, $options = self::IDNA_DEFAULT, $variant = self::INTL_IDNA_VARIANT_UTS46, &$idna_info = [])
{
if (\PHP_VERSION_ID > 80400 && '' === $domainName) {
throw new \ValueError('idn_to_utf8(): Argument #1 ($domain) cannot be empty');
}

if (self::INTL_IDNA_VARIANT_2003 === $variant) {
@trigger_error('idn_to_utf8(): INTL_IDNA_VARIANT_2003 is deprecated', \E_USER_DEPRECATED);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Mbstring/bootstrap80.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $
}

if (!function_exists('mb_ucfirst')) {
function mb_ucfirst($string, ?string $encoding = null): string { return p\Mbstring::mb_ucfirst($string, $encoding); }
function mb_ucfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_ucfirst($string, $encoding); }
}

if (!function_exists('mb_lcfirst')) {
function mb_lcfirst($string, ?string $encoding = null): string { return p\Mbstring::mb_lcfirst($string, $encoding); }
function mb_lcfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_lcfirst($string, $encoding); }
}

if (!function_exists('mb_trim')) {
Expand Down
4 changes: 2 additions & 2 deletions src/Php84/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ function array_all(array $array, callable $callback): bool { return p\Php84::arr

if (extension_loaded('mbstring')) {
if (!function_exists('mb_ucfirst')) {
function mb_ucfirst($string, ?string $encoding = null): string { return p\Php84::mb_ucfirst($string, $encoding); }
function mb_ucfirst(string $string, ?string $encoding = null): string { return p\Php84::mb_ucfirst($string, $encoding); }
}

if (!function_exists('mb_lcfirst')) {
function mb_lcfirst($string, ?string $encoding = null): string { return p\Php84::mb_lcfirst($string, $encoding); }
function mb_lcfirst(string $string, ?string $encoding = null): string { return p\Php84::mb_lcfirst($string, $encoding); }
}

if (!function_exists('mb_trim')) {
Expand Down
12 changes: 11 additions & 1 deletion src/Util/TestListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ function {$f['name']}{$f['signature']}
$polyfillSignature = ReflectionCaster::castFunctionAbstract(new \ReflectionFunction($testNamespace.'\\'.$f['name']), [], new Stub(), true);
$polyfillSignature = ReflectionCaster::getSignature($polyfillSignature);

if ('mb_get_info' === $r->name && false === strpos($originalSignature, '|null') && false !== strpos($polyfillSignature, '|null')) {
// Added to PHP 8.2.14/8.3.1
$originalSignature .= '|null';
}

if (false === strpos($bootstrap->getPath(), '80.php')) {
// mixed return type cannot be used before PHP 8
$originalSignature = str_replace(': mixed', '', $originalSignature);
}

$map = [
'?' => '',
'IDNA_DEFAULT' => \PHP_VERSION_ID >= 80100 ? 'IDNA_DEFAULT' : '0',
Expand All @@ -131,7 +141,7 @@ function {$f['name']}{$f['signature']}
'array|string|null $from_encoding' => 'array|string $from_encoding',
];

if (strtr($polyfillSignature, $map) !== $originalSignature) {
if (strtr($polyfillSignature, $map) !== str_replace('?', '', $originalSignature)) {
$warnings[] = TestListener::warning("Incompatible signature for PHP >= 8:\n- {$f['name']}$originalSignature\n+ {$f['name']}$polyfillSignature");
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* and charset data to a format suitable for other Utf8 classes.
*
* See https://unicode.org/Public/UNIDATA/ for unicode data
* See https://github.com/unicode-org/cldr/blob/master/common/transforms/ for Latin-ASCII.xml
* See https://github.com/unicode-org/cldr/blob/main/common/transforms/ for Latin-ASCII.xml
*
* @author Nicolas Grekas <p@tchwork.com>
*
Expand Down
33 changes: 22 additions & 11 deletions tests/Intl/Icu/AbstractIntlDateFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public function testConstructorDefaultTimeZone()
{
$formatter = $this->getDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT);

$this->assertEquals(date_default_timezone_get(), $formatter->getTimeZoneId());
$this->assertSame(date_default_timezone_get(), $formatter->getTimeZoneId());

$this->assertEquals(
$this->assertSame(
$this->getDateTime(0, $formatter->getTimeZoneId())->format('M j, Y, g:i A'),
$formatter->format(0)
str_replace("\u{202F}", ' ', $formatter->format(0))
);
}

Expand All @@ -50,7 +50,7 @@ public function testConstructorWithoutDateType()
{
$formatter = $this->getDateFormatter('en', null, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN);

$this->assertSame('EEEE, MMMM d, y \'at\' h:mm a', $formatter->getPattern());
$this->assertSame('EEEE, MMMM d, y \'at\' h:mm a', str_replace("\u{202F}", ' ', $formatter->getPattern()));
}

/**
Expand All @@ -60,7 +60,7 @@ public function testConstructorWithoutTimeType()
{
$formatter = $this->getDateFormatter('en', IntlDateFormatter::SHORT, null, 'UTC', IntlDateFormatter::GREGORIAN);

$this->assertSame('M/d/yy, h:mm:ss a zzzz', $formatter->getPattern());
$this->assertSame('M/d/yy, h:mm:ss a zzzz', str_replace("\u{202F}", ' ', $formatter->getPattern()));
}

/**
Expand Down Expand Up @@ -498,7 +498,11 @@ public function testFormatIgnoresPatternForRelativeDateType()
$datetime = \DateTime::createFromFormat('U', time(), new \DateTimeZone('GMT'));
$datetime->setTime(0, 0, 0);

$this->assertSame('today at 12:00:00 AM Greenwich Mean Time', $formatter->format($datetime));
$formatted = $formatter->format($datetime);
$formatted = str_replace(' at ', ', ', $formatted);
$formatted = str_replace("\u{202F}", ' ', $formatted);

$this->assertSame('today, 12:00:00 AM Greenwich Mean Time', $formatted);
}

/**
Expand All @@ -507,7 +511,7 @@ public function testFormatIgnoresPatternForRelativeDateType()
public function testDateAndTimeType($timestamp, $datetype, $timetype, $expected)
{
$formatter = $this->getDateFormatter('en', $datetype, $timetype, 'UTC');
$this->assertSame($expected, $formatter->format($timestamp));
$this->assertSame($expected, str_replace("\u{202F}", ' ', $formatter->format($timestamp)));
}

public static function dateAndTimeTypeProvider()
Expand All @@ -533,7 +537,14 @@ public function testRelativeDateType($timestamp, $datetype, $timetype, $expected
$datetime->setTime(0, 0, 0);

$formatter = $this->getDateFormatter('en', $datetype, $timetype, 'UTC');
$this->assertSame($expected, $formatter->format($datetime));

$formatted = $formatter->format($datetime);

// Ignore differences that vary by version of PHP or ICU
$formatted = str_replace(' at ', ', ', $formatted);
$formatted = str_replace("\u{202F}", ' ', $formatted);

$this->assertSame($expected, $formatted);
}

public static function relativeDateTypeProvider()
Expand All @@ -545,17 +556,17 @@ public static function relativeDateTypeProvider()
[0, IntlDateFormatter::RELATIVE_SHORT, IntlDateFormatter::NONE, '1/1/70'],

[time(), IntlDateFormatter::RELATIVE_FULL, IntlDateFormatter::NONE, 'today'],
[time(), IntlDateFormatter::RELATIVE_LONG, IntlDateFormatter::FULL, 'today at 12:00:00 AM Coordinated Universal Time'],
[time(), IntlDateFormatter::RELATIVE_LONG, IntlDateFormatter::FULL, 'today, 12:00:00 AM Coordinated Universal Time'],
[time(), IntlDateFormatter::RELATIVE_MEDIUM, IntlDateFormatter::LONG, 'today, 12:00:00 AM UTC'],
[time(), IntlDateFormatter::RELATIVE_SHORT, IntlDateFormatter::SHORT, 'today, 12:00 AM'],

[strtotime('-1 day', time()), IntlDateFormatter::RELATIVE_FULL, IntlDateFormatter::NONE, 'yesterday'],
[strtotime('-1 day', time()), IntlDateFormatter::RELATIVE_LONG, IntlDateFormatter::FULL, 'yesterday at 12:00:00 AM Coordinated Universal Time'],
[strtotime('-1 day', time()), IntlDateFormatter::RELATIVE_LONG, IntlDateFormatter::FULL, 'yesterday, 12:00:00 AM Coordinated Universal Time'],
[strtotime('-1 day', time()), IntlDateFormatter::RELATIVE_MEDIUM, IntlDateFormatter::LONG, 'yesterday, 12:00:00 AM UTC'],
[strtotime('-1 day', time()), IntlDateFormatter::RELATIVE_SHORT, IntlDateFormatter::SHORT, 'yesterday, 12:00 AM'],

[strtotime('+1 day', time()), IntlDateFormatter::RELATIVE_FULL, IntlDateFormatter::NONE, 'tomorrow'],
[strtotime('+1 day', time()), IntlDateFormatter::RELATIVE_LONG, IntlDateFormatter::FULL, 'tomorrow at 12:00:00 AM Coordinated Universal Time'],
[strtotime('+1 day', time()), IntlDateFormatter::RELATIVE_LONG, IntlDateFormatter::FULL, 'tomorrow, 12:00:00 AM Coordinated Universal Time'],
[strtotime('+1 day', time()), IntlDateFormatter::RELATIVE_MEDIUM, IntlDateFormatter::LONG, 'tomorrow, 12:00:00 AM UTC'],
[strtotime('+1 day', time()), IntlDateFormatter::RELATIVE_SHORT, IntlDateFormatter::SHORT, 'tomorrow, 12:00 AM'],
];
Expand Down
15 changes: 12 additions & 3 deletions tests/Intl/Idn/IdnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,12 @@ static function (array $matches): string {
*/
public function testToUnicode($source, $toUnicode, $toUnicodeStatus, $toAsciiN, $toAsciiNStatus, $toAsciiT, $toAsciiTStatus)
{
if (\PHP_VERSION_ID >= 80400 && '' === $source) {
$this->expectException(\ValueError::class);
}

$options = \IDNA_CHECK_BIDI | \IDNA_CHECK_CONTEXTJ | \IDNA_USE_STD3_RULES | \IDNA_NONTRANSITIONAL_TO_UNICODE;
$result = idn_to_utf8($source, $options, \INTL_IDNA_VARIANT_UTS46, $info);
idn_to_utf8($source, $options, \INTL_IDNA_VARIANT_UTS46, $info);

if (null === $info) {
$this->markTestSkipped('PHP Bug #72506.');
Expand Down Expand Up @@ -151,7 +155,8 @@ public function testToUnicode($source, $toUnicode, $toUnicodeStatus, $toAsciiN,
public function testToAsciiNonTransitional($source, $toUnicode, $toUnicodeStatus, $toAsciiN, $toAsciiNStatus, $toAsciiT, $toAsciiTStatus)
{
$options = \IDNA_CHECK_BIDI | \IDNA_CHECK_CONTEXTJ | \IDNA_USE_STD3_RULES | \IDNA_NONTRANSITIONAL_TO_ASCII;
$result = idn_to_ascii($source, $options, \INTL_IDNA_VARIANT_UTS46, $info);

idn_to_ascii($source, $options, \INTL_IDNA_VARIANT_UTS46, $info);

if (null === $info) {
$this->markTestSkipped('PHP Bug #72506.');
Expand All @@ -178,8 +183,12 @@ public function testToAsciiNonTransitional($source, $toUnicode, $toUnicodeStatus
*/
public function testToAsciiTransitional($source, $toUnicode, $toUnicodeStatus, $toAsciiN, $toAsciiNStatus, $toAsciiT, $toAsciiTStatus)
{
if (\PHP_VERSION_ID >= 80400 && '' === $source) {
$this->expectException(\ValueError::class);
}

$options = \IDNA_CHECK_BIDI | \IDNA_CHECK_CONTEXTJ | \IDNA_USE_STD3_RULES;
$result = idn_to_ascii($source, $options, \INTL_IDNA_VARIANT_UTS46, $info);
idn_to_ascii($source, $options, \INTL_IDNA_VARIANT_UTS46, $info);

if (null === $info) {
$this->markTestSkipped('PHP Bug #72506.');
Expand Down
6 changes: 3 additions & 3 deletions tests/Mbstring/MbstringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,9 @@ public static function paddingEncodingProvider(): iterable

public static function mbStrPadInvalidArgumentsProvider(): iterable
{
yield ['mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string', '▶▶', 6, '', \STR_PAD_RIGHT];
yield ['mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string', '▶▶', 6, '', \STR_PAD_LEFT];
yield ['mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string', '▶▶', 6, '', \STR_PAD_BOTH];
yield ['mb_str_pad(): Argument #3 ($pad_string)', '▶▶', 6, '', \STR_PAD_RIGHT];
yield ['mb_str_pad(): Argument #3 ($pad_string)', '▶▶', 6, '', \STR_PAD_LEFT];
yield ['mb_str_pad(): Argument #3 ($pad_string)', '▶▶', 6, '', \STR_PAD_BOTH];
yield ['mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH', '▶▶', 6, ' ', 123456];
yield ['mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "unexisting" given', '▶▶', 6, ' ', \STR_PAD_BOTH, 'unexisting'];
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Php74/Php74Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@ class A
private $priv = 3;
}

#[\AllowDynamicProperties]
class B extends A
{
private $priv = 4;
}

#[\AllowDynamicProperties]
class AO extends \ArrayObject
{
private $priv = 1;
Expand Down
10 changes: 5 additions & 5 deletions tests/Php83/Php83Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public static function paddingEncodingProvider(): iterable

public static function mbStrPadInvalidArgumentsProvider(): iterable
{
yield ['mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string', '▶▶', 6, '', \STR_PAD_RIGHT];
yield ['mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string', '▶▶', 6, '', \STR_PAD_LEFT];
yield ['mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string', '▶▶', 6, '', \STR_PAD_BOTH];
yield ['mb_str_pad(): Argument #3 ($pad_string)', '▶▶', 6, '', \STR_PAD_RIGHT];
yield ['mb_str_pad(): Argument #3 ($pad_string)', '▶▶', 6, '', \STR_PAD_LEFT];
yield ['mb_str_pad(): Argument #3 ($pad_string)', '▶▶', 6, '', \STR_PAD_BOTH];
yield ['mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH', '▶▶', 6, ' ', 123456];
yield ['mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "unexisting" given', '▶▶', 6, ' ', \STR_PAD_BOTH, 'unexisting'];
}
Expand Down Expand Up @@ -280,7 +280,7 @@ public function testInvalidStrIncrement(string $errorMessage, string $string)

public static function strInvalidIncrementProvider(): iterable
{
yield ['str_increment(): Argument #1 ($string) cannot be empty', ''];
yield ['str_increment(): Argument #1 ($string)', ''];
yield ['str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters', '-cc'];
yield ['str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters', 'Z '];
yield ['str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters', ' Z'];
Expand Down Expand Up @@ -311,7 +311,7 @@ public function testInvalidStrDecrement(string $errorMessage, string $string)

public static function strInvalidDecrementProvider(): iterable
{
yield ['str_decrement(): Argument #1 ($string) cannot be empty', ''];
yield ['str_decrement(): Argument #1 ($string)', ''];
yield ['str_decrement(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters', '我喜歡雞肉'];
yield ['str_decrement(): Argument #1 ($string) "0" is out of decrement range', '0'];
yield ['str_decrement(): Argument #1 ($string) "a" is out of decrement range', 'a'];
Expand Down
2 changes: 1 addition & 1 deletion tests/update-unidata.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
file_put_contents(__DIR__.'/unicode/data/'.$file, $data);
}

$data = file_get_contents('https://github.com/unicode-org/cldr/raw/master/common/transforms/Latin-ASCII.xml');
$data = file_get_contents('https://github.com/unicode-org/cldr/raw/main/common/transforms/Latin-ASCII.xml');
file_put_contents(__DIR__.'/unicode/data/Latin-ASCII.xml', $data);

Compiler::translitMap(__DIR__.'/../src/Iconv/Resources/charset/');
Expand Down

0 comments on commit 0ef7d4e

Please sign in to comment.