Created
October 30, 2012 19:46
-
-
Save kurtpayne/3982546 to your computer and use it in GitHub Desktop.
6821 unit test super fun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: includes/mock-image-editor.php | |
=================================================================== | |
--- includes/mock-image-editor.php (revision 1108) | |
+++ includes/mock-image-editor.php (working copy) | |
@@ -5,13 +5,12 @@ | |
class WP_Image_Editor_Mock extends WP_Image_Editor { | |
public static $load_return = true; | |
- public static $test_return = true; | |
protected function load() { | |
return self::$load_return; | |
} | |
- public function test() { | |
- return self::$test_return; | |
+ public static function test() { | |
+ return true; | |
} | |
public function supports_mime_type( $mime_type ) { | |
return true; | |
Index: tests/image/resize_gd.php | |
=================================================================== | |
--- tests/image/resize_gd.php (revision 0) | |
+++ tests/image/resize_gd.php (working copy) | |
@@ -0,0 +1,15 @@ | |
+<?php | |
+ | |
+/** | |
+ * @group image | |
+ * @group media | |
+ * @group upload | |
+ */ | |
+class Test_Image_Resize_GD extends WP_Tests_Image_Resize_UnitTestCase { | |
+ | |
+ /** | |
+ * Use the GD image editor engine | |
+ * @var string | |
+ */ | |
+ public $editor_engine = 'gd'; | |
+} | |
Property changes on: tests/image/resize_gd.php | |
___________________________________________________________________ | |
Added: svn:eol-style | |
## -0,0 +1 ## | |
+LF | |
\ No newline at end of property | |
Index: tests/image/base.php | |
=================================================================== | |
--- tests/image/base.php (revision 0) | |
+++ tests/image/base.php (working copy) | |
@@ -0,0 +1,33 @@ | |
+<?php | |
+ | |
+/** | |
+ * @group image | |
+ */ | |
+abstract class WP_Image_UnitTestCase extends WP_UnitTestCase { | |
+ | |
+ /** | |
+ * Set the image editor engine according to the unit test's specification | |
+ */ | |
+ public function setUp() { | |
+ $class = 'WP_Image_Editor_' . $this->editor_engine; | |
+ if ( ! call_user_func( array( $class, 'test' ) ) ) { | |
+ $this->markTestSkipped( sprintf('The image editor engine %s is not supported on this system', $this->eitor_engine) ); | |
+ } | |
+ add_filter( 'image_editor_class', array( $this, 'setEngine') ); | |
+ } | |
+ | |
+ /** | |
+ * Undo the image editor override | |
+ */ | |
+ public function tearDown() { | |
+ remove_filter( 'image_editor_class', array( $this, 'setEngine' ) ); | |
+ } | |
+ | |
+ /** | |
+ * Override the image editor engine | |
+ * @return string | |
+ */ | |
+ public function setEngine() { | |
+ return 'WP_Image_Editor_' . $this->editor_engine; | |
+ } | |
+} | |
Property changes on: tests/image/base.php | |
___________________________________________________________________ | |
Added: svn:eol-style | |
## -0,0 +1 ## | |
+LF | |
\ No newline at end of property | |
Index: tests/image/resize_imagick.php | |
=================================================================== | |
--- tests/image/resize_imagick.php (revision 0) | |
+++ tests/image/resize_imagick.php (working copy) | |
@@ -0,0 +1,15 @@ | |
+<?php | |
+ | |
+/** | |
+ * @group image | |
+ * @group media | |
+ * @group upload | |
+ */ | |
+class Test_Image_Resize_Imagick extends WP_Tests_Image_Resize_UnitTestCase { | |
+ | |
+ /** | |
+ * Use the imagick image editor engine | |
+ * @var string | |
+ */ | |
+ public $editor_engine = 'imagick'; | |
+} | |
Property changes on: tests/image/resize_imagick.php | |
___________________________________________________________________ | |
Added: svn:eol-style | |
## -0,0 +1 ## | |
+LF | |
\ No newline at end of property | |
Index: tests/image/editor.php | |
=================================================================== | |
--- tests/image/editor.php (revision 1108) | |
+++ tests/image/editor.php (working copy) | |
@@ -20,34 +20,32 @@ | |
if ( !class_exists( 'WP_Image_Editor' ) ) | |
$this->markTestSkipped(); | |
- // Include our custom mock | |
- include_once( DIR_TESTDATA . '/../includes/mock-image-editor.php' ); | |
- | |
- // Mock up an abstract image editor based on WP_Image_Editor | |
- // note: this *HAS* to start with 'WP_Image_Editor_' | |
- $className = 'WP_Image_Editor_' . substr( md5( uniqid() ), -12 ); | |
- $this->editor = $this->getMockForAbstractClass( 'WP_Image_Editor', array( | |
- 'get_size', | |
- 'get_suffix' | |
- ), $className, false ); | |
- | |
- // Override the filters to set our own image editor | |
- add_filter( 'image_editor_class', array( $this, 'image_editor_class' ) ); | |
- add_filter( 'wp_editors', array( $this, 'wp_editors' ) ); | |
+ // Mock image editor ... this requires a custom mock because we can't | |
+ // use phpunit's mockbuilder to set the return value for load() before | |
+ // get_instance() is called | |
+ include_once( DIR_TESTDATA . '/../includes/mock-image-editor.php' ); | |
+ WP_Image_Editor_Mock::$load_return = true; | |
+ add_filter( 'image_editor_class', array( $this, 'set_mock' ) ); | |
// Un-cache the chosen image implementation | |
$this->_uncache_implementation(); | |
} | |
/** | |
- * Tear down test fixture | |
+ * Remove our mock | |
*/ | |
public function tearDown() { | |
- remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) ); | |
- remove_filter( 'wp_editors', array( $this, 'wp_editors' ) ); | |
+ remove_filter( 'image_editor_class', array( $this, 'set_mock' ) ); | |
} | |
/** | |
+ * Swap out the PHPUnit mock with our custom mock | |
+ */ | |
+ public function set_mock() { | |
+ return 'WP_Image_Editor_Mock'; | |
+ } | |
+ | |
+ /** | |
* Unset the static implementation cache | |
*/ | |
protected function _uncache_implementation() { | |
@@ -56,45 +54,18 @@ | |
$var->setAccessible( true ); | |
$var->setValue( $class, null ); | |
} | |
- | |
- /** | |
- * Override the wp_editors filter | |
- * @return array | |
- */ | |
- public function wp_editors() { | |
- return array( preg_replace('/^WP_Image_Editor_/', '', get_class( $this->editor ) ) ); | |
- } | |
- | |
- /** | |
- * Override the image_editor_class filter | |
- * @return mixed | |
- */ | |
- public function image_editor_class() { | |
- return get_class( $this->editor ); | |
- } | |
- | |
+ | |
/** | |
* Test get_instance where load returns true | |
* @ticket 6821 | |
*/ | |
public function test_get_instance_load_returns_true() { | |
- // Swap out the PHPUnit mock with our custom mock | |
- $func = create_function( '', 'return "WP_Image_Editor_Mock";'); | |
- remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) ); | |
- add_filter( 'image_editor_class', $func ); | |
- | |
- // Set load() to return true | |
- WP_Image_Editor_Mock::$load_return = true; | |
- | |
// Load an image | |
$editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); | |
// Everything should work | |
$this->assertInstanceOf( 'WP_Image_Editor_Mock', $editor ); | |
- | |
- // Remove our custom Mock | |
- remove_filter( 'image_editor_class', $func ); | |
} | |
/** | |
@@ -103,72 +74,16 @@ | |
*/ | |
public function test_get_instance_load_returns_false() { | |
- // Swap out the PHPUnit mock with our custom mock | |
- $func = create_function( '', 'return "WP_Image_Editor_Mock";'); | |
- remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) ); | |
- add_filter( 'image_editor_class', $func ); | |
- | |
- // Set load() to return true | |
- WP_Image_Editor_Mock::$load_return = new WP_Error(); | |
+ // Fail during load | |
+ WP_Image_Editor_Mock::$load_return = new WP_Error( '' ); | |
// Load an image | |
$editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); | |
// Everything should work | |
$this->assertInstanceOf( 'WP_Error', $editor ); | |
- | |
- // Remove our custom Mock | |
- remove_filter( 'image_editor_class', $func ); | |
- } | |
- | |
- /** | |
- * Test the "test" method | |
- * @ticket 6821 | |
- */ | |
- public function test_test_returns_true() { | |
- | |
- // $editor::test() returns true | |
- $this->editor->staticExpects( $this->once() ) | |
- ->method( 'test' ) | |
- ->will( $this->returnValue( true ) ); | |
- | |
- // Load an image | |
- $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); | |
- | |
- // Everything should work | |
- $this->assertInstanceOf( get_class( $this->editor ), $editor ); | |
- } | |
- | |
- /** | |
- * Test the "test" method returns false and the fallback editor is chosen | |
- * @ticket 6821 | |
- */ | |
- public function test_test_returns_false() { | |
- | |
- // $editor::test() returns true | |
- $this->editor->staticExpects( $this->once() ) | |
- ->method( 'test' ) | |
- ->will( $this->returnValue( false ) ); | |
- | |
- // Set a fallback editor | |
- $className = preg_replace('/^WP_Image_Editor_/', '', get_class( $this->editor ) ); | |
- $func = create_function( '', "return array('$className', 'Mock');" ); | |
- remove_filter( 'wp_editors', array( $this, 'wp_editors' ) ); | |
- remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) ); | |
- add_filter( 'wp_editors', $func ); | |
- | |
- // Load an image | |
- WP_Image_Editor_Mock::$load_return = true; | |
- $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); | |
- | |
- // Everything should work | |
- $this->assertInstanceOf( 'WP_Image_Editor_Mock', $editor ); | |
- | |
- // Unhook | |
- remove_filter( 'image_editor_class', '__return_null' ); | |
- remove_filter( 'wp_editors', $func ); | |
} | |
- | |
+ | |
/** | |
* Test test_quality | |
* @ticket 6821 | |
@@ -218,7 +133,7 @@ | |
$this->assertEquals( 'canola-new.jpg', basename( $editor->generate_filename( 'new' ) ) ); | |
// Test with a destination dir only | |
- $this->assertEquals(trailingslashit( realpath( get_temp_dir() ) ), trailingslashit( realpath( dirname( $editor->generate_filename( null, get_temp_dir() ) ) ) ) ); | |
+ $this->assertEquals( trailingslashit( realpath( get_temp_dir() ) ), trailingslashit( realpath( dirname( $editor->generate_filename( null, get_temp_dir() ) ) ) ) ); | |
// Test with a suffix only | |
$this->assertEquals( 'canola-100x50.png', basename( $editor->generate_filename( null, null, 'png' ) ) ); | |
@@ -234,10 +149,7 @@ | |
public function test_get_size() { | |
$editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); | |
- | |
- // Size should be false by default | |
- $this->assertNull( $editor->get_size() ); | |
- | |
+ | |
// Set a size | |
$size = array( | |
'height' => 50, | |
@@ -257,10 +169,7 @@ | |
public function test_get_suffix() { | |
$editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); | |
- | |
- // Size should be false by default | |
- $this->assertFalse( $editor->get_suffix() ); | |
- | |
+ | |
// Set a size | |
$size = array( | |
'height' => 50, | |
Index: tests/image/resize.php | |
=================================================================== | |
--- tests/image/resize.php (revision 1108) | |
+++ tests/image/resize.php (working copy) | |
@@ -5,15 +5,9 @@ | |
* @group media | |
* @group upload | |
*/ | |
-class Tests_Image_Resize extends WP_UnitTestCase { | |
+abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase { | |
// image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=75) | |
- function setUp() { | |
- if ( ! extension_loaded( 'gd' ) ) | |
- $this->markTestSkipped( 'The gd PHP extension is not loaded.' ); | |
- parent::setUp(); | |
- } | |
- | |
function test_resize_jpg() { | |
$image = image_resize( DIR_TESTDATA.'/images/test-image.jpg', 25, 25 ); | |
$this->assertEquals( 'test-image-25x25.jpg', basename($image) ); | |
@@ -128,17 +122,9 @@ | |
* @ticket 6821 | |
*/ | |
public function test_resize_non_existent_image() { | |
- $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick'); | |
- foreach ( $classes as $class ) { | |
- if ( ! call_user_func( array( $class, 'test' ) ) ) { | |
- continue; | |
- } | |
- $filter = create_function( '', "return '$class';" ); | |
- add_filter( 'image_editor_class', $filter ); | |
- $image = image_resize( DIR_TESTDATA.'/images/test-non-existent-image.jpg', 25, 25 ); | |
- $this->assertInstanceOf( 'WP_Error', $image ); | |
- $this->assertEquals( 'error_loading_image', $image->get_error_code() ); | |
- } | |
+ $image = image_resize( DIR_TESTDATA.'/images/test-non-existent-image.jpg', 25, 25 ); | |
+ $this->assertInstanceOf( 'WP_Error', $image ); | |
+ $this->assertEquals( 'error_loading_image', $image->get_error_code() ); | |
} | |
/** | |
@@ -146,16 +132,8 @@ | |
* @ticket 6821 | |
*/ | |
public function test_resize_bad_image() { | |
- $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick'); | |
- foreach ( $classes as $class ) { | |
- if ( ! call_user_func( array( $class, 'test' ) ) ) { | |
- continue; | |
- } | |
- $filter = create_function( '', "return '$class';" ); | |
- add_filter( 'image_editor_class', $filter ); | |
- $image = image_resize( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 ); | |
- $this->assertInstanceOf( 'WP_Error', $image ); | |
- $this->assertEquals( 'invalid_image', $image->get_error_code() ); | |
- } | |
+ $image = image_resize( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 ); | |
+ $this->assertInstanceOf( 'WP_Error', $image ); | |
+ $this->assertEquals( 'invalid_image', $image->get_error_code() ); | |
} | |
} | |
Index: wordpress/wp-includes/class-wp-image-editor-imagick.php | |
=================================================================== | |
--- wordpress/wp-includes/class-wp-image-editor-imagick.php (revision 22359) | |
+++ wordpress/wp-includes/class-wp-image-editor-imagick.php (working copy) | |
@@ -33,7 +33,7 @@ | |
* | |
* @return boolean | |
*/ | |
- public function test() { | |
+ public static function test() { | |
if ( ! extension_loaded( 'imagick' ) ) | |
return false; | |
Index: wordpress/wp-includes/class-wp-image-editor-gd.php | |
=================================================================== | |
--- wordpress/wp-includes/class-wp-image-editor-gd.php (revision 22359) | |
+++ wordpress/wp-includes/class-wp-image-editor-gd.php (working copy) | |
@@ -32,7 +32,7 @@ | |
* | |
* @return boolean | |
*/ | |
- public function test() { | |
+ public static function test() { | |
if ( ! extension_loaded('gd') || ! function_exists('gd_info') ) | |
return false; | |
Index: wordpress/wp-includes/class-wp-image-editor.php | |
=================================================================== | |
--- wordpress/wp-includes/class-wp-image-editor.php (revision 22359) | |
+++ wordpress/wp-includes/class-wp-image-editor.php (working copy) | |
@@ -76,7 +76,9 @@ | |
return self::$implementation; | |
} | |
- abstract public function test(); // returns bool | |
+ public static function test() { | |
+ return false; | |
+ } | |
abstract protected function load(); // returns bool|WP_Error | |
abstract public function supports_mime_type( $mime_type ); // returns bool | |
abstract public function resize( $max_w, $max_h, $crop = false ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment