mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
feat: add Cart::fake(), assertion methods, and CartFactory
Adds Cart::fake() which swaps the active driver to NullDriver and returns a CartFactory fluent test builder. Adds assertion methods (assertContains, assertCount, assertTotalQuantity, assertSubTotal, assertTotal, assertConditionApplied, assertEmpty, assertNotEmpty) to Cart for use in tests. CartFactory provides withItems(), withCondition(), and withCoupon() as chainable helpers for seeding cart state.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
use Wearepixel\Cart\CartCondition;
|
||||
use Wearepixel\Cart\Drivers\SessionDriver;
|
||||
use Wearepixel\Cart\Testing\CartFactory;
|
||||
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||
|
||||
function makeCartForFactory(): Cart
|
||||
{
|
||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch');
|
||||
|
||||
return new Cart(
|
||||
new SessionDriver(new SessionMock, 'TESTKEY'),
|
||||
$events,
|
||||
'cart',
|
||||
require __DIR__ . '/../../Helpers/ConfigMock.php',
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(function () {
|
||||
$this->cart = makeCartForFactory();
|
||||
$this->factory = new CartFactory($this->cart);
|
||||
});
|
||||
|
||||
afterEach(fn() => Mockery::close());
|
||||
|
||||
describe('CartFactory', function () {
|
||||
test('withItems adds N generic items to the cart', function () {
|
||||
$this->factory->withItems(3);
|
||||
|
||||
expect($this->cart->getContent()->count())->toBe(3);
|
||||
expect($this->cart->getTotalQuantity())->toBe(3);
|
||||
});
|
||||
|
||||
test('withCondition adds a condition to the cart', function () {
|
||||
$condition = new CartCondition(['name' => 'GST', 'type' => 'tax', 'value' => '10%', 'target' => 'subtotal']);
|
||||
$this->factory->withCondition($condition);
|
||||
|
||||
expect($this->cart->getCondition('GST'))->not->toBeNull();
|
||||
});
|
||||
|
||||
test('methods are chainable', function () {
|
||||
$result = $this->factory->withItems(2)->withCondition(
|
||||
new CartCondition(['name' => 'Discount', 'type' => 'discount', 'value' => '-5%', 'target' => 'subtotal'])
|
||||
);
|
||||
|
||||
expect($result)->toBeInstanceOf(CartFactory::class);
|
||||
expect($this->cart->getContent()->count())->toBe(2);
|
||||
expect($this->cart->getCondition('Discount'))->not->toBeNull();
|
||||
});
|
||||
|
||||
test('withItems generates unique item IDs', function () {
|
||||
$this->factory->withItems(3);
|
||||
$ids = $this->cart->getContent()->keys()->toArray();
|
||||
|
||||
expect(array_unique($ids))->toHaveCount(3);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user