Stabilize discovery workflow

This commit is contained in:
2026-05-29 17:16:46 +08:00
parent 15772bc17e
commit 3981ffdf5e
40 changed files with 2039 additions and 211 deletions
@@ -0,0 +1,82 @@
<?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;
}
}
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Domovoy\Tests\Inventory;
use Domovoy\Models\Device;
use Domovoy\Models\DiscoveredHost;
use Domovoy\Repositories\DeviceRepository;
use Domovoy\Services\Inventory\MergeSuggestionService;
use PHPUnit\Framework\TestCase;
final class MergeSuggestionServiceTest extends TestCase
{
public function testSuggestsDeviceByIpWhenMacAndHostnameDoNotMatch(): void
{
$device = new Device();
$device->id = 10;
$device->name = 'router';
$device->primaryIp = '192.168.1.1';
$repository = new FakeDeviceRepository(ipMatch: $device);
$service = new MergeSuggestionService($repository);
$host = new DiscoveredHost();
$host->ipAddress = '192.168.1.1';
$suggestions = $service->findSuggestions($host);
self::assertSame($device, $suggestions[0]['device']);
self::assertSame(40, $suggestions[0]['confidence']);
self::assertSame('IP адрес совпадает', $suggestions[0]['reason']);
}
}
final class FakeDeviceRepository extends DeviceRepository
{
public function __construct(
private ?Device $macMatch = null,
private ?Device $nameMatch = null,
private ?Device $ipMatch = null
) {
}
public function findByMac(string $macAddress): ?Device
{
return $this->macMatch;
}
public function findByName(string $name): ?Device
{
return $this->nameMatch;
}
public function findByIp(string $ipAddress): ?Device
{
return $this->ipMatch;
}
}