66 lines
2.6 KiB
PHP
66 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Services\GiftVouchers\GiftVoucherCheckoutService;
|
|
use App\Services\LemonSqueezy\LemonSqueezyCheckoutService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Mockery;
|
|
use Tests\TestCase;
|
|
|
|
class GiftVoucherCheckoutServiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_it_lists_tiers_with_checkout_flag(): void
|
|
{
|
|
config()->set('gift-vouchers.tiers', [
|
|
['key' => 'gift-a', 'label' => 'A', 'amount' => 10, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => 'pri_a'],
|
|
['key' => 'gift-b', 'label' => 'B', 'amount' => 20, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => null],
|
|
]);
|
|
|
|
$service = $this->app->make(GiftVoucherCheckoutService::class);
|
|
|
|
$tiers = $service->tiers();
|
|
|
|
$this->assertCount(2, $tiers);
|
|
$this->assertTrue($tiers[0]['can_checkout']);
|
|
$this->assertFalse($tiers[1]['can_checkout']);
|
|
}
|
|
|
|
public function test_it_creates_checkout_link_with_metadata(): void
|
|
{
|
|
config()->set('gift-vouchers.tiers', [
|
|
['key' => 'gift-a', 'label' => 'A', 'amount' => 10, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => 'pri_a'],
|
|
]);
|
|
|
|
$checkoutService = Mockery::mock(LemonSqueezyCheckoutService::class);
|
|
$checkoutService->shouldReceive('createVariantCheckout')
|
|
->once()
|
|
->with('pri_a', Mockery::on(function (array $customData) {
|
|
return ($customData['type'] ?? null) === 'gift_voucher'
|
|
&& ($customData['tier_key'] ?? null) === 'gift-a'
|
|
&& ($customData['purchaser_email'] ?? null) === 'buyer@example.com'
|
|
&& ($customData['recipient_email'] ?? null) === 'friend@example.com'
|
|
&& ($customData['recipient_name'] ?? null) === 'Friend'
|
|
&& ($customData['message'] ?? null) === 'Hi';
|
|
}), Mockery::type('array'))
|
|
->andReturn(['checkout_url' => 'https://lemonsqueezy.test/checkout/123', 'id' => 'chk_123']);
|
|
|
|
$this->app->instance(LemonSqueezyCheckoutService::class, $checkoutService);
|
|
|
|
$service = $this->app->make(GiftVoucherCheckoutService::class);
|
|
|
|
$checkout = $service->create([
|
|
'tier_key' => 'gift-a',
|
|
'purchaser_email' => 'buyer@example.com',
|
|
'recipient_email' => 'friend@example.com',
|
|
'recipient_name' => 'Friend',
|
|
'message' => 'Hi',
|
|
]);
|
|
|
|
$this->assertSame('https://lemonsqueezy.test/checkout/123', $checkout['checkout_url']);
|
|
$this->assertSame('chk_123', $checkout['id']);
|
|
}
|
|
}
|