Make WordPress Core

Ticket #45299: 45299.3.diff

File 45299.3.diff, 4.1 KB (added by desrosj, 6 years ago)

Add some unit tests.

  • src/wp-includes/wp-db.php

     
    24562456
    24572457                $new_array = array();
    24582458                // Extract the column values
    2459                 for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
    2460                         $new_array[$i] = $this->get_var( null, $x, $i );
     2459                if ( $this->last_result ) {
     2460                        for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
     2461                                $new_array[ $i ] = $this->get_var( null, $x, $i );
     2462                        }
    24612463                }
    24622464                return $new_array;
    24632465        }
     
    24972499                } elseif ( $output == OBJECT_K ) {
    24982500                        // Return an array of row objects with keys from column 1
    24992501                        // (Duplicates are discarded)
    2500                         foreach ( $this->last_result as $row ) {
    2501                                 $var_by_ref = get_object_vars( $row );
    2502                                 $key = array_shift( $var_by_ref );
    2503                                 if ( ! isset( $new_array[ $key ] ) )
    2504                                         $new_array[ $key ] = $row;
     2502                        if ( $this->last_result ) {
     2503                                foreach ( $this->last_result as $row ) {
     2504                                        $var_by_ref = get_object_vars( $row );
     2505                                        $key = array_shift( $var_by_ref );
     2506                                        if ( ! isset( $new_array[ $key ] ) ) {
     2507                                                $new_array[ $key ] = $row;
     2508                                        }
     2509                                }
    25052510                        }
    25062511                        return $new_array;
    25072512                } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  • tests/phpunit/tests/db.php

     
    156156         * @dataProvider data_like_query
    157157         * @param $data string The haystack, raw.
    158158         * @param $like string The like phrase, raw.
    159         * @param $result string The expected comparison result; '1' = true, '0' = false
     159        * @param $result string The expected comparison result; '1' = true, '0' = false
    160160         */
    161161        function test_like_query( $data, $like, $result ) {
    162162                global $wpdb;
     
    559559                $this->assertEquals( 'Walter Sobchak', $row->display_name );
    560560        }
    561561
     562        /**
     563         * Test the `get_col()` method.
     564         *
     565         * @param string|null        $query       The query to run.
     566         * @param string|array       $expected    The expected resulting value.
     567         * @param arrray|string|null $last_result The value to assign to `$wpdb->last_result`.
     568         * @param int|string         $column      The column index to retrieve.
     569         *
     570         * @dataProvider data_test_get_col
     571         *
     572         * @ticket 45299
     573         */
     574        function test_get_col( $query, $expected, $last_result, $column ) {
     575                global $wpdb;
     576
     577                $wpdb->last_result = $last_result;
     578
     579                $result = $wpdb->get_col( $query, $column );
     580
     581                if ( $query ) {
     582                        $this->assertSame( $query, $wpdb->last_query );
     583                }
     584               
     585                if ( is_array( $expected ) ) {
     586                        $this->assertSame( $expected, $result );
     587                } else {
     588                        $this->assertContains( $expected, $result );
     589                }
     590        }
     591
     592        /**
     593         * Data provider for testing `get_col()`.
     594         *
     595         * @return array {
     596         *     Arguments for testing `get_col()`.
     597         *
     598         *     @type string|null        $query       The query to run.
     599         *     @type string|array       $expected    The resulting expected value.
     600         *     @type arrray|string|null $last_result The value to assign to `$wpdb->last_result`.
     601         *     @type int|string         $column      The column index to retrieve.
     602         */
     603        function data_test_get_col() {
     604                global $wpdb;
     605
     606                return array(
     607                        array(
     608                                "SELECT display_name FROM $wpdb->users",
     609                                'admin',
     610                                array(),
     611                                0,
     612                        ),
     613                        array(
     614                                "SELECT user_login, user_email FROM $wpdb->users",
     615                                'admin',
     616                                array(),
     617                                0,
     618                        ),
     619                        array(
     620                                "SELECT user_login, user_email FROM $wpdb->users",
     621                                'admin@example.org',
     622                                array(),
     623                                1,
     624                        ),
     625                        array(
     626                                "SELECT user_login, user_email FROM $wpdb->users",
     627                                'admin@example.org',
     628                                array(),
     629                                '1',
     630                        ),
     631                        array(
     632                                "SELECT user_login, user_email FROM $wpdb->users",
     633                                array( null ),
     634                                array(),
     635                                3,
     636                        ),
     637                        array(
     638                                '',
     639                                array(),
     640                                null,
     641                                0,
     642                        ),
     643                        array(
     644                                null,
     645                                array(),
     646                                '',
     647                                0
     648                        ),
     649                );
     650        }
     651
    562652        function test_replace() {
    563653                global $wpdb;
    564654                $rows1 = $wpdb->insert( $wpdb->users, array( 'display_name' => 'Walter Sobchak' ) );