HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/tests/Unit/Subscription/SubscriptionManagementServiceTest.php
<?php

namespace Tests\Unit\Subscription;

use App\Model\Customer;
use App\Model\Order;
use App\Model\Plan;
use App\Model\Subscription;
use App\Services\Plans\Lifecycle\SubscriptionLifecycle;
use App\Services\Subscription\SubscriptionManagementService;
use Mockery;
use Tests\TestCase;

class SubscriptionManagementServiceTest extends TestCase
{
    protected function tearDown(): void
    {
        Mockery::close();
        parent::tearDown();
    }

    public function test_assign_plan_creates_subscription_order_credits_and_log()
    {
        $service = Mockery::mock(SubscriptionManagementService::class)->makePartial();
        $service->shouldAllowMockingProtectedMethods();

        $customer = Mockery::mock(Customer::class)->makePartial();
        $plan = Mockery::mock(Plan::class)->makePartial();
        $subscription = Mockery::mock(Subscription::class)->makePartial();
        $order = Mockery::mock(Order::class)->makePartial();

        $customer->shouldReceive('getNewOrActiveSubscription')->once()->andReturn(null);

        $service->shouldReceive('runInTransaction')->once()->andReturnUsing(function ($callback) {
            return $callback();
        });
        $service->shouldReceive('createNewSubscription')->once()->with($customer, $plan)->andReturn($subscription);
        $service->shouldReceive('createNewSubscriptionOrder')->once()->with($subscription)->andReturn($order);

        // SubscriptionLifecycle::onSubscribe primes both cold DB state and
        // hot tracker files — single init point that replaced the old
        // setDefaultEmailCredits + setDefaultVerificationCredits pair.
        $lifecycle = Mockery::mock(SubscriptionLifecycle::class);
        $lifecycle->shouldReceive('onSubscribe')->once()->with($subscription);
        $this->app->instance(SubscriptionLifecycle::class, $lifecycle);

        $service->shouldReceive('logPlanSelection')->once()->with($subscription, $order);

        $result = $service->assignPlan($customer, $plan);

        $this->assertSame($subscription, $result);
    }

    public function test_assign_plan_throws_when_customer_already_has_new_or_active_subscription()
    {
        $service = Mockery::mock(SubscriptionManagementService::class)->makePartial();
        $service->shouldAllowMockingProtectedMethods();

        $customer = Mockery::mock(Customer::class)->makePartial();
        $plan = Mockery::mock(Plan::class)->makePartial();
        $existingSubscription = Mockery::mock(Subscription::class)->makePartial();

        $customer->shouldReceive('getNewOrActiveSubscription')->once()->andReturn($existingSubscription);

        $service->shouldReceive('runInTransaction')->once()->andReturnUsing(function ($callback) {
            return $callback();
        });
        $service->shouldReceive('createNewSubscription')->never();

        $this->expectException(\Exception::class);
        $this->expectExceptionMessage(trans('messages.customer.already_new_active_sub'));

        $service->assignPlan($customer, $plan);
    }
}