Files
portal-api/tests/Feature/Api/Service/StoreOrderAddonTest.php
T
2026-09-27 11:42:40 +07:00

140 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Service;
use App\Models\Addon;
use App\Models\Customer;
use App\Models\Product;
use App\Models\ProductPricings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class StoreOrderAddonTest extends TestCase
{
use RefreshDatabase;
private Customer $customer;
private Product $product;
protected function setUp(): void
{
parent::setUp();
$this->customer = Customer::factory()->create(['total' => 10_000_000]);
$this->product = Product::factory()->create([
'product_type' => 'vps',
'is_active' => true,
]);
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'monthly',
'price' => 100_000,
'is_active' => true,
]);
}
private function actingAsCustomer(): static
{
return $this->actingAs($this->customer, 'client')
->withHeaders(['x-access-token' => $this->buildClientAccessToken()]);
}
private function storeOrder(array $addons): \Illuminate\Testing\TestResponse
{
return $this->actingAsCustomer()->postJson('/v1/orders', [
'product_id' => $this->product->uuid,
'billing_cycle' => 'monthly',
'addons' => $addons,
]);
}
public function test_addon_belonging_to_another_product_type_is_rejected(): void
{
$hostingAddon = Addon::create([
'type' => 'hosting',
'code' => 'storage',
'name' => 'Storage',
'min' => 1,
'max' => 100,
'step' => 1,
'price' => 10_000,
'status' => true,
]);
$response = $this->storeOrder([
['addon_id' => $hostingAddon->id, 'qty' => 1],
]);
$response->assertStatus(422)->assertJsonPath('success', false);
$this->assertSame(10_000_000, $this->customer->fresh()->total);
}
public function test_inactive_addon_is_rejected(): void
{
$inactiveAddon = Addon::create([
'type' => 'vps',
'code' => 'cpu-inactive',
'name' => 'CPU',
'min' => 1,
'max' => 32,
'step' => 1,
'price' => 25_000,
'status' => false,
]);
$response = $this->storeOrder([
['addon_id' => $inactiveAddon->id, 'qty' => 1],
]);
$response->assertStatus(422)->assertJsonPath('success', false);
}
public function test_qty_above_max_is_rejected(): void
{
$cpuAddon = Addon::create([
'type' => 'vps',
'code' => 'cpu',
'name' => 'CPU',
'min' => 1,
'max' => 32,
'step' => 1,
'price' => 25_000,
'status' => true,
]);
$response = $this->storeOrder([
['addon_id' => $cpuAddon->id, 'qty' => 999],
]);
$response->assertStatus(422)->assertJsonPath('success', false);
}
public function test_valid_addon_within_bounds_is_accepted(): void
{
$cpuAddon = Addon::create([
'type' => 'vps',
'code' => 'cpu-ok',
'name' => 'CPU',
'min' => 1,
'max' => 32,
'step' => 1,
'price' => 25_000,
'status' => true,
]);
$response = $this->storeOrder([
['addon_id' => $cpuAddon->id, 'qty' => 4],
]);
$response->assertStatus(201)->assertJsonPath('success', true);
$this->assertDatabaseHas('invoices', [
'customer_id' => $this->customer->id,
'total' => 100_000 + (25_000 * 4),
]);
}
}