Skip to content

Instantly share code, notes, and snippets.

@markoheijnen
Last active December 15, 2015 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markoheijnen/5312334 to your computer and use it in GitHub Desktop.
Save markoheijnen/5312334 to your computer and use it in GitHub Desktop.
Structure example for deprecated methods
Index: tests/image/size.php
===================================================================
--- tests/image/size.php (revision 1257)
+++ tests/image/size.php (working copy)
@@ -95,57 +95,6 @@
$this->assertequals(array(525, 700), $out);
}
- function test_shrink_dimensions_default() {
- $out = wp_shrink_dimensions(640, 480);
- $this->assertEquals(array(128, 96), $out);
-
- $out = wp_shrink_dimensions(480, 640);
- $this->assertEquals(array(72, 96), $out);
- }
-
- function test_shrink_dimensions_smaller() {
- // image size is smaller than the constraint - no effect
- $out = wp_shrink_dimensions(500, 600, 1024, 768);
- $this->assertEquals(array(500, 600), $out);
-
- $out = wp_shrink_dimensions(600, 500, 1024, 768);
- $this->assertEquals(array(600, 500), $out);
- }
-
- function test_shrink_dimensions_equal() {
- // image size is equal to the constraint - no effect
- $out = wp_shrink_dimensions(500, 600, 500, 600);
- $this->assertEquals(array(500, 600), $out);
-
- $out = wp_shrink_dimensions(600, 500, 600, 500);
- $this->assertEquals(array(600, 500), $out);
- }
-
- function test_shrink_dimensions_larger() {
- // image size is larger than the constraint - result should be constrained
- $out = wp_shrink_dimensions(1024, 768, 500, 600);
- $this->assertequals(array(500, 375), $out);
-
- $out = wp_shrink_dimensions(300, 800, 500, 600);
- $this->assertequals(array(225, 600), $out);
- }
-
- function test_shrink_dimensions_boundary() {
- // one dimension is larger than the constraint, one smaller - result should be constrained
- $out = wp_shrink_dimensions(1024, 768, 500, 800);
- $this->assertequals(array(500, 375), $out);
-
- $out = wp_shrink_dimensions(1024, 768, 2000, 700);
- $this->assertequals(array(933, 700), $out);
-
- // portrait
- $out = wp_shrink_dimensions(768, 1024, 800, 500);
- $this->assertequals(array(375, 500), $out);
-
- $out = wp_shrink_dimensions(768, 1024, 2000, 700);
- $this->assertequals(array(525, 700), $out);
- }
-
function test_constrain_size_for_editor_thumb() {
$out = image_constrain_size_for_editor(600, 400, 'thumb');
$this->assertEquals(array(150, 100), $out);
Index: tests/deprecated/image.php
===================================================================
--- tests/deprecated/image.php (revision 0)
+++ tests/deprecated/image.php (revision 0)
@@ -0,0 +1,104 @@
+<?php
+/**
+ * @group deprecated
+ */
+class Test_Deprecated_Image extends WP_Deprecated_UnitTestCase {
+
+ /**
+ * Test that wp_save_image_file has a deprecated argument when passed a GD resource
+ * @ticket 6821
+ */
+ public function test_wp_save_image_file_deprecated_with_gd_resource() {
+ if ( !function_exists( 'imagejpeg' ) )
+ $this->markTestSkipped( 'jpeg support unavailable' );
+
+ // Call wp_save_image_file
+ include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
+ $file = wp_tempnam();
+ $img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' );
+ wp_save_image_file( $file, $img, 'image/jpeg', 1 );
+ imagedestroy( $img );
+ @unlink($file);
+
+ // Check if the arg was deprecated
+ $check = $this->was_deprecated( 'argument', 'wp_save_image_file' );
+ $this->assertNotEmpty( $check );
+ }
+
+ /**
+ * Test that wp_save_image_file doesn't have a deprecated argument when passed a WP_Image_Editor
+ * @ticket 6821
+ */
+ public function test_wp_save_image_file_not_deprecated_with_wp_image_editor() {
+ if ( !function_exists( 'imagejpeg' ) )
+ $this->markTestSkipped( 'jpeg support unavailable' );
+
+ // Call wp_save_image_file
+ include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
+ $file = wp_tempnam();
+ $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
+ wp_save_image_file( $file, $img, 'image/jpeg', 1 );
+ unset( $img );
+ @unlink($file);
+
+ // Check if the arg was deprecated
+ $check = $this->was_deprecated( 'argument', 'wp_save_image_file' );
+ $this->assertFalse( $check );
+ }
+
+
+
+
+ function test_shrink_dimensions_default() {
+ $out = wp_shrink_dimensions(640, 480);
+ $this->assertEquals(array(128, 96), $out);
+
+ $out = wp_shrink_dimensions(480, 640);
+ $this->assertEquals(array(72, 96), $out);
+ }
+
+ function test_shrink_dimensions_smaller() {
+ // image size is smaller than the constraint - no effect
+ $out = wp_shrink_dimensions(500, 600, 1024, 768);
+ $this->assertEquals(array(500, 600), $out);
+
+ $out = wp_shrink_dimensions(600, 500, 1024, 768);
+ $this->assertEquals(array(600, 500), $out);
+ }
+
+ function test_shrink_dimensions_equal() {
+ // image size is equal to the constraint - no effect
+ $out = wp_shrink_dimensions(500, 600, 500, 600);
+ $this->assertEquals(array(500, 600), $out);
+
+ $out = wp_shrink_dimensions(600, 500, 600, 500);
+ $this->assertEquals(array(600, 500), $out);
+ }
+
+ function test_shrink_dimensions_larger() {
+ // image size is larger than the constraint - result should be constrained
+ $out = wp_shrink_dimensions(1024, 768, 500, 600);
+ $this->assertequals(array(500, 375), $out);
+
+ $out = wp_shrink_dimensions(300, 800, 500, 600);
+ $this->assertequals(array(225, 600), $out);
+ }
+
+ function test_shrink_dimensions_boundary() {
+ // one dimension is larger than the constraint, one smaller - result should be constrained
+ $out = wp_shrink_dimensions(1024, 768, 500, 800);
+ $this->assertequals(array(500, 375), $out);
+
+ $out = wp_shrink_dimensions(1024, 768, 2000, 700);
+ $this->assertequals(array(933, 700), $out);
+
+ // portrait
+ $out = wp_shrink_dimensions(768, 1024, 800, 500);
+ $this->assertequals(array(375, 500), $out);
+
+ $out = wp_shrink_dimensions(768, 1024, 2000, 700);
+ $this->assertequals(array(525, 700), $out);
+ }
+
+
+}
\ No newline at end of file
Index: tests/functions/deprecated.php
===================================================================
--- tests/functions/deprecated.php (revision 1257)
+++ tests/functions/deprecated.php (working copy)
@@ -1,177 +0,0 @@
-<?php
-
-/**
- * Test cases for deprecated functions, arguments, and files
- *
- * @package WordPress
- * @subpackage Unit Tests
- * @since 3.5
- * @group deprecated
- */
-class Test_Functions_Deprecated extends WP_UnitTestCase {
-
- /**
- * List of functions that have been passed through _deprecated_function()
- * @var string[]
- */
- protected $_deprecated_functions = array();
-
- /**
- * List of arguments that have been passed through _deprecated_argument()
- * @var string[]
- */
- protected $_deprecated_arguments = array();
-
- /**
- * List of files that have been passed through _deprecated_file()
- * @var string[]
- */
- protected $_deprecated_files = array();
-
- /**
- * Set up the test fixture
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->_deprecated_functions = array();
- $this->_deprecated_arguments = array();
- $this->_deprecated_files = array();
- add_action( 'deprecated_function_run' , array( $this, 'deprecated_function' ), 10, 3 );
- add_action( 'deprecated_function_trigger_error', '__return_false' );
- add_action( 'deprecated_argument_run' , array( $this, 'deprecated_argument' ), 10, 3 );
- add_action( 'deprecated_argument_trigger_error', '__return_false' );
- add_action( 'deprecated_file_included' , array( $this, 'deprecated_file' ), 10, 4 );
- add_action( 'deprecated_file_trigger_error', '__return_false' );
- }
-
- /**
- * Tear down the test fixture
- * @return void
- */
- public function teardown() {
- remove_action( 'deprecated_function_run' , array( $this, 'deprecated_function' ), 10, 3 );
- remove_action( 'deprecated_function_trigger_error', '__return_false' );
- remove_action( 'deprecated_argument_run' , array( $this, 'deprecated_argument' ), 10, 3 );
- remove_action( 'deprecated_argument_trigger_error', '__return_false' );
- remove_action( 'deprecated_file_included' , array( $this, 'deprecated_argument' ), 10, 4 );
- remove_action( 'deprecated_file_trigger_error', '__return_false' );
- parent::tearDown();
- }
-
- /**
- * Catch functions that have passed through _deprecated_function
- * @param string $function
- * @param string $replacement
- * @param float $version
- * @return void
- */
- public function deprecated_function( $function, $replacement, $version ) {
- $this->_deprecated_functions[] = array(
- 'function' => $function,
- 'replacement' => $replacement,
- 'version' => $version
- );
- }
-
- /**
- * Catch arguments that have passed through _deprecated_argument
- * @param string $argument
- * @param string $message
- * @param float $version
- * @return void
- */
- public function deprecated_argument( $argument, $message, $version ) {
- $this->_deprecated_arguments[] = array(
- 'argument' => $argument,
- 'message' => $message,
- 'version' => $version
- );
- }
-
- /**
- * Catch arguments that have passed through _deprecated_argument
- * @param string $argument
- * @param string $message
- * @param float $version
- * @return void
- */
- public function deprecated_file( $file, $version, $replacement, $message ) {
- $this->_deprecated_files[] = array(
- 'file' => $file,
- 'version' => $version,
- 'replacement' => $replacement,
- 'message' => $message
- );
- }
-
- /**
- * Check if something was deprecated
- * @param string $type argument|function|file
- * @param string $name
- * @return array|false
- */
- protected function was_deprecated( $type, $name ) {
- switch ( $type ) {
- case 'argument' :
- $search = $this->_deprecated_arguments;
- $key = 'argument';
- break;
- case 'function' :
- $search = $this->_deprecated_functions;
- $key = 'function';
- break;
- default :
- $search = $this->_deprecated_files;
- $key = 'file';
- }
- foreach ( $search as $v ) {
- if ( $name == $v[$key] ) {
- return $v;
- }
- }
- return false;
- }
-
- /**
- * Test that wp_save_image_file has a deprecated argument when passed a GD resource
- * @ticket 6821
- */
- public function test_wp_save_image_file_deprecated_with_gd_resource() {
- if ( !function_exists( 'imagejpeg' ) )
- $this->markTestSkipped( 'jpeg support unavailable' );
-
- // Call wp_save_image_file
- include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
- $file = wp_tempnam();
- $img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' );
- wp_save_image_file( $file, $img, 'image/jpeg', 1 );
- imagedestroy( $img );
- @unlink($file);
-
- // Check if the arg was deprecated
- $check = $this->was_deprecated( 'argument', 'wp_save_image_file' );
- $this->assertNotEmpty( $check );
- }
-
- /**
- * Test that wp_save_image_file doesn't have a deprecated argument when passed a WP_Image_Editor
- * @ticket 6821
- */
- public function test_wp_save_image_file_not_deprecated_with_wp_image_editor() {
- if ( !function_exists( 'imagejpeg' ) )
- $this->markTestSkipped( 'jpeg support unavailable' );
-
- // Call wp_save_image_file
- include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
- $file = wp_tempnam();
- $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
- wp_save_image_file( $file, $img, 'image/jpeg', 1 );
- unset( $img );
- @unlink($file);
-
- // Check if the arg was deprecated
- $check = $this->was_deprecated( 'argument', 'wp_save_image_file' );
- $this->assertFalse( $check );
- }
-}
Index: includes/testcase-deprecated.php
===================================================================
--- includes/testcase-deprecated.php (revision 0)
+++ includes/testcase-deprecated.php (revision 0)
@@ -0,0 +1,137 @@
+<?php
+/**
+ * Test cases for deprecated functions, arguments, and files
+ *
+ * @package WordPress
+ * @subpackage UnitTests
+ * @since 3.4.0
+ */
+abstract class WP_Deprecated_UnitTestCase extends WP_UnitTestCase {
+
+ /**
+ * List of functions that have been passed through _deprecated_function()
+ * @var string[]
+ */
+ protected $_deprecated_functions = array();
+
+ /**
+ * List of arguments that have been passed through _deprecated_argument()
+ * @var string[]
+ */
+ protected $_deprecated_arguments = array();
+
+ /**
+ * List of files that have been passed through _deprecated_file()
+ * @var string[]
+ */
+ protected $_deprecated_files = array();
+
+ /**
+ * Set up the test fixture
+ * @return void
+ */
+ public function setUp() {
+ parent::setUp();
+
+ $this->_deprecated_functions = array();
+ $this->_deprecated_arguments = array();
+ $this->_deprecated_files = array();
+
+ add_action( 'deprecated_function_run' , array( $this, 'deprecated_function' ), 10, 3 );
+ add_action( 'deprecated_function_trigger_error', '__return_false' );
+ add_action( 'deprecated_argument_run' , array( $this, 'deprecated_argument' ), 10, 3 );
+ add_action( 'deprecated_argument_trigger_error', '__return_false' );
+ add_action( 'deprecated_file_included' , array( $this, 'deprecated_file' ), 10, 4 );
+ add_action( 'deprecated_file_trigger_error', '__return_false' );
+ }
+
+ /**
+ * Tear down the test fixture
+ * @return void
+ */
+ public function teardown() {
+ remove_action( 'deprecated_function_run' , array( $this, 'deprecated_function' ), 10, 3 );
+ remove_action( 'deprecated_function_trigger_error', '__return_false' );
+ remove_action( 'deprecated_argument_run' , array( $this, 'deprecated_argument' ), 10, 3 );
+ remove_action( 'deprecated_argument_trigger_error', '__return_false' );
+ remove_action( 'deprecated_file_included' , array( $this, 'deprecated_argument' ), 10, 4 );
+ remove_action( 'deprecated_file_trigger_error', '__return_false' );
+
+ parent::tearDown();
+ }
+
+ /**
+ * Catch functions that have passed through _deprecated_function
+ * @param string $function
+ * @param string $replacement
+ * @param float $version
+ * @return void
+ */
+ public function deprecated_function( $function, $replacement, $version ) {
+ $this->_deprecated_functions[] = array(
+ 'function' => $function,
+ 'replacement' => $replacement,
+ 'version' => $version
+ );
+ }
+
+ /**
+ * Catch arguments that have passed through _deprecated_argument
+ * @param string $argument
+ * @param string $message
+ * @param float $version
+ * @return void
+ */
+ public function deprecated_argument( $argument, $message, $version ) {
+ $this->_deprecated_arguments[] = array(
+ 'argument' => $argument,
+ 'message' => $message,
+ 'version' => $version
+ );
+ }
+
+ /**
+ * Catch arguments that have passed through _deprecated_argument
+ * @param string $argument
+ * @param string $message
+ * @param float $version
+ * @return void
+ */
+ public function deprecated_file( $file, $version, $replacement, $message ) {
+ $this->_deprecated_files[] = array(
+ 'file' => $file,
+ 'version' => $version,
+ 'replacement' => $replacement,
+ 'message' => $message
+ );
+ }
+
+ /**
+ * Check if something was deprecated
+ * @param string $type argument|function|file
+ * @param string $name
+ * @return array|false
+ */
+ protected function was_deprecated( $type, $name ) {
+ switch ( $type ) {
+ case 'argument' :
+ $search = $this->_deprecated_arguments;
+ $key = 'argument';
+ break;
+ case 'function' :
+ $search = $this->_deprecated_functions;
+ $key = 'function';
+ break;
+ default :
+ $search = $this->_deprecated_files;
+ $key = 'file';
+ }
+ foreach ( $search as $v ) {
+ if ( $name == $v[$key] ) {
+ return $v;
+ }
+ }
+ return false;
+ }
+
+}
Index: includes/bootstrap.php
===================================================================
--- includes/bootstrap.php (revision 1257)
+++ includes/bootstrap.php (working copy)
@@ -81,6 +81,7 @@
require dirname( __FILE__ ) . '/testcase.php';
require dirname( __FILE__ ) . '/testcase-xmlrpc.php';
require dirname( __FILE__ ) . '/testcase-ajax.php';
+require dirname( __FILE__ ) . '/testcase-deprecated.php';
require dirname( __FILE__ ) . '/exceptions.php';
require dirname( __FILE__ ) . '/utils.php';
@markoheijnen
Copy link
Author

Question from Kurt:
but where do you put deprecated tests?

tests/deprecated/image.php
or
tests/image/deprecated.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment