Files
server/tests/php/Unit/Console/Tenant/TenantListCommandTest.php
T
Sebastian b570150258
JS Unit Tests / test (pull_request) Failing after 43s
Build Test / build (pull_request) Successful in 44s
PHP Unit Tests / test (pull_request) Successful in 1m9s
PHP Integration Tests / Integration Tests (pull_request) Successful in 1m28s
chore: implement php test suites
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-07-23 22:20:35 -04:00

94 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXT\Unit\Console\Tenant;
use KTXC\Console\Tenant\TenantListCommand;
use KTXC\Models\Tenant\DomainCollection;
use KTXC\Models\Tenant\TenantObject;
use KTXC\Service\TenantService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
class TenantListCommandTest extends TestCase
{
private TenantService&MockObject $tenantService;
private CommandTester $tester;
protected function setUp(): void
{
$this->tenantService = $this->createMock(TenantService::class);
$this->tester = new CommandTester(
new TenantListCommand($this->tenantService)
);
}
public function testReportsWhenNoTenantsExist(): void
{
$this->tenantService->method('list')->willReturn([]);
$status = $this->tester->execute([]);
$this->assertSame(Command::SUCCESS, $status);
$this->assertStringContainsString('No tenants found', $this->tester->getDisplay());
}
public function testListsTenantsWithDomains(): void
{
$acme = (new TenantObject())
->setIdentifier('acme')
->setLabel('Acme Inc')
->setEnabled(true)
->setDomains(new DomainCollection(['acme.test', 'acme.example']));
$wayne = (new TenantObject())
->setIdentifier('wayne')
->setLabel('Wayne Corp')
->setEnabled(false)
->setDomains(new DomainCollection(['wayne.test']));
$this->tenantService->method('list')->willReturn(['a' => $acme, 'b' => $wayne]);
$status = $this->tester->execute([]);
$display = $this->tester->getDisplay();
$this->assertSame(Command::SUCCESS, $status);
$this->assertStringContainsString('acme', $display);
$this->assertStringContainsString('Acme Inc', $display);
$this->assertStringContainsString('acme.test, acme.example', $display);
$this->assertStringContainsString('Wayne Corp', $display);
$this->assertStringContainsString('wayne.test', $display);
$this->assertStringContainsString('Total: 2 tenant(s)', $display);
}
public function testHandlesTenantWithoutDomains(): void
{
$legacy = (new TenantObject())
->setIdentifier('legacy')
->setLabel('Legacy')
->setEnabled(true);
$this->tenantService->method('list')->willReturn(['l' => $legacy]);
$status = $this->tester->execute([]);
$this->assertSame(Command::SUCCESS, $status);
$this->assertStringContainsString('legacy', $this->tester->getDisplay());
}
public function testFailsWhenServiceThrows(): void
{
$this->tenantService
->method('list')
->willThrowException(new \RuntimeException('datastore unavailable'));
$status = $this->tester->execute([]);
$this->assertSame(Command::FAILURE, $status);
$this->assertStringContainsString('datastore unavailable', $this->tester->getDisplay());
}
}