-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b97b9df
commit 85802c3
Showing
1 changed file
with
131 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,131 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use DuncanMcClean\SimpleCommerce\Customers\GuestCustomer; | ||
use DuncanMcClean\SimpleCommerce\Facades\Cart; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Statamic\Facades\Role; | ||
use Statamic\Facades\User; | ||
use Statamic\Facades\UserGroup; | ||
use Tests\TestCase; | ||
|
||
class HandlesCustomerInformationTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Cart::forgetCurrentCart(); | ||
} | ||
|
||
#[Test] | ||
public function logged_in_user_is_assigned_to_cart() | ||
{ | ||
$cart = $this->makeCart(); | ||
$user = User::make()->save(); | ||
|
||
$this->assertNull($cart->customer()); | ||
|
||
$this | ||
->actingAs($user) | ||
->patch('/!/simple-commerce/cart') | ||
->assertRedirect(); | ||
|
||
$this->assertEquals($user->id(), $cart->fresh()->customer()->id()); | ||
} | ||
|
||
#[Test] | ||
public function logged_in_user_can_be_updated() | ||
{ | ||
$this->makeCart(); | ||
$user = User::make()->set('name', 'John Doe')->email('[email protected]')->save(); | ||
|
||
$this | ||
->actingAs($user) | ||
->patch('/!/simple-commerce/cart', [ | ||
'customer' => [ | ||
'name' => 'Jane Doe', | ||
'email' => '[email protected]', | ||
], | ||
]) | ||
->assertRedirect(); | ||
|
||
$this->assertEquals('Jane Doe', $user->fresh()->name()); | ||
$this->assertEquals('[email protected]', $user->fresh()->email()); | ||
} | ||
|
||
#[Test] | ||
public function logged_in_user_cant_change_their_permissions() | ||
{ | ||
$this->makeCart(); | ||
$user = User::make()->save(); | ||
|
||
Role::make('manager')->save(); | ||
UserGroup::make('staff')->save(); | ||
|
||
$this | ||
->actingAs($user) | ||
->patch('/!/simple-commerce/cart', [ | ||
'customer' => [ | ||
'super' => true, | ||
'roles' => ['manager'], | ||
'groups' => ['staff'], | ||
], | ||
]) | ||
->assertRedirect(); | ||
|
||
$this->assertFalse($user->fresh()->isSuper()); | ||
$this->assertEmpty($user->fresh()->roles()->all()); | ||
$this->assertEmpty($user->fresh()->groups()->all()); | ||
} | ||
|
||
#[Test] | ||
public function guest_customer_is_assigned_to_cart() | ||
{ | ||
$cart = $this->makeCart(); | ||
|
||
$this->assertNull($cart->customer()); | ||
|
||
$this | ||
->patch('/!/simple-commerce/cart', [ | ||
'customer' => [ | ||
'name' => 'John Doe', | ||
'email' => '[email protected]', | ||
], | ||
]) | ||
->assertRedirect(); | ||
|
||
$this->assertInstanceOf(GuestCustomer::class, $cart->fresh()->customer()); | ||
$this->assertEquals('John Doe', $cart->fresh()->customer()->name()); | ||
$this->assertEquals('[email protected]', $cart->fresh()->customer()->email()); | ||
} | ||
|
||
#[Test] | ||
public function guest_customer_can_be_updated() | ||
{ | ||
$cart = $this->makeCart(); | ||
$cart->customer(['name' => 'John Doe', 'email' => '[email protected]', 'foo' => 'bar'])->save(); | ||
|
||
$this | ||
->patch('/!/simple-commerce/cart', [ | ||
'customer' => [ | ||
'name' => 'Jane Doe', | ||
'email' => '[email protected]', | ||
], | ||
]) | ||
->assertRedirect(); | ||
|
||
$this->assertEquals('Jane Doe', $cart->fresh()->customer()->name()); | ||
$this->assertEquals('[email protected]', $cart->fresh()->customer()->email()); | ||
} | ||
|
||
protected function makeCart() | ||
{ | ||
$cart = tap(Cart::make())->save(); | ||
|
||
Cart::setCurrent($cart); | ||
|
||
return $cart; | ||
} | ||
} |