5
5
use Illuminate \Foundation \Testing \Concerns \InteractsWithDatabase ;
6
6
use Illuminate \Foundation \Testing \WithFaker ;
7
7
use Illuminate \Foundation \Testing \RefreshDatabase ;
8
+ use phpDocumentor \Reflection \DocBlock \Description ;
8
9
use R64 \Checkout \Helpers \Price ;
9
10
use R64 \Checkout \Models \Cart ;
11
+ use R64 \Checkout \Models \CartItem ;
10
12
use R64 \Checkout \Models \Coupon ;
11
13
use R64 \Checkout \Models \Customer ;
12
14
use R64 \Checkout \Models \Product ;
13
15
use R64 \Checkout \Tests \TestCase ;
14
16
15
17
class CartControllerTest extends TestCase
16
18
{
17
- use RefreshDatabase, WithFaker, InteractsWithDatabase ;
19
+ use RefreshDatabase;
18
20
19
21
private $ cartStructure = [
20
22
'cart_token ' ,
@@ -31,8 +33,8 @@ class CartControllerTest extends TestCase
31
33
'customer_note ' ,
32
34
'product ' => [
33
35
'name ' ,
34
- 'image '
35
- ]
36
+ 'image ' ,
37
+ ],
36
38
],
37
39
],
38
40
];
@@ -103,10 +105,138 @@ public function anybody_can_create_cart_with_one_item()
103
105
$ this ->assertCount (1 , $ cart ['cart_items ' ]);
104
106
$ this ->assertDatabaseHas ('carts ' , [
105
107
'token ' => $ cart ['cart_token ' ],
106
- 'items_subtotal ' => $ product ->getPrice ()
108
+ 'items_subtotal ' => $ product ->getPrice (),
107
109
]);
108
110
}
109
111
112
+ /**
113
+ * @test
114
+ * PUT /api/carts/{cart}
115
+ */
116
+ public function anybody_can_update_a_cart ()
117
+ {
118
+ $ cart = factory (Cart::class)->state ('with_product ' )->create ();
119
+
120
+ $ this ->json ('PUT ' , "/api/carts/ {$ cart ->token }" , [
121
+ 'customer_notes ' => "here we go, it's a note " ,
122
+ ])
123
+ ->assertStatus (200 )
124
+ ->assertJson (['success ' => true ])
125
+ ->assertJsonStructure ([
126
+ 'success ' ,
127
+ 'data ' => $ this ->cartStructure ,
128
+ ]);
129
+
130
+ $ this ->assertDatabaseHas ('carts ' , [
131
+ 'customer_notes ' => "here we go, it's a note " ,
132
+ ]);
133
+ }
134
+
135
+ /**
136
+ * @test
137
+ * PUT /api/carts/{cart}
138
+ */
139
+ public function billing_information_is_the_same_as_shipping_by_default ()
140
+ {
141
+ $ cart = factory (Cart::class)->create ();
142
+
143
+ $ response = $ this ->json ('PUT ' , "/api/carts/ {$ cart ->token }" , [
144
+ 'shipping_first_name ' => "first name " ,
145
+ 'shipping_last_name ' => "last name " ,
146
+ 'shipping_address_line1 ' => "line 1 " ,
147
+ 'shipping_address_line2 ' => "line 2 " ,
148
+ 'shipping_address_city ' => "city " ,
149
+ 'shipping_address_region ' => "region " ,
150
+ 'shipping_address_zipcode ' => "zipcode " ,
151
+ 'shipping_address_phone ' => "123123 " ,
152
+ ])
153
+ ->assertStatus (200 )
154
+ ->assertJson (['success ' => true ])
155
+ ->assertJsonStructure ([
156
+ 'success ' ,
157
+ 'data ' => $ this ->cartStructure ,
158
+ ]);
159
+
160
+ $ response = json_decode ($ response ->getContent (), true )['data ' ];
161
+
162
+ $ this ->assertEquals ($ response ['shipping_first_name ' ], $ response ['billing_first_name ' ]);
163
+ $ this ->assertEquals ($ response ['shipping_last_name ' ], $ response ['billing_last_name ' ]);
164
+ $ this ->assertEquals ($ response ['shipping_address_line1 ' ], $ response ['billing_address_line1 ' ]);
165
+ $ this ->assertEquals ($ response ['shipping_address_line2 ' ], $ response ['billing_address_line2 ' ]);
166
+ $ this ->assertEquals ($ response ['shipping_address_city ' ], $ response ['billing_address_city ' ]);
167
+ $ this ->assertEquals ($ response ['shipping_address_region ' ], $ response ['billing_address_region ' ]);
168
+ $ this ->assertEquals ($ response ['shipping_address_zipcode ' ], $ response ['billing_address_zipcode ' ]);
169
+ $ this ->assertEquals ($ response ['shipping_address_phone ' ], $ response ['billing_address_phone ' ]);
170
+ }
171
+
172
+ /**
173
+ * @test
174
+ * PUT /api/carts/{cart}
175
+ */
176
+ public function billing_information_is_not_the_same_as_shipping_when_billing_same_false ()
177
+ {
178
+ $ cart = factory (Cart::class)->create (['billing_same ' => false ]);
179
+
180
+ $ response = $ this ->json ('PUT ' , "/api/carts/ {$ cart ->token }" , [
181
+ 'shipping_first_name ' => "first name " ,
182
+ 'shipping_last_name ' => "last name " ,
183
+ 'shipping_address_line1 ' => "line 1 " ,
184
+ 'shipping_address_line2 ' => "line 2 " ,
185
+ 'shipping_address_city ' => "city " ,
186
+ 'shipping_address_region ' => "region " ,
187
+ 'shipping_address_zipcode ' => "zipcode " ,
188
+ 'shipping_address_phone ' => "123123 " ,
189
+ 'billing_first_name ' => 'billing first name '
190
+ ])
191
+ ->assertStatus (200 )
192
+ ->assertJson (['success ' => true ])
193
+ ->assertJsonStructure ([
194
+ 'success ' ,
195
+ 'data ' => $ this ->cartStructure ,
196
+ ]);
197
+
198
+ $ response = json_decode ($ response ->getContent (), true )['data ' ];
199
+
200
+ $ this ->assertEquals ('billing first name ' , $ response ['billing_first_name ' ]);
201
+
202
+ $ this ->assertNull ($ response ['billing_last_name ' ]);
203
+ $ this ->assertNull ($ response ['billing_address_line1 ' ]);
204
+ $ this ->assertNull ($ response ['billing_address_line2 ' ]);
205
+ $ this ->assertNull ($ response ['billing_address_city ' ]);
206
+ $ this ->assertNull ($ response ['billing_address_region ' ]);
207
+ $ this ->assertNull ($ response ['billing_address_zipcode ' ]);
208
+ $ this ->assertNull ($ response ['billing_address_phone ' ]);
209
+ }
210
+
211
+ /**
212
+ * @test
213
+ * PUT /api/carts/{cart}
214
+ */
215
+ public function discount_code_discounts_the_items_subtotal ()
216
+ {
217
+ $ cart = factory (Cart::class)->create ();
218
+ $ product = factory (Product::class)->create (['price ' => 100000 ]);
219
+ CartItem::makeOne ($ cart , ['product_id ' => $ product ->id ]);
220
+
221
+ $ coupon = factory (Coupon::class)->state ('$10off ' )->create ();
222
+
223
+ $ response = $ this ->json ('PUT ' , "/api/carts/ {$ cart ->token }" , [
224
+ 'coupon_code ' => $ coupon ->code ,
225
+ ])
226
+ ->assertStatus (200 )
227
+ ->assertJson (['success ' => true ])
228
+ ->assertJsonStructure ([
229
+ 'success ' ,
230
+ 'data ' => $ this ->cartStructure ,
231
+ ]);
232
+
233
+ $ response = json_decode ($ response ->getContent (), true )['data ' ];
234
+
235
+ $ this ->assertEquals ('10.00 ' , $ response ['discount ' ]);
236
+ $ this ->assertEquals ('1,000.00 ' , $ response ['items_subtotal ' ]);
237
+ $ this ->assertEquals ('990.00 ' , $ response ['total ' ]);
238
+ }
239
+
110
240
/**
111
241
* @test
112
242
* DELETE /api/carts
@@ -189,7 +319,7 @@ public function customer_can_create_cart_with_one_item()
189
319
public function customer_can_update_a_cart ()
190
320
{
191
321
$ customer = factory (Customer::class)->create ([
192
-
322
+
193
323
]);
194
324
$ cart = factory (Cart::class)->state ('with_product ' )->create ([
195
325
'customer_id ' => $ customer ->id ,
@@ -207,7 +337,7 @@ public function customer_can_update_a_cart()
207
337
]);
208
338
209
339
$ this ->assertDatabaseHas ('carts ' , [
210
- 'customer_email ' =>
'[email protected] '
340
+ 'customer_email ' =>
'[email protected] ' ,
211
341
]);
212
342
}
213
343
}
0 commit comments