| | 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 | |