Make WordPress Core


Ignore:
Timestamp:
08/30/2021 08:18:31 PM (5 years ago)
Author:
hellofromTonya
Message:

Tests: Test custom assertions parameter data type in WP_UnitTestCase_Base.

The following changes improve tests stability.

The assertEqualFields() method expects an object and a fields array as inputs and subsequently approaches the received parameters as such, but did not verify whether the received parameters are of the expected types.

Along the same lines, the assertSameSets(), assertEqualSets(), assertSameSetsWithIndex() and the assertEqualSetsWithIndex() methods all expect arrays for both the actual as well as the expected values and uses the array function [k]sort() on both, but never verified that the received inputs were actually arrays, which could lead to PHP errors on the sorting function calls.

Follow-up to [30687], [42343], [48937], [48939], [51480], [51481].

Props jrf.
See #53363.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r51657 r51697  
    686686         */
    687687        public function assertEqualFields( $object, $fields, $message = '' ) {
     688                $this->assertIsObject( $object, $message . ' Passed $object is not an object.' );
     689                $this->assertIsArray( $fields, $message . ' Passed $fields is not an array.' );
     690                $this->assertNotEmpty( $fields, $message . ' Fields array is empty.' );
     691
    688692                foreach ( $fields as $field_name => $field_value ) {
    689693                        $this->assertObjectHasAttribute( $field_name, $object, $message . " Property $field_name does not exist on the object." );
    … …  
    761765         */
    762766        public function assertSameSets( $expected, $actual, $message = '' ) {
     767                $this->assertIsArray( $expected, $message . ' Expected value must be an array.' );
     768                $this->assertIsArray( $actual, $message . ' Value under test is not an array.' );
     769
    763770                sort( $expected );
    764771                sort( $actual );
    … …  
    777784         */
    778785        public function assertEqualSets( $expected, $actual, $message = '' ) {
     786                $this->assertIsArray( $expected, $message . ' Expected value must be an array.' );
     787                $this->assertIsArray( $actual, $message . ' Value under test is not an array.' );
     788
    779789                sort( $expected );
    780790                sort( $actual );
    … …  
    793803         */
    794804        public function assertSameSetsWithIndex( $expected, $actual, $message = '' ) {
     805                $this->assertIsArray( $expected, $message . ' Expected value must be an array.' );
     806                $this->assertIsArray( $actual, $message . ' Value under test is not an array.' );
     807
    795808                ksort( $expected );
    796809                ksort( $actual );
    … …  
    809822         */
    810823        public function assertEqualSetsWithIndex( $expected, $actual, $message = '' ) {
     824                $this->assertIsArray( $expected, $message . ' Expected value must be an array.' );
     825                $this->assertIsArray( $actual, $message . ' Value under test is not an array.' );
     826
    811827                ksort( $expected );
    812828                ksort( $actual );
Note: See TracChangeset for help on using the changeset viewer.