Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: binafy/laravel-cart
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.1.1
Choose a base ref
...
head repository: binafy/laravel-cart
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.1.2
Choose a head ref
  • 4 commits
  • 2 files changed
  • 1 contributor

Commits on Jan 26, 2025

  1. Update Cart.php

    milwad-dev committed Jan 26, 2025
    Copy the full SHA
    0f6aca9 View commit details
  2. Update CartStoreTest.php

    milwad-dev committed Jan 26, 2025
    Copy the full SHA
    8718615 View commit details
  3. Update CartStoreTest.php

    milwad-dev committed Jan 26, 2025
    Copy the full SHA
    bf23142 View commit details
  4. Merge pull request #35 from binafy/change-int-to-float-for-calculated…

    …PriceByQuantity
    
    [1.x] Change int to float for calculated price by quantity
    milwad-dev authored Jan 26, 2025
    Copy the full SHA
    c24e107 View commit details
Showing with 14 additions and 4 deletions.
  1. +2 −2 src/Models/Cart.php
  2. +12 −2 tests/Feature/Models/CartStoreTest.php
4 changes: 2 additions & 2 deletions src/Models/Cart.php
Original file line number Diff line number Diff line change
@@ -85,11 +85,11 @@ public function scopeFirstOrCreateWithStoreItems(
/**
* Calculate price by quantity of items.
*/
public function calculatedPriceByQuantity(): int
public function calculatedPriceByQuantity(): float
{
$totalPrice = 0;
foreach ($this->items()->get() as $item) {
$totalPrice += (int) $item->quantity * (int) $item->itemable->getPrice();
$totalPrice += (int) $item->quantity * (float) $item->itemable->getPrice();
}

return $totalPrice;
14 changes: 12 additions & 2 deletions tests/Feature/Models/CartStoreTest.php
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
use function Pest\Laravel\assertDatabaseCount;
use function Pest\Laravel\assertDatabaseHas;
use function Pest\Laravel\assertDatabaseMissing;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertInstanceOf;

/*
@@ -188,11 +189,20 @@
$cart->storeItems($items);

// Assertions
\PHPUnit\Framework\assertEquals(230000, $cart->calculatedPriceByQuantity());
assertEquals(230000, $cart->calculatedPriceByQuantity());

// Float
$cart->storeItems([
[
'itemable' => Product::query()->create(['title' => 'Product float', 'price' => 15000.3]),
'quantity' => 2,
],
]);
assertEquals(260000.6, $cart->calculatedPriceByQuantity());

// DB Assertions
assertDatabaseCount('carts', 1);
assertDatabaseCount('cart_items', 3);
assertDatabaseCount('cart_items', 4);
assertDatabaseHas('cart_items', [
'itemable_id' => $product1->id,
'itemable_type' => $product1::class,