lukepolo / laracart

Laravel Shopping Cart Package
https://laracart.lukepolo.com
MIT License
576 stars 86 forks source link

php8.1 problems #310

Closed it-can closed 2 years ago

it-can commented 2 years ago

I get no results on php8.1 (on php8.0.13 it works perfectly), I have this test:

I use Laracart 1.12.0-RC-1 (same problem in the latest version)

<?php

namespace Tests\Unit;

use LaraCart;
use LukePOLO\LaraCart\Coupons\Fixed;
use LukePOLO\LaraCart\Coupons\Percentage;
use Tests\TestCase;

class CartTest extends TestCase
{
    /**
     *
     */
    protected function setUpCart()
    {
        // Add to cart
        LaraCart::add(
            1,
            'test',
            52,
            107.44,
            [
                'tax' => 0.21,
            ]
        );
    }

    /**
     * Test cart + discount
     *
     * @return void
     */
    public function testCart()
    {
        $this->setUpCart();

        $this->assertEquals(5586.88, LaraCart::subTotal(false));
        $this->assertEquals(0, LaraCart::totalDiscount(false));
        $this->assertEquals(1173.24, LaraCart::taxTotal(false));
        $this->assertEquals(6760.12, LaraCart::total(false));
    }

    public function testDiscountPercentage()
    {
        $this->setUpCart();

        // Test discount %
        $coupon = new Percentage('7,5%', 0.075);
        LaraCart::addCoupon($coupon);

        $this->assertEquals(5586.88, LaraCart::subTotal(false));
        $this->assertEquals(419.02, LaraCart::totalDiscount(false));
        $this->assertEquals(1085.25, LaraCart::taxTotal(false));
        $this->assertEquals(6253.11, LaraCart::total(false));
    }

    public function testDiscountFixed()
    {
        $this->setUpCart();

        // Test discount fixed
        $coupon = new Fixed('100 euro', 100);
        LaraCart::addCoupon($coupon);

        $this->assertEquals(5586.88, LaraCart::subTotal(false));
        $this->assertEquals(100, LaraCart::totalDiscount(false));
        $this->assertEquals(1152.24, LaraCart::taxTotal(false));
        $this->assertEquals(6639.12, LaraCart::total(false));
    }
}

php 8.0.13 output

phpunit
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.0.13
Configuration: /Users/test/test/laravel/test/phpunit.xml

........                                                            8 / 8 (100%)

Time: 00:00.467, Memory: 30.00 MB

OK (8 tests, 21 assertions)

php 8.1 output

phpunit
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.0
Configuration: /Users/test/websites/laravel/test/phpunit.xml
FFF.....                                                            8 / 8 (100%)

Time: 00:00.499, Memory: 30.00 MB

There were 3 failures:

1) Tests\Unit\CartTest::testCart
Failed asserting that '0.00' matches expected 5586.88.

/Users/test/websites/laravel/test/tests/Unit/CartTest.php:38

2) Tests\Unit\CartTest::testDiscountPercentage
Failed asserting that '0.00' matches expected 5586.88.

/Users/test/websites/laravel/test/tests/Unit/CartTest.php:52

3) Tests\Unit\CartTest::testDiscountFixed
Failed asserting that '0.00' matches expected 5586.88.

/Users/test/websites/laravel/test/tests/Unit/CartTest.php:66

FAILURES!
Tests: 8, Assertions: 12, Failures: 3.
it-can commented 2 years ago

Ok it seems that qty is not being set on php8.1

output php 8.0.13

LukePOLO\LaraCart\CartItem^ {#867
  #itemHash: "7af08c2a78f26847e87a02cb9a8bcc8e"
  #itemModel: null
  #itemModelRelations: []
  +locale: null
  +lineItem: false
  +discount: 0
  +active: true
  +subItems: []
  +couponInfo: []
  +currencyCode: null
  +options: array:6 [
    "id" => 1
    "qty" => 52
    "name" => "test"
    "taxable" => true
    "price" => 107.44
    "tax" => 0.21
  ]
}

php 8.1

LukePOLO\LaraCart\CartItem^ {#866
  #itemHash: "7af08c2a78f26847e87a02cb9a8bcc8e"
  #itemModel: null
  #itemModelRelations: []
  +locale: null
  +lineItem: false
  +discount: 0
  +active: true
  +subItems: []
  +couponInfo: []
  +currencyCode: null
  +options: & array:5 [
    "id" => 1
    "name" => "test"
    "price" => 107.44
    "tax" => 0.21
    "taxable" => true
  ]
}
it-can commented 2 years ago

mmm removing this line makes it works again...

https://github.com/lukepolo/laracart/blob/master/src/CartItem.php#L95

lukepolo commented 2 years ago

does 8.1 do something by passing by reference oddly or something?

I am no longer using PHP as my main language, so im not caught up in the details of the releases.

it-can commented 2 years ago

I think this line is the problem

https://github.com/lukepolo/laracart/blob/master/src/CartItem.php#L92

$cartItemArray = (array) $this;

I think this should be:

$cartItemArray = (array) clone $this;