135 lines
4.5 KiB
PHP
135 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Marketing;
|
|
|
|
use App\Models\Event;
|
|
use App\Models\EventPackage;
|
|
use App\Models\Package;
|
|
use App\Models\PackagePurchase;
|
|
use App\Models\Tenant;
|
|
use App\Models\TenantPackage;
|
|
use App\Models\User;
|
|
use App\Notifications\Customer\WithdrawalConfirmed;
|
|
use App\Services\LemonSqueezy\LemonSqueezyOrderService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Inertia\Testing\AssertableInertia as Assert;
|
|
use Tests\TestCase;
|
|
|
|
class WithdrawalConfirmationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_withdrawal_page_lists_eligible_purchases(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
|
$user->forceFill(['tenant_id' => $tenant->id])->save();
|
|
|
|
$package = Package::factory()->create(['type' => 'endcustomer']);
|
|
$purchase = PackagePurchase::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'provider' => 'lemonsqueezy',
|
|
'provider_id' => 'ord_123',
|
|
'refunded' => false,
|
|
'type' => 'endcustomer_event',
|
|
'purchased_at' => now()->subDays(2),
|
|
]);
|
|
|
|
$response = $this->actingAs($user)->get('/de/widerruf');
|
|
|
|
$response->assertOk();
|
|
$response->assertInertia(fn (Assert $page) => $page
|
|
->component('marketing/WithdrawalConfirm')
|
|
->where('windowDays', 14)
|
|
->has('eligiblePurchases', 1)
|
|
->where('eligiblePurchases.0.id', $purchase->id)
|
|
);
|
|
}
|
|
|
|
public function test_withdrawal_confirmation_refunds_and_sends_email(): void
|
|
{
|
|
Notification::fake();
|
|
|
|
$user = User::factory()->create();
|
|
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
|
$user->forceFill(['tenant_id' => $tenant->id])->save();
|
|
|
|
$package = Package::factory()->create(['type' => 'endcustomer']);
|
|
$purchase = PackagePurchase::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'provider' => 'lemonsqueezy',
|
|
'provider_id' => 'ord_456',
|
|
'refunded' => false,
|
|
'type' => 'endcustomer_event',
|
|
'purchased_at' => now()->subDays(5),
|
|
]);
|
|
|
|
$tenantPackage = TenantPackage::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'active' => true,
|
|
]);
|
|
|
|
$this->mock(LemonSqueezyOrderService::class, function ($mock) {
|
|
$mock->shouldReceive('refund')
|
|
->once()
|
|
->andReturn([]);
|
|
});
|
|
|
|
$response = $this->actingAs($user)->post('/de/widerruf', [
|
|
'purchase_id' => $purchase->id,
|
|
]);
|
|
|
|
$response->assertSessionHas('success');
|
|
$this->assertTrue($purchase->fresh()->refunded);
|
|
$this->assertFalse($tenantPackage->fresh()->active);
|
|
|
|
Notification::assertSentOnDemand(
|
|
WithdrawalConfirmed::class,
|
|
function (WithdrawalConfirmed $notification, array $channels) {
|
|
return in_array('mail', $channels, true);
|
|
}
|
|
);
|
|
}
|
|
|
|
public function test_withdrawal_rejected_when_event_exists(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
|
$user->forceFill(['tenant_id' => $tenant->id])->save();
|
|
|
|
$package = Package::factory()->create(['type' => 'endcustomer']);
|
|
$purchase = PackagePurchase::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'provider' => 'lemonsqueezy',
|
|
'provider_id' => 'ord_789',
|
|
'refunded' => false,
|
|
'type' => 'endcustomer_event',
|
|
'purchased_at' => now()->subDays(3),
|
|
]);
|
|
|
|
$event = Event::factory()->create(['tenant_id' => $tenant->id]);
|
|
EventPackage::create([
|
|
'event_id' => $event->id,
|
|
'package_id' => $package->id,
|
|
'purchased_price' => $package->price,
|
|
'purchased_at' => now(),
|
|
]);
|
|
|
|
$this->mock(LemonSqueezyOrderService::class, function ($mock) {
|
|
$mock->shouldReceive('refund')->never();
|
|
});
|
|
|
|
$response = $this->actingAs($user)->post('/de/widerruf', [
|
|
'purchase_id' => $purchase->id,
|
|
]);
|
|
|
|
$response->assertSessionHas('error');
|
|
$this->assertFalse($purchase->fresh()->refunded);
|
|
}
|
|
}
|