Last active
July 11, 2021 14:28
-
-
Save doulmi/28e7acf0810561a7f632296d7c0ab09e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature; | |
use App\Models\CartItem; | |
use App\Models\Sku; | |
use App\Models\Spu; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use Tests\TestCase; | |
use App\Models\User; | |
use Illuminate\Support\Facades\Auth; | |
class CartControllerTest extends TestCase | |
{ | |
use RefreshDatabase; | |
public function setUp() : void | |
{ | |
parent::setUp(); | |
$this->seed('DatabaseSeeder'); | |
$this->actingAs(User::last()); | |
} | |
public function test_doesnt_add_new_item_in_cart_when_no_enough_stock() | |
{ | |
$spu = Spu::saleable()->first(); | |
$sku = Sku::where('spu_id', $spu->id)->first(); | |
$quantity = $sku->stock + 1; | |
$this->assertEquals(0, CartItem::count()); | |
$response = $this->postJson('api/cart', [ | |
'sku_id' => $sku->id, | |
'spu_id' => $sku->spu_id, | |
'quantity' => $quantity, | |
]); | |
$response->assertStatus(422) | |
->assertJson([ | |
'errors' => ['cartItem' => ['库存不足']] | |
]); | |
$this->assertEquals(0, CartItem::count()); | |
} | |
public function test_doesnt_add_new_item_in_cart_when_spu_is_not_saleable() | |
{ | |
$sku = Sku::with('spu')->first(); | |
$sku->spu->update(['saleable' => false]); | |
$quantity = $sku->stock; | |
$this->assertEquals(0, CartItem::count()); | |
$response = $this->postJson('api/cart', [ | |
'sku_id' => $sku->id, | |
'spu_id' => $sku->spu_id, | |
'quantity' => $quantity, | |
]); | |
$response->assertStatus(422) | |
->assertJson([ | |
'errors' => ['cartItem' => ['该产品已下架']] | |
]); | |
$this->assertEquals(0, CartItem::count()); | |
} | |
public function test_adds_new_item_in_cart() | |
{ | |
$spu = Spu::saleable()->first(); | |
$sku = Sku::where('spu_id', $spu->id)->first(); | |
$quantity = $sku->stock; | |
$this->assertEquals(0, CartItem::count()); | |
$response = $this->postJson('api/cart', [ | |
'sku_id' => $sku->id, | |
'spu_id' => $spu->id, | |
'quantity' => $quantity, | |
]); | |
$response->assertStatus(200) | |
->assertJson(CartItem::build($spu, $sku, $quantity)); | |
$this->assertEquals(1, CartItem::count()); | |
} | |
public function test_updates_item_quantity_when_product_exists_in_cart() | |
{ | |
$spu = Spu::saleable()->first(); | |
$sku = Sku::where('spu_id', $spu->id)->first(); | |
$quantity = 1; | |
CartItem::create(CartItem::build($spu, $sku, $quantity, Auth::id())); | |
$this->assertEquals($quantity, CartItem::count()); | |
$this->assertEquals($quantity, CartItem::first()->quantity); | |
$response = $this->postJson('api/cart', [ | |
'sku_id' => $sku->id, | |
'spu_id' => $spu->id, | |
'quantity' => $sku->stock - $quantity, | |
]); | |
$response->assertStatus(200); | |
$this->assertEquals($sku->stock, CartItem::first()->quantity); | |
} | |
public function test_doesnt_add_item_when_quantity_is_greater_than_stock() | |
{ | |
$spu = Spu::saleable()->first(); | |
$sku = Sku::where('spu_id', $spu->id)->first(); | |
$quantity = $sku->stock; | |
$response = $this->postJson('api/cart', [ | |
'sku_id' => $sku->id, | |
'spu_id' => $spu->id, | |
'quantity' => $quantity, | |
]); | |
$response->assertStatus(200); | |
$this->assertEquals($sku->stock, CartItem::where('sku_id', $sku->id)->first()->quantity); | |
$response = $this->postJson('api/cart', [ | |
'sku_id' => $sku->id, | |
'spu_id' => $spu->id, | |
'quantity' => 1, | |
]); | |
$response->assertStatus(422) | |
->assertJson([ | |
'errors' => ['cartItem' => ['库存不足']] | |
]); | |
} | |
public function test_deletes_item_from_cart() | |
{ | |
$spu = Spu::with('skus')->saleable()->first(); | |
foreach ($spu->skus as $sku) { | |
CartItem::create(CartItem::build($spu, $sku, $sku->stock, Auth::id())); | |
} | |
$this->assertEquals(count($spu->skus), CartItem::count()); | |
$cartItem = CartItem::last(); | |
$response = $this->deleteJson('api/cart/' . $cartItem->id); | |
$response->assertStatus(200); | |
$this->assertEquals(count($spu->skus) - 1, CartItem::count()); | |
} | |
public function test_displays_all_cart_items() | |
{ | |
$spu = Spu::with('skus')->saleable()->first(); | |
foreach ($spu->skus as $sku) { | |
CartItem::create(CartItem::build($spu, $sku, $sku->stock, Auth::id())); | |
} | |
$response = $this->getJson('api/cart'); | |
$response->assertStatus(200); | |
$data= json_decode($response->getContent()); | |
$this->assertCount(CartItem::count(), $data->items); | |
$this->assertEquals(CartItem::get()->sum(fn ($item) => bcmul($item->price, $item->quantity, 2)), $data->total); | |
} | |
public function test_remove_unsaleable_items() | |
{ | |
[$spu1, $spu2] = Spu::with('skus')->saleable()->limit(2)->get(); | |
foreach ([$spu1, $spu2] as $spu) { | |
foreach ($spu->skus as $sku) { | |
CartItem::create(CartItem::build($spu, $sku, $sku->stock, Auth::id())); | |
} | |
} | |
$this->assertEquals(count($spu1->skus) + count($spu2->skus), CartItem::count()); | |
$spu1->update(['saleable' => false]); | |
$response = $this->postJson('api/cart/remove_unsaleable_items'); | |
$response->assertStatus(200)->assertSeeText(count($spu1->skus)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment