Skip to content

Commit 6e0aa5d

Browse files
authored
Merge pull request #322 from wp-graphql/devops/graphql-testcase
WPGraphQLTestCase implemented
2 parents e14e712 + 888ac96 commit 6e0aa5d

21 files changed

+2828
-1927
lines changed

composer.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@
3333
},
3434
"require-dev": {
3535
"composer/installers": "^1.9",
36-
"lucatume/wp-browser": "^2.4",
36+
"lucatume/wp-browser": "^2.6.",
37+
"wp-graphql/wp-graphql-testcase": "^1.0.1",
3738
"codeception/module-asserts": "^1.0",
3839
"codeception/util-universalframework": "^1.0",
40+
"codeception/module-rest": "^1.2",
3941
"simpod/php-coveralls-mirror": "^3.0",
4042
"stripe/stripe-php": "^7.28",
4143
"wp-coding-standards/wpcs": "^2.3",
4244
"johnpbloch/wordpress": "~5.3",
43-
"wp-graphql/wp-graphql": "^1.1.4",
4445
"wp-graphql/wp-graphql-jwt-authentication": "^0.4.1",
46+
"wpackagist-plugin/wp-graphql": "^1.1.8",
4547
"wpackagist-plugin/woocommerce": "^4.4",
4648
"wpackagist-plugin/woocommerce-gateway-stripe": "^4.5",
4749
"wpackagist-theme/twentytwentyone": "^1.0",
@@ -50,7 +52,8 @@
5052
},
5153
"config": {
5254
"optimize-autoloader": true,
53-
"process-timeout": 0
55+
"process-timeout": 0,
56+
"sort-packages": true
5457
},
5558
"autoload": {
5659
"files": [
@@ -67,7 +70,10 @@
6770
"autoload-dev": {
6871
"files": [
6972
"tests/_data/config.php"
70-
]
73+
],
74+
"psr-4": {
75+
"Tests\\WPGraphQL\\WooCommerce\\": "tests/_support/"
76+
}
7177
},
7278
"extra": {
7379
"wordpress-install-dir": "local/public",

includes/model/class-product.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,16 @@ protected function init() {
329329
return ! is_null( $this->wc_data->is_sold_individually() ) ? $this->wc_data->is_sold_individually() : null;
330330
},
331331
'weight' => function() {
332-
return ! is_null( $this->wc_data->get_weight() ) ? $this->wc_data->get_weight() : null;
332+
return ! empty( $this->wc_data->get_weight() ) ? $this->wc_data->get_weight() : null;
333333
},
334334
'length' => function() {
335-
return ! is_null( $this->wc_data->get_length() ) ? $this->wc_data->get_length() : null;
335+
return ! empty( $this->wc_data->get_length() ) ? $this->wc_data->get_length() : null;
336336
},
337337
'width' => function() {
338-
return ! is_null( $this->wc_data->get_width() ) ? $this->wc_data->get_width() : null;
338+
return ! empty( $this->wc_data->get_width() ) ? $this->wc_data->get_width() : null;
339339
},
340340
'height' => function() {
341-
return ! is_null( $this->wc_data->get_height() ) ? $this->wc_data->get_height() : null;
341+
return ! empty( $this->wc_data->get_height() ) ? $this->wc_data->get_height() : null;
342342
},
343343
'shippingClassId' => function () {
344344
return ! empty( $this->wc_data->get_image_id() ) ? $this->wc_data->get_shipping_class_id() : null;

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
),

phpcs.xml.dist

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@
33
<config name="installed_paths" value="vendor/wp-coding-standards/wpcs" />
44
<description>Generally-applicable sniffs for WordPress plugins.</description>
55

6-
<!-- What to scan -->
7-
<exclude-pattern>*/vendor/*</exclude-pattern>
8-
<exclude-pattern>*/node_modules/*</exclude-pattern>
9-
<exclude-pattern>local/*</exclude-pattern>
10-
<exclude-pattern>*/tests/*</exclude-pattern>
11-
126
<!-- How to scan -->
137
<!-- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
148
<!-- Annotated ruleset: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
15-
<arg value="sp"/> <!-- Show sniff and progress -->
169
<arg name="basepath" value="./"/><!-- Strip the file paths down to the relevant bit -->
17-
<arg name="colors"/>
1810
<arg name="extensions" value="php"/>
11+
<arg name="colors"/>
12+
<arg value="sp"/> <!-- Show sniff and progress -->
13+
1914
<arg name="parallel" value="8"/><!-- Enables parallel processing when available for faster results. -->
2015
<!-- Rules: Check PHP version compatibility -->
2116
<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
@@ -46,4 +41,14 @@
4641
<property name="blank_line_check" value="true"/>
4742
</properties>
4843
</rule>
44+
45+
<!-- What to scan -->
46+
<file>wp-graphql-woocommerce.php</file>
47+
<file>access-functions.php</file>
48+
<file>class-inflect.php</file>
49+
<file>includes/</file>
50+
<exclude-pattern>/vendor/</exclude-pattern>
51+
<exclude-pattern>/node_modules/</exclude-pattern>
52+
<exclude-pattern>local/*</exclude-pattern>
53+
<exclude-pattern>/tests/</exclude-pattern>
4954
</ruleset>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Factory class for the WooCommerce's Cart 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+
* Cart factory class for testing.
15+
*/
16+
class CartFactory {
17+
/**
18+
* Add products to the cart.
19+
*
20+
* @param array ...$products Product to be added to the cart.
21+
*
22+
* @return array
23+
*/
24+
public function add( ...$products ) {
25+
$keys = array();
26+
27+
foreach( $products as $product ) {
28+
if ( gettype( $product ) === 'array' ) {
29+
if ( empty( $product['product_id'] ) ) {
30+
codecept_debug( $product );
31+
codecept_debug( 'IS AN INVALID CART ITEM' );
32+
continue;
33+
}
34+
35+
$keys[] = WC()->cart->add_to_cart(
36+
$product['product_id'],
37+
! empty( $product['quantity'] ) ? $product['quantity'] : 1,
38+
! empty( $product['variation_id'] ) ? $product['variation_id'] : 0,
39+
! empty( $product['variation'] ) ? $product['variation'] : array(),
40+
! empty( $product['cart_item_data'] ) ? $product['cart_item_data'] : array()
41+
);
42+
} else {
43+
WC()->cart->add_to_cart( $product, 1 );
44+
}
45+
}
46+
47+
return $keys;
48+
}
49+
50+
public function remove( ...$keys ) {
51+
foreach ( $keys as $key ) {
52+
$success = \WC()->cart->remove_cart_item( $key );
53+
if ( false === $success ) {
54+
codecept_debug( "FAILED TO REMOVE ITEM {$key} FROM CART." );
55+
}
56+
}
57+
}
58+
}
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+
}

0 commit comments

Comments
 (0)