Make WordPress Core

Ticket #21533: 21533-unit-test.patch

File 21533-unit-test.patch, 1.9 KB (added by kurtpayne, 12 years ago)
  • tests/db.php

     
    1414        protected $_queries = array();
    1515
    1616        /**
     17         * PHP Errors
     18         * @var array
     19         */
     20        protected $_php_errors = array();
     21       
     22        /**
     23         * PHP error handler
     24         * @var callable|null
     25         */
     26        protected $_error_handler = null;
     27
     28        /**
    1729         * Set up the test fixture
    1830         */
    1931        public function setUp() {
     
    7890                // Restore locale
    7991                setlocale( LC_ALL, $current_locale );
    8092        }
     93       
     94        /**
     95         * Test that flush doesn't fail with a non-existent result
     96         * @ticket 21533
     97         */
     98        public function test_no_mysql_free_warnings() {
     99                global $wpdb;
     100               
     101                // Start with an empty resource
     102                $wpdb->result = null;
     103                $wpdb->suppress_errors( true );
     104                $this->assertNotInternalType( 'resource', $wpdb->result );
     105               
     106                // Watch for errors
     107                $this->error_handler = set_error_handler( array( $this, '_error_handler' ), E_WARNING );
     108               
     109                // Make a bad query
     110                $wpdb->query('SELECT * FROM non_existent_table');
     111                $this->assertNotInternalType( 'resource', $wpdb->result );
     112               
     113                // Look for warnings related to mysql_free_result
     114                foreach ( $this->_php_errors as $error ) {
     115                        $this->assertNotContains( 'mysql_free_result() expects parameter 1 to be resource, null given', $error['message'] );
     116                }
     117
     118                // Restore the original handler
     119                restore_error_handler();
     120        }
     121       
     122        /**
     123         * Log the error and call the original error handler
     124         * @param int $type
     125         * @param string $message
     126         * @param string $file
     127         * @param int $line
     128         * @return bool
     129         */
     130        public function _error_handler( $type, $message, $file, $line ) {
     131                $this->_php_errors[] = array(
     132                    'type'    => $type,
     133                    'message' => $message,
     134                    'file'    => $file,
     135                    'line'    => $line
     136                );
     137                if ( !is_callable( $this->_error_handler ) ) {
     138                        return false;
     139                }
     140                return call_user_func_array( $this->_error_handler, func_get_args() );
     141        }
    81142}