Skip to content

Commit 90bf8a5

Browse files
committed
Add can_use_icu(). Make phpunit6 compat. stty back.
1 parent 5591e23 commit 90bf8a5

File tree

6 files changed

+63
-19
lines changed

6 files changed

+63
-19
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ php:
55
- 5.4
66
- 5.5
77
- 5.6
8+
- 7.0
9+
- 7.1
810

911
before_script:
1012
- php -m
11-
- php --info | grep -i 'intl\|pcre'
13+
- php --info | grep -i 'intl\|icu\|pcre'
1214

13-
script: phpunit
15+
script: phpunit --debug
1416

1517
notifications:
1618
email:

lib/cli/Shell.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ static public function columns() {
5050
}
5151
} else {
5252
if ( ! ( $columns = (int) getenv( 'COLUMNS' ) ) ) {
53-
if ( getenv( 'TERM' ) ) {
54-
$size = exec( '/usr/bin/env stty size 2>/dev/null' );
55-
if ( '' !== $size && preg_match( '/[0-9]+ ([0-9]+)/', $size, $matches ) ) {
56-
$columns = (int) $matches[1];
57-
}
58-
if ( ! $columns ) {
53+
$size = exec( '/usr/bin/env stty size 2>/dev/null' );
54+
if ( '' !== $size && preg_match( '/[0-9]+ ([0-9]+)/', $size, $matches ) ) {
55+
$columns = (int) $matches[1];
56+
}
57+
if ( ! $columns ) {
58+
if ( getenv( 'TERM' ) ) {
5959
$columns = (int) exec( '/usr/bin/env tput cols 2>/dev/null' );
6060
}
6161
}

lib/cli/cli.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function safe_strlen( $str, $encoding = false ) {
165165
$test_safe_strlen = getenv( 'PHP_CLI_TOOLS_TEST_SAFE_STRLEN' );
166166

167167
// Assume UTF-8 if no encoding given - `grapheme_strlen()` will return null if given non-UTF-8 string.
168-
if ( ( ! $encoding || 'UTF-8' === $encoding ) && function_exists( 'grapheme_strlen' ) && null !== ( $length = grapheme_strlen( $str ) ) ) {
168+
if ( ( ! $encoding || 'UTF-8' === $encoding ) && can_use_icu() && null !== ( $length = grapheme_strlen( $str ) ) ) {
169169
if ( ! $test_safe_strlen || ( $test_safe_strlen & 1 ) ) {
170170
return $length;
171171
}
@@ -220,7 +220,7 @@ function safe_substr( $str, $start, $length = false, $is_width = false, $encodin
220220
$test_safe_substr = getenv( 'PHP_CLI_TOOLS_TEST_SAFE_SUBSTR' );
221221

222222
// Assume UTF-8 if no encoding given - `grapheme_substr()` will return false (not null like `grapheme_strlen()`) if given non-UTF-8 string.
223-
if ( ( ! $encoding || 'UTF-8' === $encoding ) && function_exists( 'grapheme_substr' ) && false !== ( $try = grapheme_substr( $str, $start, $length ) ) ) {
223+
if ( ( ! $encoding || 'UTF-8' === $encoding ) && can_use_icu() && false !== ( $try = grapheme_substr( $str, $start, $length ) ) ) {
224224
if ( ! $test_safe_substr || ( $test_safe_substr & 1 ) ) {
225225
return $is_width ? _safe_substr_eaw( $try, $length ) : $try;
226226
}
@@ -328,7 +328,7 @@ function strwidth( $string, $encoding = false ) {
328328
$test_strwidth = getenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH' );
329329

330330
// Assume UTF-8 if no encoding given - `grapheme_strlen()` will return null if given non-UTF-8 string.
331-
if ( ( ! $encoding || 'UTF-8' === $encoding ) && function_exists( 'grapheme_strlen' ) && null !== ( $width = grapheme_strlen( $string ) ) ) {
331+
if ( ( ! $encoding || 'UTF-8' === $encoding ) && can_use_icu() && null !== ( $width = grapheme_strlen( $string ) ) ) {
332332
if ( ! $test_strwidth || ( $test_strwidth & 1 ) ) {
333333
return $width + preg_match_all( $eaw_regex, $string, $dummy /*needed for PHP 5.3*/ );
334334
}
@@ -356,6 +356,22 @@ function strwidth( $string, $encoding = false ) {
356356
return safe_strlen( $string, $encoding );
357357
}
358358

359+
/**
360+
* Returns whether ICU is modern enough not to flake out.
361+
*
362+
* @return bool
363+
*/
364+
function can_use_icu() {
365+
static $can_use_icu = null;
366+
367+
if ( null === $can_use_icu ) {
368+
// Choosing ICU 54, Unicode 7.0.
369+
$can_use_icu = defined( 'INTL_ICU_VERSION' ) && version_compare( INTL_ICU_VERSION, '54.1', '>=' ) && function_exists( 'grapheme_strlen' ) && function_exists( 'grapheme_substr' );
370+
}
371+
372+
return $can_use_icu;
373+
}
374+
359375
/**
360376
* Returns whether PCRE Unicode extended grapheme cluster '\X' is available for use.
361377
*
@@ -367,8 +383,8 @@ function can_use_pcre_x() {
367383
if ( null === $can_use_pcre_x ) {
368384
// '\X' introduced (as Unicde extended grapheme cluster) in PCRE 8.32 - see https://vcs.pcre.org/pcre/code/tags/pcre-8.32/ChangeLog?view=markup line 53.
369385
// Older versions of PCRE were bundled with PHP <= 5.3.23 & <= 5.4.13.
370-
$unfc_pcre_version = substr( PCRE_VERSION, 0, strspn( PCRE_VERSION, '0123456789.' ) ); // Remove any trailing date stuff.
371-
$can_use_pcre_x = version_compare( $unfc_pcre_version, '8.32', '>=' ) && false !== @preg_match( '/\X/u', '' );
386+
$pcre_version = substr( PCRE_VERSION, 0, strspn( PCRE_VERSION, '0123456789.' ) ); // Remove any trailing date stuff.
387+
$can_use_pcre_x = version_compare( $pcre_version, '8.32', '>=' ) && false !== @preg_match( '/\X/u', '' );
372388
}
373389

374390
return $can_use_pcre_x;

tests/bootstrap.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
require dirname( dirname( __FILE__ ) ) . '/lib/cli/cli.php';
44

5+
/**
6+
* Compatibility with PHPUnit 6+
7+
*/
8+
if ( class_exists( 'PHPUnit\Runner\Version' ) ) {
9+
require_once dirname( __FILE__ ) . '/phpunit6-compat.php';
10+
}
11+
512
function cli_autoload( $className ) {
613
$className = ltrim($className, '\\');
714
$fileName = '';
@@ -20,4 +27,4 @@ function cli_autoload( $className ) {
2027
require dirname( dirname( __FILE__ ) ) . '/lib/' . $fileName;
2128
}
2229

23-
spl_autoload_register( 'cli_autoload' );
30+
spl_autoload_register( 'cli_autoload' );

tests/phpunit6-compat.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
// From core "tests/phpunit/includes/phpunit6-compat.php" without `getTickets()` (see https://core.trac.wordpress.org/ticket/39822).
3+
4+
if ( class_exists( 'PHPUnit\Runner\Version' ) && version_compare( PHPUnit\Runner\Version::id(), '6.0', '>=' ) ) {
5+
6+
class_alias( 'PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase' );
7+
class_alias( 'PHPUnit\Framework\Exception', 'PHPUnit_Framework_Exception' );
8+
class_alias( 'PHPUnit\Framework\ExpectationFailedException', 'PHPUnit_Framework_ExpectationFailedException' );
9+
class_alias( 'PHPUnit\Framework\Error\Notice', 'PHPUnit_Framework_Error_Notice' );
10+
class_alias( 'PHPUnit\Framework\Error\Warning', 'PHPUnit_Framework_Error_Warning' );
11+
class_alias( 'PHPUnit\Framework\Test', 'PHPUnit_Framework_Test' );
12+
class_alias( 'PHPUnit\Framework\Warning', 'PHPUnit_Framework_Warning' );
13+
class_alias( 'PHPUnit\Framework\AssertionFailedError', 'PHPUnit_Framework_AssertionFailedError' );
14+
class_alias( 'PHPUnit\Framework\TestSuite', 'PHPUnit_Framework_TestSuite' );
15+
class_alias( 'PHPUnit\Framework\TestListener', 'PHPUnit_Framework_TestListener' );
16+
class_alias( 'PHPUnit\Util\GlobalState', 'PHPUnit_Util_GlobalState' );
17+
class_alias( 'PHPUnit\Util\Getopt', 'PHPUnit_Util_Getopt' );
18+
19+
}

tests/test-cli.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function test_various_substr() {
104104
// Latin, kana, Latin, Latin combining, Thai combining, Hangul.
105105
$str = 'lムnöม้p를';
106106

107-
if ( function_exists( 'grapheme_substr' ) ) {
107+
if ( \cli\can_use_icu() ) {
108108
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_SUBSTR=1' ); // Tests grapheme_substr().
109109
$this->assertSame( '', \cli\safe_substr( $str, 0, 0 ) );
110110
$this->assertSame( 'l', \cli\safe_substr( $str, 0, 1 ) );
@@ -381,7 +381,7 @@ function test_strwidth() {
381381
// 4 characters, one a double-width Han = 5 spacing chars, with 2 combining chars. Adapted from http://unicode.org/faq/char_combmark.html#7 (combining acute accent added after "a").
382382
$str = "a\xCC\x81\xE0\xA4\xA8\xE0\xA4\xBF\xE4\xBA\x9C\xF0\x90\x82\x83";
383383

384-
if ( function_exists( 'grapheme_strlen' ) ) {
384+
if ( \cli\can_use_icu() ) {
385385
$this->assertSame( 5, \cli\strwidth( $str ) ); // Tests grapheme_strlen().
386386
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH=2' ); // Test preg_match_all( '/\X/u' ).
387387
$this->assertSame( 5, \cli\strwidth( $str ) );
@@ -396,7 +396,7 @@ function test_strwidth() {
396396
}
397397

398398
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH=8' ); // Test safe_strlen().
399-
if ( function_exists( 'grapheme_strlen' ) || \cli\can_use_pcre_x() ) {
399+
if ( \cli\can_use_icu() || \cli\can_use_pcre_x() ) {
400400
$this->assertSame( 4, \cli\strwidth( $str ) ); // safe_strlen() (correctly) does not account for double-width Han so out by 1.
401401
} elseif ( function_exists( 'mb_strlen' ) && function_exists( 'mb_detect_order' ) ) {
402402
$this->assertSame( 4, \cli\strwidth( $str ) ); // safe_strlen() (correctly) does not account for double-width Han so out by 1.
@@ -411,7 +411,7 @@ function test_strwidth() {
411411

412412
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH' );
413413

414-
if ( function_exists( 'grapheme_strlen' ) ) {
414+
if ( \cli\can_use_icu() ) {
415415
$this->assertSame( 11, \cli\strwidth( $str ) ); // Tests grapheme_strlen().
416416
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH=2' ); // Test preg_match_all( '/\X/u' ).
417417
$this->assertSame( 11, \cli\strwidth( $str ) );
@@ -473,7 +473,7 @@ function test_safe_strlen() {
473473
// ASCII l, 3-byte kana, ASCII n, ASCII o + 2-byte combining umlaut, 6-byte Thai combining, ASCII, 3-byte Hangul. grapheme length 7, bytes 18.
474474
$str = 'lムnöม้p를';
475475

476-
if ( function_exists( 'grapheme_strlen' ) ) {
476+
if ( \cli\can_use_icu() ) {
477477
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_STRLEN' ); // Test grapheme_strlen().
478478
$this->assertSame( 7, \cli\safe_strlen( $str ) );
479479
if ( \cli\can_use_pcre_x() ) {

0 commit comments

Comments
 (0)