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 <?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/** /**
* Tests for the product reviews REST API. * Tests for the product reviews REST API.
* *
@ -50,40 +53,42 @@ class WC_Tests_API_Product_Reviews_V2 extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 10, count( $product_reviews ) ); $this->assertEquals( 10, count( $product_reviews ) );
$this->assertContains( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $review_id, array(
'date_created' => $product_reviews[0]['date_created'], 'id' => $review_id,
'date_created_gmt' => $product_reviews[0]['date_created_gmt'], 'date_created' => $product_reviews[0]['date_created'],
'product_id' => $product->get_id(), 'date_created_gmt' => $product_reviews[0]['date_created_gmt'],
'product_name' => $product->get_name(), 'product_id' => $product->get_id(),
'product_permalink' => $product->get_permalink(), 'product_name' => $product->get_name(),
'status' => 'approved', 'product_permalink' => $product->get_permalink(),
'reviewer' => 'admin', 'status' => 'approved',
'reviewer_email' => 'woo@woo.local', 'reviewer' => 'admin',
'review' => "<p>Review content here</p>\n", 'reviewer_email' => 'woo@woo.local',
'rating' => 0, 'review' => "<p>Review content here</p>\n",
'verified' => false, 'rating' => 0,
'reviewer_avatar_urls' => $product_reviews[0]['reviewer_avatar_urls'], 'verified' => false,
'_links' => array( 'reviewer_avatar_urls' => $product_reviews[0]['reviewer_avatar_urls'],
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v3/products/reviews/' . $review_id ), array(
'href' => rest_url( '/wc/v3/products/reviews/' . $review_id ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v3/products/reviews' ),
'href' => rest_url( '/wc/v3/products/reviews' ), ),
), ),
), 'up' => array(
'up' => array( array(
array( 'href' => rest_url( '/wc/v3/products/' . $product->get_id() ),
'href' => rest_url( '/wc/v3/products/' . $product->get_id() ), ),
), ),
), ),
), ),
), $product_reviews[0]
$product_reviews )
); );
} }

View File

@ -6,6 +6,7 @@
* @since 3.0.0 * @since 3.0.0
*/ */
use Automattic\WooCommerce\Utilities\ArrayUtil;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
/** /**
@ -482,29 +483,39 @@ class Settings_V2 extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products' ) );
$data = $response->get_data(); $data = $response->get_data();
$this->assertTrue( is_array( $data ) ); $this->assertTrue( is_array( $data ) );
$this->assertContains( $data_download_required_login = null;
array( foreach ( $data as $setting ) {
'id' => 'woocommerce_downloads_require_login', if ( 'woocommerce_downloads_require_login' === $setting['id'] ) {
'label' => 'Access restriction', $data_download_required_login = $setting;
'description' => 'Downloads require login', break;
'type' => 'checkbox', }
'default' => 'no', }
'tip' => 'This setting does not apply to guest purchases.', $this->assertNotEmpty( $data_download_required_login );
'value' => 'no', $this->assertEmpty(
'_links' => array( ArrayUtil::deep_assoc_array_diff(
'self' => array( array(
array( 'id' => 'woocommerce_downloads_require_login',
'href' => rest_url( '/wc/v2/settings/products/woocommerce_downloads_require_login' ), 'label' => 'Access restriction',
'description' => 'Downloads require login',
'type' => 'checkbox',
'default' => 'no',
'tip' => 'This setting does not apply to guest purchases.',
'value' => 'no',
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v2/settings/products/woocommerce_downloads_require_login' ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v2/settings/products' ),
'href' => rest_url( '/wc/v2/settings/products' ), ),
), ),
), ),
), ),
), $data_download_required_login
$data )
); );
// test get single. // test get single.
@ -540,29 +551,41 @@ class Settings_V2 extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertContains( $recipient_setting = null;
array( foreach ( $settings as $setting ) {
'id' => 'recipient', if ( 'recipient' === $setting['id'] ) {
'label' => 'Recipient(s)', $recipient_setting = $setting;
'description' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.', break;
'type' => 'text', }
'default' => '', }
'tip' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
'value' => '', $this->assertNotEmpty( $recipient_setting );
'_links' => array(
'self' => array( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'href' => rest_url( '/wc/v2/settings/email_new_order/recipient' ), array(
'id' => 'recipient',
'label' => 'Recipient(s)',
'description' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
'type' => 'text',
'default' => '',
'tip' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
'value' => '',
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v2/settings/email_new_order/recipient' ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v2/settings/email_new_order' ),
'href' => rest_url( '/wc/v2/settings/email_new_order' ), ),
), ),
), ),
), ),
), $recipient_setting
$settings )
); );
// test get single. // test get single.

View File

@ -1,4 +1,7 @@
<?php <?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/** /**
* Tests for the Shipping Methods REST API. * Tests for the Shipping Methods REST API.
* *
@ -44,25 +47,37 @@ class Shipping_Methods_V2 extends WC_REST_Unit_Test_Case {
$methods = $response->get_data(); $methods = $response->get_data();
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertContains(
array( $free_shipping_method = null;
'id' => 'free_shipping', foreach ( $methods as $method ) {
'title' => 'Free shipping', if ( 'free_shipping' === $method['id'] ) {
'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.', $free_shipping_method = $method;
'_links' => array( break;
'self' => array( }
array( }
'href' => rest_url( '/wc/v2/shipping_methods/free_shipping' ), $this->assertNotEmpty( $free_shipping_method );
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => 'free_shipping',
'title' => 'Free shipping',
'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.',
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v2/shipping_methods/free_shipping' ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping_methods' ),
'href' => rest_url( '/wc/v2/shipping_methods' ), ),
), ),
), ),
), ),
), $free_shipping_method
$methods )
); );
} }

View File

@ -1,5 +1,7 @@
<?php <?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/** /**
* Shipping Zones API Tests * Shipping Zones API Tests
* @package WooCommerce\Tests\API * @package WooCommerce\Tests\API
@ -74,30 +76,32 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 ); $this->assertEquals( count( $data ), 1 );
$this->assertContains( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $data[0]['id'], array(
'name' => 'Locations not covered by your other zones', 'id' => $data[0]['id'],
'order' => 0, 'name' => 'Locations not covered by your other zones',
'_links' => array( 'order' => 0,
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] ), array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping/zones' ),
'href' => rest_url( '/wc/v2/shipping/zones' ), ),
), ),
), 'describedby' => array(
'describedby' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] . '/locations' ),
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] . '/locations' ), ),
), ),
), ),
), ),
), $data[0]
$data )
); );
// Create a zone and make sure it's in the response // Create a zone and make sure it's in the response
@ -108,30 +112,32 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 2 ); $this->assertEquals( count( $data ), 2 );
$this->assertContains( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $data[1]['id'], array(
'name' => 'Zone 1', 'id' => $data[1]['id'],
'order' => 0, 'name' => 'Zone 1',
'_links' => array( 'order' => 0,
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] ), array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping/zones' ),
'href' => rest_url( '/wc/v2/shipping/zones' ), ),
), ),
), 'describedby' => array(
'describedby' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] . '/locations' ),
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] . '/locations' ), ),
), ),
), ),
), ),
), $data[1]
$data )
); );
} }
@ -195,30 +201,32 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 201, $response->get_status() ); $this->assertEquals( 201, $response->get_status() );
$this->assertEquals( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $data['id'], array(
'name' => 'Test Zone', 'id' => $data['id'],
'order' => 1, 'name' => 'Test Zone',
'_links' => array( 'order' => 1,
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] ), array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping/zones' ),
'href' => rest_url( '/wc/v2/shipping/zones' ), ),
), ),
), 'describedby' => array(
'describedby' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] . '/locations' ),
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] . '/locations' ), ),
), ),
), ),
), ),
), $data
$data )
); );
} }
@ -260,30 +268,32 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $zone->get_id(), array(
'name' => 'Zone Test', 'id' => $zone->get_id(),
'order' => 2, 'name' => 'Zone Test',
'_links' => array( 'order' => 2,
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping/zones' ),
'href' => rest_url( '/wc/v2/shipping/zones' ), ),
), ),
), 'describedby' => array(
'describedby' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), ),
), ),
), ),
), ),
), $data
$data )
); );
} }
@ -359,30 +369,32 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $zone->get_id(), array(
'name' => 'Test Zone', 'id' => $zone->get_id(),
'order' => 0, 'name' => 'Test Zone',
'_links' => array( 'order' => 0,
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping/zones' ),
'href' => rest_url( '/wc/v2/shipping/zones' ), ),
), ),
), 'describedby' => array(
'describedby' => array( array(
array( 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), ),
), ),
), ),
), ),
), $data
$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( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 ); $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 ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 200, $response->get_status() ); $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 <?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/** /**
* Tests for the product reviews REST API. * Tests for the product reviews REST API.
* *
@ -50,40 +53,42 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 10, count( $product_reviews ) ); $this->assertEquals( 10, count( $product_reviews ) );
$this->assertContains( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $review_id, array(
'date_created' => $product_reviews[0]['date_created'], 'id' => $review_id,
'date_created_gmt' => $product_reviews[0]['date_created_gmt'], 'date_created' => $product_reviews[0]['date_created'],
'product_id' => $product->get_id(), 'date_created_gmt' => $product_reviews[0]['date_created_gmt'],
'product_name' => $product->get_name(), 'product_id' => $product->get_id(),
'product_permalink' => $product->get_permalink(), 'product_name' => $product->get_name(),
'status' => 'approved', 'product_permalink' => $product->get_permalink(),
'reviewer' => 'admin', 'status' => 'approved',
'reviewer_email' => 'woo@woo.local', 'reviewer' => 'admin',
'review' => "<p>Review content here</p>\n", 'reviewer_email' => 'woo@woo.local',
'rating' => 0, 'review' => "<p>Review content here</p>\n",
'verified' => false, 'rating' => 0,
'reviewer_avatar_urls' => $product_reviews[0]['reviewer_avatar_urls'], 'verified' => false,
'_links' => array( 'reviewer_avatar_urls' => $product_reviews[0]['reviewer_avatar_urls'],
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v3/products/reviews/' . $review_id ), array(
'href' => rest_url( '/wc/v3/products/reviews/' . $review_id ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v3/products/reviews' ),
'href' => rest_url( '/wc/v3/products/reviews' ), ),
), ),
), 'up' => array(
'up' => array( array(
array( 'href' => rest_url( '/wc/v3/products/' . $product->get_id() ),
'href' => rest_url( '/wc/v3/products/' . $product->get_id() ), ),
), ),
), ),
), ),
), $product_reviews[0]
$product_reviews )
); );
} }

View File

@ -6,6 +6,7 @@
* @since 3.5.0 * @since 3.5.0
*/ */
use Automattic\WooCommerce\Utilities\ArrayUtil;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
/** /**
@ -481,29 +482,42 @@ class Settings extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products' ) );
$data = $response->get_data(); $data = $response->get_data();
$this->assertTrue( is_array( $data ) ); $this->assertTrue( is_array( $data ) );
$this->assertContains(
array( $setting_downloads_required = null;
'id' => 'woocommerce_downloads_require_login', foreach ( $data as $setting ) {
'label' => 'Access restriction', if ( 'woocommerce_downloads_require_login' === $setting['id'] ) {
'description' => 'Downloads require login', $setting_downloads_required = $setting;
'type' => 'checkbox', break;
'default' => 'no', }
'tip' => 'This setting does not apply to guest purchases.', }
'value' => 'no',
'_links' => array( $this->assertNotEmpty( $setting_downloads_required );
'self' => array(
array( $this->assertEmpty(
'href' => rest_url( '/wc/v3/settings/products/woocommerce_downloads_require_login' ), ArrayUtil::deep_assoc_array_diff(
array(
'id' => 'woocommerce_downloads_require_login',
'label' => 'Access restriction',
'description' => 'Downloads require login',
'type' => 'checkbox',
'default' => 'no',
'tip' => 'This setting does not apply to guest purchases.',
'value' => 'no',
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v3/settings/products/woocommerce_downloads_require_login' ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v3/settings/products' ),
'href' => rest_url( '/wc/v3/settings/products' ), ),
), ),
), ),
), ),
), $setting_downloads_required
$data )
); );
// test get single. // test get single.
@ -539,29 +553,41 @@ class Settings extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertContains( $recipient_setting = null;
array( foreach ( $settings as $setting ) {
'id' => 'recipient', if ( 'recipient' === $setting['id'] ) {
'label' => 'Recipient(s)', $recipient_setting = $setting;
'description' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.', break;
'type' => 'text', }
'default' => '', }
'tip' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
'value' => '', $this->assertNotEmpty( $recipient_setting );
'_links' => array(
'self' => array( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'href' => rest_url( '/wc/v3/settings/email_new_order/recipient' ), array(
'id' => 'recipient',
'label' => 'Recipient(s)',
'description' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
'type' => 'text',
'default' => '',
'tip' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
'value' => '',
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v3/settings/email_new_order/recipient' ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v3/settings/email_new_order' ),
'href' => rest_url( '/wc/v3/settings/email_new_order' ), ),
), ),
), ),
), ),
), $recipient_setting
$settings )
); );
// test get single. // test get single.

View File

@ -1,4 +1,7 @@
<?php <?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/** /**
* Tests for the Shipping Methods REST API. * Tests for the Shipping Methods REST API.
* *
@ -44,25 +47,37 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
$methods = $response->get_data(); $methods = $response->get_data();
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertContains(
array( $free_shipping = null;
'id' => 'free_shipping', foreach ( $methods as $method ) {
'title' => 'Free shipping', if ( 'free_shipping' === $method['id'] ) {
'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.', $free_shipping = $method;
'_links' => array( break;
'self' => array( }
array( }
'href' => rest_url( '/wc/v3/shipping_methods/free_shipping' ), $this->assertNotEmpty( $free_shipping );
$this->assertEmpty(
ArrayUtil::deep_assoc_array_diff(
array(
'id' => 'free_shipping',
'title' => 'Free shipping',
'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.',
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v3/shipping_methods/free_shipping' ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping_methods' ),
'href' => rest_url( '/wc/v3/shipping_methods' ), ),
), ),
), ),
), ),
), $free_shipping
$methods )
); );
} }

View File

@ -1,5 +1,7 @@
<?php <?php
use Automattic\WooCommerce\Utilities\ArrayUtil;
/** /**
* Shipping Zones API Tests * Shipping Zones API Tests
* *
@ -77,30 +79,32 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 ); $this->assertEquals( count( $data ), 1 );
$this->assertContains( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $data[0]['id'], array(
'name' => 'Locations not covered by your other zones', 'id' => $data[0]['id'],
'order' => 0, 'name' => 'Locations not covered by your other zones',
'_links' => array( 'order' => 0,
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[0]['id'] ), array(
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[0]['id'] ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping/zones' ),
'href' => rest_url( '/wc/v3/shipping/zones' ), ),
), ),
), 'describedby' => array(
'describedby' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping/zones/' . $data[0]['id'] . '/locations' ),
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[0]['id'] . '/locations' ), ),
), ),
), ),
), ),
), $data[0]
$data )
); );
// Create a zone and make sure it's in the response // Create a zone and make sure it's in the response
@ -111,30 +115,32 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 2 ); $this->assertEquals( count( $data ), 2 );
$this->assertContains( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $data[1]['id'], array(
'name' => 'Zone 1', 'id' => $data[1]['id'],
'order' => 0, 'name' => 'Zone 1',
'_links' => array( 'order' => 0,
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[1]['id'] ), array(
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[1]['id'] ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping/zones' ),
'href' => rest_url( '/wc/v3/shipping/zones' ), ),
), ),
), 'describedby' => array(
'describedby' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping/zones/' . $data[1]['id'] . '/locations' ),
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[1]['id'] . '/locations' ), ),
), ),
), ),
), ),
), $data[1]
$data )
); );
} }
@ -202,30 +208,32 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 201, $response->get_status() ); $this->assertEquals( 201, $response->get_status() );
$this->assertEquals( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $data['id'], array(
'name' => 'Test Zone', 'id' => $data['id'],
'order' => 1, 'name' => 'Test Zone',
'_links' => array( 'order' => 1,
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v3/shipping/zones/' . $data['id'] ), array(
'href' => rest_url( '/wc/v3/shipping/zones/' . $data['id'] ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping/zones' ),
'href' => rest_url( '/wc/v3/shipping/zones' ), ),
), ),
), 'describedby' => array(
'describedby' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping/zones/' . $data['id'] . '/locations' ),
'href' => rest_url( '/wc/v3/shipping/zones/' . $data['id'] . '/locations' ), ),
), ),
), ),
), ),
), $data
$data )
); );
} }
@ -269,30 +277,32 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $zone->get_id(), array(
'name' => 'Zone Test', 'id' => $zone->get_id(),
'order' => 2, 'name' => 'Zone Test',
'_links' => array( 'order' => 2,
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ), array(
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping/zones' ),
'href' => rest_url( '/wc/v3/shipping/zones' ), ),
), ),
), 'describedby' => array(
'describedby' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ), ),
), ),
), ),
), ),
), $data
$data )
); );
} }
@ -373,30 +383,32 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $this->assertEmpty(
array( ArrayUtil::deep_assoc_array_diff(
'id' => $zone->get_id(), array(
'name' => 'Test Zone', 'id' => $zone->get_id(),
'order' => 0, 'name' => 'Test Zone',
'_links' => array( 'order' => 0,
'self' => array( '_links' => array(
array( 'self' => array(
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ), array(
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
),
), ),
), 'collection' => array(
'collection' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping/zones' ),
'href' => rest_url( '/wc/v3/shipping/zones' ), ),
), ),
), 'describedby' => array(
'describedby' => array( array(
array( 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ), ),
), ),
), ),
), ),
), $data
$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( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 ); $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 ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $expected, $data ); $this->assertEmpty( ArrayUtil::deep_assoc_array_diff( $expected, $data ) );
} }
/** /**