mirror of
https://github.com/anna-sara/lan_kiosk
synced 2026-09-03 10:25:02 +02:00
128 lines
4 KiB
PHP
128 lines
4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Customer;
|
|
use App\Models\CustomerGroup;
|
|
use App\Models\Deposit;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Registering a deposit over the counter.
|
|
*/
|
|
class DepositTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private function customer(array $attributes = []): Customer
|
|
{
|
|
$customer = new Customer();
|
|
|
|
$customer->forceFill(array_merge([
|
|
'lan_id' => 1,
|
|
'name' => 'Erik Olsson',
|
|
'guardian_name' => 'Karin Olsson',
|
|
'deposit' => 0,
|
|
'amount_left' => 0,
|
|
'give_leftover' => 0,
|
|
'is_in_group' => 0,
|
|
], $attributes))->save();
|
|
|
|
return $customer;
|
|
}
|
|
|
|
private function asUser(): self
|
|
{
|
|
$this->actingAs(User::factory()->create(), 'sanctum');
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function test_a_deposit_is_credited_and_recorded(): void
|
|
{
|
|
$customer = $this->customer(['lan_id' => 42, 'deposit' => 100, 'amount_left' => 100]);
|
|
|
|
$this->asUser()->postJson('/api/register_deposit', [
|
|
'customer_id' => 42,
|
|
'deposit' => 250,
|
|
'give_leftover' => 1,
|
|
])->assertOk();
|
|
|
|
$customer->refresh();
|
|
$this->assertSame(350, $customer->deposit);
|
|
$this->assertSame(350, $customer->amount_left);
|
|
$this->assertSame(1, (int) $customer->give_leftover);
|
|
$this->assertSame(250, Deposit::first()->amount);
|
|
}
|
|
|
|
public function test_a_group_members_deposit_lands_on_the_group(): void
|
|
{
|
|
$group = CustomerGroup::create(['name' => 'Klanen']);
|
|
|
|
$groupCustomer = $this->customer([
|
|
'lan_id' => 10, 'name' => 'Klanen', 'deposit' => 500, 'amount_left' => 500,
|
|
'is_in_group' => 0, 'customer_group_id' => $group->id,
|
|
]);
|
|
$member = $this->customer([
|
|
'lan_id' => 11, 'name' => 'Anna', 'is_in_group' => 1, 'customer_group_id' => $group->id,
|
|
]);
|
|
|
|
$this->asUser()->postJson('/api/register_deposit', [
|
|
'customer_id' => 11,
|
|
'deposit' => 200,
|
|
'give_leftover' => 0,
|
|
])->assertOk();
|
|
|
|
$this->assertSame(700, $groupCustomer->refresh()->deposit);
|
|
$this->assertSame(700, $groupCustomer->amount_left);
|
|
$this->assertSame(0, $member->refresh()->deposit);
|
|
|
|
// The receipt is kept on the person who paid, the balance on the group
|
|
$this->assertSame($member->id, Deposit::first()->customer_id);
|
|
}
|
|
|
|
public function test_an_unknown_customer_gives_404_and_writes_nothing(): void
|
|
{
|
|
$this->asUser()->postJson('/api/register_deposit', [
|
|
'customer_id' => 999999,
|
|
'deposit' => 250,
|
|
'give_leftover' => 0,
|
|
])->assertStatus(404);
|
|
|
|
$this->assertSame(0, Deposit::count());
|
|
}
|
|
|
|
public function test_a_member_whose_group_row_is_missing_gives_404_and_writes_nothing(): void
|
|
{
|
|
// A broken state: the member points at a group that has no account row
|
|
$member = $this->customer([
|
|
'lan_id' => 11, 'name' => 'Anna', 'is_in_group' => 1, 'customer_group_id' => 999,
|
|
]);
|
|
|
|
$this->asUser()->postJson('/api/register_deposit', [
|
|
'customer_id' => 11,
|
|
'deposit' => 200,
|
|
'give_leftover' => 0,
|
|
])->assertStatus(404);
|
|
|
|
// No receipt for money that was never credited anywhere
|
|
$this->assertSame(0, Deposit::count());
|
|
$this->assertSame(0, $member->refresh()->deposit);
|
|
}
|
|
|
|
public function test_the_endpoint_is_closed_to_anyone_not_logged_in(): void
|
|
{
|
|
$customer = $this->customer(['lan_id' => 42]);
|
|
|
|
$this->postJson('/api/register_deposit', [
|
|
'customer_id' => 42,
|
|
'deposit' => 250,
|
|
'give_leftover' => 0,
|
|
])->assertStatus(401);
|
|
|
|
$this->assertSame(0, Deposit::count());
|
|
$this->assertSame(0, $customer->refresh()->deposit);
|
|
}
|
|
}
|