63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Dns;
|
|
|
|
use RuntimeException;
|
|
|
|
class FakeHostDnsDirectory implements HostDnsDirectory
|
|
{
|
|
/** @var array<string, string> name => ip */
|
|
public array $ips = [];
|
|
|
|
/** @var array<string, string> name => fqdn */
|
|
public array $fqdns = [];
|
|
|
|
public bool $failWrite = false;
|
|
|
|
public bool $failRemove = false;
|
|
|
|
/** @var array<string, array<int, string>> key => fqdns */
|
|
public array $groups = [];
|
|
|
|
public function write(string $name, string $fqdn, string $ip): void
|
|
{
|
|
if ($this->failWrite) {
|
|
throw new RuntimeException('dns-hosts volume unavailable');
|
|
}
|
|
|
|
$this->ips[$name] = $ip;
|
|
$this->fqdns[$name] = $fqdn;
|
|
}
|
|
|
|
public function remove(string $name): void
|
|
{
|
|
if ($this->failRemove) {
|
|
throw new RuntimeException('dns-hosts volume unavailable');
|
|
}
|
|
|
|
unset($this->ips[$name], $this->fqdns[$name]);
|
|
}
|
|
|
|
public function writeMany(string $key, array $fqdns, string $ip): void
|
|
{
|
|
if ($this->failWrite) {
|
|
throw new RuntimeException('dns-hosts volume unavailable');
|
|
}
|
|
|
|
if ($fqdns === []) {
|
|
// FileHostDnsDirectory::writeMany() ruft bei einer leeren Liste
|
|
// remove() auf, und remove() loescht die Datei komplett — nicht
|
|
// nur die Zeilen darin, die als IP-Zuordnung durchgingen. Ohne
|
|
// $fqdns hier mit zu leeren, wich der Fake vom echten Verhalten
|
|
// ab: ein Test haette `$fake->fqdns[$key]` noch stehen sehen,
|
|
// waehrend die echte Datei laengst weg war.
|
|
unset($this->groups[$key], $this->ips[$key], $this->fqdns[$key]);
|
|
|
|
return;
|
|
}
|
|
|
|
$this->groups[$key] = array_values($fqdns);
|
|
$this->ips[$key] = $ip;
|
|
}
|
|
}
|