domovoy/tests/Inventory/DeviceServiceMergeTest.php

83 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Domovoy\Tests\Inventory;
use Domovoy\Models\Device;
use Domovoy\Models\DiscoveredHost;
use Domovoy\Repositories\DeviceRepository;
use Domovoy\Repositories\DiscoveredHostRepository;
use Domovoy\Services\Inventory\DeviceService;
use PHPUnit\Framework\TestCase;
final class DeviceServiceMergeTest extends TestCase
{
public function testMergeDiscoveredHostLinksHostAndFillsBlankDeviceFields(): void
{
$device = new Device();
$device->id = 5;
$device->name = 'server';
$host = new DiscoveredHost();
$host->id = 9;
$host->ipAddress = '192.168.1.50';
$host->macAddress = 'aa:bb:cc:dd:ee:ff';
$host->hostname = 'server.local';
$host->vendor = 'Intel';
$devices = new MergeDeviceRepository($device);
$hosts = new MergeHostRepository($host);
$service = new DeviceService($devices, $hosts);
$service->mergeDiscoveredHost(9, 5);
self::assertSame('192.168.1.50', $device->primaryIp);
self::assertSame('aa:bb:cc:dd:ee:ff', $device->macAddress);
self::assertSame('server.local', $device->hostname);
self::assertSame('Intel', $device->vendor);
self::assertSame('merged', $host->status);
self::assertSame('5', $host->matchedDeviceId);
self::assertSame($device, $devices->savedDevice);
self::assertSame($host, $hosts->savedHost);
}
}
final class MergeDeviceRepository extends DeviceRepository
{
public ?Device $savedDevice = null;
public function __construct(private Device $device)
{
}
public function findById(int $id): ?Device
{
return $id === $this->device->id ? $this->device : null;
}
public function save(Device $device): void
{
$this->savedDevice = $device;
}
}
final class MergeHostRepository extends DiscoveredHostRepository
{
public ?DiscoveredHost $savedHost = null;
public function __construct(private DiscoveredHost $host)
{
}
public function findById(int $id): ?DiscoveredHost
{
return $id === $this->host->id ? $this->host : null;
}
public function save(DiscoveredHost $host): void
{
$this->savedHost = $host;
}
}