Skip to content

Commit

Permalink
Merge pull request #111 from Norris1z/master
Browse files Browse the repository at this point in the history
Use current instance when checking if storedCartWithIdentifierExists
  • Loading branch information
Patrick committed Dec 27, 2020
2 parents 8b1ae2d + b68d5f1 commit 786477b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ If you want to retrieve the cart from the database and restore it, all you have
If you want to merge the cart with another one from the database, all you have to do is call the `merge($identifier)` where `$identifier` is the key you specified for the `store` method. You can also define if you want to keep the discount and tax rates of the items and if you want to dispatch "cart.added" events.

// Merge the contents of 'savedcart' into 'username'.
Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, $dispatchAdd);
Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, $dispatchAdd, 'savedcartinstance');

### Erasing the cart
If you want to erase the cart from the database, all you have to do is call the `erase($identifier)` where `$identifier` is the key you specified for the `store` method.
Expand Down
2 changes: 1 addition & 1 deletion README_Idn.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ Jika Anda ingin mengambil keranjang dari database dan mengembalikannya, yang har
Jika Anda ingin menggabungkan keranjang dengan keranjang lain dari basis data, yang harus Anda lakukan adalah memanggil `gabungan ($ identifier)` di mana `$ identifier` adalah kunci yang Anda tentukan untuk metode` store`. Anda juga dapat menentukan apakah Anda ingin mempertahankan potongan harga dan tarif pajak item.

// Merge the contents of 'savedcart' into 'username'.
Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate);
Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, 'savedcartinstance');

## Pengecualian

Expand Down
2 changes: 1 addition & 1 deletion README_uk-UA.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ foreach(Cart::content() as $row) {
Якщо ви хочете злити кошик із іншим кошиком, збереженим у базі даних, вам знадобиться викликати метод `merge($identifier)`, де `$identifier` - це ключ, який ви зазначили у методі`store`. Ви також можете визначити чи хочете ви зберегти знижку і ставку оподаткування для товарів.

// Merge the contents of 'savedcart' into 'username'.
Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate);
Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, 'savedcartinstance');

## Перехоплення

Expand Down
32 changes: 18 additions & 14 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,15 @@ public function store($identifier)
$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(),
Expand All @@ -664,17 +666,17 @@ public function restore($identifier)
$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();
Expand All @@ -692,7 +694,7 @@ public function restore($identifier)
$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();
}

/**
Expand All @@ -708,11 +710,13 @@ public function erase($identifier)
$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');
}
Expand All @@ -727,14 +731,14 @@ public function erase($identifier)
*
* @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);

Expand Down Expand Up @@ -832,9 +836,9 @@ private function isMulti($item)
*
* @return bool
*/
private function storedCartWithIdentifierExists($identifier)
private function storedCartInstanceWithIdentifierExists($instance, $identifier)
{
return $this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->exists();
return $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance'=> $instance])->exists();
}

/**
Expand Down
88 changes: 79 additions & 9 deletions tests/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,11 +865,11 @@ public function it_can_store_the_cart_in_a_database()

$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 */
Expand Down Expand Up @@ -905,15 +905,15 @@ public function it_can_store_and_retrieve_cart_from_the_database_with_correct_ti

$cart->store($identifier);

Event::assertDispatched('cart.stored');

sleep(1);
$afterSecondStore = Carbon::now();

$cart->restore($identifier);

$this->assertTrue($beforeStore->lessThanOrEqualTo($cart->createdAt()) && $afterStore->greaterThanOrEqualTo($cart->createdAt()));
$this->assertTrue($beforeSecondStore->lessThanOrEqualTo($cart->updatedAt()) && $afterSecondStore->greaterThanOrEqualTo($cart->updatedAt()));

Event::assertDispatched('cart.stored');
}

/**
Expand All @@ -936,9 +936,9 @@ public function it_will_throw_an_exception_when_a_cart_was_already_stored_using_

$cart->store($identifier = 123);

$cart->store($identifier);

Event::assertDispatched('cart.stored');

$cart->store($identifier);
}

/** @test */
Expand All @@ -962,11 +962,11 @@ public function it_can_restore_a_cart_from_the_database()

$cart->restore($identifier);

Event::assertDispatched('cart.restored');

$this->assertItemsInCart(1, $cart);

$this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => 'default']);

Event::assertDispatched('cart.restored');
}

/** @test */
Expand Down Expand Up @@ -1011,7 +1011,7 @@ 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->update('027c91341fd5cf4d2579b49c4b6a90da', ['qty'=>2]);
$cart->update('027c91341fd5cf4d2579b49c4b6a90da', ['qty' => 2]);

$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');

Expand Down Expand Up @@ -1453,4 +1453,74 @@ private function setConfigFormat($decimals, $decimalPoint, $thousandSeperator)
$this->app['config']->set('cart.format.decimal_point', $decimalPoint);
$this->app['config']->set('cart.format.thousand_separator', $thousandSeperator);
}

/** @test */
public function it_can_store__mutiple_instances_of_the_cart_in_a_database()
{
$this->artisan('migrate', [
'--database' => 'testing',
]);

Event::fake();

$cart = $this->getCart();

$cart->add(new BuyableProduct());

$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($identifier);

Event::assertDispatched('cart.stored');

$newInstanceSerialized = serialize($newInstance->content());

$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => Cart::DEFAULT_INSTANCE, 'content' => $serialized]);

$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]);
}

/** @test */
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);
$this->assertEquals(5000, $cart->priceTotalFloat());
}

/** @test */
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);
$this->assertEquals('5,000.00', $cart->priceTotal());
$this->assertEquals('5,000.0000', $cart->priceTotal(4, '.', ','));
}

/** @test */
public function it_can_erase_saved_cart_from_the_database()
{
$this->artisan('migrate', [
'--database' => 'testing',
]);

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->store($identifier = 'test');
$cart->erase($identifier);
Event::assertDispatched('cart.erased');
$this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => Cart::DEFAULT_INSTANCE]);
}
}

0 comments on commit 786477b

Please sign in to comment.