58 lines
1.9 KiB
PHP
58 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Livewire\Concerns\WithFleetContext;
|
|
use App\Models\Server;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* activeServer() calls fleet(), and pages call activeServer() several times per render, so the
|
|
* fleet query used to run once per call. fleet() now memoises for the request — proven here by
|
|
* counting the `servers` selects across multiple reads.
|
|
*/
|
|
class WithFleetContextTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_fleet_query_runs_once_across_repeated_reads(): void
|
|
{
|
|
$server = Server::create(['name' => 'box', 'ip' => '10.0.0.1', 'ssh_port' => 22, 'status' => 'online']);
|
|
$server->credential()->create(['username' => 'root', 'auth_type' => 'password', 'secret' => 'x']);
|
|
|
|
$component = new class
|
|
{
|
|
use WithFleetContext;
|
|
};
|
|
|
|
DB::enableQueryLog();
|
|
$component->fleet();
|
|
$component->fleet();
|
|
$component->activeServer(); // internally calls fleet() again
|
|
|
|
$serverSelects = collect(DB::getQueryLog())
|
|
->filter(fn (array $q): bool => str_contains($q['query'], 'from "servers"'))
|
|
->count();
|
|
DB::disableQueryLog();
|
|
|
|
$this->assertSame(1, $serverSelects); // memoised: one query despite three fleet() reads
|
|
}
|
|
|
|
public function test_fleet_still_returns_the_fleet(): void
|
|
{
|
|
$a = Server::create(['name' => 'alpha', 'ip' => '10.0.0.1', 'ssh_port' => 22, 'status' => 'online']);
|
|
$b = Server::create(['name' => 'bravo', 'ip' => '10.0.0.2', 'ssh_port' => 22, 'status' => 'online']);
|
|
|
|
$component = new class
|
|
{
|
|
use WithFleetContext;
|
|
};
|
|
|
|
$ids = $component->fleet()->pluck('id');
|
|
$this->assertTrue($ids->contains($a->id));
|
|
$this->assertTrue($ids->contains($b->id));
|
|
}
|
|
}
|