Make WordPress Core

Changeset 41730


Ignore:
Timestamp:
10/04/2017 01:26:15 PM (9 years ago)
Author:
johnbillion
Message:

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

Location:
trunk/tests/phpunit
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/pomo/pluralForms.php

    r41725 r41730  
    55 */
    66class PluralFormsTest extends WP_UnitTestCase {
    7         /**
    8          * Legacy plural form function.
    9          *
    10          * @param int $nplurals
    11          * @param string $expression
    12          */
    13         protected static function make_plural_form_function($nplurals, $expression) {
    14                 $expression = str_replace('n', '$n', $expression);
    15                 $func_body = "
    16                         \$index = (int)($expression);
    17                         return (\$index < $nplurals)? \$index : $nplurals - 1;";
    18                 return create_function('$n', $func_body);
    19         }
    20 
    217        /**
    228         * Parenthesize plural expression.
     
    5238        }
    5339
     40        /**
     41         * @ticket 41562
     42         * @group external-http
     43         */
     44        public function test_locales_provider() {
     45                $locales = self::locales_provider();
     46
     47                $this->assertNotEmpty( $locales, 'Unable to retrieve GP_Locales file' );
     48        }
     49
    5450        public static function locales_provider() {
    5551                if ( ! class_exists( 'GP_Locales' ) ) {
    5652                        $filename = download_url( 'https://raw.githubusercontent.com/GlotPress/GlotPress-WP/develop/locales/locales.php' );
    5753                        if ( is_wp_error( $filename ) ) {
    58                                 self::markTestSkipped( 'Unable to retrieve GP_Locales file' );
     54                                return array();
    5955                        }
    6056                        require_once $filename;                 
     
    7470
    7571        /**
     72         * @ticket 41562
    7673         * @dataProvider locales_provider
    7774         * @group external-http
    7875         */
    7976        public function test_regression( $lang, $nplurals, $expression ) {
     77                if ( version_compare( phpversion(), '7.2', '>=' ) ) {
     78                        $this->markTestSkipped( 'Lambda functions are deprecated in PHP 7.2' );
     79                }
     80
     81                require_once dirname( dirname( dirname( __FILE__ ) ) ) . '/includes/plural-form-function.php';
     82
    8083                $parenthesized = self::parenthesize_plural_expression( $expression );
    81                 $old_style = self::make_plural_form_function( $nplurals, $parenthesized );
     84                $old_style = tests_make_plural_form_function( $nplurals, $parenthesized );
    8285                $pluralForms = new Plural_Forms( $expression );
    8386
     
    145148
    146149        /**
     150         * @ticket 41562
    147151         * @dataProvider simple_provider
    148152         */
     
    198202
    199203        /**
     204         * Ensures that an exception is thrown when an invalid plural form is encountered.
     205         *
     206         * The `@expectedException Exception` notation for PHPUnit cannot be used because expecting an
     207         * exception of type `Exception` is not supported before PHPUnit 3.7. The CI tests for PHP 5.2
     208         * run on PHPUnit 3.6.
     209         *
     210         * @ticket 41562
    200211         * @dataProvider data_exceptions
    201212         */
     
    208219                } catch ( Exception $e ) {
    209220                        $this->assertEquals( $expected_exception, $e->getMessage() );
    210                 }
    211         }
    212 
     221                        return;
     222                }
     223
     224                $this->fail( 'Expected exception was not thrown.' );
     225        }
     226
     227        /**
     228         * @ticket 41562
     229         */
    213230        public function test_cache() {
    214231                $mock = $this->getMockBuilder( 'Plural_Forms' )
Note: See TracChangeset for help on using the changeset viewer.