This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #7
- Loading branch information
Showing
5 changed files
with
362 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class CartController extends Controller { | ||
|
||
/** | ||
* Add a product in given amount to cart. | ||
* | ||
* @param Request $request | ||
* @return Response | ||
*/ | ||
public function add(Request $request): Response { | ||
$args = $request->validate([ | ||
"product" => ["required", "exists:products,id"], | ||
"amount" => ["required", "numeric", "gt:0"] | ||
]); | ||
|
||
Auth::user()->addCartItem($args["product"], $args["amount"]); | ||
|
||
return response(status: 200); | ||
} | ||
|
||
/** | ||
* Set product amount in cart. | ||
* | ||
* @param Request $request | ||
* @return Response | ||
*/ | ||
public function set(Request $request): Response { | ||
$args = $request->validate([ | ||
"product" => ["required", "exists:products,id"], | ||
"amount" => ["required", "numeric", "gt:0"] | ||
]); | ||
|
||
Auth::user()->setCartItem($args["product"], $args["amount"]); | ||
|
||
return response(status: 200); | ||
} | ||
|
||
/** | ||
* Remove product from cart. | ||
* | ||
* @param Request $request | ||
* @return Response | ||
*/ | ||
public function remove(Request $request): Response { | ||
$args = $request->validate([ | ||
"product" => ["required", "exists:products,id", ] | ||
]); | ||
|
||
Auth::user()->removeCartItem($args["product"]); | ||
|
||
return response(status: 200); | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Product; | ||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Illuminate\Support\Facades\Auth; | ||
use Tests\TestCase; | ||
|
||
class CartTest extends TestCase { | ||
use RefreshDatabase; | ||
|
||
/** | ||
* Return errors when sent data are either missing or invalid. | ||
*/ | ||
public function test_add_invalid_data(): void { | ||
$user = User::factory()->create(); | ||
|
||
Auth::login($user); | ||
|
||
$missingData = $this->postJson("/cart/add"); | ||
$missingData->assertInvalid(["product", "amount"]); | ||
|
||
$invalidData = $this->postJson( | ||
"/cart/add", | ||
[ | ||
"product" => 1, | ||
"amount" => -5 | ||
] | ||
); | ||
|
||
$invalidData->assertInvalid(["product", "amount"]); | ||
} | ||
|
||
/** | ||
* Successfully add item when valid arguments are given. | ||
*/ | ||
public function test_add_successful() { | ||
$user = User::factory()->create(); | ||
$product = Product::factory()->create(); | ||
|
||
Auth::login($user); | ||
|
||
$response = $this->postJson( | ||
"/cart/add", | ||
[ | ||
"product" => $product->id, | ||
"amount" => 5 | ||
] | ||
); | ||
|
||
$response->assertSuccessful(); | ||
$this->assertEquals(5, $user->cart()->find($product)->pivot->amount); | ||
|
||
$response = $this->postJson( | ||
"/cart/add", | ||
[ | ||
"product" => $product->id, | ||
"amount" => 5 | ||
] | ||
); | ||
|
||
$response->assertSuccessful(); | ||
$this->assertEquals(10, $user->cart()->find($product)->pivot->amount); | ||
} | ||
|
||
/** | ||
* Return errors when sent data are either missing or invalid. | ||
*/ | ||
public function test_set_invalid_data(): void { | ||
$user = User::factory()->create(); | ||
|
||
Auth::login($user); | ||
|
||
$missingData = $this->postJson("/cart/set"); | ||
$missingData->assertInvalid(["product", "amount"]); | ||
|
||
$invalidData = $this->postJson( | ||
"/cart/set", | ||
[ | ||
"product" => 1, | ||
"amount" => -5 | ||
] | ||
); | ||
|
||
$invalidData->assertInvalid(["product", "amount"]); | ||
} | ||
|
||
/** | ||
* Successfully set item when valid arguments are given. | ||
*/ | ||
public function test_set_successful() { | ||
$user = User::factory()->create(); | ||
$product = Product::factory()->create(); | ||
|
||
Auth::login($user); | ||
|
||
$response = $this->postJson( | ||
"/cart/set", | ||
[ | ||
"product" => $product->id, | ||
"amount" => 5 | ||
] | ||
); | ||
|
||
$response->assertSuccessful(); | ||
$this->assertEquals(5, $user->cart()->find($product)->pivot->amount); | ||
} | ||
|
||
/** | ||
* Return errors when sent data are either missing or invalid. | ||
*/ | ||
public function test_remove_invalid_data(): void { | ||
$user = User::factory()->create(); | ||
|
||
Auth::login($user); | ||
|
||
$missingData = $this->postJson("/cart/remove"); | ||
$missingData->assertInvalid(["product"]); | ||
|
||
$invalidData = $this->postJson( | ||
"/cart/set", | ||
[ | ||
"product" => 1 | ||
] | ||
); | ||
|
||
$invalidData->assertInvalid(["product"]); | ||
} | ||
|
||
/** | ||
* Successfully remove item from cart when valid arguments are given. | ||
*/ | ||
public function test_remove_successful() { | ||
$user = User::factory()->create(); | ||
$product = Product::factory()->create(); | ||
|
||
Auth::login($user); | ||
|
||
$response = $this->postJson( | ||
"/cart/remove", | ||
[ | ||
"product" => $product->id, | ||
] | ||
); | ||
|
||
$response->assertSuccessful(); | ||
$this->assertEquals(null, $user->cart()->find($product)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use App\Models\Product; | ||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
use function PHPUnit\Framework\assertEquals; | ||
use function PHPUnit\Framework\assertTrue; | ||
|
||
class CartTest extends TestCase { | ||
use RefreshDatabase; | ||
|
||
/** | ||
* Get user's cart items. | ||
*/ | ||
public function test_get_items() { | ||
$products = Product::factory()->count(5)->create(); | ||
$user = User::factory()->create(); | ||
|
||
foreach ($products as $product) { | ||
$user->cart()->attach($product, ["amount" => 1]); | ||
} | ||
|
||
foreach ($user->getCartItems() as $item) { | ||
$exists = false; | ||
|
||
foreach ($products as $product) { | ||
if($product->id == $item->id) { | ||
$exists = true; | ||
break; | ||
} | ||
} | ||
|
||
assertTrue($exists); | ||
} | ||
} | ||
|
||
/** | ||
* Add product to cart with given amount and increase it if it exists. | ||
*/ | ||
public function test_add_item(): void { | ||
$user = User::factory()->create(); | ||
$product = Product::factory()->create(); | ||
|
||
$user->addCartItem($product, 5); | ||
$this->assertEquals(5, $user->getAmount($product)); | ||
|
||
$user->addCartItem($product, 5); | ||
$this->assertEquals(10, $user->getAmount($product)); | ||
} | ||
|
||
/** | ||
* Set product with given amount in cart. | ||
*/ | ||
public function test_set_item(): void { | ||
$user = User::factory()->create(); | ||
$product = Product::factory()->create(); | ||
|
||
$user->setCartItem($product, 5); | ||
$this->assertEquals(5, $user->getAmount($product)); | ||
} | ||
|
||
/** | ||
* Remove product from cart. | ||
*/ | ||
public function test_remove_item(): void { | ||
$user = User::factory()->create(); | ||
$product = Product::factory()->create(); | ||
|
||
$user->cart()->attach($product, ["amount" => 1]); | ||
|
||
$user->removeCartItem($product); | ||
assertEquals(0, $user->cart()->count()); | ||
} | ||
} |