CartFeatureTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace Tests\Feature\Front\Cart;
  3. use App\Shop\Addresses\Address;
  4. use App\Shop\Carts\Repositories\CartRepository;
  5. use App\Shop\Carts\ShoppingCart;
  6. use App\Shop\Cities\City;
  7. use App\Shop\Customers\Customer;
  8. use App\Shop\ProductAttributes\ProductAttribute;
  9. use App\Shop\Products\Product;
  10. use Illuminate\Auth\Events\Lockout;
  11. use Tests\TestCase;
  12. class CartFeatureTest extends TestCase
  13. {
  14. /** @test */
  15. public function it_can_see_the_success_page()
  16. {
  17. $this
  18. ->actingAs($this->customer, 'web')
  19. ->get(route('checkout.success'))
  20. ->assertStatus(200);
  21. }
  22. /** @test */
  23. public function it_can_see_the_cancel_page()
  24. {
  25. $this
  26. ->actingAs($this->customer, 'web')
  27. ->get(route('checkout.cancel'))
  28. ->assertStatus(200);
  29. }
  30. /** @test */
  31. public function it_can_add_to_cart_products_with_combination()
  32. {
  33. $product = factory(Product::class)->create();
  34. $productAttribute = factory(ProductAttribute::class)->create([
  35. 'product_id' => $product->id
  36. ]);
  37. $data = [
  38. 'product' => $product->id,
  39. 'quantity' => 1,
  40. 'productAttribute' => $productAttribute->id
  41. ];
  42. $this
  43. ->post(route('cart.store', $data))
  44. ->assertStatus(302)
  45. ->assertSessionHas('message', 'Add to cart successful')
  46. ->assertRedirect(route('cart.index'));
  47. }
  48. /** @test */
  49. public function it_shows_the_checkout_page_after_successful_login()
  50. {
  51. $data = ['email' => $this->customer->email, 'password' => 'secret'];
  52. $this
  53. ->post(route('cart.login'), $data)
  54. ->assertStatus(302)
  55. ->assertRedirect(route('checkout.index'));
  56. }
  57. /** @test */
  58. public function it_throws_the_too_many_login_attempts_event()
  59. {
  60. $this->expectsEvents(Lockout::class);
  61. $customer = factory(Customer::class)->create();
  62. for ($i=0; $i <= 5; $i++) {
  63. $data = [
  64. 'email' => $customer->email,
  65. 'password' => 'unknown'
  66. ];
  67. $this->post(route('cart.login'), $data);
  68. }
  69. }
  70. /** @test */
  71. public function it_shows_the_login_form_when_checking_out()
  72. {
  73. $this->get(route('cart.login'))
  74. ->assertStatus(200)
  75. ->assertSee('Email')
  76. ->assertSee('Password')
  77. ->assertSee('Login now')
  78. ->assertSee('I forgot my password');
  79. }
  80. /** @test */
  81. public function it_shows_error_page_when_checking_out_without_item_in_the_cart()
  82. {
  83. $this
  84. ->actingAs($this->customer, 'checkout')
  85. ->get(route('checkout.index'))
  86. ->assertStatus(200)
  87. ->assertSee('No products in cart yet.')
  88. ->assertSee('Show now!');
  89. }
  90. /** @test */
  91. public function it_errors_when_customer_has_no_address_yet_upon_checkout()
  92. {
  93. $cartRepo = new CartRepository(new ShoppingCart);
  94. $cartRepo->addToCart($this->product, 1);
  95. $this
  96. ->actingAs($this->customer, 'checkout')
  97. ->get(route('checkout.index'))
  98. ->assertStatus(200)
  99. ->assertSee('No address found. You need to create an address first here.');
  100. }
  101. /** @test */
  102. public function it_shows_the_checkout_page_when_user_is_logged_in()
  103. {
  104. $cartRepo = new CartRepository(new ShoppingCart);
  105. $cartRepo->addToCart($this->product, 1);
  106. factory(City::class)->create();
  107. factory(Address::class)->create([
  108. 'customer_id' => $this->customer->id
  109. ]);
  110. $this
  111. ->actingAs($this->customer, 'checkout')
  112. ->get(route('checkout.index'))
  113. ->assertStatus(200)
  114. ->assertSee('Billing Address')
  115. ->assertSee('Delivery Address')
  116. ->assertSee('Choose payment')
  117. ->assertSee('Cover')
  118. ->assertSee('Name')
  119. ->assertSee('Quantity')
  120. ->assertSee('Price')
  121. ->assertSee('Subtotal')
  122. ->assertSee('Shipping')
  123. ->assertSee('Tax')
  124. ->assertSee('Total');
  125. }
  126. /** @test */
  127. public function it_redirects_to_login_screen_when_checking_out_while_you_are_still_logged_out()
  128. {
  129. $this
  130. ->get(route('checkout.index'))
  131. ->assertStatus(302)
  132. ->assertRedirect(route('login'));
  133. }
  134. /** @test */
  135. public function it_can_remove_the_item_in_the_cart()
  136. {
  137. $product = factory(Product::class)->create();
  138. $data = [
  139. 'product' => $product->id,
  140. 'quantity' => 1
  141. ];
  142. $this
  143. ->actingAs($this->customer, 'web')
  144. ->post(route('cart.store', $data));
  145. $cartRepo = new CartRepository(new ShoppingCart());
  146. $items = $cartRepo->getCartItems();
  147. $items->each(function ($item) {
  148. $this
  149. ->actingAs($this->customer, 'web')
  150. ->delete(route('cart.destroy', $item->rowId))
  151. ->assertStatus(302)
  152. ->assertRedirect(route('cart.index'))
  153. ->assertSessionHas('message', 'Removed to cart successful');
  154. });
  155. }
  156. /** @test */
  157. public function it_can_update_the_cart()
  158. {
  159. $product = factory(Product::class)->create();
  160. $data = [
  161. 'product' => $product->id,
  162. 'quantity' => 1
  163. ];
  164. $this
  165. ->actingAs($this->customer, 'web')
  166. ->post(route('cart.store', $data));
  167. $cartRepo = new CartRepository(new ShoppingCart());
  168. $items = $cartRepo->getCartItems();
  169. $items->each(function ($item) {
  170. $this
  171. ->actingAs($this->customer, 'web')
  172. ->put(route('cart.update', $item->rowId), ['quantity' => 1])
  173. ->assertStatus(302)
  174. ->assertRedirect(route('cart.index'))
  175. ->assertSessionHas('message', 'Update cart successful');
  176. });
  177. }
  178. /** @test */
  179. public function it_can_show_the_customer_cart()
  180. {
  181. $product = factory(Product::class)->create();
  182. $data = [
  183. 'product' => $product->id,
  184. 'quantity' => 1
  185. ];
  186. $this
  187. ->actingAs($this->customer, 'web')
  188. ->post(route('cart.store', $data));
  189. $this
  190. ->actingAs($this->customer, 'web')
  191. ->get(route('cart.index'))
  192. ->assertStatus(200)
  193. ->assertSee($product->name);
  194. }
  195. /** @test */
  196. public function it_errors_when_adding_a_product_to_cart_without_the_product()
  197. {
  198. $data = [
  199. 'quantity' => 1
  200. ];
  201. $this
  202. ->post(route('cart.store', $data))
  203. ->assertStatus(302)
  204. ->assertSessionHasErrors(['product' => 'The product field is required.']);
  205. }
  206. /** @test */
  207. public function it_errors_when_adding_a_product_to_cart_without_the_quantity()
  208. {
  209. $product = factory(Product::class)->create();
  210. $data = [
  211. 'product' => $product->id
  212. ];
  213. $this
  214. ->post(route('cart.store', $data))
  215. ->assertStatus(302)
  216. ->assertSessionHasErrors(['quantity' => 'The quantity field is required.']);
  217. }
  218. /** @test */
  219. public function it_can_add_to_cart()
  220. {
  221. $product = factory(Product::class)->create();
  222. $data = [
  223. 'product' => $product->id,
  224. 'quantity' => 1
  225. ];
  226. $this
  227. ->post(route('cart.store', $data))
  228. ->assertStatus(302)
  229. ->assertSessionHas('message', 'Add to cart successful')
  230. ->assertRedirect(route('cart.index'));
  231. }
  232. }