mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
Use instance in query if there are more than one instances and add tests
This commit is contained in:
@@ -98,7 +98,7 @@ class Cart
|
|||||||
$instance = $instance->getInstanceIdentifier();
|
$instance = $instance->getInstanceIdentifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->instance = 'cart.'.$instance;
|
$this->instance = 'cart.' . $instance;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -831,7 +831,11 @@ class Cart
|
|||||||
*/
|
*/
|
||||||
private function storedCartWithIdentifierExists($identifier)
|
private function storedCartWithIdentifierExists($identifier)
|
||||||
{
|
{
|
||||||
return $this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->where('instance', $this->currentInstance())->exists();
|
$data = ['identifier' => $identifier];
|
||||||
|
if ($this->countInstances() > 1) {
|
||||||
|
$data['instance'] = $this->currentInstance();
|
||||||
|
}
|
||||||
|
return $this->getConnection()->table($this->getTableName())->where($data)->exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ class CartTest extends TestCase
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->app->afterResolving('migrator', function ($migrator) {
|
$this->app->afterResolving('migrator', function ($migrator) {
|
||||||
$migrator->path(realpath(__DIR__.'/../src/Database/migrations'));
|
$migrator->path(realpath(__DIR__ . '/../src/Database/migrations'));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1011,7 +1011,7 @@ class CartTest extends TestCase
|
|||||||
$cart = $this->getCartDiscount(50);
|
$cart = $this->getCartDiscount(50);
|
||||||
$cart->add(new BuyableProduct(1, 'First item', 10.00), 1);
|
$cart->add(new BuyableProduct(1, 'First item', 10.00), 1);
|
||||||
|
|
||||||
$cart->update('027c91341fd5cf4d2579b49c4b6a90da', ['qty'=>2]);
|
$cart->update('027c91341fd5cf4d2579b49c4b6a90da', ['qty' => 2]);
|
||||||
|
|
||||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||||
|
|
||||||
@@ -1453,4 +1453,36 @@ class CartTest extends TestCase
|
|||||||
$this->app['config']->set('cart.format.decimal_point', $decimalPoint);
|
$this->app['config']->set('cart.format.decimal_point', $decimalPoint);
|
||||||
$this->app['config']->set('cart.format.thousand_separator', $thousandSeperator);
|
$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);
|
||||||
|
|
||||||
|
$serialized = serialize($cart->content());
|
||||||
|
|
||||||
|
$newInstance = $this->getCart();
|
||||||
|
$newInstance->instance($instanceName = 'someinstance');
|
||||||
|
$newInstance->add(new BuyableProduct());
|
||||||
|
$newInstance->store($newIdentifier = 456);
|
||||||
|
$newInstanceSerialized = serialize($newInstance->content());
|
||||||
|
|
||||||
|
$this->assertDatabaseCount('shoppingcart', 2);
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]);
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('shoppingcart', ['identifier' => $newIdentifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]);
|
||||||
|
|
||||||
|
Event::assertDispatched('cart.stored');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user