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

@ -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,6 +52,10 @@ class ApplepayModule implements ModuleInterface {
}
);
add_action(
'init',
static function () use ( $c, $module ) {
// Check if the module is applicable, correct country, currency, ... etc.
if ( ! $c->get( 'applepay.eligible' ) ) {
return;
@ -78,12 +83,14 @@ class ApplepayModule implements ModuleInterface {
return;
}
$this->load_assets( $c, $apple_payment_method );
$this->handle_validation_file( $c );
$this->render_buttons( $c, $apple_payment_method );
$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();
}
);
}
/**
* Returns the key for the module.

View file

@ -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 );
}

View file

@ -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 {
@ -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.' );
}
}
}

View file

@ -50,6 +50,10 @@ class GooglepayModule implements ModuleInterface {
}
);
add_action(
'init',
static function () use ( $c ) {
// Check if the module is applicable, correct country, currency, ... etc.
if ( ! $c->get( 'googlepay.eligible' ) ) {
return;
@ -72,15 +76,9 @@ class GooglepayModule implements ModuleInterface {
}
// Initializes button rendering.
add_action(
'wp',
static function () use ( $c, $button ) {
if ( is_admin() ) {
return;
}
if ( ! is_admin() ) {
$button->render();
}
);
// Enqueue frontend scripts.
add_action(
@ -110,6 +108,7 @@ class GooglepayModule implements ModuleInterface {
if ( ! is_admin() ) {
return;
}
/**
* Should add this to the ButtonInterface.
*
@ -132,7 +131,7 @@ class GooglepayModule implements ModuleInterface {
// Adds GooglePay component to the backend button preview settings.
add_action(
'woocommerce_paypal_payments_admin_gateway_settings',
function( array $settings ) use ( $c, $button ): array {
function( array $settings ) use ( $c ): array {
if ( is_array( $settings['components'] ) ) {
$settings['components'][] = 'googlepay';
}
@ -149,6 +148,9 @@ class GooglepayModule implements ModuleInterface {
$endpoint->handle_request();
}
);
}
);
}
/**

View file

@ -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()
);