92 lines
3.4 KiB
PHP
92 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Filament\Clusters\DailyOps\Resources\TenantCheckoutHealths\TenantCheckoutHealthResource;
|
|
use App\Models\CheckoutSession;
|
|
use App\Models\Package;
|
|
use App\Models\PackagePurchase;
|
|
use App\Models\Tenant;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
class TenantCheckoutHealthResourceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_it_counts_checkout_metrics_for_default_provider(): void
|
|
{
|
|
$tenant = Tenant::factory()->create();
|
|
$package = Package::factory()->endcustomer()->paid()->create();
|
|
|
|
PackagePurchase::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'provider' => CheckoutSession::PROVIDER_PAYPAL,
|
|
'price' => 100.00,
|
|
'refunded' => false,
|
|
'purchased_at' => now()->subDay(),
|
|
]);
|
|
|
|
PackagePurchase::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'provider' => CheckoutSession::PROVIDER_LEMONSQUEEZY,
|
|
'price' => 50.00,
|
|
'refunded' => false,
|
|
'purchased_at' => now()->subDay(),
|
|
]);
|
|
|
|
CheckoutSession::query()->create([
|
|
'id' => (string) Str::uuid(),
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'package_snapshot' => [
|
|
'id' => $package->id,
|
|
'name' => $package->name,
|
|
'price' => $package->price,
|
|
'currency' => $package->currency ?? 'EUR',
|
|
],
|
|
'status' => CheckoutSession::STATUS_PROCESSING,
|
|
'provider' => CheckoutSession::PROVIDER_PAYPAL,
|
|
'status_history' => [],
|
|
'currency' => $package->currency ?? 'EUR',
|
|
'amount_subtotal' => 100.00,
|
|
'amount_total' => 100.00,
|
|
'amount_discount' => 0.00,
|
|
]);
|
|
|
|
CheckoutSession::query()->create([
|
|
'id' => (string) Str::uuid(),
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'package_snapshot' => [
|
|
'id' => $package->id,
|
|
'name' => $package->name,
|
|
'price' => $package->price,
|
|
'currency' => $package->currency ?? 'EUR',
|
|
],
|
|
'status' => CheckoutSession::STATUS_REQUIRES_CUSTOMER_ACTION,
|
|
'provider' => CheckoutSession::PROVIDER_LEMONSQUEEZY,
|
|
'status_history' => [],
|
|
'currency' => $package->currency ?? 'EUR',
|
|
'amount_subtotal' => 50.00,
|
|
'amount_total' => 50.00,
|
|
'amount_discount' => 0.00,
|
|
]);
|
|
|
|
$tenantWithCounts = TenantCheckoutHealthResource::getEloquentQuery()
|
|
->whereKey($tenant->id)
|
|
->firstOrFail();
|
|
|
|
$this->assertSame(1, (int) $tenantWithCounts->checkout_transaction_count);
|
|
$this->assertSame(1, (int) $tenantWithCounts->checkout_transaction_count_window);
|
|
$this->assertSame(0, (int) $tenantWithCounts->checkout_refund_count_window);
|
|
$this->assertSame(100.00, (float) $tenantWithCounts->checkout_transaction_total);
|
|
$this->assertSame(100.00, (float) $tenantWithCounts->checkout_transaction_total_window);
|
|
$this->assertSame(1, (int) $tenantWithCounts->checkout_processing_count);
|
|
$this->assertSame(0, (int) $tenantWithCounts->checkout_requires_action_count);
|
|
}
|
|
}
|