Files
ladill-care/tests/Feature/CareProTest.php
T
isaaccladandCursor 22646dfb62
Deploy Ladill Care / deploy (push) Successful in 1m46s
Add Care Enterprise per-branch billing and Paystack prepaid plans.
Mirrors Queue with GHS 499/branch Enterprise and 6/12/24 month Paystack checkout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-30 01:55:38 +00:00

217 lines
7.0 KiB
PHP

<?php
namespace Tests\Feature;
use App\Http\Middleware\EnsurePlatformSession;
use App\Models\Branch;
use App\Models\Member;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class CareProTest extends TestCase
{
use RefreshDatabase;
protected User $owner;
protected Organization $organization;
protected function setUp(): void
{
parent::setUp();
$this->withoutMiddleware(EnsurePlatformSession::class);
config(['billing.api_url' => 'https://billing.test']);
$this->owner = User::create([
'public_id' => 'care-owner-001',
'name' => 'Owner',
'email' => 'owner@example.com',
]);
$this->organization = Organization::create([
'owner_ref' => $this->owner->public_id,
'name' => 'Care Org',
'slug' => 'care-org',
'settings' => ['onboarded' => true, 'facility_type' => 'clinic'],
]);
Member::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'user_ref' => $this->owner->public_id,
'role' => 'hospital_admin',
]);
Branch::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'name' => 'HQ',
'is_active' => true,
]);
}
public function test_pro_page_renders_three_tier_pricing(): void
{
$this->actingAs($this->owner)
->get(route('care.pro.index'))
->assertOk()
->assertSee('Choose your Care plan')
->assertSee('GHS 199')
->assertSee('GHS 499')
->assertSee('Upgrade to Pro')
->assertSee('Enterprise');
}
public function test_sidebar_shows_upgrade_to_pro_on_dashboard(): void
{
$this->actingAs($this->owner)
->get(route('care.dashboard'))
->assertOk()
->assertSee('Upgrade to Pro');
}
public function test_subscribe_upgrades_organization_to_pro(): void
{
Http::fake([
'billing.test/can-afford*' => Http::response(['affordable' => true]),
'billing.test/debit' => Http::response(['ok' => true]),
]);
$this->actingAs($this->owner)
->post(route('care.pro.subscribe'))
->assertRedirect(route('care.pro.index'))
->assertSessionHas('success');
$settings = $this->organization->fresh()->settings;
$this->assertSame('pro', $settings['plan']);
$this->assertSame('wallet_monthly', $settings['billing_method']);
}
public function test_subscribe_enterprise_requires_multiple_branches(): void
{
Http::fake([
'billing.test/can-afford*' => Http::response(['affordable' => true]),
'billing.test/debit' => Http::response(['ok' => true]),
]);
$this->actingAs($this->owner)
->post(route('care.pro.subscribe-enterprise'))
->assertRedirect(route('care.pro.index'))
->assertSessionHas('error');
Branch::create([
'owner_ref' => $this->organization->owner_ref,
'organization_id' => $this->organization->id,
'name' => 'Second',
'is_active' => true,
]);
$this->actingAs($this->owner)
->post(route('care.pro.subscribe-enterprise'))
->assertRedirect(route('care.pro.index'))
->assertSessionHas('success');
$settings = $this->organization->fresh()->settings;
$this->assertSame('enterprise', $settings['plan']);
$this->assertSame(2, $settings['enterprise_billed_branches']);
}
public function test_subscribe_prepaid_redirects_to_paystack(): void
{
Http::fake([
'billing.test/plan-checkout' => Http::response([
'checkout_url' => 'https://checkout.paystack.com/test',
'reference' => 'ref-123',
]),
]);
$this->actingAs($this->owner)
->post(route('care.pro.subscribe-prepaid'), [
'plan' => 'pro',
'months' => 12,
])
->assertRedirect('https://checkout.paystack.com/test');
}
public function test_paystack_callback_activates_prepaid_pro(): void
{
Http::fake([
'billing.test/plan-checkout/verify' => Http::response([
'paid' => true,
'plan' => 'pro',
'months' => 12,
'metadata' => ['organization_id' => $this->organization->id],
]),
]);
$this->actingAs($this->owner)
->get(route('care.pro.paystack.callback', ['reference' => 'ref-123']))
->assertRedirect(route('care.pro.index'))
->assertSessionHas('success');
$settings = $this->organization->fresh()->settings;
$this->assertSame('pro', $settings['plan']);
$this->assertFalse($settings['auto_renew']);
$this->assertSame('paystack_prepaid', $settings['billing_method']);
}
public function test_pro_renew_extends_subscription_when_wallet_charges(): void
{
$this->organization->update([
'settings' => [
'onboarded' => true,
'facility_type' => 'clinic',
'plan' => 'pro',
'auto_renew' => true,
'plan_expires_at' => now()->subDay()->toIso8601String(),
],
]);
Http::fake([
'billing.test/debit' => Http::response(['ok' => true]),
]);
$this->artisan('care:pro-renew')->assertSuccessful();
$settings = $this->organization->fresh()->settings;
$this->assertSame('pro', $settings['plan']);
$this->assertTrue(now()->parse($settings['plan_expires_at'])->isFuture());
}
public function test_enterprise_renew_charges_per_active_branch(): void
{
Branch::create([
'owner_ref' => $this->organization->owner_ref,
'organization_id' => $this->organization->id,
'name' => 'Second',
'is_active' => true,
]);
$this->organization->update([
'settings' => array_merge($this->organization->settings ?? [], [
'plan' => 'enterprise',
'auto_renew' => true,
'enterprise_billed_branches' => 2,
'plan_expires_at' => now()->subDay()->toIso8601String(),
]),
]);
Http::fake([
'billing.test/debit' => function ($request) {
$this->assertSame(99800, (int) $request['amount_minor']);
return Http::response(['ok' => true]);
},
]);
$this->artisan('care:pro-renew')->assertSuccessful();
$settings = $this->organization->fresh()->settings;
$this->assertSame('enterprise', $settings['plan']);
$this->assertSame(2, $settings['enterprise_billed_branches']);
}
}