-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Country queries implemented. (#736)
* feat: Country queries implemented. * chore: WPCS compliance met and late-minute renames.
- Loading branch information
Showing
6 changed files
with
196 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* WPObject Type - Country_State_Type | ||
* | ||
* Registers CountryState WPObject type | ||
* | ||
* @package WPGraphQL\WooCommerce\Type\WPObject | ||
* @since 0.12.4 | ||
*/ | ||
|
||
namespace WPGraphQL\WooCommerce\Type\WPObject; | ||
|
||
/** | ||
* Class Country_State_Type | ||
*/ | ||
class Country_State_Type { | ||
|
||
/** | ||
* Registers type | ||
*/ | ||
public static function register() { | ||
register_graphql_object_type( | ||
'CountryState', | ||
[ | ||
'description' => __( 'shipping country state object', 'wp-graphql-woocommerce' ), | ||
'fields' => [ | ||
'code' => [ | ||
'type' => [ 'non_null' => 'String' ], | ||
'description' => __( 'Country state code', 'wp-graphql-woocommerce' ), | ||
'resolve' => function ( $source ) { | ||
return ! empty( $source['code'] ) ? $source['code'] : null; | ||
}, | ||
], | ||
'name' => [ | ||
'type' => [ 'non_null' => 'String' ], | ||
'description' => __( 'Country state name', 'wp-graphql-woocommerce' ), | ||
'resolve' => function ( $source ) { | ||
return ! empty( $source['name'] ) ? $source['name'] : null; | ||
}, | ||
], | ||
], | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
class RootQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase { | ||
public function testShippingCountriesQuery() { | ||
// Create shipping zones and shipping rates. | ||
update_option( 'woocommerce_allowed_countries', 'specific' ); | ||
update_option( 'woocommerce_specific_allowed_countries', [ 'US', 'CA' ] ); | ||
|
||
// Create query | ||
$query = ' | ||
query { | ||
allowedCountries | ||
} | ||
'; | ||
|
||
$response = $this->graphql( compact( 'query' ) ); | ||
$expected = [ | ||
$this->expectedField( 'allowedCountries.#', 'US' ), | ||
$this->expectedField( 'allowedCountries.#', 'CA' ), | ||
$this->not()->expectedField( 'allowedCountries.#', 'GB' ), | ||
]; | ||
|
||
$this->assertQuerySuccessful( $response, $expected ); | ||
} | ||
|
||
public function testShippingCountryStatesQuery() { | ||
// Create shipping zones and shipping rates. | ||
update_option( 'woocommerce_allowed_countries', 'specific' ); | ||
update_option( 'woocommerce_specific_allowed_countries', [ 'US', 'CA' ] ); | ||
|
||
// Create query | ||
$query = ' | ||
query ($country: CountriesEnum!) { | ||
allowedCountryStates(country: $country) { | ||
name | ||
code | ||
} | ||
} | ||
'; | ||
|
||
$variables = [ 'country' => 'US' ]; | ||
$response = $this->graphql( compact( 'query', 'variables' ) ); | ||
$expected = [ | ||
$this->expectedObject( | ||
'allowedCountryStates.#', | ||
[ | ||
$this->expectedField( 'name', 'Alaska' ), | ||
$this->expectedField( 'code', 'AL' ), | ||
] | ||
), | ||
$this->expectedObject( | ||
'allowedCountryStates.#', | ||
[ | ||
$this->not()->expectedField( 'name', 'Ontario' ), | ||
$this->not()->expectedField( 'code', 'ON' ), | ||
] | ||
), | ||
]; | ||
|
||
$this->assertQuerySuccessful( $response, $expected ); | ||
|
||
$variables = [ 'country' => 'CA' ]; | ||
$response = $this->graphql( compact( 'query', 'variables' ) ); | ||
$expected = [ | ||
$this->expectedObject( | ||
'allowedCountryStates.#', | ||
[ | ||
$this->expectedField( 'name', 'Ontario' ), | ||
$this->expectedField( 'code', 'ON' ), | ||
] | ||
), | ||
$this->expectedObject( | ||
'allowedCountryStates.#', | ||
[ | ||
$this->not()->expectedField( 'name', 'Alaska' ), | ||
$this->not()->expectedField( 'code', 'AL' ), | ||
] | ||
), | ||
]; | ||
|
||
$this->assertQuerySuccessful( $response, $expected ); | ||
} | ||
} |