Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.

This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using assertSame() should generally be preferred to assertEquals() where appropriate, to make the tests more reliable.

Props johnbillion, jrf, SergeyBiryukov.
See #38266.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php

    r48357 r48937  
    1717        );
    1818        $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) );
    19         $this->assertEquals( 1.10, rest_sanitize_value_from_schema( '1.10', $schema ) );
     19        $this->assertSame( 1.10, rest_sanitize_value_from_schema( '1.10', $schema ) );
    2020        $this->assertEquals( 1, rest_sanitize_value_from_schema( '1abc', $schema ) );
    2121        $this->assertEquals( 0, rest_sanitize_value_from_schema( 'abc', $schema ) );
     
    2727            'type' => 'integer',
    2828        );
    29         $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) );
    30         $this->assertEquals( 1, rest_sanitize_value_from_schema( '1.10', $schema ) );
    31         $this->assertEquals( 1, rest_sanitize_value_from_schema( '1abc', $schema ) );
    32         $this->assertEquals( 0, rest_sanitize_value_from_schema( 'abc', $schema ) );
    33         $this->assertEquals( 0, rest_sanitize_value_from_schema( array(), $schema ) );
     29        $this->assertSame( 1, rest_sanitize_value_from_schema( 1, $schema ) );
     30        $this->assertSame( 1, rest_sanitize_value_from_schema( '1.10', $schema ) );
     31        $this->assertSame( 1, rest_sanitize_value_from_schema( '1abc', $schema ) );
     32        $this->assertSame( 0, rest_sanitize_value_from_schema( 'abc', $schema ) );
     33        $this->assertSame( 0, rest_sanitize_value_from_schema( array(), $schema ) );
    3434    }
    3535
     
    3838            'type' => 'string',
    3939        );
    40         $this->assertEquals( 'Hello', rest_sanitize_value_from_schema( 'Hello', $schema ) );
    41         $this->assertEquals( '1.10', rest_sanitize_value_from_schema( '1.10', $schema ) );
    42         $this->assertEquals( '1.1', rest_sanitize_value_from_schema( 1.1, $schema ) );
    43         $this->assertEquals( '1', rest_sanitize_value_from_schema( 1, $schema ) );
     40        $this->assertSame( 'Hello', rest_sanitize_value_from_schema( 'Hello', $schema ) );
     41        $this->assertSame( '1.10', rest_sanitize_value_from_schema( '1.10', $schema ) );
     42        $this->assertSame( '1.1', rest_sanitize_value_from_schema( 1.1, $schema ) );
     43        $this->assertSame( '1', rest_sanitize_value_from_schema( 1, $schema ) );
    4444    }
    4545
     
    4848            'type' => 'boolean',
    4949        );
    50         $this->assertEquals( true, rest_sanitize_value_from_schema( '1', $schema ) );
    51         $this->assertEquals( true, rest_sanitize_value_from_schema( 'true', $schema ) );
    52         $this->assertEquals( true, rest_sanitize_value_from_schema( '100', $schema ) );
    53         $this->assertEquals( true, rest_sanitize_value_from_schema( 1, $schema ) );
    54         $this->assertEquals( false, rest_sanitize_value_from_schema( '0', $schema ) );
    55         $this->assertEquals( false, rest_sanitize_value_from_schema( 'false', $schema ) );
    56         $this->assertEquals( false, rest_sanitize_value_from_schema( 0, $schema ) );
     50        $this->assertTrue( rest_sanitize_value_from_schema( '1', $schema ) );
     51        $this->assertTrue( rest_sanitize_value_from_schema( 'true', $schema ) );
     52        $this->assertTrue( rest_sanitize_value_from_schema( '100', $schema ) );
     53        $this->assertTrue( rest_sanitize_value_from_schema( 1, $schema ) );
     54        $this->assertFalse( rest_sanitize_value_from_schema( '0', $schema ) );
     55        $this->assertFalse( rest_sanitize_value_from_schema( 'false', $schema ) );
     56        $this->assertFalse( rest_sanitize_value_from_schema( 0, $schema ) );
    5757    }
    5858
     
    6262            'format' => 'email',
    6363        );
    64         $this->assertEquals( 'email@example.com', rest_sanitize_value_from_schema( 'email@example.com', $schema ) );
    65         $this->assertEquals( 'a@b.c', rest_sanitize_value_from_schema( 'a@b.c', $schema ) );
    66         $this->assertEquals( 'invalid', rest_sanitize_value_from_schema( 'invalid', $schema ) );
     64        $this->assertSame( 'email@example.com', rest_sanitize_value_from_schema( 'email@example.com', $schema ) );
     65        $this->assertSame( 'a@b.c', rest_sanitize_value_from_schema( 'a@b.c', $schema ) );
     66        $this->assertSame( 'invalid', rest_sanitize_value_from_schema( 'invalid', $schema ) );
    6767    }
    6868
     
    7373        );
    7474
    75         $this->assertEquals( '127.0.0.1', rest_sanitize_value_from_schema( '127.0.0.1', $schema ) );
    76         $this->assertEquals( 'hello', rest_sanitize_value_from_schema( 'hello', $schema ) );
    77         $this->assertEquals( '2001:DB8:0:0:8:800:200C:417A', rest_sanitize_value_from_schema( '2001:DB8:0:0:8:800:200C:417A', $schema ) );
     75        $this->assertSame( '127.0.0.1', rest_sanitize_value_from_schema( '127.0.0.1', $schema ) );
     76        $this->assertSame( 'hello', rest_sanitize_value_from_schema( 'hello', $schema ) );
     77        $this->assertSame( '2001:DB8:0:0:8:800:200C:417A', rest_sanitize_value_from_schema( '2001:DB8:0:0:8:800:200C:417A', $schema ) );
    7878    }
    7979
     
    8686            'format' => 'hex-color',
    8787        );
    88         $this->assertEquals( '#000000', rest_sanitize_value_from_schema( '#000000', $schema ) );
    89         $this->assertEquals( '#FFF', rest_sanitize_value_from_schema( '#FFF', $schema ) );
    90         $this->assertEquals( '', rest_sanitize_value_from_schema( 'WordPress', $schema ) );
     88        $this->assertSame( '#000000', rest_sanitize_value_from_schema( '#000000', $schema ) );
     89        $this->assertSame( '#FFF', rest_sanitize_value_from_schema( '#FFF', $schema ) );
     90        $this->assertSame( '', rest_sanitize_value_from_schema( 'WordPress', $schema ) );
    9191    }
    9292
     
    9999            'format' => 'uuid',
    100100        );
    101         $this->assertEquals( '44', rest_sanitize_value_from_schema( 44, $schema ) );
    102         $this->assertEquals( 'hello', rest_sanitize_value_from_schema( 'hello', $schema ) );
    103         $this->assertEquals(
     101        $this->assertSame( '44', rest_sanitize_value_from_schema( 44, $schema ) );
     102        $this->assertSame( 'hello', rest_sanitize_value_from_schema( 'hello', $schema ) );
     103        $this->assertSame(
    104104            '123e4567-e89b-12d3-a456-426655440000',
    105105            rest_sanitize_value_from_schema( '123e4567-e89b-12d3-a456-426655440000', $schema )
     
    152152            ),
    153153        );
    154         $this->assertEquals( array( 'ribs', 'brisket' ), rest_sanitize_value_from_schema( array( 'ribs', 'brisket' ), $schema ) );
    155         $this->assertEquals( array( 'coleslaw' ), rest_sanitize_value_from_schema( array( 'coleslaw' ), $schema ) );
     154        $this->assertSame( array( 'ribs', 'brisket' ), rest_sanitize_value_from_schema( array( 'ribs', 'brisket' ), $schema ) );
     155        $this->assertSame( array( 'coleslaw' ), rest_sanitize_value_from_schema( array( 'coleslaw' ), $schema ) );
    156156    }
    157157
     
    164164            ),
    165165        );
    166         $this->assertEquals( array( 'ribs', 'chicken' ), rest_sanitize_value_from_schema( 'ribs,chicken', $schema ) );
    167         $this->assertEquals( array( 'chicken', 'coleslaw' ), rest_sanitize_value_from_schema( 'chicken,coleslaw', $schema ) );
    168         $this->assertEquals( array( 'chicken', 'coleslaw' ), rest_sanitize_value_from_schema( 'chicken,coleslaw,', $schema ) );
     166        $this->assertSame( array( 'ribs', 'chicken' ), rest_sanitize_value_from_schema( 'ribs,chicken', $schema ) );
     167        $this->assertSame( array( 'chicken', 'coleslaw' ), rest_sanitize_value_from_schema( 'chicken,coleslaw', $schema ) );
     168        $this->assertSame( array( 'chicken', 'coleslaw' ), rest_sanitize_value_from_schema( 'chicken,coleslaw,', $schema ) );
    169169    }
    170170
     
    176176            ),
    177177        );
    178         $this->assertEquals(
     178        $this->assertSame(
    179179            array( '1', '2' ),
    180180            rest_sanitize_value_from_schema(
     
    290290            )
    291291        );
    292         $this->assertEquals( array( 'a' => array() ), rest_sanitize_value_from_schema( array( 'a' => null ), $schema ) );
     292        $this->assertSame( array( 'a' => array() ), rest_sanitize_value_from_schema( array( 'a' => null ), $schema ) );
    293293    }
    294294
     
    309309     */
    310310    public function test_type_object_accepts_empty_string() {
    311         $this->assertEquals( array(), rest_sanitize_value_from_schema( '', array( 'type' => 'object' ) ) );
     311        $this->assertSame( array(), rest_sanitize_value_from_schema( '', array( 'type' => 'object' ) ) );
    312312    }
    313313
     
    318318            'type' => 'lalala',
    319319        );
    320         $this->assertEquals( 'Best lyrics', rest_sanitize_value_from_schema( 'Best lyrics', $schema ) );
    321         $this->assertEquals( 1.10, rest_sanitize_value_from_schema( 1.10, $schema ) );
    322         $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) );
     320        $this->assertSame( 'Best lyrics', rest_sanitize_value_from_schema( 'Best lyrics', $schema ) );
     321        $this->assertSame( 1.10, rest_sanitize_value_from_schema( 1.10, $schema ) );
     322        $this->assertSame( 1, rest_sanitize_value_from_schema( 1, $schema ) );
    323323    }
    324324
     
    329329            'type' => null,
    330330        );
    331         $this->assertEquals( 'Nothing', rest_sanitize_value_from_schema( 'Nothing', $schema ) );
    332         $this->assertEquals( 1.10, rest_sanitize_value_from_schema( 1.10, $schema ) );
    333         $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) );
     331        $this->assertSame( 'Nothing', rest_sanitize_value_from_schema( 'Nothing', $schema ) );
     332        $this->assertSame( 1.10, rest_sanitize_value_from_schema( 1.10, $schema ) );
     333        $this->assertSame( 1, rest_sanitize_value_from_schema( 1, $schema ) );
    334334    }
    335335
     
    341341
    342342        $this->assertNull( rest_sanitize_value_from_schema( null, $schema ) );
    343         $this->assertEquals( '2019-09-19T18:00:00', rest_sanitize_value_from_schema( '2019-09-19T18:00:00', $schema ) );
    344         $this->assertEquals( 'lalala', rest_sanitize_value_from_schema( 'lalala', $schema ) );
     343        $this->assertSame( '2019-09-19T18:00:00', rest_sanitize_value_from_schema( '2019-09-19T18:00:00', $schema ) );
     344        $this->assertSame( 'lalala', rest_sanitize_value_from_schema( 'lalala', $schema ) );
    345345    }
    346346
     
    353353            'format' => 'hex-color',
    354354        );
    355         $this->assertEquals( array( '#fff' ), rest_sanitize_value_from_schema( '#fff', $schema ) );
    356         $this->assertEquals( array( '#qrst' ), rest_sanitize_value_from_schema( '#qrst', $schema ) );
     355        $this->assertSame( array( '#fff' ), rest_sanitize_value_from_schema( '#fff', $schema ) );
     356        $this->assertSame( array( '#qrst' ), rest_sanitize_value_from_schema( '#qrst', $schema ) );
    357357    }
    358358
     
    365365
    366366        $schema = array( 'format' => 'hex-color' );
    367         $this->assertEquals( '#abc', rest_sanitize_value_from_schema( '#abc', $schema ) );
    368         $this->assertEquals( '', rest_sanitize_value_from_schema( '#jkl', $schema ) );
     367        $this->assertSame( '#abc', rest_sanitize_value_from_schema( '#abc', $schema ) );
     368        $this->assertSame( '', rest_sanitize_value_from_schema( '#jkl', $schema ) );
    369369    }
    370370
     
    379379            'type'   => 'str',
    380380        );
    381         $this->assertEquals( '#abc', rest_sanitize_value_from_schema( '#abc', $schema ) );
    382         $this->assertEquals( '', rest_sanitize_value_from_schema( '#jkl', $schema ) );
     381        $this->assertSame( '#abc', rest_sanitize_value_from_schema( '#abc', $schema ) );
     382        $this->assertSame( '', rest_sanitize_value_from_schema( '#jkl', $schema ) );
    383383    }
    384384
     
    393393        );
    394394
    395         $this->assertEquals( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
    396         $this->assertEquals( array( 'raw' => 'My Value' ), rest_sanitize_value_from_schema( array( 'raw' => 'My Value' ), $schema ) );
    397         $this->assertEquals( array( 'raw' => '1' ), rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
     395        $this->assertSame( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
     396        $this->assertSame( array( 'raw' => 'My Value' ), rest_sanitize_value_from_schema( array( 'raw' => 'My Value' ), $schema ) );
     397        $this->assertSame( array( 'raw' => '1' ), rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
    398398    }
    399399
     
    416416        $this->assertFalse( rest_sanitize_value_from_schema( 0, $schema ) );
    417417
    418         $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => true ), $schema ) );
    419         $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => '1' ), $schema ) );
    420         $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
    421 
    422         $this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => false ), $schema ) );
    423         $this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => '0' ), $schema ) );
    424         $this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => 0 ), $schema ) );
    425 
    426         $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => 'something non boolean' ), $schema ) );
     418        $this->assertSame( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => true ), $schema ) );
     419        $this->assertSame( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => '1' ), $schema ) );
     420        $this->assertSame( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
     421
     422        $this->assertSame( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => false ), $schema ) );
     423        $this->assertSame( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => '0' ), $schema ) );
     424        $this->assertSame( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => 0 ), $schema ) );
     425
     426        $this->assertSame( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => 'something non boolean' ), $schema ) );
    427427    }
    428428
     
    438438        );
    439439
    440         $this->assertEquals( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
     440        $this->assertSame( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
    441441    }
    442442
     
    452452        );
    453453
    454         $this->assertEquals( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
     454        $this->assertSame( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
    455455    }
    456456
Note: See TracChangeset for help on using the changeset viewer.