Skip to content

Commit 59584b9

Browse files
Add more cart controller tests
1 parent 9322a55 commit 59584b9

File tree

4 files changed

+139
-8
lines changed

4 files changed

+139
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Homestead.yaml
1212
npm-debug.log
1313
yarn-error.log
1414
.env
15+
.phpunit.result.cache

.phpunit.result.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Http/Resources/ProductResource.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public function toArray($request)
1616
{
1717
return [
1818
'name' => $this->getName(),
19-
'image' => $this->getImageUrl()
19+
'image' => $this->getImageUrl(),
20+
'price' => displayMoney($this->getPrice())
2021
];
2122
}
2223
}

tests/Controllers/CartControllerTest.php

Lines changed: 136 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
66
use Illuminate\Foundation\Testing\WithFaker;
77
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use phpDocumentor\Reflection\DocBlock\Description;
89
use R64\Checkout\Helpers\Price;
910
use R64\Checkout\Models\Cart;
11+
use R64\Checkout\Models\CartItem;
1012
use R64\Checkout\Models\Coupon;
1113
use R64\Checkout\Models\Customer;
1214
use R64\Checkout\Models\Product;
1315
use R64\Checkout\Tests\TestCase;
1416

1517
class CartControllerTest extends TestCase
1618
{
17-
use RefreshDatabase, WithFaker, InteractsWithDatabase;
19+
use RefreshDatabase;
1820

1921
private $cartStructure = [
2022
'cart_token',
@@ -31,8 +33,8 @@ class CartControllerTest extends TestCase
3133
'customer_note',
3234
'product' => [
3335
'name',
34-
'image'
35-
]
36+
'image',
37+
],
3638
],
3739
],
3840
];
@@ -103,10 +105,138 @@ public function anybody_can_create_cart_with_one_item()
103105
$this->assertCount(1, $cart['cart_items']);
104106
$this->assertDatabaseHas('carts', [
105107
'token' => $cart['cart_token'],
106-
'items_subtotal' => $product->getPrice()
108+
'items_subtotal' => $product->getPrice(),
107109
]);
108110
}
109111

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+
110240
/**
111241
* @test
112242
* DELETE /api/carts
@@ -189,7 +319,7 @@ public function customer_can_create_cart_with_one_item()
189319
public function customer_can_update_a_cart()
190320
{
191321
$customer = factory(Customer::class)->create([
192-
'email' => '[email protected]'
322+
'email' => '[email protected]',
193323
]);
194324
$cart = factory(Cart::class)->state('with_product')->create([
195325
'customer_id' => $customer->id,
@@ -207,7 +337,7 @@ public function customer_can_update_a_cart()
207337
]);
208338

209339
$this->assertDatabaseHas('carts', [
210-
'customer_email' => '[email protected]'
340+
'customer_email' => '[email protected]',
211341
]);
212342
}
213343
}

0 commit comments

Comments
 (0)