Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Sep 5, 2024
1 parent b97b9df commit 85802c3
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions tests/Feature/HandlesCustomerInformationTest.php
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;
}
}

0 comments on commit 85802c3

Please sign in to comment.