Make WordPress Core

Changeset 61451


Ignore:
Timestamp:
01/08/2026 06:29:27 PM (3 weeks ago)
Author:
SergeyBiryukov
Message:

Tests: Refactor is_email() tests to use a data provider.

Follow-up to [121/tests], [401/tests].

Props salcode, geekofshire, swissspidy, iseulde, miqrogroove, SergeyBiryukov.
Fixes #31313.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/formatting/isEmail.php

    r53562 r61451  
    77 */
    88class Tests_Formatting_IsEmail extends WP_UnitTestCase {
    9     public function test_returns_the_email_address_if_it_is_valid() {
    10         $data = array(
     9
     10    /**
     11     * @dataProvider valid_email_provider
     12     */
     13    public function test_returns_the_email_address_if_it_is_valid( $email ) {
     14        $this->assertSame( $email, is_email( $email ), "is_email() should return the email address for $email." );
     15    }
     16
     17    /**
     18     * Data provider for valid email addresses.
     19     *
     20     * @return array
     21     */
     22    public static function valid_email_provider() {
     23        $valid_emails = array(
    1124            'bob@example.com',
    1225            'phil@example.info',
     
    1629            'bill+ted@example.com',
    1730        );
    18         foreach ( $data as $datum ) {
    19             $this->assertSame( $datum, is_email( $datum ), $datum );
     31
     32        foreach ( $valid_emails as $email ) {
     33            yield $email => array( $email );
    2034        }
    2135    }
    2236
    23     public function test_returns_false_if_given_an_invalid_email_address() {
    24         $data = array(
     37    /**
     38     * @dataProvider invalid_email_provider
     39     */
     40    public function test_returns_false_if_given_an_invalid_email_address( $email ) {
     41        $this->assertFalse( is_email( $email ), "is_email() should return false for $email." );
     42    }
     43
     44    /**
     45     * Data provider for invalid email addresses.
     46     *
     47     * @return array
     48     */
     49    public static function invalid_email_provider() {
     50        $invalid_emails = array(
    2551            'khaaaaaaaaaaaaaaan!',
    2652            'http://bob.example.com/',
     
    3056            'a@b.c',
    3157        );
    32         foreach ( $data as $datum ) {
    33             $this->assertFalse( is_email( $datum ), $datum );
     58
     59        foreach ( $invalid_emails as $email ) {
     60            yield $email => array( $email );
    3461        }
    3562    }
Note: See TracChangeset for help on using the changeset viewer.