CartUnitTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Tests\Unit\Cart;
  3. use App\Shop\Carts\Exceptions\ProductInCartNotFoundException;
  4. use App\Shop\Carts\Repositories\CartRepository;
  5. use App\Shop\Carts\ShoppingCart;
  6. use Gloudemans\Shoppingcart\CartItem;
  7. use Gloudemans\Shoppingcart\Exceptions\InvalidRowIDException;
  8. use Tests\TestCase;
  9. class CartUnitTest extends TestCase
  10. {
  11. /** @test */
  12. public function it_can_retrieve_the_saved_cart_from_database()
  13. {
  14. $cartRepo = new CartRepository(new ShoppingCart);
  15. $cartRepo->addToCart($this->product, 1);
  16. $cartRepo->saveCart($this->customer);
  17. // retrieve the saved cart from database
  18. $cartRepo2 = new CartRepository(new ShoppingCart);
  19. $cartRepo2->openCart($this->customer);
  20. $savedCartItem = $cartRepo->getCartItems()->first();
  21. $cartItemFromDb = $cartRepo2->getCartItems()->first();
  22. $this->assertInstanceOf(CartItem::class, $cartItemFromDb);
  23. $this->assertEquals($savedCartItem->name, $cartItemFromDb->name);
  24. $this->assertEquals($savedCartItem->price, $cartItemFromDb->price);
  25. $this->assertEquals($savedCartItem->qty, $cartItemFromDb->qty);
  26. }
  27. /** @test */
  28. public function it_can_store_the_cart_in_database()
  29. {
  30. $cartRepo = new CartRepository(new ShoppingCart);
  31. $cartRepo->addToCart($this->product, 1);
  32. $cartRepo->saveCart($this->customer);
  33. $this->assertDatabaseHas('shoppingcart', [
  34. 'identifier' => $this->customer->email,
  35. 'instance' => 'default',
  36. 'content' => serialize($cartRepo->getCartItems())
  37. ]);
  38. }
  39. /** @test */
  40. public function it_can_clear_out_your_cart()
  41. {
  42. $cartRepo = new CartRepository(new ShoppingCart);
  43. $cartRepo->addToCart($this->product, 1);
  44. $this->assertCount(1, $cartRepo->getCartItems());
  45. $cartRepo->clearCart();
  46. $this->assertCount(0, $cartRepo->getCartItems());
  47. }
  48. /** @test */
  49. public function it_returns_all_the_items_in_the_cart()
  50. {
  51. $qty = 1;
  52. $cartRepo = new CartRepository(new ShoppingCart);
  53. $cartRepo->addToCart($this->product, $qty);
  54. $lists = $cartRepo->getCartItems();
  55. foreach ($lists as $list) {
  56. $this->assertEquals($this->product->name, $list->name);
  57. $this->assertEquals($this->product->price, $list->price);
  58. $this->assertEquals($qty, $list->qty);
  59. }
  60. }
  61. /** @test */
  62. public function it_can_show_the_specific_item_in_the_cart()
  63. {
  64. $cartRepo = new CartRepository(new ShoppingCart);
  65. $cartRepo->addToCart($this->product, 1);
  66. $items = $cartRepo->getCartItems();
  67. $product = [];
  68. foreach ($items as $item) {
  69. $product[] = $cartRepo->findItem($item->rowId);
  70. }
  71. $this->assertEquals($product[0]->name, $this->product->name);
  72. }
  73. /** @test */
  74. public function it_can_update_the_cart_qty_in_the_cart()
  75. {
  76. $cartRepo = new CartRepository(new ShoppingCart);
  77. $cartRepo->addToCart($this->product, 1);
  78. $items = $cartRepo->getCartItems();
  79. $rowId = [];
  80. foreach ($items as $item) {
  81. $rowId[] = $item->rowId;
  82. $cartRepo->updateQuantityInCart($item->rowId, 3);
  83. }
  84. $this->assertEquals(3, $cartRepo->findItem($rowId[0])->qty);
  85. }
  86. /** @test */
  87. public function it_can_return_the_total_value_of_the_items_in_the_cart()
  88. {
  89. $qty = 3;
  90. $cartRepo = new CartRepository(new ShoppingCart);
  91. $cartRepo->addToCart($this->product, $qty);
  92. $total = $cartRepo->getTotal();
  93. $totalPrice = $this->product->price * $qty;
  94. $grandTotal = $totalPrice + $cartRepo->getTax();
  95. $this->assertEquals($grandTotal, $total);
  96. }
  97. /** @test */
  98. public function it_can_return_the_sub_total_of_the_items()
  99. {
  100. $cartRepo = new CartRepository(new ShoppingCart);
  101. $cartRepo->addToCart($this->product, 1);
  102. $cartRepo->addToCart($this->product, 1);
  103. $subtotal = $cartRepo->getSubTotal();
  104. $this->assertEquals(10, $subtotal);
  105. }
  106. /** @test */
  107. public function it_can_count_the_total_items_in_the_cart()
  108. {
  109. $cartRepo = new CartRepository(new ShoppingCart);
  110. $cartRepo->addToCart($this->product, 1);
  111. $count = $cartRepo->countItems();
  112. $this->assertEquals(1, $count);
  113. }
  114. /** @test */
  115. public function it_errors_when_removing_item_in_the_cart_with_cart_rowID_not_existing()
  116. {
  117. $this->expectException(ProductInCartNotFoundException::class);
  118. $cartRepo = new CartRepository(new ShoppingCart);
  119. $cartRepo->addToCart($this->product, 1);
  120. $cartRepo->removeToCart('unknown');
  121. }
  122. /** @test */
  123. public function it_can_remove_item_in_the_cart()
  124. {
  125. $cartRepo = new CartRepository(new ShoppingCart);
  126. $cartRepo->addToCart($this->product, 1);
  127. $items = $cartRepo->getCartItems();
  128. foreach ($items as $item) {
  129. $this->expectException(InvalidRowIDException::class);
  130. $cartRepo->removeToCart($item->rowId);
  131. $cartRepo->findItem($item->rowId);
  132. }
  133. }
  134. /** @test */
  135. public function it_can_add_to_cart()
  136. {
  137. $cartRepo = new CartRepository(new ShoppingCart);
  138. $cartRepo->addToCart($this->product, 1);
  139. $items = $cartRepo->getCartItems();
  140. foreach ($items as $item) {
  141. $this->assertEquals($this->product->name, $item->name);
  142. }
  143. }
  144. }