From 952e98c217a36a2c21129792e83f0e5e15d870b1 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 4 Oct 2017 13:26:15 +0000 Subject: [PATCH] I18N: Improvements to the tests for plural forms. * Move the `create_function()` code into a file that's only loaded, and into a test that's only run, on PHP <= 7.2 to avoid deprecated warnings in 7.2+. * Convert the test skipping into a failure if the GlotPress locale file cannot be downloaded. * Ensure `test_exceptions` fails if an exception is not thrown. * Docs improvements See #41562, #40109 git-svn-id: https://develop.svn.wordpress.org/trunk@41730 602fd350-edb4-49c9-b593-d223f7449a82 --- .../phpunit/includes/plural-form-function.php | 16 ++++++ tests/phpunit/tests/pomo/pluralForms.php | 49 +++++++++++++------ 2 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 tests/phpunit/includes/plural-form-function.php diff --git a/tests/phpunit/includes/plural-form-function.php b/tests/phpunit/includes/plural-form-function.php new file mode 100644 index 0000000000..c09558b1ff --- /dev/null +++ b/tests/phpunit/includes/plural-form-function.php @@ -0,0 +1,16 @@ +assertNotEmpty( $locales, 'Unable to retrieve GP_Locales file' ); + } + public static function locales_provider() { if ( ! class_exists( 'GP_Locales' ) ) { $filename = download_url( 'https://raw.githubusercontent.com/GlotPress/GlotPress-WP/develop/locales/locales.php' ); if ( is_wp_error( $filename ) ) { - self::markTestSkipped( 'Unable to retrieve GP_Locales file' ); + return array(); } require_once $filename; } @@ -73,12 +69,19 @@ class PluralFormsTest extends WP_UnitTestCase { } /** + * @ticket 41562 * @dataProvider locales_provider * @group external-http */ public function test_regression( $lang, $nplurals, $expression ) { + if ( version_compare( phpversion(), '7.2', '>=' ) ) { + $this->markTestSkipped( 'Lambda functions are deprecated in PHP 7.2' ); + } + + require_once dirname( dirname( dirname( __FILE__ ) ) ) . '/includes/plural-form-function.php'; + $parenthesized = self::parenthesize_plural_expression( $expression ); - $old_style = self::make_plural_form_function( $nplurals, $parenthesized ); + $old_style = tests_make_plural_form_function( $nplurals, $parenthesized ); $pluralForms = new Plural_Forms( $expression ); $generated_old = array(); @@ -144,6 +147,7 @@ class PluralFormsTest extends WP_UnitTestCase { } /** + * @ticket 41562 * @dataProvider simple_provider */ public function test_simple( $expression, $expected ) { @@ -197,6 +201,13 @@ class PluralFormsTest extends WP_UnitTestCase { } /** + * Ensures that an exception is thrown when an invalid plural form is encountered. + * + * The `@expectedException Exception` notation for PHPUnit cannot be used because expecting an + * exception of type `Exception` is not supported before PHPUnit 3.7. The CI tests for PHP 5.2 + * run on PHPUnit 3.6. + * + * @ticket 41562 * @dataProvider data_exceptions */ public function test_exceptions( $expression, $expected_exception, $call_get ) { @@ -207,9 +218,15 @@ class PluralFormsTest extends WP_UnitTestCase { } } catch ( Exception $e ) { $this->assertEquals( $expected_exception, $e->getMessage() ); + return; } + + $this->fail( 'Expected exception was not thrown.' ); } + /** + * @ticket 41562 + */ public function test_cache() { $mock = $this->getMockBuilder( 'Plural_Forms' ) ->setMethods(array('execute'))