Files
portal-api/tests/Unit/VMDataServiceTest.php
T
2025-07-24 16:18:27 +07:00

320 lines
10 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit;
use App\Models\Customer;
use App\Models\ModuleHost;
use App\Models\ModuleIpAddress;
use App\Models\ModuleVmware;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Services\VMDataService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Mockery;
class VMDataServiceTest extends TestCase
{
use RefreshDatabase;
private VMDataService $vmDataService;
protected function setUp(): void
{
parent::setUp();
$this->vmDataService = new VMDataService();
}
/** @test */
public function it_can_save_vm_data_successfully()
{
// Arrange
$customer = Customer::factory()->create();
$order = Order::factory()->create(['customer_id' => $customer->id]);
$host = ModuleHost::factory()->create();
$product = Product::factory()->create(['module_host_id' => $host->id]);
$orderItem = OrderItem::factory()->create([
'order_id' => $order->id,
'product_id' => $product->id
]);
$vmwareId = 'vm-12345';
$ipAddresses = [
['ip' => '192.168.1.100', 'type' => 'IPV4', 'state' => 'PREFERRED']
];
// Act
$result = $this->vmDataService->saveVMData($orderItem, $vmwareId, $ipAddresses);
// Assert
$this->assertTrue($result['success']);
$this->assertInstanceOf(ModuleVmware::class, $result['vm']);
$this->assertInstanceOf(ModuleIpAddress::class, $result['ip_address']);
$this->assertEquals($vmwareId, $result['vm']->vmware_id);
$this->assertEquals('192.168.1.100', $result['ip_address']->ip);
$this->assertEquals($customer->id, $result['vm']->customer_id);
$this->assertEquals($order->id, $result['vm']->order_id);
}
/** @test */
public function it_can_update_existing_vm_data()
{
// Arrange
$customer = Customer::factory()->create();
$order = Order::factory()->create(['customer_id' => $customer->id]);
$host = ModuleHost::factory()->create();
$product = Product::factory()->create(['module_host_id' => $host->id]);
$orderItem = OrderItem::factory()->create([
'order_id' => $order->id,
'product_id' => $product->id
]);
$vmwareId = 'vm-12345';
$ipAddresses = [
['ip' => '192.168.1.100', 'type' => 'IPV4', 'state' => 'PREFERRED']
];
// Create existing VM
$existingVM = ModuleVmware::factory()->create([
'vmware_id' => $vmwareId,
'name' => $product->name
]);
// Act
$result = $this->vmDataService->saveVMData($orderItem, $vmwareId, $ipAddresses);
// Assert
$this->assertTrue($result['success']);
$this->assertEquals($existingVM->id, $result['vm']->id);
$this->assertEquals($order->id, $result['vm']->order_id);
}
/** @test */
public function it_can_update_vm_status()
{
// Arrange
$vm = ModuleVmware::factory()->create(['vmware_id' => 'vm-12345']);
// Act
$result = $this->vmDataService->updateVMStatus('vm-12345', 2);
// Assert
$this->assertTrue($result);
$this->assertEquals(2, $vm->fresh()->status);
}
/** @test */
public function it_can_update_vm_ip_address()
{
// Arrange
$ipAddress = ModuleIpAddress::factory()->create(['ip' => '192.168.1.100']);
$vm = ModuleVmware::factory()->create([
'vmware_id' => 'vm-12345',
'module_ip_address_id' => $ipAddress->id
]);
// Act
$result = $this->vmDataService->updateVMIPAddress('vm-12345', '192.168.1.200');
// Assert
$this->assertTrue($result);
$this->assertEquals('192.168.1.200', $ipAddress->fresh()->ip);
$this->assertEquals(1, $ipAddress->fresh()->status);
}
/** @test */
public function it_returns_false_when_vm_not_found_for_status_update()
{
// Act
$result = $this->vmDataService->updateVMStatus('non-existent-vm', 1);
// Assert
$this->assertFalse($result);
}
/** @test */
public function it_returns_false_when_vm_not_found_for_ip_update()
{
// Act
$result = $this->vmDataService->updateVMIPAddress('non-existent-vm', '192.168.1.100');
// Assert
$this->assertFalse($result);
}
/** @test */
public function it_handles_payment_fields_with_default_values()
{
// Arrange
$customer = Customer::factory()->create();
$order = Order::factory()->create([
'customer_id' => $customer->id,
'first_payment' => null, // Test null value
'next_payment' => '', // Test empty string
'next_invoice' => 'invalid', // Test invalid value
]);
$host = ModuleHost::factory()->create();
$product = Product::factory()->create(['module_host_id' => $host->id]);
$orderItem = OrderItem::factory()->create([
'order_id' => $order->id,
'product_id' => $product->id
]);
$vmwareId = 'vm-12345';
$ipAddresses = [
['ip' => '192.168.1.100', 'type' => 'IPV4', 'state' => 'PREFERRED']
];
// Act
$result = $this->vmDataService->saveVMData($orderItem, $vmwareId, $ipAddresses);
// Assert
$this->assertTrue($result['success']);
$this->assertEquals(0, $result['vm']->first_payment); // Should default to 0
$this->assertEquals(0, $result['vm']->next_payment); // Should default to 0
$this->assertEquals(0, $result['vm']->next_invoice); // Should default to 0
$this->assertEquals('0.0.0.0', $result['ip_address']->gateway); // Should default to 0.0.0.0
}
/** @test */
public function it_handles_valid_payment_fields()
{
// Arrange
$customer = Customer::factory()->create();
$order = Order::factory()->create([
'customer_id' => $customer->id,
'first_payment' => 100000,
'next_payment' => 200000,
'next_invoice' => 300000,
]);
$host = ModuleHost::factory()->create();
$product = Product::factory()->create(['module_host_id' => $host->id]);
$orderItem = OrderItem::factory()->create([
'order_id' => $order->id,
'product_id' => $product->id
]);
$vmwareId = 'vm-12345';
$ipAddresses = [
['ip' => '192.168.1.100', 'type' => 'IPV4', 'state' => 'PREFERRED']
];
// Act
$result = $this->vmDataService->saveVMData($orderItem, $vmwareId, $ipAddresses);
// Assert
$this->assertTrue($result['success']);
$this->assertEquals(100000, $result['vm']->first_payment);
$this->assertEquals(200000, $result['vm']->next_payment);
$this->assertEquals(300000, $result['vm']->next_invoice);
}
/**
* Test saving VM data with server_name in order item (should create new records)
*/
public function test_save_vm_data_with_server_name(): void
{
// Arrange
$orderItem = OrderItem::factory()->create([
'server_name' => 'linuxtestpb.com'
]);
$vmwareId = 'vm-12345';
$ipAddresses = [
['ip' => '192.168.1.100', 'type' => 'ipv4']
];
// Mock the product and related models
$product = Mockery::mock();
$product->shouldReceive('getAttribute')->andReturnSelf();
$product->name = 'Test Product';
$product->host = Mockery::mock(['id' => 1]);
$product->ipAddress = null;
$orderItem->shouldReceive('getProductAttribute')->andReturn($product);
$orderItem->order = Mockery::mock([
'id' => 1,
'customer' => Mockery::mock(['id' => 1]),
'first_payment' => 100,
'next_payment' => 200,
'next_invoice' => 300,
'next_due_date' => now()->addMonth()
]);
// Mock ModuleIpAddress - should only create
$ipAddress = Mockery::mock(ModuleIpAddress::class);
$ipAddress->id = 1;
ModuleIpAddress::shouldReceive('create')
->andReturn($ipAddress);
// Mock ModuleVmware - should only create
$vm = Mockery::mock(ModuleVmware::class);
$vm->id = 1;
$vm->vmware_id = $vmwareId;
ModuleVmware::shouldReceive('create')
->andReturn($vm);
// Act
$result = $this->vmDataService->saveVMData($orderItem, $vmwareId, $ipAddresses);
// Assert
$this->assertTrue($result['success']);
$this->assertEquals($vm, $result['vm']);
$this->assertEquals($ipAddress, $result['ip_address']);
$this->assertArrayNotHasKey('skipped', $result);
}
/**
* Test saving VM data without server_name in order item (should skip saving)
*/
public function test_save_vm_data_without_server_name(): void
{
// Arrange
$orderItem = OrderItem::factory()->create([
'server_name' => null
]);
$vmwareId = 'vm-12345';
$ipAddresses = [
['ip' => '192.168.1.100', 'type' => 'ipv4']
];
// Act
$result = $this->vmDataService->saveVMData($orderItem, $vmwareId, $ipAddresses);
// Assert
$this->assertTrue($result['success']);
$this->assertTrue($result['skipped']);
$this->assertEquals('Bỏ qua lưu dữ liệu VM vì không có server_name trong order item', $result['message']);
$this->assertArrayNotHasKey('vm', $result);
$this->assertArrayNotHasKey('ip_address', $result);
}
/**
* Test saving VM data with empty server_name in order item (should skip saving)
*/
public function test_save_vm_data_with_empty_server_name(): void
{
// Arrange
$orderItem = OrderItem::factory()->create([
'server_name' => ''
]);
$vmwareId = 'vm-12345';
$ipAddresses = [
['ip' => '192.168.1.100', 'type' => 'ipv4']
];
// Act
$result = $this->vmDataService->saveVMData($orderItem, $vmwareId, $ipAddresses);
// Assert
$this->assertTrue($result['success']);
$this->assertTrue($result['skipped']);
$this->assertEquals('Bỏ qua lưu dữ liệu VM vì không có server_name trong order item', $result['message']);
$this->assertArrayNotHasKey('vm', $result);
$this->assertArrayNotHasKey('ip_address', $result);
}
}