Fix lint and tests

This commit is contained in:
Pedro Silva 2023-10-26 10:58:05 +01:00
parent 889df4d88c
commit 46b47dc97b
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
8 changed files with 181 additions and 164 deletions

View file

@ -109,7 +109,7 @@ return array(
$container->get( 'applepay.data_to_scripts' ), $container->get( 'applepay.data_to_scripts' ),
$container->get( 'wcgateway.settings.status' ), $container->get( 'wcgateway.settings.status' ),
$container->get( 'button.helper.cart-products' ) $container->get( 'button.helper.cart-products' )
); );
}, },
'applepay.blocks-payment-method' => static function ( ContainerInterface $container ): PaymentMethodTypeInterface { 'applepay.blocks-payment-method' => static function ( ContainerInterface $container ): PaymentMethodTypeInterface {
return new BlocksPaymentMethod( return new BlocksPaymentMethod(

View file

@ -40,6 +40,7 @@ class ApplepayModule implements ModuleInterface {
* {@inheritDoc} * {@inheritDoc}
*/ */
public function run( ContainerInterface $c ): void { public function run( ContainerInterface $c ): void {
$module = $this;
// Clears product status when appropriate. // Clears product status when appropriate.
add_action( add_action(
@ -51,38 +52,44 @@ class ApplepayModule implements ModuleInterface {
} }
); );
// Check if the module is applicable, correct country, currency, ... etc. add_action(
if ( ! $c->get( 'applepay.eligible' ) ) { 'init',
return; static function () use ( $c, $module ) {
}
// Load the button handler. // Check if the module is applicable, correct country, currency, ... etc.
$apple_payment_method = $c->get( 'applepay.button' ); if ( ! $c->get( 'applepay.eligible' ) ) {
// add onboarding and referrals hooks. return;
assert( $apple_payment_method instanceof ApplepayButton ); }
$apple_payment_method->initialize();
// Show notice if there are product availability issues. // Load the button handler.
$availability_notice = $c->get( 'applepay.availability_notice' ); $apple_payment_method = $c->get( 'applepay.button' );
assert( $availability_notice instanceof AvailabilityNotice ); // add onboarding and referrals hooks.
$availability_notice->execute(); assert( $apple_payment_method instanceof ApplepayButton );
$apple_payment_method->initialize();
// Return if server not supported. // Show notice if there are product availability issues.
if ( ! $c->get( 'applepay.server_supported' ) ) { $availability_notice = $c->get( 'applepay.availability_notice' );
return; assert( $availability_notice instanceof AvailabilityNotice );
} $availability_notice->execute();
// Check if this merchant can activate / use the buttons. // Return if server not supported.
// 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.server_supported' ) ) {
if ( ( ! $c->get( 'applepay.available' ) ) && $c->get( 'applepay.is_referral' ) ) { return;
return; }
}
$this->load_assets( $c, $apple_payment_method ); // Check if this merchant can activate / use the buttons.
$this->handle_validation_file( $c ); // We allow non referral merchants as they can potentially still use ApplePay, we just have no way of checking the capability.
$this->render_buttons( $c, $apple_payment_method ); 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();
}
);
} }
/** /**

View file

@ -62,21 +62,21 @@ class ApplePayDataObjectHttp {
/** /**
* The product variations. * The product variations.
* *
* @var string * @var array
*/ */
protected $product_variations = array(); protected $product_variations = array();
/** /**
* The product extra. * The product extra.
* *
* @var string * @var array
*/ */
protected $product_extra = array(); protected $product_extra = array();
/** /**
* The product booking. * The product booking.
* *
* @var string * @var array
*/ */
protected $product_booking = array(); protected $product_booking = array();
@ -295,7 +295,7 @@ class ApplePayDataObjectHttp {
/** /**
* Pre-processes request data to transform it to a standard format. * Pre-processes request data to transform it to a standard format.
* *
* @param array $data * @param array $data The data.
* @return array * @return array
*/ */
protected function preprocess_request_data( array $data ): array { protected function preprocess_request_data( array $data ): array {
@ -602,7 +602,7 @@ class ApplePayDataObjectHttp {
/** /**
* Returns the product variations. * Returns the product variations.
* *
* @return string * @return array
*/ */
public function product_variations(): array { public function product_variations(): array {
return $this->product_variations; return $this->product_variations;
@ -611,7 +611,7 @@ class ApplePayDataObjectHttp {
/** /**
* Returns the product extra. * Returns the product extra.
* *
* @return string * @return array
*/ */
public function product_extra(): array { public function product_extra(): array {
return $this->product_extra; return $this->product_extra;
@ -620,7 +620,7 @@ class ApplePayDataObjectHttp {
/** /**
* Returns the product booking. * Returns the product booking.
* *
* @return string * @return array
*/ */
public function product_booking(): array { public function product_booking(): array {
return $this->product_booking; return $this->product_booking;
@ -698,6 +698,10 @@ class ApplePayDataObjectHttp {
) )
); );
if ( ! $data ) {
return false;
}
return $this->append_products_to_data( $data, $_POST ); return $this->append_products_to_data( $data, $_POST );
} }

View file

@ -254,7 +254,7 @@ return array(
}, },
'button.helper.cart-products' => static function ( ContainerInterface $container ): CartProductsHelper { '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 ); return new CartProductsHelper( $data_store );
}, },

View file

@ -52,11 +52,11 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
CartProductsHelper $cart_products, CartProductsHelper $cart_products,
LoggerInterface $logger LoggerInterface $logger
) { ) {
$this->smart_button = $smart_button; $this->smart_button = $smart_button;
$this->cart = clone $cart; $this->cart = clone $cart;
$this->request_data = $request_data; $this->request_data = $request_data;
$this->cart_products = $cart_products; $this->cart_products = $cart_products;
$this->logger = $logger; $this->logger = $logger;
$this->logger_tag = 'simulation'; $this->logger_tag = 'simulation';
} }

View file

@ -53,7 +53,7 @@ class CartProductsHelper {
/** /**
* Sets a new cart instance. * Sets a new cart instance.
* *
* @param WC_Cart $cart * @param WC_Cart $cart The cart.
* @return void * @return void
*/ */
public function set_cart( WC_Cart $cart ): void { public function set_cart( WC_Cart $cart ): void {
@ -89,7 +89,7 @@ class CartProductsHelper {
/** /**
* Returns product information from a data array. * 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 * @return array|null
*/ */
public function product_from_data( array $product ): ?array { public function product_from_data( array $product ): ?array {
@ -135,20 +135,20 @@ class CartProductsHelper {
if ( $product['product']->is_type( 'booking' ) ) { if ( $product['product']->is_type( 'booking' ) ) {
$success = $success && $this->add_booking_product( $success = $success && $this->add_booking_product(
$product['product'], $product['product'],
$product['booking'] $product['booking']
); );
} elseif ( $product['product']->is_type( 'variable' ) ) { } elseif ( $product['product']->is_type( 'variable' ) ) {
$success = $success && $this->add_variable_product( $success = $success && $this->add_variable_product(
$product['product'], $product['product'],
$product['quantity'], $product['quantity'],
$product['variations'] $product['variations']
); );
} else { } else {
$success = $success && $this->add_product( $success = $success && $this->add_product(
$product['product'], $product['product'],
$product['quantity'] $product['quantity']
); );
} }
} }
@ -169,7 +169,9 @@ class CartProductsHelper {
* @throws Exception When product could not be added. * @throws Exception When product could not be added.
*/ */
public function add_product( \WC_Product $product, int $quantity ): bool { 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 ); $cart_item_key = $this->cart->add_to_cart( $product->get_id(), $quantity );
@ -194,7 +196,9 @@ class CartProductsHelper {
int $quantity, int $quantity,
array $post_variations array $post_variations
): bool { ): bool {
$this->validate_cart(); if ( ! $this->cart ) {
throw new Exception( 'Cart not set.' );
}
$variations = array(); $variations = array();
foreach ( $post_variations as $key => $value ) { foreach ( $post_variations as $key => $value ) {
@ -230,7 +234,9 @@ class CartProductsHelper {
\WC_Product $product, \WC_Product $product,
array $data array $data
): bool { ): bool {
$this->validate_cart(); if ( ! $this->cart ) {
throw new Exception( 'Cart not set.' );
}
if ( ! is_callable( 'wc_bookings_get_posted_data' ) ) { if ( ! is_callable( 'wc_bookings_get_posted_data' ) ) {
return false; return false;
@ -252,10 +258,12 @@ class CartProductsHelper {
* Removes stored cart items from WooCommerce cart. * Removes stored cart items from WooCommerce cart.
* *
* @return void * @return void
* @throws Exception * @throws Exception Throws if there's a failure removing the cart items.
*/ */
public function remove_cart_items(): void { 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 ) { foreach ( $this->cart_item_keys as $cart_item_key ) {
if ( ! $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 * @return array
*/ */
public function cart_item_keys(): array { public function cart_item_keys(): array {
return $this->cart_item_keys; 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.' );
}
}
} }

View file

@ -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( add_action(
'wp', 'init',
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,
static function () use ( $c ) { static function () use ( $c ) {
$endpoint = $c->get( 'googlepay.endpoint.update-payment-data' );
assert( $endpoint instanceof UpdatePaymentDataEndpoint ); // Check if the module is applicable, correct country, currency, ... etc.
$endpoint->handle_request(); 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();
}
);
} }
); );
} }

View file

@ -6,6 +6,7 @@ namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Button\Helper\CartProductsHelper;
use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\TestCase;
use Mockery; use Mockery;
use WooCommerce\WooCommerce\Logging\Logger\NullLogger; use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
@ -91,12 +92,16 @@ class ChangeCartEndpointTest extends TestCase
->expects('from_wc_cart') ->expects('from_wc_cart')
->andReturn($pu); ->andReturn($pu);
$productsHelper = new CartProductsHelper(
$dataStore
);
$testee = new ChangeCartEndpoint( $testee = new ChangeCartEndpoint(
$cart, $cart,
$shipping, $shipping,
$requestData, $requestData,
$purchase_unit_factory, $purchase_unit_factory,
$dataStore, $productsHelper,
new NullLogger() new NullLogger()
); );