140 lines
4.3 KiB
PHP
140 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Coupon;
|
|
use App\Models\Package;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Coupons\CouponService;
|
|
use App\Services\LemonSqueezy\LemonSqueezyCheckoutService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Mockery;
|
|
use Tests\TestCase;
|
|
|
|
class LemonSqueezyCheckoutControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Mockery::close();
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_authenticated_user_can_create_checkout_with_coupon(): void
|
|
{
|
|
$tenant = Tenant::factory()->create([
|
|
'lemonsqueezy_customer_id' => 'cus_123',
|
|
]);
|
|
|
|
$user = User::factory()->for($tenant)->create();
|
|
|
|
$package = Package::factory()->create([
|
|
'lemonsqueezy_variant_id' => 'pri_123',
|
|
'lemonsqueezy_product_id' => 'pro_123',
|
|
'price' => 120,
|
|
]);
|
|
|
|
$coupon = Coupon::factory()->create([
|
|
'code' => 'SAVE15',
|
|
'lemonsqueezy_discount_id' => 'dsc_123',
|
|
]);
|
|
$coupon->packages()->attach($package);
|
|
|
|
$couponServiceMock = Mockery::mock(CouponService::class);
|
|
$couponServiceMock->shouldReceive('preview')
|
|
->once()
|
|
->andReturn([
|
|
'coupon' => $coupon,
|
|
'pricing' => [
|
|
'currency' => 'EUR',
|
|
'subtotal' => 120.0,
|
|
'discount' => 18.0,
|
|
'tax' => 0,
|
|
'total' => 102.0,
|
|
'formatted' => [
|
|
'subtotal' => '€120.00',
|
|
'discount' => '-€18.00',
|
|
'tax' => '€0.00',
|
|
'total' => '€102.00',
|
|
],
|
|
'breakdown' => [],
|
|
],
|
|
'source' => 'manual',
|
|
]);
|
|
$this->instance(CouponService::class, $couponServiceMock);
|
|
|
|
$checkoutServiceMock = Mockery::mock(LemonSqueezyCheckoutService::class);
|
|
$checkoutServiceMock->shouldReceive('createCheckout')
|
|
->once()
|
|
->andReturn([
|
|
'checkout_url' => 'https://example.com/checkout/test',
|
|
'id' => 'chk_123',
|
|
]);
|
|
$this->instance(LemonSqueezyCheckoutService::class, $checkoutServiceMock);
|
|
|
|
$this->be($user);
|
|
|
|
$response = $this->postJson(route('lemonsqueezy.checkout.create'), [
|
|
'package_id' => $package->id,
|
|
'coupon_code' => 'SAVE15',
|
|
'accepted_terms' => true,
|
|
]);
|
|
|
|
$response->assertOk()
|
|
->assertJsonPath('checkout_url', 'https://example.com/checkout/test')
|
|
->assertJsonStructure([
|
|
'checkout_session_id',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('checkout_sessions', [
|
|
'package_id' => $package->id,
|
|
'coupon_code' => 'SAVE15',
|
|
]);
|
|
}
|
|
|
|
public function test_local_environment_simulates_checkout_without_api_call(): void
|
|
{
|
|
$this->app->detectEnvironment(fn () => 'local');
|
|
$this->withoutMiddleware();
|
|
|
|
$tenant = Tenant::factory()->create([
|
|
'lemonsqueezy_customer_id' => 'cus_123',
|
|
]);
|
|
|
|
$user = User::factory()->for($tenant)->create();
|
|
|
|
$package = Package::factory()->create([
|
|
'lemonsqueezy_variant_id' => 'pri_123',
|
|
'lemonsqueezy_product_id' => 'pro_123',
|
|
'price' => 120,
|
|
]);
|
|
|
|
$checkoutServiceMock = Mockery::mock(LemonSqueezyCheckoutService::class);
|
|
$checkoutServiceMock->shouldNotReceive('createCheckout');
|
|
$this->instance(LemonSqueezyCheckoutService::class, $checkoutServiceMock);
|
|
|
|
$this->be($user);
|
|
|
|
$response = $this->postJson(route('lemonsqueezy.checkout.create'), [
|
|
'package_id' => $package->id,
|
|
'accepted_terms' => true,
|
|
]);
|
|
|
|
$response->assertOk()
|
|
->assertJsonPath('simulated', true)
|
|
->assertJsonStructure([
|
|
'checkout_session_id',
|
|
'order_id',
|
|
'id',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('checkout_sessions', [
|
|
'package_id' => $package->id,
|
|
'lemonsqueezy_checkout_id' => $response->json('id'),
|
|
]);
|
|
}
|
|
}
|