Deploy Ladill Events / deploy (push) Successful in 51s
Co-authored-by: Cursor <cursoragent@cursor.com>
129 lines
4.7 KiB
PHP
129 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Http\Middleware\EnsurePlatformSession;
|
|
use App\Models\Events\ProSubscription;
|
|
use App\Models\User;
|
|
use App\Services\Events\SubscriptionService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Tests\TestCase;
|
|
|
|
class EventsProTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->withoutMiddleware(EnsurePlatformSession::class);
|
|
config([
|
|
'billing.api_url' => 'https://ladill.com/api/billing',
|
|
'billing.api_key' => 'events-billing-key',
|
|
'events.pro.enabled' => true,
|
|
'events.pro.price_minor' => 4900,
|
|
'events.plans.pro.price_minor' => 4900,
|
|
'events.plans.enterprise.price_minor' => 14900,
|
|
'events.pro.period_days' => 30,
|
|
'events.pro.grace_days' => 3,
|
|
'events.free.max_live_events' => 2,
|
|
'events.free.max_tickets_per_month' => 100,
|
|
]);
|
|
}
|
|
|
|
private function user(): User
|
|
{
|
|
return User::factory()->create(['public_id' => 'usr_'.uniqid()]);
|
|
}
|
|
|
|
public function test_subscribe_charges_wallet_and_activates(): void
|
|
{
|
|
Http::fake(['*/debit' => Http::response(['id' => 1], 201)]);
|
|
$user = $this->user();
|
|
$svc = app(SubscriptionService::class);
|
|
|
|
[$ok] = $svc->subscribe($user);
|
|
|
|
$this->assertTrue($ok);
|
|
$this->assertTrue($svc->isPro($user));
|
|
Http::assertSent(fn ($r) => str_ends_with(parse_url($r->url(), PHP_URL_PATH) ?: '', '/debit')
|
|
&& $r['amount_minor'] === 4900
|
|
&& $r['service'] === 'events');
|
|
}
|
|
|
|
public function test_subscribe_enterprise_charges_wallet_and_activates(): void
|
|
{
|
|
Http::fake(['*/debit' => Http::response(['id' => 1], 201)]);
|
|
$user = $this->user();
|
|
$svc = app(SubscriptionService::class);
|
|
|
|
[$ok] = $svc->subscribeEnterprise($user);
|
|
|
|
$this->assertTrue($ok);
|
|
$this->assertTrue($svc->isEnterprise($user));
|
|
$sub = ProSubscription::where('user_id', $user->id)->first();
|
|
$this->assertSame('enterprise', $sub->plan);
|
|
$this->assertSame(14900, $sub->price_minor);
|
|
}
|
|
|
|
public function test_free_user_is_gated_to_event_limit(): void
|
|
{
|
|
$user = $this->user();
|
|
$svc = app(SubscriptionService::class);
|
|
|
|
$this->assertTrue($svc->canCreateEvent($user));
|
|
$this->assertSame(0, $svc->liveEventCount($user));
|
|
}
|
|
|
|
public function test_free_user_cannot_use_payment_gateway(): void
|
|
{
|
|
$user = $this->user();
|
|
$svc = app(SubscriptionService::class);
|
|
|
|
$this->assertFalse($svc->canUsePaymentGateway($user));
|
|
$this->assertFalse(app(\App\Services\Payments\MerchantGatewayService::class)->isConfigured($user));
|
|
}
|
|
|
|
public function test_pro_user_can_use_payment_gateway_when_configured(): void
|
|
{
|
|
Http::fake(['*/debit' => Http::response(['id' => 1], 201)]);
|
|
$user = $this->user();
|
|
$svc = app(SubscriptionService::class);
|
|
$svc->subscribe($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,
|
|
]);
|
|
|
|
$this->assertTrue($svc->canUsePaymentGateway($user));
|
|
$this->assertTrue(app(\App\Services\Payments\MerchantGatewayService::class)->isConfigured($user));
|
|
}
|
|
|
|
public function test_invalid_credentials_and_downgrade_disable_owner_gateway_without_deleting_keys(): void
|
|
{
|
|
$user = $this->user();
|
|
ProSubscription::create([
|
|
'user_id' => $user->id, 'plan' => 'enterprise', 'status' => 'active',
|
|
'price_minor' => 14900, 'current_period_end' => now()->addMonth(),
|
|
]);
|
|
$setting = \App\Models\PaymentGatewaySetting::create([
|
|
'owner_ref' => $user->public_id, 'provider' => 'paystack',
|
|
'public_key' => 'bad', 'secret_key' => 'bad', 'is_active' => true,
|
|
]);
|
|
$gateway = app(\App\Services\Payments\MerchantGatewayService::class);
|
|
$this->assertFalse($gateway->shouldUseOwnerGateway($user));
|
|
|
|
$setting->update(['public_key' => 'pk_test_saved', 'secret_key' => 'sk_test_saved']);
|
|
$this->assertTrue($gateway->shouldUseOwnerGateway($user));
|
|
|
|
ProSubscription::where('user_id', $user->id)->update(['status' => 'past_due']);
|
|
$this->assertFalse($gateway->shouldUseOwnerGateway($user->fresh()));
|
|
$this->assertDatabaseHas('payment_gateway_settings', ['id' => $setting->id, 'is_active' => true]);
|
|
}
|
|
}
|