Make WordPress Core

Ticket #21533: 21533-unit-test.2.patch

File 21533-unit-test.2.patch, 2.6 KB (added by kurtpayne, 12 years ago)

Updated with test for get too

  • 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         * Test that __get throw warnings on a non-existent property
     124         * @ticket 21533
     125         */
     126        public function test_no_getter_warnings() {
     127                global $wpdb;
     128               
     129                // Watch for errors
     130                $this->error_handler = set_error_handler( array( $this, '_error_handler' ), E_NOTICE );
     131               
     132                // Look for a non-existent property
     133                $this->assertNull( $wpdb->non_existent_property );
     134               
     135                // Look for notices related to a non existent property
     136                foreach ( $this->_php_errors as $error ) {
     137                        $this->assertNotContains( 'Undefined property: wpdb::$non_existent_property', $error['message'] );
     138                }
     139
     140                // Restore the original handler
     141                restore_error_handler();
     142        }
     143       
     144        /**
     145         * Log the error and call the original error handler
     146         * @param int $type
     147         * @param string $message
     148         * @param string $file
     149         * @param int $line
     150         * @return bool
     151         */
     152        public function _error_handler( $type, $message, $file, $line ) {
     153                $this->_php_errors[] = array(
     154                    'type'    => $type,
     155                    'message' => $message,
     156                    'file'    => $file,
     157                    'line'    => $line
     158                );
     159                if ( !is_callable( $this->_error_handler ) ) {
     160                        return false;
     161                }
     162                return call_user_func_array( $this->_error_handler, func_get_args() );
     163        }
    81164}