diff --git a/bin/_lib.sh b/bin/_lib.sh index 07404afe..74265b5a 100755 --- a/bin/_lib.sh +++ b/bin/_lib.sh @@ -2,12 +2,7 @@ set +u -install_wordpress() { - if [ -f $WP_CORE_DIR/wp-config.php ]; then - echo "Wordpress already installed." - return; - fi - +composer_wordpress_config() { # Set the wordpress install directory and plugin paths in the composer.json composer config --unset extra.wordpress-install-dir; composer config extra.wordpress-install-dir $WP_CORE_DIR; @@ -19,6 +14,22 @@ install_wordpress() { \"$THEMES_DIR/{\$name}/\": [\"type:wordpress-theme\"] }" + # Set WPackagist repository + composer config repositories.wpackagist composer https://wpackagist.org + + # Enable plugins + composer config --no-plugins allow-plugins.composer/installers true + composer config --no-plugins allow-plugins.johnpbloch/wordpress-core-installer true +} + +install_wordpress() { + if [ -f $WP_CORE_DIR/wp-config.php ]; then + echo "Wordpress already installed." + return; + fi + + composer_wordpress_config + # Install Wordpress + integrated plugins for testing/development. composer install composer require --dev -W \ @@ -26,7 +37,7 @@ install_wordpress() { wp-graphql/wp-graphql-jwt-authentication \ wpackagist-plugin/woocommerce \ wpackagist-plugin/woocommerce-gateway-stripe \ - wpackagist-plugin/wp-graphql:* \ + wpackagist-plugin/wp-graphql \ wpackagist-theme/twentytwentyone \ wp-cli/wp-cli-bundle } @@ -52,8 +63,11 @@ install_local_test_library() { # Install testing library dependencies. composer install composer require --dev \ - lucatume/wp-browser \ - codeception/module-asserts:^1.0 \ + lucatume/wp-browser:^3.1 \ + codeception/codeception:^4.2 \ + symfony/finder:* \ + codeception/lib-asserts:^1.0 \ + codeception/module-asserts:^1.3.1 \ codeception/module-rest:^2.0 \ codeception/util-universalframework:^1.0 \ wp-graphql/wp-graphql-testcase:^2.3 \ @@ -82,12 +96,25 @@ remove_local_test_library() { # Remove testing library dependencies. composer remove --dev wp-graphql/wp-graphql-testcase \ codeception/module-asserts \ + codeception/codeception \ + codeception/lib-asserts \ + symfony/finder \ codeception/module-rest \ codeception/util-universalframework \ lucatume/wp-browser \ stripe/stripe-php } +cleanup_composer_file() { + echo "Removing extra config..." + composer config --unset extra + echo "Removing repositories..." + composer config --unset repositories + + composer config --unset config.allow-plugins + echo "composer.json cleaned!" +} + cleanup_local_files() { if [ -n "$(ls -A $WP_CORE_DIR)" ]; then echo "Removing final test files..." @@ -98,7 +125,7 @@ cleanup_local_files() { fi echo "Rebuilding lock file..." - rm -rf $PROJECT_ROOT_DIR/vendor $PROJECT_ROOT_DIR/composer.lock + rm -rf $PROJECT_ROOT_DIR/vendor composer install --no-dev } diff --git a/bin/cleanup.local.sh b/bin/cleanup.local.sh index 49087d7e..d45c3205 100755 --- a/bin/cleanup.local.sh +++ b/bin/cleanup.local.sh @@ -25,13 +25,12 @@ remove_wordpress # Delete symlink to remove_project_symlink -# Remove "extra" config. -composer config --unset extra.wordpress-install-dir -composer config --unset extra.installer-paths - # Remove testing dependencies from Composer. remove_local_test_library +# Clean composer.json. +cleanup_composer_file + # Delete any missed files in the removal of the local installation. # And rebuild composer.lock cleanup_local_files diff --git a/composer.json b/composer.json index 9b930e4c..c0073b9e 100644 --- a/composer.json +++ b/composer.json @@ -20,12 +20,6 @@ "role": "Lead developer" } ], - "repositories": [ - { - "type": "composer", - "url": "https://wpackagist.org" - } - ], "require": { "php": ">=7.1.0", "firebase/php-jwt": "^6.1.0" @@ -39,8 +33,7 @@ "process-timeout": 0, "sort-packages": true, "allow-plugins": { - "johnpbloch/wordpress-core-installer": true, - "composer/installers": true + "johnpbloch/wordpress-core-installer": true } }, "autoload-dev": { @@ -48,18 +41,16 @@ "tests/_data/config.php" ], "psr-4": { - "Tests\\WPGraphQL\\WooCommerce\\": "tests/_support/" + "Tests\\WPGraphQL\\WooCommerce\\": "tests/_support/" } }, - "extra": { - }, "scripts": { "installTestEnv": "bash bin/install-test-env.local.sh", "runPreCommitCleanup": "bash bin/cleanup.local.sh", "runWPUnitTest": "vendor/bin/codecept run wpunit", "dBuild": "env $(sed -e '/^#/d' .env.testing) docker-compose build", "dRunApp": "env $(sed -e '/^#/d' .env.testing) docker-compose up testable_app app_db mailhog", - "dRunTestingDb":"if [ ! \"$(docker ps -a | grep testing_db)\" ]; then env $(sed -e '/^#/d' .env.testing) docker-compose up -d testing_db; fi", + "dRunTestingDb": "if [ ! \"$(docker ps -a | grep testing_db)\" ]; then env $(sed -e '/^#/d' .env.testing) docker-compose up -d testing_db; fi", "dSetMainDb": "docker-compose exec testable_app ./setup-database.sh main", "dSetTestingDb": "docker-compose exec testable_app wait-for-it -s -t 300 testing_db:3306 -- ./setup-database.sh testing", "dRunTest": [ @@ -69,30 +60,31 @@ ], "dRunTestStandalone": "env $(sed -e '/^#/d' .env.testing) docker-compose run --rm run_tests", "dDestroy": "docker-compose down -v", - "lint": "vendor/bin/phpcs -vp ." + "lint": "vendor/bin/phpcs -vp .", + "fix": "vendor/bin/phpcbf -vp ." }, "support": { "issues": "https://github.com/wp-graphql/wp-graphql-woocommerce/issues", "source": "https://github.com/wp-graphql/wp-graphql-woocommerce" }, "archive": { - "name": "wp-graphql-wooocommerce", - "exclude": [ - "/.*", - "/bin", - "/docs", - "/local", - "/plugin-build", - "/tests", - "!/vendor", - "/codeception.dist.yml", - "/codeception.yml", - "/composer.json", - "/composer.lock", - "/docker-compose.yml", - "/Dockerfile", - "/netlify.toml", - "/README.md" - ] + "name": "wp-graphql-wooocommerce", + "exclude": [ + "/.*", + "/bin", + "/docs", + "/local", + "/plugin-build", + "/tests", + "!/vendor", + "/codeception.dist.yml", + "/codeception.yml", + "/composer.json", + "/composer.lock", + "/docker-compose.yml", + "/Dockerfile", + "/netlify.toml", + "/README.md" + ] } } diff --git a/composer.lock b/composer.lock index 59fed2e0..3ee419c5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0ab5692d9101c6380ebe27e247a1a13a", + "content-hash": "49e1ecf4a41487b3de0547b8e3860c6a", "packages": [ { "name": "firebase/php-jwt", @@ -187,5 +187,5 @@ "php": ">=7.1.0" }, "platform-dev": [], - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.3.0" } diff --git a/docker-compose.yml b/docker-compose.yml index 97007516..e17ce04d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,8 +4,6 @@ services: # Main application database. app_db: image: mysql - volumes: - - app_db_data command: --default-authentication-plugin=mysql_native_password environment: MYSQL_ROOT_PASSWORD: ${ROOT_PASSWORD:-password} @@ -43,7 +41,8 @@ services: env_file: .env.testing environment: APACHE_RUN_USER: "#1000" # Ensure Apache can write to the filesystem. - XDEBUG_CONFIG: remote_host=10.0.0.52 idekey=vscode + DOCKER_GATEWAY_HOST: "172.17.0.1" + XDEBUG_CONFIG: remote_host=${DOCKER_GATEWAY_HOST:-host.docker.internal} idekey=vscode networks: development: ports: @@ -99,6 +98,3 @@ services: networks: development: testing: - -volumes: - app_db_data: {} diff --git a/includes/connection/class-orders.php b/includes/connection/class-orders.php index f8646914..878fac4a 100644 --- a/includes/connection/class-orders.php +++ b/includes/connection/class-orders.php @@ -114,29 +114,29 @@ class Orders { } $meta_query = false; - if ( ! empty ( $customer->get_id() ) ) { + if ( ! empty( $customer->get_id() ) ) { $meta_query = [ [ - 'key' => '_customer_user', - 'value' => $customer->get_id(), - 'compare' => '=', + 'key' => '_customer_user', + 'value' => $customer->get_id(), + 'compare' => '=', ], ]; - } else if ( ! empty( $customer->get_billing_email() ) ) { + } elseif ( ! empty( $customer->get_billing_email() ) ) { $meta_query = [ 'relation' => 'AND', [ - 'key' => '_billing_email', - 'value' => $customer->get_billing_email(), - 'compare' => '=', + 'key' => '_billing_email', + 'value' => $customer->get_billing_email(), + 'compare' => '=', ], [ - 'key' => '_customer_user', - 'value' => 0, - 'compare' => '=', - ] + 'key' => '_customer_user', + 'value' => 0, + 'compare' => '=', + ], ]; - } + }//end if // Bail if needed info not found on customer object. if ( false === $meta_query ) { diff --git a/includes/connection/class-products.php b/includes/connection/class-products.php index 5f68840d..d72cec17 100644 --- a/includes/connection/class-products.php +++ b/includes/connection/class-products.php @@ -366,11 +366,11 @@ class Products { '_wc_average_rating', '_sale_price_dates_from', '_sale_price_dates_to', - 'total_sales' + 'total_sales', ]; - $sort_fields = apply_filters( 'graphql_woocommerce_products_add_sort_fields', $default_fields ); + $sort_fields = apply_filters( 'graphql_woocommerce_products_add_sort_fields', $default_fields ); foreach ( $args['where']['orderby'] as $orderby_input ) { - if (in_array($orderby_input['field'], $sort_fields)){ + if ( in_array( $orderby_input['field'], $sort_fields, true ) ) { $order = $orderby_input['order']; if ( $backward ) { @@ -546,7 +546,7 @@ class Products { * @return array Query arguments. */ public static function map_input_fields_to_wp_query( $query_args, $args, $source, $all_args, $context, $info, $post_type ) { - $post_type = is_array( $post_type ) ? $post_type : array( $post_type ); + $post_type = is_array( $post_type ) ? $post_type : [ $post_type ]; if ( ! in_array( 'product', $post_type, true ) && ! in_array( 'product_variation', $post_type, true ) ) { return $query_args; } diff --git a/tests/_support/Factory/OrderFactory.php b/tests/_support/Factory/OrderFactory.php index 31ea0b1f..dc6af2e0 100644 --- a/tests/_support/Factory/OrderFactory.php +++ b/tests/_support/Factory/OrderFactory.php @@ -340,7 +340,7 @@ class OrderFactory extends \WP_UnitTest_Factory_For_Thing { } public function set_to_customer_billing_address( $order, $customer, $save = true ) { - $order = \wc_get_order( $order ); + $order = \wc_get_order( $order ); if ( ! $customer ) { return $order; } @@ -366,7 +366,7 @@ class OrderFactory extends \WP_UnitTest_Factory_For_Thing { } public function set_to_customer_shipping_address( $order, $customer, $save = true ) { - $order = \wc_get_order( $order ); + $order = \wc_get_order( $order ); if ( ! $customer ) { return $order; } diff --git a/tests/wpunit/CustomerMutationsTest.php b/tests/wpunit/CustomerMutationsTest.php index e3269f8d..4d980fbb 100644 --- a/tests/wpunit/CustomerMutationsTest.php +++ b/tests/wpunit/CustomerMutationsTest.php @@ -1,12 +1,11 @@ helper = $this->getModule( '\Helper\Wpunit' )->customer(); // Register Info $this->first_name = 'Peter'; @@ -99,9 +98,9 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { ); } - private function registerCustomer( $input ) { - $mutation = ' - mutation register( $input: RegisterCustomerInput! ) { + private function executeRegisterCustomerMutation( $input ) { + $query = ' + mutation( $input: RegisterCustomerInput! ) { registerCustomer( input: $input ) { clientMutationId authToken @@ -145,20 +144,14 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { '; $variables = [ 'input' => $input ]; - $actual = graphql( - [ - 'query' => $mutation, - 'operation_name' => 'register', - 'variables' => $variables, - ] - ); + $response = $this->graphql( compact( 'query', 'variables' ) ); - return $actual; + return $response; } - private function updateCustomer( $input ) { - $mutation = ' - mutation update( $input: UpdateCustomerInput! ) { + private function executeUpdateCustomerMutation( $input ) { + $query = ' + mutation( $input: UpdateCustomerInput! ) { updateCustomer( input: $input ) { clientMutationId authToken @@ -199,15 +192,9 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { '; $variables = [ 'input' => $input ]; - $actual = graphql( - [ - 'query' => $mutation, - 'operation_name' => 'update', - 'variables' => $variables, - ] - ); + $response = $this->graphql( compact( 'query', 'variables' ) ); - return $actual; + return $response; } public function testRegisterMutationWithoutCustomerInfo() { @@ -216,7 +203,7 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { * * Tests mutation without a providing WooCommerce specific customer information. */ - $actual = $this->registerCustomer( + $response = $this->executeRegisterCustomerMutation( [ 'clientMutationId' => 'someId', 'username' => $this->username, @@ -227,35 +214,29 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { ] ); - // use --debug flag to view. - codecept_debug( $actual ); - + // Assert user created. $user = get_user_by( 'email', 'peter.parker@dailybugle.com' ); $this->assertTrue( is_a( $user, WP_User::class ) ); $expected = [ - 'data' => [ - 'registerCustomer' => [ - 'clientMutationId' => 'someId', - 'authToken' => \WPGraphQL\JWT_Authentication\Auth::get_token( $user ), - 'refreshToken' => \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ), - 'customer' => [ - 'databaseId' => $user->ID, - 'email' => $this->email, - 'username' => $this->username, - 'firstName' => $this->first_name, - 'lastName' => $this->last_name, - 'billing' => $this->empty_billing(), - 'shipping' => $this->empty_shipping(), - ], - 'viewer' => [ - 'userId' => $user->ID, - ], - ], - ], + $this->expectedField( 'registerCustomer.authToken', \WPGraphQL\JWT_Authentication\Auth::get_token( $user ) ), + $this->expectedField( 'registerCustomer.refreshToken', \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ) ), + $this->expectedObject( + 'registerCustomer.customer', + [ + $this->expectedField( 'databaseId', $user->ID ), + $this->expectedField( 'email', $this->email ), + $this->expectedField( 'username', $this->username ), + $this->expectedField( 'firstName', $this->first_name ), + $this->expectedField( 'lastName', $this->last_name ), + $this->expectedField( 'billing', $this->empty_billing() ), + $this->expectedField( 'shipping', $this->empty_shipping() ), + ] + ), + $this->expectedField( 'registerCustomer.viewer.userId', $user->ID ), ]; - $this->assertEquals( $expected, $actual ); + $this->assertQuerySuccessful( $response, $expected ); } public function testRegisterMutationWithBillingInfo() { @@ -264,7 +245,7 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { * * Tests mutation with customer billing information. */ - $actual = $this->registerCustomer( + $response = $this->executeRegisterCustomerMutation( [ 'clientMutationId' => 'someId', 'username' => $this->username, @@ -276,35 +257,28 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { ] ); - // use --debug flag to view. - codecept_debug( $actual ); - $user = get_user_by( 'email', 'peter.parker@dailybugle.com' ); $this->assertTrue( is_a( $user, WP_User::class ) ); $expected = [ - 'data' => [ - 'registerCustomer' => [ - 'clientMutationId' => 'someId', - 'authToken' => \WPGraphQL\JWT_Authentication\Auth::get_token( $user ), - 'refreshToken' => \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ), - 'customer' => [ - 'databaseId' => $user->ID, - 'email' => $this->email, - 'username' => $this->username, - 'firstName' => $this->first_name, - 'lastName' => $this->last_name, - 'billing' => array_merge( $this->empty_billing(), $this->billing ), - 'shipping' => $this->empty_shipping(), - ], - 'viewer' => [ - 'userId' => $user->ID, - ], - ], - ], + $this->expectedField( 'registerCustomer.authToken', \WPGraphQL\JWT_Authentication\Auth::get_token( $user ) ), + $this->expectedField( 'registerCustomer.refreshToken', \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ) ), + $this->expectedObject( + 'registerCustomer.customer', + [ + $this->expectedField( 'databaseId', $user->ID ), + $this->expectedField( 'email', $this->email ), + $this->expectedField( 'username', $this->username ), + $this->expectedField( 'firstName', $this->first_name ), + $this->expectedField( 'lastName', $this->last_name ), + $this->expectedField( 'billing', array_merge( $this->empty_billing(), $this->billing ) ), + $this->expectedField( 'shipping', $this->empty_shipping() ), + ] + ), + $this->expectedField( 'registerCustomer.viewer.userId', $user->ID ), ]; - $this->assertEquals( $expected, $actual ); + $this->assertQuerySuccessful( $response, $expected ); } public function testRegisterMutationWithShippingSameAsBillingInfo() { @@ -313,7 +287,7 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { * * Tests mutation using "shippingSameAsBilling" field. */ - $actual = $this->registerCustomer( + $response = $this->executeRegisterCustomerMutation( [ 'clientMutationId' => 'someId', 'username' => $this->username, @@ -326,38 +300,34 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { ] ); - // use --debug flag to view. - codecept_debug( $actual ); - $user = get_user_by( 'email', 'peter.parker@dailybugle.com' ); $this->assertTrue( is_a( $user, WP_User::class ) ); $expected = [ - 'data' => [ - 'registerCustomer' => [ - 'clientMutationId' => 'someId', - 'authToken' => \WPGraphQL\JWT_Authentication\Auth::get_token( $user ), - 'refreshToken' => \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ), - 'customer' => [ - 'databaseId' => $user->ID, - 'email' => $this->email, - 'username' => $this->username, - 'firstName' => $this->first_name, - 'lastName' => $this->last_name, - 'billing' => array_merge( $this->empty_billing(), $this->billing ), - 'shipping' => array_merge( + $this->expectedField( 'registerCustomer.authToken', \WPGraphQL\JWT_Authentication\Auth::get_token( $user ) ), + $this->expectedField( 'registerCustomer.refreshToken', \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ) ), + $this->expectedObject( + 'registerCustomer.customer', + [ + $this->expectedField( 'databaseId', $user->ID ), + $this->expectedField( 'email', $this->email ), + $this->expectedField( 'username', $this->username ), + $this->expectedField( 'firstName', $this->first_name ), + $this->expectedField( 'lastName', $this->last_name ), + $this->expectedField( 'billing', array_merge( $this->empty_billing(), $this->billing ) ), + $this->expectedField( + 'shipping', + array_merge( $this->empty_shipping(), array_intersect_key( $this->billing, $this->empty_shipping() ) - ), - ], - 'viewer' => [ - 'userId' => $user->ID, - ], - ], - ], + ) + ), + ] + ), + $this->expectedField( 'registerCustomer.viewer.userId', $user->ID ), ]; - $this->assertEquals( $expected, $actual ); + $this->assertQuerySuccessful( $response, $expected ); } public function testRegisterMutationWithBillingAndShippingInfo() { @@ -366,7 +336,7 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { * * Tests mutation with customer shipping information. */ - $actual = $this->registerCustomer( + $response = $this->executeRegisterCustomerMutation( [ 'clientMutationId' => 'someId', 'username' => $this->username, @@ -379,35 +349,28 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { ] ); - // use --debug flag to view. - codecept_debug( $actual ); - $user = get_user_by( 'email', 'peter.parker@dailybugle.com' ); $this->assertTrue( is_a( $user, WP_User::class ) ); $expected = [ - 'data' => [ - 'registerCustomer' => [ - 'clientMutationId' => 'someId', - 'authToken' => \WPGraphQL\JWT_Authentication\Auth::get_token( $user ), - 'refreshToken' => \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ), - 'customer' => [ - 'databaseId' => $user->ID, - 'email' => $this->email, - 'username' => $this->username, - 'firstName' => $this->first_name, - 'lastName' => $this->last_name, - 'billing' => array_merge( $this->empty_billing(), $this->billing ), - 'shipping' => array_merge( $this->empty_shipping(), $this->shipping ), - ], - 'viewer' => [ - 'userId' => $user->ID, - ], - ], - ], + $this->expectedField( 'registerCustomer.authToken', \WPGraphQL\JWT_Authentication\Auth::get_token( $user ) ), + $this->expectedField( 'registerCustomer.refreshToken', \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ) ), + $this->expectedObject( + 'registerCustomer.customer', + [ + $this->expectedField( 'databaseId', $user->ID ), + $this->expectedField( 'email', $this->email ), + $this->expectedField( 'username', $this->username ), + $this->expectedField( 'firstName', $this->first_name ), + $this->expectedField( 'lastName', $this->last_name ), + $this->expectedField( 'billing', array_merge( $this->empty_billing(), $this->billing ) ), + $this->expectedField( 'shipping', array_merge( $this->empty_shipping(), $this->shipping ) ), + ] + ), + $this->expectedField( 'registerCustomer.viewer.userId', $user->ID ), ]; - $this->assertEquals( $expected, $actual ); + $this->assertQuerySuccessful( $response, $expected ); } public function testUpdateMutation() { @@ -416,7 +379,7 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { * * Tests mutation without a providing WooCommerce specific customer information. */ - $this->registerCustomer( + $this->executeRegisterCustomerMutation( [ 'clientMutationId' => 'someId', 'username' => $this->username, @@ -432,10 +395,10 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { $user = get_user_by( 'email', 'peter.parker@dailybugle.com' ); $this->assertTrue( is_a( $user, WP_User::class ) ); - $actual = $this->updateCustomer( + $response = $this->executeUpdateCustomerMutation( [ 'clientMutationId' => 'someId', - 'id' => $this->helper->to_relay_id( $user->ID ), + 'id' => $this->toRelayId( 'customer', $user->ID ), 'email' => $this->new_email, 'firstName' => $this->new_first_name, 'lastName' => $this->new_last_name, @@ -444,29 +407,22 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { ] ); - // use --debug flag to view. - codecept_debug( $actual ); - $expected = [ - 'data' => [ - 'updateCustomer' => [ - 'clientMutationId' => 'someId', - 'authToken' => \WPGraphQL\JWT_Authentication\Auth::get_token( $user ), - 'refreshToken' => \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ), - 'customer' => [ - 'databaseId' => $user->ID, - 'email' => $this->new_email, - 'username' => $this->username, - 'firstName' => $this->new_first_name, - 'lastName' => $this->new_last_name, - 'billing' => array_merge( $this->empty_billing(), $this->new_billing ), - 'shipping' => array_merge( $this->empty_shipping(), $this->new_shipping ), - ], - ], - ], + $this->expectedObject( + 'updateCustomer.customer', + [ + $this->expectedField( 'databaseId', $user->ID ), + $this->expectedField( 'email', $this->new_email ), + $this->expectedField( 'username', $this->username ), + $this->expectedField( 'firstName', $this->new_first_name ), + $this->expectedField( 'lastName', $this->new_last_name ), + $this->expectedField( 'billing', array_merge( $this->empty_billing(), $this->new_billing ) ), + $this->expectedField( 'shipping', array_merge( $this->empty_shipping(), $this->new_shipping ) ), + ] + ), ]; - $this->assertEquals( $expected, $actual ); + $this->assertQuerySuccessful( $response, $expected ); } public function testUpdateMutationWithShippingSameAsBilling() { @@ -475,7 +431,7 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { * * Tests mutation without a providing WooCommerce specific customer information. */ - $this->registerCustomer( + $this->executeRegisterCustomerMutation( [ 'clientMutationId' => 'someId', 'username' => $this->username, @@ -491,40 +447,36 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { $user = get_user_by( 'email', 'peter.parker@dailybugle.com' ); $this->assertTrue( is_a( $user, WP_User::class ) ); - $actual = $this->updateCustomer( + $response = $this->executeUpdateCustomerMutation( [ 'clientMutationId' => 'someId', - 'id' => $this->helper->to_relay_id( $user->ID ), + 'id' => $this->toRelayId( 'customer', $user->ID ), 'shippingSameAsBilling' => true, ] ); - // use --debug flag to view. - codecept_debug( $actual ); - $expected = [ - 'data' => [ - 'updateCustomer' => [ - 'clientMutationId' => 'someId', - 'authToken' => \WPGraphQL\JWT_Authentication\Auth::get_token( $user ), - 'refreshToken' => \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ), - 'customer' => [ - 'databaseId' => $user->ID, - 'email' => $this->email, - 'username' => $this->username, - 'firstName' => $this->first_name, - 'lastName' => $this->last_name, - 'billing' => array_merge( $this->empty_billing(), $this->billing ), - 'shipping' => array_merge( + $this->expectedObject( + 'updateCustomer.customer', + [ + $this->expectedField( 'databaseId', $user->ID ), + $this->expectedField( 'email', $this->email ), + $this->expectedField( 'username', $this->username ), + $this->expectedField( 'firstName', $this->first_name ), + $this->expectedField( 'lastName', $this->last_name ), + $this->expectedField( 'billing', array_merge( $this->empty_billing(), $this->billing ) ), + $this->expectedField( + 'shipping', + array_merge( $this->empty_shipping(), array_intersect_key( $this->billing, $this->empty_shipping() ) - ), - ], - ], - ], + ) + ), + ] + ), ]; - $this->assertEquals( $expected, $actual ); + $this->assertQuerySuccessful( $response, $expected ); } public function testRegisterMutationWithoutAnyInfo() { @@ -533,7 +485,7 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { * * Tests mutation without a providing an username and password. */ - $actual = $this->registerCustomer( + $response = $this->executeRegisterCustomerMutation( [ 'clientMutationId' => 'someId', 'email' => $this->email, @@ -542,35 +494,24 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { ] ); - // use --debug flag to view. - codecept_debug( $actual ); - $user = get_user_by( 'email', 'peter.parker@dailybugle.com' ); $this->assertTrue( is_a( $user, WP_User::class ) ); $expected = [ - 'data' => [ - 'registerCustomer' => [ - 'clientMutationId' => 'someId', - 'authToken' => \WPGraphQL\JWT_Authentication\Auth::get_token( $user ), - 'refreshToken' => \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ), - 'customer' => [ - 'databaseId' => $user->ID, - 'email' => $this->email, - 'username' => $user->user_login, - 'firstName' => $this->first_name, - 'lastName' => $this->last_name, - 'billing' => $this->empty_billing(), - 'shipping' => $this->empty_shipping(), - ], - 'viewer' => [ - 'userId' => $user->ID, - ], - ], - ], + $this->expectedObject( + 'registerCustomer.customer', + [ + $this->expectedField( 'databaseId', $user->ID ), + $this->expectedField( 'email', $this->email ), + $this->expectedField( 'firstName', $this->first_name ), + $this->expectedField( 'lastName', $this->last_name ), + $this->expectedField( 'billing', $this->empty_billing() ), + $this->expectedField( 'shipping', $this->empty_shipping() ), + ] + ), ]; - $this->assertEquals( $expected, $actual ); + $this->assertQuerySuccessful( $response, $expected ); } public function testCustomerMutationsWithMeta() { @@ -606,30 +547,29 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { ], ]; - $actual = graphql( compact( 'query', 'variables' ) ); - codecept_debug( $actual ); + $response = $this->graphql( compact( 'query', 'variables' ) ); $user = get_user_by( 'email', 'user@woographql.test' ); $this->assertTrue( is_a( $user, WP_User::class ) ); $expected = [ - 'data' => [ - 'registerCustomer' => [ - 'customer' => [ - 'databaseId' => $user->ID, - 'email' => 'user@woographql.test', - 'metaData' => [ - [ - 'key' => 'test_meta_key', - 'value' => 'test_meta_value', - ], - ], - ], - ], - ], + $this->expectedObject( + 'registerCustomer.customer', + [ + $this->expectedField( 'databaseId', $user->ID ), + $this->expectedField( 'email', 'user@woographql.test' ), + $this->expectedNode( + 'metaData', + [ + $this->expectedField( 'key', 'test_meta_key' ), + $this->expectedField( 'value', 'test_meta_value' ), + ] + ), + ] + ), ]; - $this->assertEquals( $expected, $actual ); + $this->assertQuerySuccessful( $response, $expected ); /** * Assertion Two @@ -653,7 +593,7 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { $variables = [ 'input' => [ 'clientMutationId' => 'some_id', - 'id' => \GraphQLRelay\Relay::toGlobalId( 'customer', $user->ID ), + 'id' => $this->toRelayId( 'customer', $user->ID ), 'metaData' => [ [ 'key' => 'test_meta_key', @@ -663,27 +603,25 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { ], ]; - $actual = graphql( compact( 'query', 'variables' ) ); - codecept_debug( $actual ); - + $response = $this->graphql( compact( 'query', 'variables' ) ); $expected = [ - 'data' => [ - 'updateCustomer' => [ - 'customer' => [ - 'databaseId' => $user->ID, - 'email' => 'user@woographql.test', - 'metaData' => [ - [ - 'key' => 'test_meta_key', - 'value' => 'new_meta_value', - ], - ], - ], - ], - ], + $this->expectedObject( + 'updateCustomer.customer', + [ + $this->expectedField( 'databaseId', $user->ID ), + $this->expectedField( 'email', 'user@woographql.test' ), + $this->expectedNode( + 'metaData', + [ + $this->expectedField( 'key', 'test_meta_key' ), + $this->expectedField( 'value', 'new_meta_value' ), + ] + ), + ] + ), ]; - $this->assertEquals( $expected, $actual ); + $this->assertQuerySuccessful( $response, $expected ); /** * Assertion Three @@ -715,25 +653,23 @@ class CustomerMutationsTest extends \Codeception\TestCase\WPTestCase { ], ]; - $actual = graphql( compact( 'query', 'variables' ) ); - codecept_debug( $actual ); - + $response = $this->graphql( compact( 'query', 'variables' ) ); $expected = [ - 'data' => [ - 'updateCustomer' => [ - 'customer' => [ - 'id' => 'guest', - 'metaData' => [ - [ - 'key' => 'test_meta_key', - 'value' => 'test_meta_value', - ], - ], - ], - ], - ], + $this->expectedObject( + 'updateCustomer.customer', + [ + $this->expectedField( 'id', 'guest' ), + $this->expectedNode( + 'metaData', + [ + $this->expectedField( 'key', 'test_meta_key' ), + $this->expectedField( 'value', 'test_meta_value' ), + ] + ), + ] + ), ]; - $this->assertEquals( $expected, $actual ); + $this->assertQuerySuccessful( $response, $expected ); } } diff --git a/tests/wpunit/CustomerQueriesTest.php b/tests/wpunit/CustomerQueriesTest.php index 9fe0fe32..fb122885 100644 --- a/tests/wpunit/CustomerQueriesTest.php +++ b/tests/wpunit/CustomerQueriesTest.php @@ -406,16 +406,16 @@ class CustomerQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraph public function testCustomerToOrdersConnection() { $new_customer_id = $this->factory->customer->create(); - $order_1 = $this->factory->order->createNew( + $order_1 = $this->factory->order->createNew( [ 'customer_id' => $this->customer ] ); - $order_2 = $this->factory->order->createNew( + $order_2 = $this->factory->order->createNew( [ 'customer_id' => $new_customer_id ] ); - $guest_customer = new \WC_Customer(); + $guest_customer = new \WC_Customer(); $guest_customer->set_billing_email( 'test@test.com' ); - $order_3 = $this->factory->order->createNew( + $order_3 = $this->factory->order->createNew( [ 'customer_id' => $guest_customer->get_id(), 'billing_email' => $guest_customer->get_billing_email(), @@ -454,8 +454,8 @@ class CustomerQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraph */ $this->loginAs( 0 ); WC()->customer = $guest_customer; - $response = $this->graphql( compact( 'query' ) ); - $expected = [ + $response = $this->graphql( compact( 'query' ) ); + $expected = [ $this->expectedField( 'customer.orders.nodes.#.databaseId', $order_3 ), ];