2021-05-24 11:40:56 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Factory class for the WooCommerce's customer data objects.
|
|
|
|
|
*
|
|
|
|
|
* @since v0.10.0
|
|
|
|
|
* @package Tests\WPGraphQL\WooCommerce\Factory
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Tests\WPGraphQL\WooCommerce\Factory;
|
|
|
|
|
|
2023-08-22 20:18:47 -04:00
|
|
|
use Faker\Factory;
|
2021-05-24 11:40:56 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Customer factory class for testing.
|
|
|
|
|
*/
|
|
|
|
|
class CustomerFactory extends \WP_UnitTest_Factory_For_Thing {
|
2022-02-04 15:00:44 -05:00
|
|
|
public function __construct( $factory = null ) {
|
2021-05-24 11:40:56 -04:00
|
|
|
parent::__construct( $factory );
|
|
|
|
|
|
2022-08-26 14:53:36 -04:00
|
|
|
$this->default_generation_definitions = [];
|
2023-08-22 20:18:47 -04:00
|
|
|
$this->dummy = Factory::create();
|
2021-05-24 11:40:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create_object( $args ) {
|
2022-02-04 15:00:44 -05:00
|
|
|
if ( is_wp_error( $args ) ) {
|
|
|
|
|
codecept_debug( $args );
|
|
|
|
|
}
|
2021-05-24 11:40:56 -04:00
|
|
|
|
|
|
|
|
$customer = new \WC_Customer();
|
|
|
|
|
|
|
|
|
|
// Create customer details
|
2026-03-18 13:13:45 -04:00
|
|
|
$username = empty( $args['username'] ) ? $this->dummy->userName() : $args['username'];
|
|
|
|
|
$first_name = empty( $args['first_name'] ) ? $this->dummy->firstName() : $args['first_name'];
|
|
|
|
|
$last_name = empty( $args['last_name'] ) ? $this->dummy->lastName() : $args['last_name'];
|
|
|
|
|
$street = empty( $args['street'] ) ? $this->dummy->streetAddress() : $args['street'];
|
|
|
|
|
$city = empty( $args['city'] ) ? $this->dummy->city() : $args['city'];
|
|
|
|
|
$state = empty( $args['state'] ) ? $this->dummy->state() : $args['state'];
|
|
|
|
|
$postcode = empty( $args['postcode'] ) ? $this->dummy->postcode() : $args['postcode'];
|
|
|
|
|
$country = empty( $args['country'] ) ? 'US' : $args['country'];
|
|
|
|
|
$email = empty( $args['email'] ) ? $this->dummy->email() : $args['email'];
|
|
|
|
|
$phone = empty( $args['phone'] ) ? $this->dummy->phoneNumber() : $args['phone'];
|
2021-05-24 11:40:56 -04:00
|
|
|
|
2021-07-05 13:51:52 -04:00
|
|
|
$args = array_merge(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
|
|
|
|
'billing' => [],
|
|
|
|
|
'shipping' => [],
|
|
|
|
|
],
|
2021-07-05 13:51:52 -04:00
|
|
|
$args
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$customer->set_billing_first_name( ! empty( $args['billing']['first_name'] ) ? $args['billing']['first_name'] : $first_name );
|
|
|
|
|
$customer->set_billing_last_name( ! empty( $args['billing']['last_name'] ) ? $args['billing']['last_name'] : $last_name );
|
|
|
|
|
$customer->set_billing_address_1( ! empty( $args['billing']['address_1'] ) ? $args['billing']['address_1'] : $street );
|
|
|
|
|
$customer->set_billing_address_2( ! empty( $args['billing']['address_2'] ) ? $args['billing']['address_2'] : '' );
|
|
|
|
|
$customer->set_billing_city( ! empty( $args['billing']['city'] ) ? $args['billing']['city'] : $city );
|
|
|
|
|
$customer->set_billing_state( ! empty( $args['billing']['state'] ) ? $args['billing']['state'] : $state );
|
|
|
|
|
$customer->set_billing_postcode( ! empty( $args['billing']['postcode'] ) ? $args['billing']['postcode'] : $postcode );
|
|
|
|
|
$customer->set_billing_country( ! empty( $args['billing']['country'] ) ? $args['billing']['country'] : $country );
|
|
|
|
|
$customer->set_billing_email( ! empty( $args['billing']['email'] ) ? $args['billing']['email'] : $email );
|
|
|
|
|
$customer->set_billing_phone( ! empty( $args['billing']['phone'] ) ? $args['billing']['phone'] : $phone );
|
|
|
|
|
$customer->set_shipping_first_name( ! empty( $args['shipping']['first_name'] ) ? $args['shipping']['first_name'] : $first_name );
|
|
|
|
|
$customer->set_shipping_last_name( ! empty( $args['shipping']['last_name'] ) ? $args['shipping']['last_name'] : $last_name );
|
|
|
|
|
$customer->set_shipping_address_1( ! empty( $args['shipping']['address_1'] ) ? $args['shipping']['address_1'] : $street );
|
|
|
|
|
$customer->set_shipping_address_2( ! empty( $args['shipping']['address_2'] ) ? $args['shipping']['address_2'] : '' );
|
|
|
|
|
$customer->set_shipping_city( ! empty( $args['shipping']['city'] ) ? $args['shipping']['city'] : $city );
|
|
|
|
|
$customer->set_shipping_state( ! empty( $args['shipping']['state'] ) ? $args['shipping']['state'] : $state );
|
|
|
|
|
$customer->set_shipping_postcode( ! empty( $args['shipping']['postcode'] ) ? $args['shipping']['postcode'] : $postcode );
|
|
|
|
|
$customer->set_shipping_country( ! empty( $args['shipping']['country'] ) ? $args['shipping']['country'] : $country );
|
2023-08-22 20:18:47 -04:00
|
|
|
$customer->set_shipping_phone( ! empty( $args['shipping']['phone'] ) ? $args['shipping']['phone'] : $phone );
|
2021-07-05 13:51:52 -04:00
|
|
|
|
2021-05-24 11:40:56 -04:00
|
|
|
// Set data.
|
|
|
|
|
$customer->set_props(
|
|
|
|
|
array_merge(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2021-05-24 11:40:56 -04:00
|
|
|
'email' => $email,
|
|
|
|
|
'first_name' => $first_name,
|
|
|
|
|
'last_name' => $last_name,
|
|
|
|
|
'display_name' => $username,
|
|
|
|
|
'role' => 'customer',
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'is_paying_customer' => false,
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
2021-05-24 11:40:56 -04:00
|
|
|
$args
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Set meta data.
|
|
|
|
|
if ( ! empty( $args['meta_data'] ) ) {
|
|
|
|
|
$customer->set_meta_data( $args['meta_data'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $customer->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update_object( $object, $fields ) {
|
|
|
|
|
if ( ! $object instanceof \WC_Customer && 0 !== absint( $object ) ) {
|
|
|
|
|
$object = $this->get_object_by_id( $object );
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 15:00:44 -05:00
|
|
|
foreach ( $fields as $field => $field_value ) {
|
2022-08-26 14:53:36 -04:00
|
|
|
if ( ! is_callable( [ $object, "set_{$field}" ] ) ) {
|
2021-05-24 11:40:56 -04:00
|
|
|
throw new \Exception(
|
|
|
|
|
sprintf( '"%1$s" is not a valid %2$s coupon field.', $field, $object->get_type() )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$object->{"set_{$field}"}( $field_value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$object->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function get_object_by_id( $id ) {
|
|
|
|
|
return new \WC_Customer( $id );
|
|
|
|
|
}
|
|
|
|
|
}
|