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/mail.php

    r48645 r48937  
    8181
    8282        // We need some better assertions here but these catch the failure for now.
    83         $this->assertEqualsIgnoreEOL( $body, $mailer->get_sent()->body );
     83        $this->assertSameIgnoreEOL( $body, $mailer->get_sent()->body );
    8484        $this->assertTrue( strpos( iconv_mime_decode_headers( ( $mailer->get_sent()->header ) )['Content-Type'][0], 'boundary="----=_Part_4892_25692638.1192452070893"' ) > 0 );
    8585        $this->assertTrue( strpos( $mailer->get_sent()->header, 'charset=' ) > 0 );
     
    107107        // Retrieve the mailer instance.
    108108        $mailer = tests_retrieve_phpmailer_instance();
    109         $this->assertEquals( 'address@tld.com', $mailer->get_recipient( 'to' )->address );
    110         $this->assertEquals( 'Name', $mailer->get_recipient( 'to' )->name );
    111         $this->assertEquals( 'cc@cc.com', $mailer->get_recipient( 'cc' )->address );
    112         $this->assertEquals( 'The Carbon Guy', $mailer->get_recipient( 'cc' )->name );
    113         $this->assertEquals( 'bcc@bcc.com', $mailer->get_recipient( 'bcc' )->address );
    114         $this->assertEquals( 'The Blind Carbon Guy', $mailer->get_recipient( 'bcc' )->name );
    115         $this->assertEqualsIgnoreEOL( $message . "\n", $mailer->get_sent()->body );
     109        $this->assertSame( 'address@tld.com', $mailer->get_recipient( 'to' )->address );
     110        $this->assertSame( 'Name', $mailer->get_recipient( 'to' )->name );
     111        $this->assertSame( 'cc@cc.com', $mailer->get_recipient( 'cc' )->address );
     112        $this->assertSame( 'The Carbon Guy', $mailer->get_recipient( 'cc' )->name );
     113        $this->assertSame( 'bcc@bcc.com', $mailer->get_recipient( 'bcc' )->address );
     114        $this->assertSame( 'The Blind Carbon Guy', $mailer->get_recipient( 'bcc' )->name );
     115        $this->assertSameIgnoreEOL( $message . "\n", $mailer->get_sent()->body );
    116116    }
    117117
     
    129129        // Earlier versions of PHPMailer were not touchy about the formatting of these arguments.
    130130        $mailer = tests_retrieve_phpmailer_instance();
    131         $this->assertEquals( 'address@tld.com', $mailer->get_recipient( 'to' )->address );
    132         $this->assertEquals( 'Name', $mailer->get_recipient( 'to' )->name );
    133         $this->assertEquals( 'another_address@different-tld.com', $mailer->get_recipient( 'to', 0, 1 )->address );
    134         $this->assertEquals( 'Another Name', $mailer->get_recipient( 'to', 0, 1 )->name );
    135         $this->assertEqualsIgnoreEOL( $message . "\n", $mailer->get_sent()->body );
     131        $this->assertSame( 'address@tld.com', $mailer->get_recipient( 'to' )->address );
     132        $this->assertSame( 'Name', $mailer->get_recipient( 'to' )->name );
     133        $this->assertSame( 'another_address@different-tld.com', $mailer->get_recipient( 'to', 0, 1 )->address );
     134        $this->assertSame( 'Another Name', $mailer->get_recipient( 'to', 0, 1 )->name );
     135        $this->assertSameIgnoreEOL( $message . "\n", $mailer->get_sent()->body );
    136136    }
    137137
     
    144144
    145145        $mailer = tests_retrieve_phpmailer_instance();
    146         $this->assertEquals( 'address@tld.com', $mailer->get_recipient( 'to' )->address );
    147         $this->assertEquals( 'another_address@different-tld.com', $mailer->get_recipient( 'to', 0, 1 )->address );
    148         $this->assertEqualsIgnoreEOL( $message . "\n", $mailer->get_sent()->body );
     146        $this->assertSame( 'address@tld.com', $mailer->get_recipient( 'to' )->address );
     147        $this->assertSame( 'another_address@different-tld.com', $mailer->get_recipient( 'to', 0, 1 )->address );
     148        $this->assertSameIgnoreEOL( $message . "\n", $mailer->get_sent()->body );
    149149    }
    150150
     
    160160
    161161        $mailer = tests_retrieve_phpmailer_instance();
    162         $this->assertEquals( 'address@tld.com', $mailer->get_recipient( 'to' )->address );
    163         $this->assertEqualsIgnoreEOL( $message . "\n", $mailer->get_sent()->body );
     162        $this->assertSame( 'address@tld.com', $mailer->get_recipient( 'to' )->address );
     163        $this->assertSameIgnoreEOL( $message . "\n", $mailer->get_sent()->body );
    164164    }
    165165
     
    287287
    288288        $mailer = tests_retrieve_phpmailer_instance();
    289         $this->assertEquals( 1, substr_count( $mailer->get_sent()->header, $expected ) );
     289        $this->assertSame( 1, substr_count( $mailer->get_sent()->header, $expected ) );
    290290    }
    291291
     
    355355        foreach ( $headers as $header => $value ) {
    356356            $target_headers = preg_grep( "/^$header:/", $sent_headers );
    357             $this->assertEquals( $expected[ $header ], array_pop( $target_headers ) );
     357            $this->assertSame( $expected[ $header ], array_pop( $target_headers ) );
    358358        }
    359359    }
     
    374374        $mailer = tests_retrieve_phpmailer_instance();
    375375
    376         $this->assertEquals( '', $mailer->Sender );
     376        $this->assertSame( '', $mailer->Sender );
    377377    }
    378378
     
    390390        wp_mail( $to, $subject, $message );
    391391
    392         $this->assertEquals( 1, $ma->get_call_count() );
     392        $this->assertSame( 1, $ma->get_call_count() );
    393393
    394394        $expected_error_data = array(
     
    405405        $call_args = array_pop( $all_args );
    406406
    407         $this->assertEquals( 'wp_mail_failed', $call_args[0]->get_error_code() );
    408         $this->assertEquals( $expected_error_data, $call_args[0]->get_error_data() );
     407        $this->assertSame( 'wp_mail_failed', $call_args[0]->get_error_code() );
     408        $this->assertSame( $expected_error_data, $call_args[0]->get_error_data() );
    409409    }
    410410
Note: See TracChangeset for help on using the changeset viewer.