Make WordPress Core

Changeset 1255 in tests for trunk/tests/image/resize.php


Ignore:
Timestamp:
04/03/2013 07:24:44 PM (12 years ago)
Author:
markoheijnen
Message:

Stop using image_resize and using an helper method for it

File:
1 edited

Legend:

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

    r1148 r1255  
    77 */
    88abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase {
    9     // image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=75)
    109
    1110    function test_resize_jpg() {
    12         $image = image_resize( DIR_TESTDATA.'/images/test-image.jpg', 25, 25 );
     11        $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.jpg', 25, 25 );
     12
    1313        $this->assertEquals( 'test-image-25x25.jpg', basename($image) );
    1414        list($w, $h, $type) = getimagesize($image);
     
    1616        $this->assertEquals( 25, $h );
    1717        $this->assertEquals( IMAGETYPE_JPEG, $type );
    18         unlink($image);
     18
     19        unlink( $image );
    1920    }
    2021
    2122    function test_resize_png() {
    22         $image = image_resize( DIR_TESTDATA.'/images/test-image.png', 25, 25 );
     23        $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.png', 25, 25 );
     24
    2325        $this->assertEquals( 'test-image-25x25.png', basename($image) );
    2426        list($w, $h, $type) = getimagesize($image);
     
    2628        $this->assertEquals( 25, $h );
    2729        $this->assertEquals( IMAGETYPE_PNG, $type );
    28         unlink($image);
     30
     31        unlink( $image );
    2932    }
    3033
    3134    function test_resize_gif() {
    32         $image = image_resize( DIR_TESTDATA.'/images/test-image.gif', 25, 25 );
     35        $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.gif', 25, 25 );
     36
    3337        $this->assertEquals( 'test-image-25x25.gif', basename($image) );
    3438        list($w, $h, $type) = getimagesize($image);
     
    3640        $this->assertEquals( 25, $h );
    3741        $this->assertEquals( IMAGETYPE_GIF, $type );
    38         unlink($image);
     42
     43        unlink( $image );
    3944    }
    4045
    4146    function test_resize_larger() {
    4247        // image_resize() should refuse to make an image larger
    43         $image = image_resize( DIR_TESTDATA.'/images/test-image.jpg', 100, 100 );
     48        $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.jpg', 100, 100 );
     49
    4450        $this->assertInstanceOf( 'WP_Error', $image );
    4551        $this->assertEquals( 'error_getting_dimensions', $image->get_error_code() );
     
    4753
    4854    function test_resize_thumb_128x96() {
    49         $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 96 );
     55        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 96 );
     56
    5057        $this->assertEquals( '2007-06-17DSC_4173-63x96.jpg', basename($image) );
    5158        list($w, $h, $type) = getimagesize($image);
     
    5360        $this->assertEquals( 96, $h );
    5461        $this->assertEquals( IMAGETYPE_JPEG, $type );
    55         // is this a valid test? do different systems always generate the same file?
    56         #$this->assertEquals( 'be8f7aaa7b939970a3ded069a6e3619b', md5_file($image) );
    57         unlink($image);
     62
     63        unlink( $image );
    5864    }
    5965
    6066    function test_resize_thumb_128x0() {
    61         $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 0 );
     67        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 0 );
     68
    6269        $this->assertEquals( '2007-06-17DSC_4173-128x192.jpg', basename($image) );
    6370        list($w, $h, $type) = getimagesize($image);
     
    6572        $this->assertEquals( 192, $h );
    6673        $this->assertEquals( IMAGETYPE_JPEG, $type );
    67         // is this a valid test? do different systems always generate the same file?
    68         #$this->assertEquals( '7b1c0d5e7d4f6a18ae7541c8abf1fd09', md5_file($image) );
    69         unlink($image);
     74
     75        unlink( $image );
    7076    }
    7177
    7278    function test_resize_thumb_0x96() {
    73         $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 0, 96 );
     79        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 0, 96 );
     80
    7481        $this->assertEquals( '2007-06-17DSC_4173-63x96.jpg', basename($image) );
    7582        list($w, $h, $type) = getimagesize($image);
     
    7784        $this->assertEquals( 96, $h );
    7885        $this->assertEquals( IMAGETYPE_JPEG, $type );
    79         // is this a valid test? do different systems always generate the same file?
    80         #$this->assertEquals( 'be8f7aaa7b939970a3ded069a6e3619b', md5_file($image) );
    81         unlink($image);
     86
     87        unlink( $image );
    8288    }
    8389
    8490    function test_resize_thumb_150x150_crop() {
    85         $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 150, true );
     91        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 150, true );
     92
    8693        $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename($image) );
    8794        list($w, $h, $type) = getimagesize($image);
     
    8996        $this->assertEquals( 150, $h );
    9097        $this->assertEquals( IMAGETYPE_JPEG, $type );
    91         // is this a valid test? do different systems always generate the same file?
    92         #$this->assertEquals( '9fdcf728e1d43da89edf866d009ca7e8', md5_file($image) );
    93         unlink($image);
     98
     99        unlink( $image );
    94100    }
    95101
    96102    function test_resize_thumb_150x100_crop() {
    97         $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 100, true );
     103        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 100, true );
     104
    98105        $this->assertEquals( '2007-06-17DSC_4173-150x100.jpg', basename($image) );
    99106        list($w, $h, $type) = getimagesize($image);
     
    101108        $this->assertEquals( 100, $h );
    102109        $this->assertEquals( IMAGETYPE_JPEG, $type );
    103         // is this a valid test? do different systems always generate the same file?
    104         #$this->assertEquals( '2eeb18856505ab946074d90babc46452', md5_file($image) );
     110
    105111        unlink($image);
    106112    }
    107113
    108114    function test_resize_thumb_50x150_crop() {
    109         $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 50, 150, true );
     115        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 50, 150, true );
     116
    110117        $this->assertEquals( '2007-06-17DSC_4173-50x150.jpg', basename($image) );
    111118        list($w, $h, $type) = getimagesize($image);
     
    113120        $this->assertEquals( 150, $h );
    114121        $this->assertEquals( IMAGETYPE_JPEG, $type );
    115         // is this a valid test? do different systems always generate the same file?
    116         #$this->assertEquals( '94ed8e8463475774a5c5ff66118d3127', md5_file($image) );
    117         unlink($image);
     122
     123        unlink( $image );
    118124    }
    119125
     
    123129     */
    124130    public function test_resize_non_existent_image() {
    125         $image = image_resize( DIR_TESTDATA.'/images/test-non-existent-image.jpg', 25, 25 );
     131        $image = $this->resize_helper( DIR_TESTDATA.'/images/test-non-existent-image.jpg', 25, 25 );
     132
    126133        $this->assertInstanceOf( 'WP_Error', $image );
    127134        $this->assertEquals( 'error_loading_image', $image->get_error_code() );
     
    133140     */
    134141    public function test_resize_bad_image() {
    135         $image = image_resize( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 );
     142        $image = $this->resize_helper( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 );
    136143        $this->assertInstanceOf( 'WP_Error', $image );
    137144        $this->assertEquals( 'invalid_image', $image->get_error_code() );
    138145    }
     146
     147
     148    /**
     149     * Function to help out the tests
     150     */
     151    protected function resize_helper( $file, $width, $height, $crop = false ) {
     152        $editor = wp_get_image_editor( $file );
     153
     154        if ( is_wp_error( $editor ) )
     155            return $editor;
     156
     157        $resized = $editor->resize( $width, $height, $crop );
     158         if ( is_wp_error( $resized ) )
     159            return $resized;
     160
     161        $dest_file = $editor->generate_filename();
     162        $saved = $editor->save( $dest_file );
     163
     164        if ( is_wp_error( $saved ) )
     165            return $saved;
     166
     167        return $dest_file;
     168    }
    139169}
Note: See TracChangeset for help on using the changeset viewer.