From 59eadb74a1a71f9ff6e813b716ac2aa546f4954e Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Wed, 9 Jan 2019 14:08:09 +0100 Subject: [PATCH] Removed Cart facade from coverage (does not provide a public inteterface) Exclude migrations from coverage Added tests for InstanceIdentifier Added InstanceIdentifier support to store, restore and merge Added weight to CanBeBought trait Excluded CanBeBought trait from coverage temporarily --- phpunit.xml | 5 ++++ src/CanBeBought.php | 29 +++++++++++++++---- src/Cart.php | 6 ++++ tests/CartTest.php | 28 ++++++++++++++++++ tests/Fixtures/Identifiable.php | 51 +++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 tests/Fixtures/Identifiable.php diff --git a/phpunit.xml b/phpunit.xml index 7c286ab..66dae15 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -16,6 +16,11 @@ ./src + + ./src/CanBeBought.php + ./src/Facades + ./src/Database/migrations + \ No newline at end of file diff --git a/src/CanBeBought.php b/src/CanBeBought.php index 96e0e53..ac2c551 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -16,15 +16,20 @@ trait CanBeBought } /** - * Get the description or title of the Buyable item. + * Get the name, title or description of the Buyable item. * * @return string */ public function getBuyableDescription($options = null) { - if(property_exists($this, 'name')) return $this->name; - if(property_exists($this, 'title')) return $this->title; - if(property_exists($this, 'description')) return $this->description; + if(property_exists($this, 'name')) + return $this->name; + + if(property_exists($this, 'title')) + return $this->title; + + if(property_exists($this, 'description')) + return $this->description; return null; } @@ -36,8 +41,20 @@ trait CanBeBought */ public function getBuyablePrice($options = null) { - if(property_exists($this, 'price')) return $this->price; - + if(property_exists($this, 'price')) + return $this->price; return null; } + + /** + * Get the weight of the Buyable item. + * + * @return float + */ + public function getBuyableWeight($options = null) + { + if(property_exists($this, 'weight')) + return $this->weight; + return 0; + } } \ No newline at end of file diff --git a/src/Cart.php b/src/Cart.php index 41b572f..f123cb9 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -547,6 +547,9 @@ class Cart { $content = $this->getContent(); + if ($identifier instanceof InstanceIdentifier) + $identifier = $identifier->getInstanceIdentifier(); + if ($this->storedCartWithIdentifierExists($identifier)) throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored."); @@ -567,6 +570,9 @@ class Cart */ public function restore($identifier) { + if ($identifier instanceof InstanceIdentifier) + $identifier = $identifier->getInstanceIdentifier(); + if( ! $this->storedCartWithIdentifierExists($identifier)) return; diff --git a/tests/CartTest.php b/tests/CartTest.php index 96445ce..53f2812 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -15,6 +15,7 @@ use Illuminate\Contracts\Auth\Authenticatable; use Gloudemans\Shoppingcart\ShoppingcartServiceProvider; use Gloudemans\Tests\Shoppingcart\Fixtures\ProductModel; use Gloudemans\Tests\Shoppingcart\Fixtures\BuyableProduct; +use Gloudemans\Tests\Shoppingcart\Fixtures\Identifiable; class CartTest extends TestCase { @@ -1066,6 +1067,33 @@ class CartTest extends TestCase $this->assertEquals('500.00', $cart->weight(2)); $this->assertEquals(500.00, $cart->weightFloat()); $this->assertEquals(500.00, $cartItem->weightTotal); + $this->assertEquals('250.00', $cartItem->weight(2)); + } + + /** @test */ + public function cart_can_create_and_restore_from_instance_identifier() + { + $this->artisan('migrate', [ + '--database' => 'testing', + ]); + + Event::fake(); + + $identifier = new Identifiable('User1', 0); + $cart = $this->getCart(); + + $cart->instance($identifier); + $this->assertEquals('User1', $cart->currentInstance()); + + $cart->add(new BuyableProduct(1, 'First item', 10.00, 250), 2); + $this->assertItemsInCart(2, $cart); + + $cart->store($identifier); + $cart->destroy(); + $this->assertItemsInCart(0, $cart); + + $cart->restore($identifier); + $this->assertItemsInCart(2, $cart); } /** diff --git a/tests/Fixtures/Identifiable.php b/tests/Fixtures/Identifiable.php new file mode 100644 index 0000000..5f1aa7d --- /dev/null +++ b/tests/Fixtures/Identifiable.php @@ -0,0 +1,51 @@ +identifier = $identifier; + $this->discountRate = $discountRate; + } + + /** + * Get the unique identifier to load the Cart from + * + * @return int|string + */ + public function getInstanceIdentifier($options = null) + { + return $this->identifier; + } + + /** + * Get the unique identifier to load the Cart from + * + * @return int|string + */ + public function getInstanceGlobalDiscount($options = null) + { + return $this->discountRate; + } +} \ No newline at end of file