Make WordPress Core

Changeset 47452


Ignore:
Timestamp:
03/13/2020 08:39:02 PM (7 years ago)
Author:
SergeyBiryukov
Message:

Tests: Extract is_serialized() test cases into data providers; reuse them for maybe_serialize() and maybe_unserialize() tests.

Props pbearne, mikeschroder, SergeyBiryukov.
See #36416.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions.php

    r47122 r47452  
    139139                $this->assertEquals( $expected, wp_normalize_path( $path ) );
    140140        }
     141
    141142        function data_wp_normalize_path() {
    142143                return array(
    … …  
    221222        }
    222223
    223         function test_is_serialized() {
    224                 $cases = array(
    225                         serialize( null ),
    226                         serialize( true ),
    227                         serialize( false ),
    228                         serialize( -25 ),
    229                         serialize( 25 ),
    230                         serialize( 1.1 ),
    231                         serialize( 'this string will be serialized' ),
    232                         serialize( "a\nb" ),
    233                         serialize( array() ),
    234                         serialize( array( 1, 1, 2, 3, 5, 8, 13 ) ),
    235                         serialize(
     224        /**
     225         * @dataProvider data_is_not_serialized
     226         */
     227        function test_maybe_serialize( $value ) {
     228                if ( is_array( $value ) || is_object( $value ) ) {
     229                        $expected = serialize( $value );
     230                } else {
     231                        $expected = $value;
     232                }
     233
     234                $this->assertSame( $expected, maybe_serialize( $value ) );
     235        }
     236
     237        /**
     238         * @dataProvider data_is_serialized
     239         */
     240        function test_maybe_serialize_with_double_serialization( $value ) {
     241                $expected = serialize( $value );
     242
     243                $this->assertSame( $expected, maybe_serialize( $value ) );
     244        }
     245
     246        /**
     247         * @dataProvider data_is_serialized
     248         * @dataProvider data_is_not_serialized
     249         */
     250        function test_maybe_unserialize( $value, $is_serialized ) {
     251                if ( $is_serialized ) {
     252                        $expected = unserialize( trim( $value ) );
     253                } else {
     254                        $expected = $value;
     255                }
     256
     257                if ( is_object( $expected ) ) {
     258                        $this->assertEquals( $expected, maybe_unserialize( $value ) );
     259                } else {
     260                        $this->assertSame( $expected, maybe_unserialize( $value ) );
     261                }
     262        }
     263
     264        /**
     265         * @dataProvider data_is_serialized
     266         * @dataProvider data_is_not_serialized
     267         */
     268        function test_is_serialized( $value, $expected ) {
     269                $this->assertSame( $expected, is_serialized( $value ) );
     270        }
     271
     272        function data_is_serialized() {
     273                return array(
     274                        array( serialize( null ), true ),
     275                        array( serialize( true ), true ),
     276                        array( serialize( false ), true ),
     277                        array( serialize( -25 ), true ),
     278                        array( serialize( 25 ), true ),
     279                        array( serialize( 1.1 ), true ),
     280                        array( serialize( 'this string will be serialized' ), true ),
     281                        array( serialize( "a\nb" ), true ),
     282                        array( serialize( array() ), true ),
     283                        array( serialize( array( 1, 1, 2, 3, 5, 8, 13 ) ), true ),
     284                        array(
     285                                serialize(
     286                                        (object) array(
     287                                                'test' => true,
     288                                                '3',
     289                                                4,
     290                                        )
     291                                ),
     292                                true,
     293                        ),
     294                );
     295        }
     296
     297        function data_is_not_serialized() {
     298                return array(
     299                        array( null, false ),
     300                        array( true, false ),
     301                        array( false, false ),
     302                        array( -25, false ),
     303                        array( 25, false ),
     304                        array( 1.1, false ),
     305                        array( 'this string will be serialized', false ),
     306                        array( "a\nb", false ),
     307                        array( array(), false ),
     308                        array( array( 1, 1, 2, 3, 5, 8, 13 ), false ),
     309                        array(
    236310                                (object) array(
    237311                                        'test' => true,
    238312                                        '3',
    239313                                        4,
    240                                 )
    241                         ),
    242                 );
    243 
    244                 foreach ( $cases as $case ) {
    245                         $this->assertTrue( is_serialized( $case ), "Serialized data: $case" );
    246                 }
    247 
    248                 $not_serialized = array(
    249                         'a string',
    250                         'garbage:a:0:garbage;',
    251                         's:4:test;',
    252                 );
    253 
    254                 foreach ( $not_serialized as $case ) {
    255                         $this->assertFalse( is_serialized( $case ), "Test data: $case" );
    256                 }
     314                                ),
     315                                false,
     316                        ),
     317                        array( 'a string', false ),
     318                        array( 'garbage:a:0:garbage;', false ),
     319                        array( 's:4:test;', false ),
     320                );
    257321        }
    258322
    259323        /**
    260324         * @ticket 46570
    261          */
    262         function test_is_serialized_should_return_true_for_large_floats() {
    263                 $cases = array(
    264                         serialize( 1.7976931348623157E+308 ),
    265                         serialize( array( 1.7976931348623157E+308, 1.23e50 ) ),
    266                 );
    267 
    268                 foreach ( $cases as $case ) {
    269                         $this->assertTrue( is_serialized( $case ), "Serialized data: $case" );
    270                 }
     325         * @dataProvider data_is_serialized_should_return_true_for_large_floats
     326         */
     327        function test_is_serialized_should_return_true_for_large_floats( $value ) {
     328                $this->assertTrue( is_serialized( $value ) );
     329        }
     330
     331        function data_is_serialized_should_return_true_for_large_floats() {
     332                return array(
     333                        array( serialize( 1.7976931348623157E+308 ) ),
     334                        array( serialize( array( 1.7976931348623157E+308, 1.23e50 ) ) ),
     335                );
    271336        }
    272337
    … …  
    277342                $this->assertFalse( is_serialized( 'C:16:"Serialized_Class":6:{a:0:{}}' ) );
    278343        }
    279 
    280344
    281345        /**
Note: See TracChangeset for help on using the changeset viewer.