diff --git a/src/CanBeBought.php b/src/CanBeBought.php index 62a08a39..949c3a67 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -21,16 +21,16 @@ public function getBuyableIdentifier($options = null) */ public function getBuyableDescription($options = null) { - if (property_exists($this, 'name')) { - return $this->name; + if (($name = $this->getAttribute('name'))) { + return $name; } - if (property_exists($this, 'title')) { - return $this->title; + if (($title = $this->getAttribute('title'))) { + return $ttle; } - if (property_exists($this, 'description')) { - return $this->description; + if (($description = $this->getAttribute('description'))) { + return $description; } } @@ -41,8 +41,8 @@ public function getBuyableDescription($options = null) */ public function getBuyablePrice($options = null) { - if (property_exists($this, 'price')) { - return $this->price; + if (($price = $this->getAttribute('price'))) { + return $price; } } @@ -53,8 +53,8 @@ public function getBuyablePrice($options = null) */ public function getBuyableWeight($options = null) { - if (property_exists($this, 'weight')) { - return $this->weight; + if (($weight = $this->getAttribute('weight'))) { + return $weight; } return 0; diff --git a/tests/CartTest.php b/tests/CartTest.php index ddb00fb5..ced58798 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -82,9 +82,15 @@ public function it_can_have_multiple_instances() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'First item')); + $cart->add(new BuyableProduct([ + 'id' => 1, + 'name' => 'First item', + ])); - $cart->instance('wishlist')->add(new BuyableProduct(2, 'Second item')); + $cart->instance('wishlist')->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Second item', + ])); $this->assertItemsInCart(1, $cart->instance(Cart::DEFAULT_INSTANCE)); $this->assertItemsInCart(1, $cart->instance('wishlist')); @@ -126,7 +132,11 @@ public function it_can_add_multiple_buyable_items_at_once() $cart = $this->getCart(); - $cart->add([new BuyableProduct(1), new BuyableProduct(2)]); + $cart->add([new BuyableProduct([ + 'id' => 1, + ]), new BuyableProduct([ + 'id' => 2, + ])]); $this->assertEquals(2, $cart->count()); @@ -140,7 +150,11 @@ public function it_will_return_an_array_of_cartitems_when_you_add_multiple_items $cart = $this->getCart(); - $cartItems = $cart->add([new BuyableProduct(1), new BuyableProduct(2)]); + $cartItems = $cart->add([new BuyableProduct([ + 'id' => 1, + ]), new BuyableProduct([ + 'id' => 2, + ])]); $this->assertTrue(is_array($cartItems)); $this->assertCount(2, $cartItems); @@ -332,9 +346,14 @@ public function it_can_update_an_existing_item_in_the_cart_from_a_buyable() $cart = $this->getCart(); - $cart->add(new BuyableProduct()); + $cart->add(new BuyableProductTrait([ + 'description' => 'Description', + ])); - $cart->update('027c91341fd5cf4d2579b49c4b6a90da', new BuyableProduct(1, 'Different description')); + $cart->update('027c91341fd5cf4d2579b49c4b6a90da', new BuyableProductTrait([ + 'name' => '', + 'description' => 'Different description', + ])); $this->assertItemsInCart(1, $cart); $this->assertEquals('Different description', $cart->get('027c91341fd5cf4d2579b49c4b6a90da')->name); @@ -349,7 +368,9 @@ public function it_can_update_an_existing_item_in_the_cart_from_an_array() $cart = $this->getCart(); - $cart->add(new BuyableProduct()); + $cart->add(new BuyableProductTrait([ + 'description' => 'Description', + ])); $cart->update('027c91341fd5cf4d2579b49c4b6a90da', ['name' => 'Different description']); @@ -370,7 +391,9 @@ public function it_will_throw_an_exception_if_a_rowid_was_not_found() $cart->add(new BuyableProduct()); - $cart->update('none-existing-rowid', new BuyableProduct(1, 'Different description')); + $cart->update('none-existing-rowid', new BuyableProduct([ + 'description' => 'Different description', + ])); } /** @test */ @@ -484,8 +507,10 @@ public function it_can_get_the_content_of_the_cart() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1)); - $cart->add(new BuyableProduct(2)); + $cart->add(new BuyableProduct()); + $cart->add(new BuyableProduct([ + 'id' => 2, + ])); $content = $cart->content(); @@ -509,8 +534,10 @@ public function it_will_include_the_tax_and_subtotal_when_converted_to_an_array( { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1)); - $cart->add(new BuyableProduct(2)); + $cart->add(new BuyableProduct()); + $cart->add(new BuyableProduct([ + 'id' => 2, + ])); $content = $cart->content(); @@ -562,8 +589,14 @@ public function it_can_get_the_total_price_of_the_cart_content() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'First item', 10.00)); - $cart->add(new BuyableProduct(2, 'Second item', 25.00), 2); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + ])); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Second item', + 'price' => 25.00, + ]), 2); $this->assertItemsInCart(3, $cart); $this->assertEquals(60.00, $cart->subtotal()); @@ -574,8 +607,15 @@ public function it_can_return_a_formatted_total() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'First item', 1000.00)); - $cart->add(new BuyableProduct(2, 'Second item', 2500.00), 2); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + 'price' => 1000.00, + ])); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Second item', + 'price' => 2500.00, + ]), 2); $this->assertItemsInCart(3, $cart); $this->assertEquals('6.000,00', $cart->subtotal(2, ',', '.')); @@ -586,8 +626,13 @@ public function it_can_search_the_cart_for_a_specific_item() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some item')); - $cart->add(new BuyableProduct(2, 'Another item')); + $cart->add(new BuyableProduct([ + 'name' => 'Some item', + ])); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Another item', + ])); $cartItem = $cart->search(function ($cartItem, $rowId) { return $cartItem->name == 'Some item'; @@ -604,9 +649,17 @@ public function it_can_search_the_cart_for_multiple_items() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some item')); - $cart->add(new BuyableProduct(2, 'Some item')); - $cart->add(new BuyableProduct(3, 'Another item')); + $cart->add(new BuyableProduct([ + 'name' => 'Some item', + ])); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Some item', + ])); + $cart->add(new BuyableProduct([ + 'id' => 3, + 'name' => 'Another item', + ])); $cartItem = $cart->search(function ($cartItem, $rowId) { return $cartItem->name == 'Some item'; @@ -620,8 +673,13 @@ public function it_can_search_the_cart_for_a_specific_item_with_options() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some item'), 1, ['color' => 'red']); - $cart->add(new BuyableProduct(2, 'Another item'), 1, ['color' => 'blue']); + $cart->add(new BuyableProduct([ + 'name' => 'Some item', + ]), 1, ['color' => 'red']); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Another item', + ]), 1, ['color' => 'blue']); $cartItem = $cart->search(function ($cartItem, $rowId) { return $cartItem->options->color == 'red'; @@ -694,7 +752,10 @@ public function it_can_calculate_the_subtotal_of_a_cart_item() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 9.99), 3); + $cart->add(new BuyableProduct([ + 'name' => 'Some title', + 'price' => 9.99, + ]), 3); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -706,7 +767,10 @@ public function it_can_return_a_formatted_subtotal() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 500), 3); + $cart->add(new BuyableProduct([ + 'name' => 'Some title', + 'price' => 500, + ]), 3); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -718,7 +782,9 @@ public function it_can_calculate_tax_based_on_the_default_tax_rate_in_the_config { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'Some title', + ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -730,7 +796,9 @@ public function it_can_calculate_tax_based_on_the_specified_tax() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'Some title', + ]), 1); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); @@ -744,7 +812,10 @@ public function it_can_return_the_calculated_tax_formatted() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 10000.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'Some title', + 'price' => 10000.00, + ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -756,8 +827,14 @@ public function it_can_calculate_the_total_tax_for_all_cart_items() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 10.00), 1); - $cart->add(new BuyableProduct(2, 'Some title', 20.00), 2); + $cart->add(new BuyableProduct([ + 'name' => 'Some title', + ]), 1); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Some title', + 'price' => 20.00, + ]), 2); $this->assertEquals(10.50, $cart->tax); } @@ -767,8 +844,15 @@ public function it_can_return_formatted_total_tax() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 1000.00), 1); - $cart->add(new BuyableProduct(2, 'Some title', 2000.00), 2); + $cart->add(new BuyableProduct([ + 'name' => 'Some title', + 'price' => 1000.00, + ]), 1); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Some title', + 'price' => 2000.00, + ]), 2); $this->assertEquals('1.050,00', $cart->tax(2, ',', '.')); } @@ -778,7 +862,9 @@ public function it_can_access_tax_as_percentage() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'Some title', + ]), 1); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); @@ -792,8 +878,11 @@ public function it_can_return_the_subtotal() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 10.00), 1); - $cart->add(new BuyableProduct(2, 'Some title', 20.00), 2); + $cart->add(new BuyableProduct(), 1); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'price' => 20.00, + ]), 2); $this->assertEquals(50.00, $cart->subtotal); } @@ -803,8 +892,13 @@ public function it_can_return_formatted_subtotal() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 1000.00), 1); - $cart->add(new BuyableProduct(2, 'Some title', 2000.00), 2); + $cart->add(new BuyableProduct([ + 'price' => 1000.00, + ]), 1); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'price' => 2000.00, + ]), 2); $this->assertEquals('5000,00', $cart->subtotal(2, ',', '')); } @@ -816,8 +910,13 @@ public function it_can_return_cart_formated_numbers_by_config_values() $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Some title', 1000.00), 1); - $cart->add(new BuyableProduct(2, 'Some title', 2000.00), 2); + $cart->add(new BuyableProduct([ + 'price' => 1000.00, + ]), 1); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'price' => 2000.00, + ]), 2); $this->assertEquals('5000,00', $cart->subtotal()); $this->assertEquals('1050,00', $cart->tax()); @@ -835,7 +934,9 @@ public function it_can_return_cartItem_formated_numbers_by_config_values() $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'Some title', 2000.00), 2); + $cart->add(new BuyableProduct([ + 'price' => 2000.00, + ]), 2); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -988,7 +1089,9 @@ public function it_can_calculate_all_values() { $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'First item', 10.00), 2); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + ]), 2); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -1009,7 +1112,9 @@ public function it_can_calculate_all_values() public function it_can_calculate_all_values_after_updating_from_array() { $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'First item', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + ]), 1); $cart->update('027c91341fd5cf4d2579b49c4b6a90da', ['qty' => 2]); @@ -1032,9 +1137,14 @@ public function it_can_calculate_all_values_after_updating_from_array() public function it_can_calculate_all_values_after_updating_from_buyable() { $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'First item', 5.00), 2); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + 'price' => 5.00, + ]), 2); - $cart->update('027c91341fd5cf4d2579b49c4b6a90da', new BuyableProduct(1, 'First item', 10.00)); + $cart->update('027c91341fd5cf4d2579b49c4b6a90da', new BuyableProduct([ + 'name' => 'First item', + ])); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -1070,7 +1180,9 @@ public function can_change_tax_globally() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Item', 10.00), 2); + $cart->add(new BuyableProduct([ + 'name' => 'Item', + ]), 2); $cart->setGlobalTax(0); @@ -1084,7 +1196,9 @@ public function can_change_discount_globally() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Item', 10.00), 2); + $cart->add(new BuyableProduct([ + 'name' => 'Item', + ]), 2); $cart->setGlobalTax(0); $cart->setGlobalDiscount(50); @@ -1099,7 +1213,10 @@ public function cart_has_no_rounding_errors() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Item', 10.004), 2); + $cart->add(new BuyableProduct([ + 'name' => 'Item', + 'price' => 10.004, + ]), 2); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -1116,8 +1233,13 @@ public function it_can_merge_multiple_carts() Event::fake(); $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'Item', 10.00), 1); - $cart->add(new BuyableProduct(2, 'Item 2', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'Item', + ]), 1); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Item 2', + ]), 1); $cart->store('test'); $cart2 = $this->getCart(); @@ -1150,8 +1272,13 @@ public function it_cant_merge_non_existing_cart() ]); Event::fake(); $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'Item', 10.00), 1); - $cart->add(new BuyableProduct(2, 'Item 2', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'Item', + ]), 1); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Item 2', + ]), 1); $this->assertEquals(false, $cart->merge('doesNotExist')); $this->assertEquals(2, $cart->countItems()); } @@ -1160,7 +1287,9 @@ public function it_cant_merge_non_existing_cart() public function cart_can_calculate_all_values() { $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'First item', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $this->assertEquals('10.00', $cart->initial(2)); @@ -1179,7 +1308,9 @@ public function cart_can_calculate_all_values() public function can_access_cart_item_propertys() { $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'First item', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $this->assertEquals(50, $cartItem->discountRate); } @@ -1188,7 +1319,9 @@ public function can_access_cart_item_propertys() public function cant_access_non_existant_propertys() { $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'First item', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $this->assertEquals(null, $cartItem->doesNotExist); $this->assertEquals(null, $cart->doesNotExist); @@ -1198,7 +1331,9 @@ public function cant_access_non_existant_propertys() public function can_set_cart_item_discount() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'First item', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50); $this->assertEquals(50, $cartItem->discountRate); @@ -1208,7 +1343,10 @@ public function can_set_cart_item_discount() public function can_set_cart_item_weight_and_calculate_total_weight() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'First item', 10.00, 250), 2); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + 'weight' => 250, + ]), 2); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50); $this->assertEquals('500.00', $cart->weight(2)); @@ -1232,7 +1370,10 @@ public function cart_can_create_and_restore_from_instance_identifier() $cart->instance($identifier); $this->assertEquals('User1', $cart->currentInstance()); - $cart->add(new BuyableProduct(1, 'First item', 10.00, 250), 2); + $cart->add(new BuyableProduct([ + 'name' => 'First item', + 'weight' => 250, + ]), 2); $this->assertItemsInCart(2, $cart); $cart->store($identifier); @@ -1248,7 +1389,9 @@ public function cart_can_create_items_from_models_using_the_canbebought_trait() { $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProductTrait(1, 'First item', 10.00), 2); + $cart->add(new BuyableProductTrait([ + 'name' => 'First item', + ]), 2); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -1271,7 +1414,9 @@ public function it_does_calculate_correct_results_with_rational_qtys() // https://github.com/Crinsane/LaravelShoppingcart/issues/544 $cart = $this->getCart(); - $cart->add(new BuyableProductTrait(1, 'First item', 10.00), 0.5); + $cart->add(new BuyableProductTrait([ + 'name' => 'First item', + ]), 0.5); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -1304,8 +1449,13 @@ public function it_can_merge_without_dispatching_add_events() ]); $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'Item', 10.00), 1); - $cart->add(new BuyableProduct(2, 'Item 2', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'Item', + ]), 1); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Item 2', + ]), 1); $cart->store('test'); Event::fakeFor(function () { @@ -1334,8 +1484,13 @@ public function it_can_merge_dispatching_add_events() ]); $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct(1, 'Item', 10.00), 1); - $cart->add(new BuyableProduct(2, 'Item 2', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'Item', + ]), 1); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Item 2', + ]), 1); $cart->store('test'); Event::fakeFor(function () { @@ -1362,9 +1517,20 @@ public function it_use_correctly_rounded_values_for_totals_and_cart_summary() $cart = $this->getCartDiscount(6); - $cartItem = $cart->add(new BuyableProduct(1, 'First item', 0.18929), 1000); - $cart->add(new BuyableProduct(2, 'Second item', 4.41632), 5); - $cart->add(new BuyableProduct(3, 'Third item', 0.37995), 25); + $cartItem = $cart->add(new BuyableProduct([ + 'name' => 'First item', + 'price' => 0.18929, + ]), 1000); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Second item', + 'price' => 4.41632, + ]), 5); + $cart->add(new BuyableProduct([ + 'id' => 3, + 'name' => 'Third item', + 'price' => 0.37995, + ]), 25); $cart->setGlobalTax(22); @@ -1381,7 +1547,10 @@ public function it_use_gross_price_as_base_price() $cart = $this->getCartDiscount(0); config(['cart.calculator' => GrossPrice::class]); - $cartItem = $cart->add(new BuyableProduct(1, 'First item', 100), 2); + $cartItem = $cart->add(new BuyableProduct([ + 'name' => 'First item', + 'price' => 100, + ]), 2); $cart->setGlobalTax(22); @@ -1397,9 +1566,20 @@ public function it_use_gross_price_and_it_use_correctly_rounded_values_for_total $cart = $this->getCartDiscount(6); - $cartItem = $cart->add(new BuyableProduct(1, 'First item', 0.23093), 1000); - $cart->add(new BuyableProduct(2, 'Second item', 5.38791), 5); - $cart->add(new BuyableProduct(3, 'Third item', 0.46354), 25); + $cartItem = $cart->add(new BuyableProduct([ + 'name' => 'First item', + 'price' => 0.23093, + ]), 1000); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Second item', + 'price' => 5.38791, + ]), 5); + $cart->add(new BuyableProduct([ + 'id' => 3, + 'name' => 'Third item', + 'price' => 0.46354, + ]), 25); $cart->setGlobalTax(22); @@ -1492,7 +1672,10 @@ public function it_can_calculate_the_total_price_of_the_items_in_cart() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'first item', $price = 1000), $qty = 5); + $cart->add(new BuyableProduct([ + 'name' => 'first item', + 'price' => 1000, + ]), $qty = 5); $this->assertEquals(5000, $cart->priceTotalFloat()); } @@ -1501,7 +1684,10 @@ public function it_can_format_the_total_price_of_the_items_in_cart() { $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'first item', 1000), 5); + $cart->add(new BuyableProduct([ + 'name' => 'first item', + 'price' => 1000, + ]), 5); $this->assertEquals('5,000.00', $cart->priceTotal()); $this->assertEquals('5,000.0000', $cart->priceTotal(4, '.', ',')); } @@ -1516,8 +1702,13 @@ public function it_can_erase_saved_cart_from_the_database() Event::fake(); $cart = $this->getCart(); - $cart->add(new BuyableProduct(1, 'Item', 10.00), 1); - $cart->add(new BuyableProduct(2, 'Item 2', 10.00), 1); + $cart->add(new BuyableProduct([ + 'name' => 'Item', + ]), 1); + $cart->add(new BuyableProduct([ + 'id' => 2, + 'name' => 'Item 2', + ]), 1); $cart->store($identifier = 'test'); $cart->erase($identifier); Event::assertDispatched('cart.erased'); diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index 7ef6e5d2..be0dc7cf 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -3,43 +3,30 @@ namespace Gloudemans\Tests\Shoppingcart\Fixtures; use Gloudemans\Shoppingcart\Contracts\Buyable; +use Illuminate\Database\Eloquent\Model; -class BuyableProduct implements Buyable +class BuyableProduct extends Model implements Buyable { /** - * @var int|string - */ - private $id; - - /** - * @var string - */ - private $name; - - /** - * @var float - */ - private $price; - - /** - * @var float - */ - private $weight; - - /** - * BuyableProduct constructor. + * The attributes that are mass assignable. * - * @param int|string $id - * @param string $name - * @param float $price + * @var array */ - public function __construct($id = 1, $name = 'Item name', $price = 10.00, $weight = 0) - { - $this->id = $id; - $this->name = $name; - $this->price = $price; - $this->weight = $weight; - } + protected $fillable = [ + 'id', + 'name', + 'title', + 'description', + 'price', + 'weight', + ]; + + protected $attributes = [ + 'id' => 1, + 'name' => 'Item name', + 'price' => 10.00, + 'weight' => 0, + ]; /** * Get the identifier of the Buyable item. diff --git a/tests/Fixtures/BuyableProductTrait.php b/tests/Fixtures/BuyableProductTrait.php index 7d0c9884..29e39f31 100644 --- a/tests/Fixtures/BuyableProductTrait.php +++ b/tests/Fixtures/BuyableProductTrait.php @@ -3,43 +3,30 @@ namespace Gloudemans\Tests\Shoppingcart\Fixtures; use Gloudemans\Shoppingcart\Contracts\Buyable; +use Illuminate\Database\Eloquent\Model; -class BuyableProductTrait implements Buyable +class BuyableProductTrait extends Model implements Buyable { use \Gloudemans\Shoppingcart\CanBeBought; /** - * @var int|string - */ - private $id; - - /** - * @var string - */ - private $name; - - /** - * @var float - */ - private $price; - - /** - * @var float - */ - private $weight; - - /** - * BuyableProduct constructor. + * The attributes that are mass assignable. * - * @param int|string $id - * @param string $name - * @param float $price + * @var array */ - public function __construct($id = 1, $name = 'Item name', $price = 10.00, $weight = 0) - { - $this->id = $id; - $this->name = $name; - $this->price = $price; - $this->weight = $weight; - } + protected $fillable = [ + 'id', + 'name', + 'title', + 'description', + 'price', + 'weight', + ]; + + protected $attributes = [ + 'id' => 1, + 'name' => 'Item name', + 'price' => 10.00, + 'weight' => 0, + ]; }