Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give testing some love #495

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 8 additions & 13 deletions src/Util/TestListenerForV7.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
*/
class TestListenerForV7 extends TestSuite implements TestListenerInterface
{
private $suite;
private $trait;

public function __construct(?TestSuite $suite = null)
{
if ($suite) {
$this->suite = $suite;
$this->setName($suite->getName().' with polyfills enabled');
$this->addTest($suite);
}
Expand All @@ -38,7 +36,13 @@ public function __construct(?TestSuite $suite = null)

public function startTestSuite(TestSuite $suite): void
{
$this->trait->startTestSuite($suite);
if (null === TestListenerTrait::$enabledPolyfills) {
TestListenerTrait::$enabledPolyfills = false;
$this->trait->startTestSuite($suite);
}
if ($suite instanceof TestListener) {
TestListenerTrait::$enabledPolyfills = $suite->getName();
}
}

public function addError(Test $test, \Throwable $t, float $time): void
Expand Down Expand Up @@ -69,6 +73,7 @@ public function addSkippedTest(Test $test, \Throwable $t, float $time): void

public function endTestSuite(TestSuite $suite): void
{
TestListenerTrait::$enabledPolyfills = false;
}

public function startTest(Test $test): void
Expand All @@ -83,14 +88,4 @@ public static function warning($message): WarningTestCase
{
return new WarningTestCase($message);
}

protected function setUp(): void
{
TestListenerTrait::$enabledPolyfills = $this->suite->getName();
}

protected function tearDown(): void
{
TestListenerTrait::$enabledPolyfills = false;
}
}
21 changes: 8 additions & 13 deletions src/Util/TestListenerForV9.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@

class TestListenerForV9 extends TestSuite implements TestListenerInterface
{
private $suite;
private $trait;

public function __construct(?TestSuite $suite = null)
{
if ($suite) {
$this->suite = $suite;
$this->setName($suite->getName().' with polyfills enabled');
$this->addTest($suite);
}
Expand All @@ -35,7 +33,13 @@ public function __construct(?TestSuite $suite = null)

public function startTestSuite(TestSuite $suite): void
{
$this->trait->startTestSuite($suite->tests()[0]);
if (null === TestListenerTrait::$enabledPolyfills) {
TestListenerTrait::$enabledPolyfills = false;
$this->trait->startTestSuite($suite);
}
if ($suite instanceof TestListener) {
TestListenerTrait::$enabledPolyfills = $suite->getName();
}
}

public function addError(Test $test, \Throwable $t, float $time): void
Expand Down Expand Up @@ -69,6 +73,7 @@ public function addSkippedTest(Test $test, \Throwable $t, float $time): void

public function endTestSuite(TestSuite $suite): void
{
TestListenerTrait::$enabledPolyfills = false;
}

public function startTest(Test $test): void
Expand All @@ -83,14 +88,4 @@ public static function warning($message): WarningTestCase
{
return new WarningTestCase($message);
}

protected function setUp(): void
{
TestListenerTrait::$enabledPolyfills = $this->suite->getName();
}

protected function tearDown(): void
{
TestListenerTrait::$enabledPolyfills = false;
}
}
18 changes: 12 additions & 6 deletions src/Util/TestListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class TestListenerTrait

public function startTestSuite($mainSuite)
{
if (null !== self::$enabledPolyfills) {
return;
}
self::$enabledPolyfills = false;
$warnings = [];

foreach ($mainSuite->tests() as $suite) {
Expand Down Expand Up @@ -109,7 +105,7 @@ public function startTestSuite($mainSuite)

function {$f['name']}{$f['signature']}
{
if ('{$testClass}' === TestListenerTrait::\$enabledPolyfills) {
if ('{$testClass} with polyfills enabled' === TestListenerTrait::\$enabledPolyfills) {
{$f['return']}{$f['args']};
}

Expand All @@ -123,6 +119,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')) {
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
// 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 +137,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 <[email protected]>
*
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
Loading
Loading