wordpress-develop/tests/phpunit/tests/image/resize.php
Sergey Biryukov ddb409edca Build/Test Tools: Implement use of the void solution.
> PHPUnit 8.0.0 introduced a `void` return type declaration to the "fixture" methods – `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. As the `void` return type was not introduced until PHP 7.1, this makes it more difficult to create cross-version compatible tests when using fixtures, due to signature mismatches.
>
> The `Yoast\PHPUnitPolyfills\TestCases\TestCase` overcomes the signature mismatch by having two versions. The correct one will be loaded depending on the PHPUnit version being used.
>
> When using this TestCase, if an individual test, or another TestCase which extends this TestCase, needs to overload any of the "fixture" methods, it should do so by using a snake_case variant of the original fixture method name, i.e. `set_up_before_class()`, `set_up()`, `assert_pre_conditions()`, `assert_post_conditions()`, `tear_down()`, and `tear_down_after_class()`.
>
> The snake_case methods will automatically be called by PHPUnit.
>
> > IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use `parent::setUp()` from within an overloaded `set_up()` method. If necessary, DO call `parent::set_up()`.

Reference: https://github.com/Yoast/PHPUnit-Polyfills#testcases

This commit renames all declared fixture methods, and calls to parent versions of those fixture methods, from camelCase to snake_case.

Follow-up to [51559-51567].

Props jrf, hellofromTonya, johnbillion, netweb, dd32, pputzer, SergeyBiryukov.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51568 602fd350-edb4-49c9-b593-d223f7449a82
2021-08-07 10:29:41 +00:00

202 lines
6.0 KiB
PHP

<?php
/**
* @group image
* @group media
* @group upload
* @group resize
*/
require_once __DIR__ . '/base.php';
abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase {
public function set_up() {
parent::set_up();
add_filter( 'wp_image_editors', array( $this, 'wp_image_editors' ) );
}
public function wp_image_editors() {
return array( $this->editor_engine );
}
function test_resize_jpg() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/test-image.jpg', 25, 25 );
$this->assertSame( 'test-image-25x25.jpg', wp_basename( $image ) );
list($w, $h, $type) = getimagesize( $image );
$this->assertSame( 25, $w );
$this->assertSame( 25, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
unlink( $image );
}
function test_resize_png() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/test-image.png', 25, 25 );
if ( ! is_string( $image ) ) { // WP_Error, stop GLib-GObject-CRITICAL assertion.
$this->fail( sprintf( 'No PNG support in the editor engine %s on this system.', $this->editor_engine ) );
}
$this->assertSame( 'test-image-25x25.png', wp_basename( $image ) );
list($w, $h, $type) = getimagesize( $image );
$this->assertSame( 25, $w );
$this->assertSame( 25, $h );
$this->assertSame( IMAGETYPE_PNG, $type );
unlink( $image );
}
function test_resize_gif() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/test-image.gif', 25, 25 );
if ( ! is_string( $image ) ) { // WP_Error, stop GLib-GObject-CRITICAL assertion.
$this->fail( sprintf( 'No GIF support in the editor engine %s on this system.', $this->editor_engine ) );
}
$this->assertSame( 'test-image-25x25.gif', wp_basename( $image ) );
list($w, $h, $type) = getimagesize( $image );
$this->assertSame( 25, $w );
$this->assertSame( 25, $h );
$this->assertSame( IMAGETYPE_GIF, $type );
unlink( $image );
}
function test_resize_webp() {
$file = DIR_TESTDATA . '/images/test-image.webp';
$editor = wp_get_image_editor( $file );
// Check if the editor supports the webp mime type.
if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) {
$this->markTestSkipped( sprintf( 'No WebP support in the editor engine %s on this system.', $this->editor_engine ) );
}
$image = $this->resize_helper( $file, 25, 25 );
$this->assertSame( 'test-image-25x25.webp', wp_basename( $image ) );
list($w, $h, $type) = wp_getimagesize( $image );
$this->assertSame( 25, $w );
$this->assertSame( 25, $h );
$this->assertSame( IMAGETYPE_WEBP, $type );
unlink( $image );
}
function test_resize_larger() {
// image_resize() should refuse to make an image larger.
$image = $this->resize_helper( DIR_TESTDATA . '/images/test-image.jpg', 100, 100 );
$this->assertInstanceOf( 'WP_Error', $image );
$this->assertSame( 'error_getting_dimensions', $image->get_error_code() );
}
function test_resize_thumb_128x96() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 128, 96 );
$this->assertSame( '2007-06-17DSC_4173-64x96.jpg', wp_basename( $image ) );
list($w, $h, $type) = getimagesize( $image );
$this->assertSame( 64, $w );
$this->assertSame( 96, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
unlink( $image );
}
function test_resize_thumb_128x0() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 128, 0 );
$this->assertSame( '2007-06-17DSC_4173-128x193.jpg', wp_basename( $image ) );
list($w, $h, $type) = getimagesize( $image );
$this->assertSame( 128, $w );
$this->assertSame( 193, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
unlink( $image );
}
function test_resize_thumb_0x96() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 0, 96 );
$this->assertSame( '2007-06-17DSC_4173-64x96.jpg', wp_basename( $image ) );
list($w, $h, $type) = getimagesize( $image );
$this->assertSame( 64, $w );
$this->assertSame( 96, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
unlink( $image );
}
function test_resize_thumb_150x150_crop() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 150, 150, true );
$this->assertSame( '2007-06-17DSC_4173-150x150.jpg', wp_basename( $image ) );
list($w, $h, $type) = getimagesize( $image );
$this->assertSame( 150, $w );
$this->assertSame( 150, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
unlink( $image );
}
function test_resize_thumb_150x100_crop() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 150, 100, true );
$this->assertSame( '2007-06-17DSC_4173-150x100.jpg', wp_basename( $image ) );
list($w, $h, $type) = getimagesize( $image );
$this->assertSame( 150, $w );
$this->assertSame( 100, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
unlink( $image );
}
function test_resize_thumb_50x150_crop() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG', 50, 150, true );
$this->assertSame( '2007-06-17DSC_4173-50x150.jpg', wp_basename( $image ) );
list($w, $h, $type) = getimagesize( $image );
$this->assertSame( 50, $w );
$this->assertSame( 150, $h );
$this->assertSame( IMAGETYPE_JPEG, $type );
unlink( $image );
}
/**
* Try resizing a non-existent image
*
* @ticket 6821
*/
public function test_resize_non_existent_image() {
$image = $this->resize_helper( DIR_TESTDATA . '/images/test-non-existent-image.jpg', 25, 25 );
$this->assertInstanceOf( 'WP_Error', $image );
$this->assertSame( 'error_loading_image', $image->get_error_code() );
}
/**
* Function to help out the tests
*/
protected function resize_helper( $file, $width, $height, $crop = false ) {
$editor = wp_get_image_editor( $file );
if ( is_wp_error( $editor ) ) {
return $editor;
}
$resized = $editor->resize( $width, $height, $crop );
if ( is_wp_error( $resized ) ) {
return $resized;
}
$dest_file = $editor->generate_filename();
$saved = $editor->save( $dest_file );
if ( is_wp_error( $saved ) ) {
return $saved;
}
return $dest_file;
}
}