From 46b47dc97b1140dc42f51209a5b364e81eca7ebc Mon Sep 17 00:00:00 2001
From: Pedro Silva
Date: Thu, 26 Oct 2023 10:58:05 +0100
Subject: [PATCH] Fix lint and tests
---
modules/ppcp-applepay/services.php | 2 +-
modules/ppcp-applepay/src/ApplepayModule.php | 59 +++---
.../src/Assets/ApplePayDataObjectHttp.php | 18 +-
modules/ppcp-button/services.php | 2 +-
.../src/Endpoint/SimulateCartEndpoint.php | 10 +-
.../src/Helper/CartProductsHelper.php | 55 +++--
.../ppcp-googlepay/src/GooglepayModule.php | 192 +++++++++---------
.../Endpoint/ChangeCartEndpointTest.php | 7 +-
8 files changed, 181 insertions(+), 164 deletions(-)
diff --git a/modules/ppcp-applepay/services.php b/modules/ppcp-applepay/services.php
index 6274a5fb8..a49ab7fad 100644
--- a/modules/ppcp-applepay/services.php
+++ b/modules/ppcp-applepay/services.php
@@ -109,7 +109,7 @@ return array(
$container->get( 'applepay.data_to_scripts' ),
$container->get( 'wcgateway.settings.status' ),
$container->get( 'button.helper.cart-products' )
- );
+ );
},
'applepay.blocks-payment-method' => static function ( ContainerInterface $container ): PaymentMethodTypeInterface {
return new BlocksPaymentMethod(
diff --git a/modules/ppcp-applepay/src/ApplepayModule.php b/modules/ppcp-applepay/src/ApplepayModule.php
index c63750fbc..81aedbc2f 100644
--- a/modules/ppcp-applepay/src/ApplepayModule.php
+++ b/modules/ppcp-applepay/src/ApplepayModule.php
@@ -40,6 +40,7 @@ class ApplepayModule implements ModuleInterface {
* {@inheritDoc}
*/
public function run( ContainerInterface $c ): void {
+ $module = $this;
// Clears product status when appropriate.
add_action(
@@ -51,38 +52,44 @@ class ApplepayModule implements ModuleInterface {
}
);
- // Check if the module is applicable, correct country, currency, ... etc.
- if ( ! $c->get( 'applepay.eligible' ) ) {
- return;
- }
+ add_action(
+ 'init',
+ static function () use ( $c, $module ) {
- // Load the button handler.
- $apple_payment_method = $c->get( 'applepay.button' );
- // add onboarding and referrals hooks.
- assert( $apple_payment_method instanceof ApplepayButton );
- $apple_payment_method->initialize();
+ // Check if the module is applicable, correct country, currency, ... etc.
+ if ( ! $c->get( 'applepay.eligible' ) ) {
+ return;
+ }
- // Show notice if there are product availability issues.
- $availability_notice = $c->get( 'applepay.availability_notice' );
- assert( $availability_notice instanceof AvailabilityNotice );
- $availability_notice->execute();
+ // Load the button handler.
+ $apple_payment_method = $c->get( 'applepay.button' );
+ // add onboarding and referrals hooks.
+ assert( $apple_payment_method instanceof ApplepayButton );
+ $apple_payment_method->initialize();
- // Return if server not supported.
- if ( ! $c->get( 'applepay.server_supported' ) ) {
- return;
- }
+ // Show notice if there are product availability issues.
+ $availability_notice = $c->get( 'applepay.availability_notice' );
+ assert( $availability_notice instanceof AvailabilityNotice );
+ $availability_notice->execute();
- // Check if this merchant can activate / use the buttons.
- // We allow non referral merchants as they can potentially still use ApplePay, we just have no way of checking the capability.
- if ( ( ! $c->get( 'applepay.available' ) ) && $c->get( 'applepay.is_referral' ) ) {
- return;
- }
+ // Return if server not supported.
+ if ( ! $c->get( 'applepay.server_supported' ) ) {
+ return;
+ }
- $this->load_assets( $c, $apple_payment_method );
- $this->handle_validation_file( $c );
- $this->render_buttons( $c, $apple_payment_method );
+ // Check if this merchant can activate / use the buttons.
+ // We allow non referral merchants as they can potentially still use ApplePay, we just have no way of checking the capability.
+ if ( ( ! $c->get( 'applepay.available' ) ) && $c->get( 'applepay.is_referral' ) ) {
+ return;
+ }
- $apple_payment_method->bootstrap_ajax_request();
+ $module->load_assets( $c, $apple_payment_method );
+ $module->handle_validation_file( $c );
+ $module->render_buttons( $c, $apple_payment_method );
+
+ $apple_payment_method->bootstrap_ajax_request();
+ }
+ );
}
/**
diff --git a/modules/ppcp-applepay/src/Assets/ApplePayDataObjectHttp.php b/modules/ppcp-applepay/src/Assets/ApplePayDataObjectHttp.php
index 64b49f8f9..219e3c6d9 100644
--- a/modules/ppcp-applepay/src/Assets/ApplePayDataObjectHttp.php
+++ b/modules/ppcp-applepay/src/Assets/ApplePayDataObjectHttp.php
@@ -62,21 +62,21 @@ class ApplePayDataObjectHttp {
/**
* The product variations.
*
- * @var string
+ * @var array
*/
protected $product_variations = array();
/**
* The product extra.
*
- * @var string
+ * @var array
*/
protected $product_extra = array();
/**
* The product booking.
*
- * @var string
+ * @var array
*/
protected $product_booking = array();
@@ -295,7 +295,7 @@ class ApplePayDataObjectHttp {
/**
* Pre-processes request data to transform it to a standard format.
*
- * @param array $data
+ * @param array $data The data.
* @return array
*/
protected function preprocess_request_data( array $data ): array {
@@ -602,7 +602,7 @@ class ApplePayDataObjectHttp {
/**
* Returns the product variations.
*
- * @return string
+ * @return array
*/
public function product_variations(): array {
return $this->product_variations;
@@ -611,7 +611,7 @@ class ApplePayDataObjectHttp {
/**
* Returns the product extra.
*
- * @return string
+ * @return array
*/
public function product_extra(): array {
return $this->product_extra;
@@ -620,7 +620,7 @@ class ApplePayDataObjectHttp {
/**
* Returns the product booking.
*
- * @return string
+ * @return array
*/
public function product_booking(): array {
return $this->product_booking;
@@ -698,6 +698,10 @@ class ApplePayDataObjectHttp {
)
);
+ if ( ! $data ) {
+ return false;
+ }
+
return $this->append_products_to_data( $data, $_POST );
}
diff --git a/modules/ppcp-button/services.php b/modules/ppcp-button/services.php
index 374f15910..9267307d8 100644
--- a/modules/ppcp-button/services.php
+++ b/modules/ppcp-button/services.php
@@ -254,7 +254,7 @@ return array(
},
'button.helper.cart-products' => static function ( ContainerInterface $container ): CartProductsHelper {
- $data_store = \WC_Data_Store::load( 'product' );
+ $data_store = \WC_Data_Store::load( 'product' );
return new CartProductsHelper( $data_store );
},
diff --git a/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php b/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php
index e1b1247c4..1040cc680 100644
--- a/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php
+++ b/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php
@@ -52,11 +52,11 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
CartProductsHelper $cart_products,
LoggerInterface $logger
) {
- $this->smart_button = $smart_button;
- $this->cart = clone $cart;
- $this->request_data = $request_data;
- $this->cart_products = $cart_products;
- $this->logger = $logger;
+ $this->smart_button = $smart_button;
+ $this->cart = clone $cart;
+ $this->request_data = $request_data;
+ $this->cart_products = $cart_products;
+ $this->logger = $logger;
$this->logger_tag = 'simulation';
}
diff --git a/modules/ppcp-button/src/Helper/CartProductsHelper.php b/modules/ppcp-button/src/Helper/CartProductsHelper.php
index 506c968d6..804c26598 100644
--- a/modules/ppcp-button/src/Helper/CartProductsHelper.php
+++ b/modules/ppcp-button/src/Helper/CartProductsHelper.php
@@ -53,7 +53,7 @@ class CartProductsHelper {
/**
* Sets a new cart instance.
*
- * @param WC_Cart $cart
+ * @param WC_Cart $cart The cart.
* @return void
*/
public function set_cart( WC_Cart $cart ): void {
@@ -89,7 +89,7 @@ class CartProductsHelper {
/**
* Returns product information from a data array.
*
- * @param array $product
+ * @param array $product The product data array, usually provided by the product page form.
* @return array|null
*/
public function product_from_data( array $product ): ?array {
@@ -135,20 +135,20 @@ class CartProductsHelper {
if ( $product['product']->is_type( 'booking' ) ) {
$success = $success && $this->add_booking_product(
- $product['product'],
- $product['booking']
- );
+ $product['product'],
+ $product['booking']
+ );
} elseif ( $product['product']->is_type( 'variable' ) ) {
$success = $success && $this->add_variable_product(
- $product['product'],
- $product['quantity'],
- $product['variations']
- );
+ $product['product'],
+ $product['quantity'],
+ $product['variations']
+ );
} else {
$success = $success && $this->add_product(
- $product['product'],
- $product['quantity']
- );
+ $product['product'],
+ $product['quantity']
+ );
}
}
@@ -169,7 +169,9 @@ class CartProductsHelper {
* @throws Exception When product could not be added.
*/
public function add_product( \WC_Product $product, int $quantity ): bool {
- $this->validate_cart();
+ if ( ! $this->cart ) {
+ throw new Exception( 'Cart not set.' );
+ }
$cart_item_key = $this->cart->add_to_cart( $product->get_id(), $quantity );
@@ -194,7 +196,9 @@ class CartProductsHelper {
int $quantity,
array $post_variations
): bool {
- $this->validate_cart();
+ if ( ! $this->cart ) {
+ throw new Exception( 'Cart not set.' );
+ }
$variations = array();
foreach ( $post_variations as $key => $value ) {
@@ -230,7 +234,9 @@ class CartProductsHelper {
\WC_Product $product,
array $data
): bool {
- $this->validate_cart();
+ if ( ! $this->cart ) {
+ throw new Exception( 'Cart not set.' );
+ }
if ( ! is_callable( 'wc_bookings_get_posted_data' ) ) {
return false;
@@ -252,10 +258,12 @@ class CartProductsHelper {
* Removes stored cart items from WooCommerce cart.
*
* @return void
- * @throws Exception
+ * @throws Exception Throws if there's a failure removing the cart items.
*/
public function remove_cart_items(): void {
- $this->validate_cart();
+ if ( ! $this->cart ) {
+ throw new Exception( 'Cart not set.' );
+ }
foreach ( $this->cart_item_keys as $cart_item_key ) {
if ( ! $cart_item_key ) {
@@ -266,21 +274,12 @@ class CartProductsHelper {
}
/**
+ * Returns the cart item keys of the items added to cart.
+ *
* @return array
*/
public function cart_item_keys(): array {
return $this->cart_item_keys;
}
- /**
- * Throws the cart not set exception.
- *
- * @return void
- * @throws Exception
- */
- private function validate_cart(): void {
- if ( ! $this->cart ) {
- throw new Exception( 'Cart not set.' );
- }
- }
}
diff --git a/modules/ppcp-googlepay/src/GooglepayModule.php b/modules/ppcp-googlepay/src/GooglepayModule.php
index b2a07c47d..4a9ab5644 100644
--- a/modules/ppcp-googlepay/src/GooglepayModule.php
+++ b/modules/ppcp-googlepay/src/GooglepayModule.php
@@ -50,103 +50,105 @@ class GooglepayModule implements ModuleInterface {
}
);
- // Check if the module is applicable, correct country, currency, ... etc.
- if ( ! $c->get( 'googlepay.eligible' ) ) {
- return;
- }
-
- // Load the button handler.
- $button = $c->get( 'googlepay.button' );
- assert( $button instanceof ButtonInterface );
- $button->initialize();
-
- // Show notice if there are product availability issues.
- $availability_notice = $c->get( 'googlepay.availability_notice' );
- assert( $availability_notice instanceof AvailabilityNotice );
- $availability_notice->execute();
-
- // Check if this merchant can activate / use the buttons.
- // We allow non referral merchants as they can potentially still use GooglePay, we just have no way of checking the capability.
- if ( ( ! $c->get( 'googlepay.available' ) ) && $c->get( 'googlepay.is_referral' ) ) {
- return;
- }
-
- // Initializes button rendering.
add_action(
- 'wp',
- static function () use ( $c, $button ) {
- if ( is_admin() ) {
- return;
- }
- $button->render();
- }
- );
-
- // Enqueue frontend scripts.
- add_action(
- 'wp_enqueue_scripts',
- static function () use ( $c, $button ) {
- $smart_button = $c->get( 'button.smart-button' );
- assert( $smart_button instanceof SmartButtonInterface );
- if ( $smart_button->should_load_ppcp_script() ) {
- $button->enqueue();
- }
-
- if ( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) ) {
- /**
- * Should add this to the ButtonInterface.
- *
- * @psalm-suppress UndefinedInterfaceMethod
- */
- $button->enqueue_styles();
- }
- }
- );
-
- // Enqueue backend scripts.
- add_action(
- 'admin_enqueue_scripts',
- static function () use ( $c, $button ) {
- if ( ! is_admin() ) {
- return;
- }
- /**
- * Should add this to the ButtonInterface.
- *
- * @psalm-suppress UndefinedInterfaceMethod
- */
- $button->enqueue_admin();
- }
- );
-
- // Registers buttons on blocks pages.
- add_action(
- 'woocommerce_blocks_payment_method_type_registration',
- function( PaymentMethodRegistry $payment_method_registry ) use ( $c, $button ): void {
- if ( $button->is_enabled() ) {
- $payment_method_registry->register( $c->get( 'googlepay.blocks-payment-method' ) );
- }
- }
- );
-
- // Adds GooglePay component to the backend button preview settings.
- add_action(
- 'woocommerce_paypal_payments_admin_gateway_settings',
- function( array $settings ) use ( $c, $button ): array {
- if ( is_array( $settings['components'] ) ) {
- $settings['components'][] = 'googlepay';
- }
- return $settings;
- }
- );
-
- // Initialize AJAX endpoints.
- add_action(
- 'wc_ajax_' . UpdatePaymentDataEndpoint::ENDPOINT,
+ 'init',
static function () use ( $c ) {
- $endpoint = $c->get( 'googlepay.endpoint.update-payment-data' );
- assert( $endpoint instanceof UpdatePaymentDataEndpoint );
- $endpoint->handle_request();
+
+ // Check if the module is applicable, correct country, currency, ... etc.
+ if ( ! $c->get( 'googlepay.eligible' ) ) {
+ return;
+ }
+
+ // Load the button handler.
+ $button = $c->get( 'googlepay.button' );
+ assert( $button instanceof ButtonInterface );
+ $button->initialize();
+
+ // Show notice if there are product availability issues.
+ $availability_notice = $c->get( 'googlepay.availability_notice' );
+ assert( $availability_notice instanceof AvailabilityNotice );
+ $availability_notice->execute();
+
+ // Check if this merchant can activate / use the buttons.
+ // We allow non referral merchants as they can potentially still use GooglePay, we just have no way of checking the capability.
+ if ( ( ! $c->get( 'googlepay.available' ) ) && $c->get( 'googlepay.is_referral' ) ) {
+ return;
+ }
+
+ // Initializes button rendering.
+ if ( ! is_admin() ) {
+ $button->render();
+ }
+
+ // Enqueue frontend scripts.
+ add_action(
+ 'wp_enqueue_scripts',
+ static function () use ( $c, $button ) {
+ $smart_button = $c->get( 'button.smart-button' );
+ assert( $smart_button instanceof SmartButtonInterface );
+ if ( $smart_button->should_load_ppcp_script() ) {
+ $button->enqueue();
+ }
+
+ if ( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) ) {
+ /**
+ * Should add this to the ButtonInterface.
+ *
+ * @psalm-suppress UndefinedInterfaceMethod
+ */
+ $button->enqueue_styles();
+ }
+ }
+ );
+
+ // Enqueue backend scripts.
+ add_action(
+ 'admin_enqueue_scripts',
+ static function () use ( $c, $button ) {
+ if ( ! is_admin() ) {
+ return;
+ }
+
+ /**
+ * Should add this to the ButtonInterface.
+ *
+ * @psalm-suppress UndefinedInterfaceMethod
+ */
+ $button->enqueue_admin();
+ }
+ );
+
+ // Registers buttons on blocks pages.
+ add_action(
+ 'woocommerce_blocks_payment_method_type_registration',
+ function( PaymentMethodRegistry $payment_method_registry ) use ( $c, $button ): void {
+ if ( $button->is_enabled() ) {
+ $payment_method_registry->register( $c->get( 'googlepay.blocks-payment-method' ) );
+ }
+ }
+ );
+
+ // Adds GooglePay component to the backend button preview settings.
+ add_action(
+ 'woocommerce_paypal_payments_admin_gateway_settings',
+ function( array $settings ) use ( $c ): array {
+ if ( is_array( $settings['components'] ) ) {
+ $settings['components'][] = 'googlepay';
+ }
+ return $settings;
+ }
+ );
+
+ // Initialize AJAX endpoints.
+ add_action(
+ 'wc_ajax_' . UpdatePaymentDataEndpoint::ENDPOINT,
+ static function () use ( $c ) {
+ $endpoint = $c->get( 'googlepay.endpoint.update-payment-data' );
+ assert( $endpoint instanceof UpdatePaymentDataEndpoint );
+ $endpoint->handle_request();
+ }
+ );
+
}
);
}
diff --git a/tests/PHPUnit/Button/Endpoint/ChangeCartEndpointTest.php b/tests/PHPUnit/Button/Endpoint/ChangeCartEndpointTest.php
index 64b74d91d..427e9ce79 100644
--- a/tests/PHPUnit/Button/Endpoint/ChangeCartEndpointTest.php
+++ b/tests/PHPUnit/Button/Endpoint/ChangeCartEndpointTest.php
@@ -6,6 +6,7 @@ namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
+use WooCommerce\PayPalCommerce\Button\Helper\CartProductsHelper;
use WooCommerce\PayPalCommerce\TestCase;
use Mockery;
use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
@@ -91,12 +92,16 @@ class ChangeCartEndpointTest extends TestCase
->expects('from_wc_cart')
->andReturn($pu);
+ $productsHelper = new CartProductsHelper(
+ $dataStore
+ );
+
$testee = new ChangeCartEndpoint(
$cart,
$shipping,
$requestData,
$purchase_unit_factory,
- $dataStore,
+ $productsHelper,
new NullLogger()
);