Files
ladill-pos/tests/Feature/PosRegisterTest.php
T
isaaccladandCursor 3c0172f098
Deploy Ladill POS / deploy (push) Successful in 40s
Embed POS checkouts in a modal/sheet using the seller email.
Card/MoMo always initializes with the merchant email, and Paystack/Flutterwave/Hubtel open in-page (desktop modal, mobile bottomsheet) instead of a full redirect.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-15 08:29:07 +00:00

165 lines
6.0 KiB
PHP

<?php
namespace Tests\Feature;
use App\Http\Middleware\EnsurePlatformSession;
use App\Models\PosLocation;
use App\Models\PosProduct;
use App\Models\PosSale;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class PosRegisterTest extends TestCase
{
use RefreshDatabase;
private function user(): User
{
return User::create([
'public_id' => 'u-'.uniqid(),
'name' => 'Cashier',
'email' => uniqid().'@example.com',
]);
}
protected function setUp(): void
{
parent::setUp();
$this->withoutMiddleware(EnsurePlatformSession::class);
$this->withoutVite();
config([
'crm.url' => 'https://crm.test/api',
'crm.key' => 'test-crm-key',
]);
Http::fake([
'crm.test/api/customers*' => Http::response(['data' => []], 200),
]);
}
public function test_dashboard_renders_for_signed_in_user(): void
{
$this->actingAs($this->user())
->get(route('pos.dashboard'))
->assertOk()
->assertSee('Overview')
->assertSee('Unlock POS Pro or Business')
->assertSee('View plans')
->assertSee('favicon.ico', false);
}
public function test_boot_splash_injected_on_authenticated_pages(): void
{
$this->actingAs($this->user())->get(route('pos.dashboard'))
->assertOk()->assertSee('id="ladill-boot"', false);
}
public function test_register_renders_local_products_in_restaurant_mode(): void
{
$user = $this->user();
// ensureDefault() matches owner+name 'Main register'.
PosLocation::create(['owner_ref' => $user->public_id, 'name' => 'Main register', 'currency' => 'GHS', 'service_style' => 'restaurant']);
PosProduct::create(['owner_ref' => $user->public_id, 'name' => 'Coffee', 'price_minor' => 1500, 'currency' => 'GHS', 'is_active' => true]);
$this->actingAs($user)->get(route('pos.register'))->assertOk()->assertSee('Coffee')->assertSee('Barcode scanner');
}
public function test_retail_register_reads_crm_products_and_snapshots_lines(): void
{
Http::fake([
'crm.test/api/products*' => Http::response(['data' => [
['id' => 7, 'name' => 'Mug', 'unit_price_minor' => 2500, 'currency' => 'GHS', 'active' => true],
]], 200),
'crm.test/api/customers*' => Http::response(['data' => []], 200),
'crm.test/api/*' => Http::response(['id' => 1], 201),
]);
$user = $this->user(); // retail by default (no restaurant location)
$this->actingAs($user)->get(route('pos.register'))->assertOk()->assertSee('Mug');
// A line built from a CRM product is stored as a snapshot — no local FK.
$this->actingAs($user)->post(route('pos.register.charge'), [
'payment_method' => 'cash',
'lines' => [['product_id' => 7, 'name' => 'Mug', 'unit_price_minor' => 2500, 'quantity' => 1]],
])->assertRedirect();
$line = PosSale::where('owner_ref', $user->public_id)->firstOrFail()->lines()->firstOrFail();
$this->assertNull($line->product_id);
$this->assertSame('Mug', $line->name);
$this->assertSame(2500, $line->unit_price_minor);
}
public function test_retail_products_page_lists_crm_products(): void
{
Http::fake([
'crm.test/api/products*' => Http::response(['data' => [
['id' => 7, 'name' => 'Mug', 'sku' => 'M1', 'unit_price_minor' => 2500, 'currency' => 'GHS', 'active' => true],
]], 200),
]);
$this->actingAs($this->user())->get(route('pos.products.index'))->assertOk()->assertSee('Mug');
}
public function test_cash_sale_is_recorded(): void
{
$user = $this->user();
$this->actingAs($user)->post(route('pos.register.charge'), [
'payment_method' => 'cash',
'lines' => [
['name' => 'Tea', 'unit_price_minor' => 1000, 'quantity' => 2],
],
])->assertRedirect();
$sale = PosSale::where('owner_ref', $user->public_id)->first();
$this->assertNotNull($sale);
$this->assertSame(PosSale::STATUS_PAID, $sale->status);
$this->assertSame(PosSale::METHOD_CASH, $sale->payment_method);
$this->assertSame(2000, $sale->total_minor);
}
public function test_pay_sale_opens_embedded_checkout(): void
{
$user = $this->user();
\App\Models\PaymentGatewaySetting::create([
'owner_ref' => $user->public_id,
'provider' => \App\Models\PaymentGatewaySetting::PROVIDER_PAYSTACK,
'public_key' => 'pk_test',
'secret_key' => 'sk_test',
'is_active' => true,
]);
Http::fake([
'crm.test/api/customers*' => Http::response(['data' => []], 200),
'https://api.paystack.co/transaction/initialize' => Http::response([
'status' => true,
'data' => [
'authorization_url' => 'https://checkout.paystack.com/test',
'reference' => 'POSS-TESTREF',
],
], 200),
]);
$response = $this->actingAs($user)->post(route('pos.register.charge'), [
'payment_method' => 'pay',
'lines' => [
['name' => 'Snack', 'unit_price_minor' => 500, 'quantity' => 1],
],
]);
$sale = PosSale::where('owner_ref', $user->public_id)->first();
$this->assertNotNull($sale);
$response->assertRedirect(route('pos.sales.show', $sale));
$response->assertSessionHas('checkout_url', 'https://checkout.paystack.com/test');
$this->assertSame(PosSale::STATUS_PENDING, $sale->status);
$this->assertNotEmpty($sale->payment_reference);
Http::assertSent(fn ($r) => str_contains($r->url(), 'paystack.co')
&& ($r['email'] ?? null) === $user->email);
}
}