rename the storedCartWithIdentifier method to storedCartInstanceWithIdentifierExists, changed the signature of the merge and storedCartInstanceWithIdentifierExists methods and add instance to all stored cart retrievals

This commit is contained in:
Norris Oduro
2020-11-15 05:55:29 +00:00
parent 3244c1b306
commit 87acd5cfb2
2 changed files with 33 additions and 31 deletions

View File

@@ -633,13 +633,15 @@ class Cart
$identifier = $identifier->getInstanceIdentifier();
}
if ($this->storedCartWithIdentifierExists($identifier)) {
$instance = $this->currentInstance();
if ($this->storedCartInstanceWithIdentifierExists($instance, $identifier)) {
throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored.");
}
$this->getConnection()->table($this->getTableName())->insert([
'identifier' => $identifier,
'instance' => $this->currentInstance(),
'instance' => $instance,
'content' => serialize($content),
'created_at' => $this->createdAt ?: Carbon::now(),
'updated_at' => Carbon::now(),
@@ -661,17 +663,17 @@ class Cart
$identifier = $identifier->getInstanceIdentifier();
}
if (!$this->storedCartWithIdentifierExists($identifier)) {
$currentInstance = $this->currentInstance();
if (!$this->storedCartInstanceWithIdentifierExists($currentInstance, $identifier)) {
return;
}
$stored = $this->getConnection()->table($this->getTableName())
->where('identifier', $identifier)->first();
->where(['identifier'=> $identifier,'instance' => $currentInstance])->first();
$storedContent = unserialize(data_get($stored, 'content'));
$currentInstance = $this->currentInstance();
$this->instance(data_get($stored, 'instance'));
$content = $this->getContent();
@@ -689,7 +691,7 @@ class Cart
$this->createdAt = Carbon::parse(data_get($stored, 'created_at'));
$this->updatedAt = Carbon::parse(data_get($stored, 'updated_at'));
$this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete();
$this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance' => $currentInstance])->delete();
}
/**
@@ -705,11 +707,13 @@ class Cart
$identifier = $identifier->getInstanceIdentifier();
}
if (!$this->storedCartWithIdentifierExists($identifier)) {
$instance = $this->currentInstance();
if (!$this->storedCartInstanceWithIdentifierExists($instance, $identifier)) {
return;
}
$this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete();
$this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance' => $instance])->delete();
$this->events->dispatch('cart.erased');
}
@@ -724,14 +728,14 @@ class Cart
*
* @return bool
*/
public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true)
public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true, $instance = SELF::DEFAULT_INSTANCE)
{
if (!$this->storedCartWithIdentifierExists($identifier)) {
if (!$this->storedCartInstanceWithIdentifierExists($instance, $identifier)) {
return false;
}
$stored = $this->getConnection()->table($this->getTableName())
->where('identifier', $identifier)->first();
->where(['identifier'=> $identifier,'instance'=>$instance])->first();
$storedContent = unserialize($stored->content);
@@ -829,14 +833,9 @@ class Cart
*
* @return bool
*/
private function storedCartWithIdentifierExists($identifier)
private function storedCartInstanceWithIdentifierExists($instance, $identifier)
{
$data = ['identifier' => $identifier];
if ($this->countInstances() > 1) {
$data['instance'] = $this->currentInstance();
}
return $this->getConnection()->table($this->getTableName())->where($data)->exists();
return $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance'=> $instance])->exists();
}
/**

View File

@@ -865,11 +865,11 @@ class CartTest extends TestCase
$cart->store($identifier = 123);
Event::assertDispatched('cart.stored');
$serialized = serialize($cart->content());
$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]);
Event::assertDispatched('cart.stored');
}
/** @test */
@@ -905,6 +905,8 @@ class CartTest extends TestCase
$cart->store($identifier);
Event::assertDispatched('cart.stored');
sleep(1);
$afterSecondStore = Carbon::now();
@@ -912,8 +914,6 @@ class CartTest extends TestCase
$this->assertTrue($beforeStore->lessThanOrEqualTo($cart->createdAt()) && $afterStore->greaterThanOrEqualTo($cart->createdAt()));
$this->assertTrue($beforeSecondStore->lessThanOrEqualTo($cart->updatedAt()) && $afterSecondStore->greaterThanOrEqualTo($cart->updatedAt()));
Event::assertDispatched('cart.stored');
}
/**
@@ -936,9 +936,9 @@ class CartTest extends TestCase
$cart->store($identifier = 123);
$cart->store($identifier);
Event::assertDispatched('cart.stored');
$cart->store($identifier);
}
/** @test */
@@ -962,11 +962,11 @@ class CartTest extends TestCase
$cart->restore($identifier);
Event::assertDispatched('cart.restored');
$this->assertItemsInCart(1, $cart);
$this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => 'default']);
Event::assertDispatched('cart.restored');
}
/** @test */
@@ -1469,18 +1469,21 @@ class CartTest extends TestCase
$cart->store($identifier = 123);
Event::assertDispatched('cart.stored');
$serialized = serialize($cart->content());
$newInstance = $this->getCart();
$newInstance->instance($instanceName = 'someinstance');
$newInstance->add(new BuyableProduct());
$newInstance->store($newIdentifier = 456);
$newInstance->store($identifier);
Event::assertDispatched('cart.stored');
$newInstanceSerialized = serialize($newInstance->content());
$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]);
$this->assertDatabaseHas('shoppingcart', ['identifier' => $newIdentifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]);
Event::assertDispatched('cart.stored');
$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]);
}
}