Skip to content

Commit a51cb59

Browse files
committed
Cart* test refactored heavily to extend the WPGraphQLTestCase
1 parent 993df4b commit a51cb59

File tree

13 files changed

+8597
-687
lines changed

13 files changed

+8597
-687
lines changed

includes/type/object/class-cart-type.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,21 +398,21 @@ public static function register_cart_fee() {
398398
'type' => 'Boolean',
399399
'description' => __( 'Is fee taxable?', 'wp-graphql-woocommerce' ),
400400
'resolve' => function( $source ) {
401-
return ! empty( $source->taxable ) ? $source->taxable : null;
401+
return ! is_null( $source->taxable ) ? $source->taxable : null;
402402
},
403403
),
404404
'amount' => array(
405405
'type' => 'Float',
406406
'description' => __( 'Fee amount', 'wp-graphql-woocommerce' ),
407407
'resolve' => function( $source ) {
408-
return ! empty( $source->amount ) ? $source->amount : null;
408+
return ! is_null( $source->amount ) ? $source->amount : 0;
409409
},
410410
),
411411
'total' => array(
412412
'type' => 'Float',
413413
'description' => __( 'Fee total', 'wp-graphql-woocommerce' ),
414414
'resolve' => function( $source ) {
415-
return ! empty( $source->total ) ? $source->total : null;
415+
return ! is_null( $source->total ) ? $source->total : 0;
416416
},
417417
),
418418
),

tests/_support/Factory/CartFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Factory class for the WooCommerce's Cart data objects.
44
*
5-
* @since v0.6.0
5+
* @since v0.6.1
66
* @package Tests\WPGraphQL\WooCommerce\Factory
77
*/
88

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Factory class for the WooCommerce's coupon data objects.
4+
*
5+
* @since v0.6.1
6+
* @package Tests\WPGraphQL\WooCommerce\Factory
7+
*/
8+
9+
namespace Tests\WPGraphQL\WooCommerce\Factory;
10+
11+
use Tests\WPGraphQL\WooCommerce\Utils\Dummy;
12+
13+
/**
14+
* Coupon factory class for testing.
15+
*/
16+
class CouponFactory extends \WP_UnitTest_Factory_For_Thing {
17+
function __construct( $factory = null ) {
18+
parent::__construct( $factory );
19+
20+
$this->default_generation_definitions = array(
21+
'coupon_class' => '\WC_Coupon',
22+
);
23+
}
24+
25+
public function create_object( $args ) {
26+
if ( is_wp_error( $args ) ) codecept_debug( $args );
27+
$coupon_class = $args['coupon_class' ];
28+
unset( $args['coupon_class'] );
29+
30+
$coupon = new $coupon_class();
31+
32+
$amount = Dummy::instance()->number( 0, 75 );
33+
$coupon->set_props(
34+
array_merge(
35+
array(
36+
'code' => $amount . 'off',
37+
'amount' => floatval( $amount ),
38+
'date_expires' => null,
39+
'discount_type' => 'percent',
40+
'description' => 'Test coupon',
41+
),
42+
$args
43+
)
44+
);
45+
46+
// Set meta data.
47+
if ( ! empty( $args['meta_data'] ) ) {
48+
$coupon->set_meta_data( $args['meta_data'] );
49+
}
50+
51+
return $coupon->save();
52+
}
53+
54+
public function update_object( $object, $fields ) {
55+
if ( ! $object instanceof \WC_Coupon && 0 !== absint( $object ) ) {
56+
$object = $this->get_object_by_id( $object );
57+
}
58+
59+
foreach( $fields as $field => $field_value ) {
60+
if ( ! is_callable( array( $object, "set_{$field}" ) ) ) {
61+
throw new \Exception(
62+
sprintf( '"%1$s" is not a valid %2$s coupon field.', $field, $object->get_type() )
63+
);
64+
}
65+
66+
$object->{"set_{$field}"}( $field_value );
67+
}
68+
69+
$object->save();
70+
}
71+
72+
public function get_object_by_id( $id ) {
73+
return new \WC_Coupon( $id );
74+
}
75+
}

tests/_support/Factory/ProductFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Factory class for the WooCommerce's Product data objects.
44
*
5-
* @since v0.6.0
5+
* @since v0.6.1
66
* @package Tests\WPGraphQL\WooCommerce\Factory
77
*/
88

@@ -17,7 +17,6 @@ class ProductFactory extends \WP_UnitTest_Factory_For_Thing {
1717
function __construct( $factory = null ) {
1818
parent::__construct( $factory );
1919

20-
$name = Dummy::instance()->product();
2120
$this->default_generation_definitions = array(
2221
'product_class' => '\WC_Product_Simple',
2322
);

tests/_support/Factory/ProductVariationFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Factory class for the WooCommerce's Product variation data objects.
44
*
5-
* @since v0.6.0
5+
* @since v0.6.1
66
* @package Tests\WPGraphQL\WooCommerce\Factory
77
*/
88

tests/_support/TestCase/WooGraphQLTestCase.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class WooGraphQLTestCase extends \Tests\WPGraphQL\TestCase\WPGraphQLTestCase {
2828
/**
2929
* Creates users and loads factories.
3030
*/
31-
function setUp(): void {
31+
public function setUp(): void {
3232
parent::setUp();
3333

3434
// Create users.
@@ -40,6 +40,7 @@ function setUp(): void {
4040
'Product',
4141
'ProductVariation',
4242
'Cart',
43+
'Coupon',
4344
);
4445

4546
foreach ( $factories as $factory ) {
@@ -49,6 +50,13 @@ function setUp(): void {
4950
}
5051
}
5152

53+
public function tearDown(): void {
54+
\WC()->cart->empty_cart( true );
55+
56+
// then
57+
parent::tearDown();
58+
}
59+
5260
/**
5361
* Logs in as a "shop manager"
5462
*/

0 commit comments

Comments
 (0)