Modify API response tests to assert that expected key values are present (#51465)

Modify API response tests to assert that expected key values are present.

This differs from earlier assertion which checks for exact equality. However the earlier assertion was brittle and would fail when a new property is added, for example in https://core.trac.wordpress.org/ticket/61739.

With this change, any addition to schema will not breaking existing API tests.
This commit is contained in:
Vedanshu Jain 2024-09-18 12:58:38 +05:30 committed by GitHub
parent 1b58098848
commit 3cdf45f69c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 476 additions and 359 deletions

View File

@ -0,0 +1,5 @@
Significance: patch
Type: dev
Comment: Update unit test to account for WordPress nightly change. See core trac ticket 61739

View File

@ -1,4 +1,7 @@
<?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/**
* Tests for the product reviews REST API.
*
@ -50,7 +53,8 @@ class WC_Tests_API_Product_Reviews_V2 extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 10, count( $product_reviews ) );
$this->assertContains(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $review_id,
'date_created' => $product_reviews[0]['date_created'],
@ -83,7 +87,8 @@ class WC_Tests_API_Product_Reviews_V2 extends WC_REST_Unit_Test_Case {
),
),
),
$product_reviews
$product_reviews[0]
)
);
}

View File

@ -6,6 +6,7 @@
* @since 3.0.0
*/
use Automattic\WooCommerce\Utilities\ArrayUtil;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
/**
@ -482,7 +483,16 @@ class Settings_V2 extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products' ) );
$data = $response->get_data();
$this->assertTrue( is_array( $data ) );
$this->assertContains(
$data_download_required_login = null;
foreach ( $data as $setting ) {
if ( 'woocommerce_downloads_require_login' === $setting['id'] ) {
$data_download_required_login = $setting;
break;
}
}
$this->assertNotEmpty( $data_download_required_login );
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => 'woocommerce_downloads_require_login',
'label' => 'Access restriction',
@ -504,7 +514,8 @@ class Settings_V2 extends WC_REST_Unit_Test_Case {
),
),
),
$data
$data_download_required_login
)
);
// test get single.
@ -540,7 +551,18 @@ class Settings_V2 extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertContains(
$recipient_setting = null;
foreach ( $settings as $setting ) {
if ( 'recipient' === $setting['id'] ) {
$recipient_setting = $setting;
break;
}
}
$this->assertNotEmpty( $recipient_setting );
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => 'recipient',
'label' => 'Recipient(s)',
@ -562,7 +584,8 @@ class Settings_V2 extends WC_REST_Unit_Test_Case {
),
),
),
$settings
$recipient_setting
)
);
// test get single.

View File

@ -1,4 +1,7 @@
<?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/**
* Tests for the Shipping Methods REST API.
*
@ -44,7 +47,18 @@ class Shipping_Methods_V2 extends WC_REST_Unit_Test_Case {
$methods = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertContains(
$free_shipping_method = null;
foreach ( $methods as $method ) {
if ( 'free_shipping' === $method['id'] ) {
$free_shipping_method = $method;
break;
}
}
$this->assertNotEmpty( $free_shipping_method );
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => 'free_shipping',
'title' => 'Free shipping',
@ -62,7 +76,8 @@ class Shipping_Methods_V2 extends WC_REST_Unit_Test_Case {
),
),
),
$methods
$free_shipping_method
)
);
}

View File

@ -1,5 +1,7 @@
<?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/**
* Shipping Zones API Tests
* @package WooCommerce\Tests\API
@ -74,7 +76,8 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 );
$this->assertContains(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $data[0]['id'],
'name' => 'Locations not covered by your other zones',
@ -97,7 +100,8 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
),
),
),
$data
$data[0]
)
);
// Create a zone and make sure it's in the response
@ -108,7 +112,8 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 2 );
$this->assertContains(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $data[1]['id'],
'name' => 'Zone 1',
@ -131,7 +136,8 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
),
),
),
$data
$data[1]
)
);
}
@ -195,7 +201,8 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 201, $response->get_status() );
$this->assertEquals(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $data['id'],
'name' => 'Test Zone',
@ -219,6 +226,7 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
),
),
$data
)
);
}
@ -260,7 +268,8 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $zone->get_id(),
'name' => 'Zone Test',
@ -284,6 +293,7 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
),
),
$data
)
);
}
@ -359,7 +369,8 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $zone->get_id(),
'name' => 'Test Zone',
@ -383,6 +394,7 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
),
),
$data
)
);
}
@ -624,13 +636,13 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 );
$this->assertContains( $expected, $data );
$this->assertEmpty( ArrayUtil::deep_assoc_array_diff( $expected, $data[0] ) );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $expected, $data );
$this->assertEmpty( ArrayUtil::deep_assoc_array_diff( $expected, $data ) );
}
/**

View File

@ -1,4 +1,7 @@
<?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/**
* Tests for the product reviews REST API.
*
@ -50,7 +53,8 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 10, count( $product_reviews ) );
$this->assertContains(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $review_id,
'date_created' => $product_reviews[0]['date_created'],
@ -83,7 +87,8 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
),
),
),
$product_reviews
$product_reviews[0]
)
);
}

View File

@ -6,6 +6,7 @@
* @since 3.5.0
*/
use Automattic\WooCommerce\Utilities\ArrayUtil;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
/**
@ -481,7 +482,19 @@ class Settings extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products' ) );
$data = $response->get_data();
$this->assertTrue( is_array( $data ) );
$this->assertContains(
$setting_downloads_required = null;
foreach ( $data as $setting ) {
if ( 'woocommerce_downloads_require_login' === $setting['id'] ) {
$setting_downloads_required = $setting;
break;
}
}
$this->assertNotEmpty( $setting_downloads_required );
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => 'woocommerce_downloads_require_login',
'label' => 'Access restriction',
@ -503,7 +516,8 @@ class Settings extends WC_REST_Unit_Test_Case {
),
),
),
$data
$setting_downloads_required
)
);
// test get single.
@ -539,7 +553,18 @@ class Settings extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertContains(
$recipient_setting = null;
foreach ( $settings as $setting ) {
if ( 'recipient' === $setting['id'] ) {
$recipient_setting = $setting;
break;
}
}
$this->assertNotEmpty( $recipient_setting );
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => 'recipient',
'label' => 'Recipient(s)',
@ -561,7 +586,8 @@ class Settings extends WC_REST_Unit_Test_Case {
),
),
),
$settings
$recipient_setting
)
);
// test get single.

View File

@ -1,4 +1,7 @@
<?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/**
* Tests for the Shipping Methods REST API.
*
@ -44,7 +47,18 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
$methods = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertContains(
$free_shipping = null;
foreach ( $methods as $method ) {
if ( 'free_shipping' === $method['id'] ) {
$free_shipping = $method;
break;
}
}
$this->assertNotEmpty( $free_shipping );
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => 'free_shipping',
'title' => 'Free shipping',
@ -62,7 +76,8 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
),
),
),
$methods
$free_shipping
)
);
}

View File

@ -1,5 +1,7 @@
<?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/**
* Shipping Zones API Tests
*
@ -77,7 +79,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 );
$this->assertContains(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $data[0]['id'],
'name' => 'Locations not covered by your other zones',
@ -100,7 +103,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
),
$data
$data[0]
)
);
// Create a zone and make sure it's in the response
@ -111,7 +115,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 2 );
$this->assertContains(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $data[1]['id'],
'name' => 'Zone 1',
@ -134,7 +139,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
),
$data
$data[1]
)
);
}
@ -202,7 +208,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 201, $response->get_status() );
$this->assertEquals(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $data['id'],
'name' => 'Test Zone',
@ -226,6 +233,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
$data
)
);
}
@ -269,7 +277,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $zone->get_id(),
'name' => 'Zone Test',
@ -293,6 +302,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
$data
)
);
}
@ -373,7 +383,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals(
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => $zone->get_id(),
'name' => 'Test Zone',
@ -397,6 +408,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
$data
)
);
}
@ -644,13 +656,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 );
$this->assertContains( $expected, $data );
$this->assertEmpty( ArrayUtil::deep_assoc_array_diff( $expected, $data[0] ) );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $expected, $data );
$this->assertEmpty( ArrayUtil::deep_assoc_array_diff( $expected, $data ) );
}
/**