Skip to content

Commit

Permalink
test(mockwccart): change mockwccart to more accurately represent the …
Browse files Browse the repository at this point in the history
…real cart
  • Loading branch information
FlorianSDV committed Aug 1, 2024
1 parent ae1c35a commit 889904e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 18 deletions.
95 changes: 81 additions & 14 deletions tests/Mock/MockWcCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ public function add_to_cart(
array $variation = [],
array $cartItemData = []
): void {
$this->items[] = [
'data' => new WC_Product($productId),
'quantity' => $quantity,
];
$cartId = $this->generate_cart_id($productId, $variationId, $variation, $cartItemData);
$cartItemKey = $this->find_product_in_cart($cartId);

if ($cartItemKey) {
$this->items[$cartItemKey]['quantity'] += $quantity;
} else {
$this->items[] = [
'data' => new WC_Product($productId),
'quantity' => $quantity,
];
}
}

public function get_cart()

Check notice on line 50 in tests/Mock/MockWcCart.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/Mock/MockWcCart.php#L50

The method get_cart is not named in camelCase.
Expand All @@ -59,16 +66,12 @@ public function empty_cart(): void
public function get_shipping_packages(): array
{
// calculate weight of all products in cart
$weight = array_reduce(
$this->items,
static function (float $carry, array $item) {
/** @var \WC_Product $wcProduct */
$wcProduct = $item['data'];

return $carry + (float) $wcProduct->get_weight();
},
0
);
$weight = 0;
foreach ($this->items as $item) {
/** @var \WC_Product $wcProduct */
$wcProduct = $item['data'];
$weight += $wcProduct->get_weight() * $item['quantity'];
}

if ($weight > 10) {
return [];
Expand All @@ -78,4 +81,68 @@ static function (float $carry, array $item) {
'flat_rate:0' => [],
];
}

/**
* Generate a unique ID for the cart item being added.
*
* @param int $productId - id of the product the key is being generated for.
* @param int $variationId of the product the key is being generated for.
* @param array $variation data for the cart item.
* @param array $cartItemData other cart item data passed which affects this items uniqueness in the cart.
*
* @return string cart item key
*/
public function generate_cart_id(

Check notice on line 95 in tests/Mock/MockWcCart.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/Mock/MockWcCart.php#L95

The method generate_cart_id is not named in camelCase.
int $productId,
int $variationId = 0,
array $variation = [],
array $cartItemData = []
): string {
$idParts = [$productId];

if ($variationId && 0 !== $variationId) {
$idParts[] = $variationId;
}

if (is_array($variation) && ! empty($variation)) {
$variationKey = '';
foreach ($variation as $key => $value) {
$variationKey .= trim($key) . trim($value);
}
$idParts[] = $variationKey;
}

if (is_array($cartItemData) && ! empty($cartItemData)) {
$cartItemDataKey = '';
foreach ($cartItemData as $key => $value) {
if (is_array($value) || is_object($value)) {
$value = http_build_query($value);
}
$cartItemDataKey .= trim($key) . trim($value);
}
$idParts[] = $cartItemDataKey;
}

return apply_filters(
'woocommerce_cart_id',
md5(implode('_', $idParts))
);
}

/**
* Check if product is in the cart and return cart item key.
* Cart item key will be unique based on the item and its properties, such as variations.
* ONLY RETURNS A KEY! DOES NOT RETURN THE ITEM!
*
* @param mixed $cartId id of product to find in the cart.
*
* @return string cart item key
*/
public function find_product_in_cart($cartId = false): string

Check notice on line 141 in tests/Mock/MockWcCart.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/Mock/MockWcCart.php#L141

The method find_product_in_cart is not named in camelCase.
{
$thisItemsIsArray = is_array($this->items);
$itemAlreadyExists = isset($this->items[$cartId]);

return ! $cartId && $thisItemsIsArray && $itemAlreadyExists ? $cartId : '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"exportOnlyRecipient": -1,
"exportReturn": -1,
"exportSignature": -1,
"fitInDigitalStamp": 0,
"fitInMailbox": 1000,
"fitInDigitalStamp": 7,
"fitInMailbox": 7,
"packageType": "digital_stamp"
}
},
Expand All @@ -64,8 +64,8 @@
"exportOnlyRecipient": -1,
"exportReturn": -1,
"exportSignature": -1,
"fitInDigitalStamp": 0,
"fitInMailbox": 1000,
"fitInDigitalStamp": 7,
"fitInMailbox": 7,
"id": "product",
"packageType": "digital_stamp"
}
Expand Down

0 comments on commit 889904e

Please sign in to comment.