Use assertStringContainsString to compare images

This commit is contained in:
Claudio Sanches 2020-04-16 17:52:22 -03:00
parent a5bc29e26c
commit 02e3ec7e97
1 changed files with 14 additions and 28 deletions

View File

@ -276,27 +276,20 @@ class WC_Tests_Product_Data extends WC_Unit_Test_Case {
public function test_get_image_should_return_product_image() { public function test_get_image_should_return_product_image() {
$product = new WC_Product(); $product = new WC_Product();
$image = $this->set_product_image( $product ); $image = $this->set_product_image( $product );
$html = '<img width="186" height="144" src="' . $image['url'] . '" class="%s" alt="" />'; $needle = 'width="186" height="144" src="' . $image['url'] . '" class="%s"';
// Check for lazy loading introduced by WP 5.5. $this->assertStringContainsString(
if ( function_exists( 'wp_lazy_loading_enabled' ) ) { sprintf( $needle, 'attachment-woocommerce_thumbnail size-woocommerce_thumbnail' ),
if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
$html = str_replace( '/>', 'loading="lazy" />', $html );
}
}
$this->assertEquals(
sprintf( $html, 'attachment-woocommerce_thumbnail size-woocommerce_thumbnail' ),
$product->get_image() $product->get_image()
); );
$this->assertEquals( $this->assertStringContainsString(
sprintf( $html, 'attachment-single size-single' ), sprintf( $needle, 'attachment-single size-single' ),
$product->get_image( 'single' ) $product->get_image( 'single' )
); );
$this->assertEquals( $this->assertStringContainsString(
sprintf( $html, 'custom-class' ), sprintf( $needle, 'custom-class' ),
$product->get_image( 'single', array( 'class' => 'custom-class' ) ) $product->get_image( 'single', array( 'class' => 'custom-class' ) )
); );
@ -311,27 +304,20 @@ class WC_Tests_Product_Data extends WC_Unit_Test_Case {
$variations = $variable_product->get_children(); $variations = $variable_product->get_children();
$variation_1 = wc_get_product( $variations[0] ); $variation_1 = wc_get_product( $variations[0] );
$image = $this->set_product_image( $variable_product ); $image = $this->set_product_image( $variable_product );
$html = '<img width="186" height="144" src="' . $image['url'] . '" class="%s" alt="" />'; $needle = 'width="186" height="144" src="' . $image['url'] . '" class="%s"';
// Check for lazy loading introduced by WP 5.5. $this->assertStringContainsString(
if ( function_exists( 'wp_lazy_loading_enabled' ) ) { sprintf( $needle, 'attachment-woocommerce_thumbnail size-woocommerce_thumbnail' ),
if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
$html = str_replace( '/>', 'loading="lazy" />', $html );
}
}
$this->assertEquals(
sprintf( $html, 'attachment-woocommerce_thumbnail size-woocommerce_thumbnail' ),
$variation_1->get_image() $variation_1->get_image()
); );
$this->assertEquals( $this->assertStringContainsString(
sprintf( $html, 'attachment-single size-single' ), sprintf( $needle, 'attachment-single size-single' ),
$variation_1->get_image( 'single' ) $variation_1->get_image( 'single' )
); );
$this->assertEquals( $this->assertStringContainsString(
sprintf( $html, 'custom-class' ), sprintf( $needle, 'custom-class' ),
$variation_1->get_image( 'single', array( 'class' => 'custom-class' ) ) $variation_1->get_image( 'single', array( 'class' => 'custom-class' ) )
); );