Make WordPress Core

Changeset 1157 in tests


Ignore:
Timestamp:
11/26/2012 11:55:36 PM (14 years ago)
Author:
scribu
Message:

Simplify image editor tests.

  • use WP_Image_Editor_Mock everywhere, instead of getMockForAbstractClass()
  • instantiate editors directly, where possible
  • only use 'wp_image_editors' hook
Location:
trunk/tests/image
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/image/base.php

    r1153 r1157  
    1010         */
    1111        public function setUp() {
    12                 $class = $this->editor_engine;
    13                 if ( ! call_user_func( array( $class, 'test' ) ) ) {
     12                if ( ! call_user_func( array( $this->editor_engine, 'test' ) ) ) {
    1413                        $this->markTestSkipped( sprintf('The image editor engine %s is not supported on this system', $this->editor_engine) );
    1514                }
    16                 add_filter( 'wp_image_editor_class', array( $this, 'setEngine') );
     15
     16                add_filter( 'wp_image_editors', array( $this, 'setEngine' ), 10, 2 );
    1717        }
    1818
    … …  
    2121         */
    2222        public function tearDown() {
    23                 remove_filter( 'wp_image_editor_class', array( $this, 'setEngine' ) );
     23                remove_filter( 'wp_image_editors', array( $this, 'setEngine' ), 10, 2 );
    2424        }
    2525
    … …  
    2828         * @return string
    2929         */
    30         public function setEngine() {
    31                 return $this->editor_engine;
     30        public function setEngine( $editors ) {
     31                return array( $this->editor_engine );
    3232        }
    3333}
  • trunk/tests/image/editor.php

    r1152 r1157  
    66 * @group media
    77 */
    8 class Tests_Image_Editor extends WP_UnitTestCase {
    9 
    10         /**
    11          * Image editor
    12          * @var WP_Image_Editor
    13          */
    14         protected $editor = null;
     8class Tests_Image_Editor extends WP_Image_UnitTestCase {
     9        public $editor_engine = 'WP_Image_Editor_Mock';
    1510
    1611        /**
    … …  
    2217                include_once( DIR_TESTDATA . '/../includes/mock-image-editor.php' );
    2318
    24                 // Mock up an abstract image editor based on WP_Image_Editor
    25                 // note: this *HAS* to start with 'WP_Image_Editor_'
    26                 $className = 'WP_Image_Editor_' . substr( md5( uniqid() ), -12 );
    27                 $this->editor = $this->getMockForAbstractClass( 'WP_Image_Editor', array(
    28                         'get_size',
    29                         'get_suffix'
    30                 ), $className, false );
    31 
    32                 // Override the filters to set our own image editor
    33                 add_filter( 'wp_image_editor_class', array( $this, 'image_editor_class' ) );
    34         }
    35 
    36         /**
    37          * Tear down test fixture
    38          */
    39         public function tearDown() {
    40                 remove_filter( 'wp_image_editor_class', array( $this, 'image_editor_class' ) );
    41         }
    42 
    43         /**
    44          * Override the image_editor_class filter
    45          * @return mixed
    46          */
    47         public function image_editor_class() {
    48                 return get_class( $this->editor );
     19                parent::setUp();
    4920        }
    5021
    … …  
    5425         */
    5526        public function test_get_editor_load_returns_true() {
    56 
    57                 // Swap out the PHPUnit mock with our custom mock
    58                 $func = create_function( '', 'return "WP_Image_Editor_Mock";');
    59                 remove_filter( 'wp_image_editor_class', array( $this, 'image_editor_class' ) );
    60                 add_filter( 'wp_image_editor_class', $func );
    61 
    62                 // Set load() to return true
    63                 WP_Image_Editor_Mock::$load_return = true;
    64 
    65                 // Load an image
    6627                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
    6728
    68                 // Everything should work
    6929                $this->assertInstanceOf( 'WP_Image_Editor_Mock', $editor );
    70 
    71                 // Remove our custom Mock
    72                 remove_filter( 'wp_image_editor_class', $func );
    7330        }
    7431
    … …  
    7835         */
    7936        public function test_get_editor_load_returns_false() {
    80 
    81                 // Swap out the PHPUnit mock with our custom mock
    82                 $func = create_function( '', 'return "WP_Image_Editor_Mock";');
    83                 remove_filter( 'wp_image_editor_class', array( $this, 'image_editor_class' ) );
    84                 add_filter( 'wp_image_editor_class', $func );
    85 
    86                 // Set load() to return true
    8737                WP_Image_Editor_Mock::$load_return = new WP_Error();
    8838
    89                 // Load an image
    9039                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
    9140
    92                 // Everything should work
    9341                $this->assertInstanceOf( 'WP_Error', $editor );
    9442
    95                 // Remove our custom Mock
    96                 remove_filter( 'wp_image_editor_class', $func );
     43                WP_Image_Editor_Mock::$load_return = true;
    9744        }
    9845
  • trunk/tests/image/functions.php

    r1154 r1157  
    126126                                continue;
    127127                        }
    128                         $filter = create_function( '', "return '$class';" );
    129                         add_filter( 'wp_image_editor_class', $filter );
    130 
    131                         // Call wp_save_image_file
    132                         $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
     128
     129                        $img = new $class( DIR_TESTDATA . '/images/canola.jpg' );
     130                        $loaded = $img->load();
    133131
    134132                        // Save a file as each mime type, assert it works
    … …  
    164162                                continue;
    165163                        }
    166                         $filter = create_function( '', "return '$class';" );
    167                         add_filter( 'wp_image_editor_class', $filter );
     164
     165                        $img = new $class( DIR_TESTDATA . '/images/canola.jpg' );
     166                        $loaded = $img->load();
    168167
    169168                        // Save the file
    170                         $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
    171169                        $mime_type = 'image/gif';
    172170                        $file = wp_tempnam( 'tmp.jpg' );
    … …  
    209207                                continue;
    210208                        }
    211                         $filter = create_function( '', "return '$class';" );
    212                         add_filter( 'wp_image_editor_class', $filter );
     209
     210                        $img = new $class( DIR_TESTDATA . '/images/canola.jpg' );
     211                        $loaded = $img->load();
    213212
    214213                        // Save the image as each file extension, check the mime type
    … …  
    243242                $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
    244243                foreach ( $classes as $class ) {
    245                         if ( ! call_user_func( array( $class, 'test' ) ) ) {
    246                                 continue;
    247                         }
    248                         $filter = create_function( '', "return '$class';" );
    249                         add_filter( 'wp_image_editor_class', $filter );
    250                         $editor = wp_get_image_editor( DIR_TESTDATA );
    251                         $this->assertInstanceOf( 'WP_Error', $editor );
    252                         $this->assertEquals( 'error_loading_image', $editor->get_error_code() );
     244                        // If the image editor isn't available, skip it
     245                        if ( ! call_user_func( array( $class, 'test' ) ) ) {
     246                                continue;
     247                        }
     248
     249                        $editor = new $class( DIR_TESTDATA );
     250                        $loaded = $editor->load();
     251
     252                        $this->assertInstanceOf( 'WP_Error', $loaded );
     253                        $this->assertEquals( 'error_loading_image', $loaded->get_error_code() );
    253254                }
    254255        }
Note: See TracChangeset for help on using the changeset viewer.