mirror of
https://github.com/anna-sara/lan_kiosk
synced 2026-09-03 11:35:01 +02:00
165 lines
5.3 KiB
PHP
165 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Customer;
|
|
use App\Models\Purchase;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class PurchaseTest 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' => 500,
|
|
'amount_left' => 500,
|
|
'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_purchase_is_recorded_and_taken_off_the_balance(): void
|
|
{
|
|
$customer = $this->customer(['deposit' => 500, 'amount_left' => 500]);
|
|
|
|
$this->asUser()->postJson('/api/register_purchase', [
|
|
'customer_id' => $customer->id,
|
|
'amount' => 120,
|
|
])->assertRedirect('customer/' . $customer->id);
|
|
|
|
$customer->refresh();
|
|
$this->assertSame(380, $customer->amount_left);
|
|
// What was paid in is history and does not change when something is bought
|
|
$this->assertSame(500, $customer->deposit);
|
|
|
|
$purchase = Purchase::first();
|
|
$this->assertSame($customer->id, $purchase->customer_id);
|
|
$this->assertSame(120, $purchase->amount);
|
|
}
|
|
|
|
public function test_several_purchases_add_up(): void
|
|
{
|
|
$customer = $this->customer(['deposit' => 500, 'amount_left' => 500]);
|
|
|
|
foreach ([100, 50, 25] as $amount) {
|
|
$this->asUser()->postJson('/api/register_purchase', [
|
|
'customer_id' => $customer->id,
|
|
'amount' => $amount,
|
|
]);
|
|
}
|
|
|
|
$this->assertSame(325, $customer->refresh()->amount_left);
|
|
$this->assertSame(3, Purchase::count());
|
|
}
|
|
|
|
public function test_the_purchase_shows_up_on_the_customer(): void
|
|
{
|
|
$customer = $this->customer();
|
|
|
|
$this->asUser()->postJson('/api/register_purchase', [
|
|
'customer_id' => $customer->id,
|
|
'amount' => 120,
|
|
]);
|
|
|
|
$this->assertSame(1, $customer->purchases()->count());
|
|
$this->assertSame(120, $customer->purchases()->first()->amount);
|
|
}
|
|
|
|
public function test_a_purchase_larger_than_the_balance_makes_it_negative(): void
|
|
{
|
|
// Nothing stops this today — the kiosk staff is trusted to check the balance
|
|
$customer = $this->customer(['deposit' => 100, 'amount_left' => 100]);
|
|
|
|
$this->asUser()->postJson('/api/register_purchase', [
|
|
'customer_id' => $customer->id,
|
|
'amount' => 250,
|
|
]);
|
|
|
|
$this->assertSame(-150, $customer->refresh()->amount_left);
|
|
}
|
|
|
|
public function test_a_group_purchase_is_taken_from_the_group_balance(): void
|
|
{
|
|
// Purchases are registered from the account page, so for a group it is the
|
|
// group's own customer row that is charged
|
|
$group = $this->customer([
|
|
'lan_id' => 10, 'name' => 'Klanen', 'deposit' => 800, 'amount_left' => 800,
|
|
'is_in_group' => 0, 'customer_group_id' => 7,
|
|
]);
|
|
$this->customer([
|
|
'lan_id' => 11, 'name' => 'Anna', 'deposit' => 0, 'amount_left' => 0,
|
|
'is_in_group' => 1, 'customer_group_id' => 7,
|
|
]);
|
|
|
|
$this->asUser()->postJson('/api/register_purchase', [
|
|
'customer_id' => $group->id,
|
|
'amount' => 200,
|
|
]);
|
|
|
|
$this->assertSame(600, $group->refresh()->amount_left);
|
|
}
|
|
|
|
public function test_it_requires_a_customer(): void
|
|
{
|
|
$this->asUser()->postJson('/api/register_purchase', ['amount' => 120])
|
|
->assertStatus(422);
|
|
|
|
$this->assertSame(0, Purchase::count());
|
|
}
|
|
|
|
public function test_an_unknown_customer_gives_404_and_writes_nothing(): void
|
|
{
|
|
$this->asUser()->postJson('/api/register_purchase', [
|
|
'customer_id' => 999999,
|
|
'amount' => 120,
|
|
])->assertStatus(404);
|
|
|
|
// No line item for a purchase that no balance ever paid for
|
|
$this->assertSame(0, Purchase::count());
|
|
}
|
|
|
|
public function test_the_purchase_and_the_deduction_are_written_together(): void
|
|
{
|
|
$customer = $this->customer(['deposit' => 500, 'amount_left' => 500]);
|
|
|
|
$this->asUser()->postJson('/api/register_purchase', [
|
|
'customer_id' => $customer->id,
|
|
'amount' => 120,
|
|
]);
|
|
|
|
// The balance always matches what the line items add up to
|
|
$this->assertSame(1, Purchase::count());
|
|
$this->assertSame(500 - (int) Purchase::sum('amount'), $customer->refresh()->amount_left);
|
|
}
|
|
|
|
public function test_the_endpoint_is_closed_to_anyone_not_logged_in(): void
|
|
{
|
|
$customer = $this->customer();
|
|
|
|
$this->postJson('/api/register_purchase', [
|
|
'customer_id' => $customer->id,
|
|
'amount' => 120,
|
|
])->assertStatus(401);
|
|
|
|
$this->assertSame(500, $customer->refresh()->amount_left);
|
|
$this->assertSame(0, Purchase::count());
|
|
}
|
|
}
|