chore: implement php test suites
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

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-07-23 22:20:35 -04:00
parent e81b848a02
commit b570150258
11 changed files with 337 additions and 340 deletions
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace KTXT;
use PHPUnit\Framework\TestCase;
class BaseTest extends TestCase
{
public function testBasicAssertion(): void
{
$this->assertTrue(true);
}
public function testArrayOperations(): void
{
$array = ['foo' => 'bar'];
$this->assertArrayHasKey('foo', $array);
$this->assertEquals('bar', $array['foo']);
}
public function testStringOperations(): void
{
$string = 'Hello, World!';
$this->assertStringContainsString('World', $string);
$this->assertEquals(13, strlen($string));
}
}
@@ -0,0 +1,175 @@
<?php
declare(strict_types=1);
namespace KTXT\Unit\Console\Tenant;
use KTXC\Console\Tenant\TenantCreateCommand;
use KTXC\Models\Tenant\TenantObject;
use KTXC\Service\TenantService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
class TenantCreateCommandTest extends TestCase
{
private TenantService&MockObject $tenantService;
private CommandTester $tester;
private ?TenantObject $deposited = null;
protected function setUp(): void
{
$this->tenantService = $this->createMock(TenantService::class);
$this->tester = new CommandTester(
new TenantCreateCommand($this->tenantService, new NullLogger())
);
}
/**
* Configure the service mock to accept and capture the deposited tenant
*/
private function expectDeposit(): void
{
$this->tenantService
->method('deposit')
->willReturnCallback(function (TenantObject $tenant): TenantObject {
$tenant->setId('6a5d84498ac04b41fa0c43a2');
$this->deposited = $tenant;
return $tenant;
});
}
public function testCreatesTenantWithGeneratedIdentifier(): void
{
$this->expectDeposit();
$status = $this->tester->execute(['domain' => ['acme.test']]);
$this->assertSame(Command::SUCCESS, $status);
$this->assertNotNull($this->deposited);
$this->assertMatchesRegularExpression(
'/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i',
$this->deposited->getIdentifier()
);
$this->assertTrue($this->deposited->getEnabled());
$this->assertSame(['acme.test'], $this->deposited->getDomains()->getArrayCopy());
$this->assertStringContainsString('created successfully', $this->tester->getDisplay());
}
public function testCreatesTenantWithExplicitIdentifier(): void
{
$this->expectDeposit();
$status = $this->tester->execute([
'domain' => ['acme.test'],
'--identifier' => 'acme',
]);
$this->assertSame(Command::SUCCESS, $status);
$this->assertSame('acme', $this->deposited->getIdentifier());
}
public function testLabelDefaultsToFirstDomain(): void
{
$this->expectDeposit();
$this->tester->execute(['domain' => ['acme.test', 'acme.example']]);
$this->assertSame('acme.test', $this->deposited->getLabel());
}
public function testAppliesOptions(): void
{
$this->expectDeposit();
$status = $this->tester->execute([
'domain' => ['acme.test'],
'--label' => 'Acme Inc',
'--description' => 'Test tenant',
'--disabled' => true,
]);
$this->assertSame(Command::SUCCESS, $status);
$this->assertSame('Acme Inc', $this->deposited->getLabel());
$this->assertSame('Test tenant', $this->deposited->getDescription());
$this->assertFalse($this->deposited->getEnabled());
}
public function testTrimsAndDeduplicatesDomains(): void
{
$this->expectDeposit();
$this->tester->execute(['domain' => [' acme.test ', 'acme.test', 'acme.example']]);
$this->assertSame(
['acme.test', 'acme.example'],
$this->deposited->getDomains()->getArrayCopy()
);
}
public function testFailsWhenNoValidDomainProvided(): void
{
$this->tenantService->expects($this->never())->method('deposit');
$status = $this->tester->execute(['domain' => [' ']]);
$this->assertSame(Command::FAILURE, $status);
$this->assertStringContainsString('At least one non-empty domain is required', $this->tester->getDisplay());
}
public function testFailsWhenIdentifierAlreadyExists(): void
{
$this->tenantService
->method('fetchById')
->with('acme')
->willReturn(new TenantObject());
$this->tenantService->expects($this->never())->method('deposit');
$status = $this->tester->execute([
'domain' => ['acme.test'],
'--identifier' => 'acme',
]);
$this->assertSame(Command::FAILURE, $status);
$this->assertStringContainsString('already exists', $this->tester->getDisplay());
}
public function testFailsWhenDomainAssignedToAnotherTenant(): void
{
$owner = (new TenantObject())->setIdentifier('other');
$this->tenantService
->method('fetchByDomain')
->with('acme.test')
->willReturn($owner);
$this->tenantService->expects($this->never())->method('deposit');
$status = $this->tester->execute(['domain' => ['acme.test']]);
$this->assertSame(Command::FAILURE, $status);
$this->assertStringContainsString("already assigned to tenant 'other'", $this->tester->getDisplay());
}
public function testFailsWhenDepositReturnsNull(): void
{
$this->tenantService->method('deposit')->willReturn(null);
$status = $this->tester->execute(['domain' => ['acme.test']]);
$this->assertSame(Command::FAILURE, $status);
$this->assertStringContainsString('Failed to create tenant', $this->tester->getDisplay());
}
public function testFailsWhenServiceThrows(): void
{
$this->tenantService
->method('deposit')
->willThrowException(new \RuntimeException('datastore unavailable'));
$status = $this->tester->execute(['domain' => ['acme.test']]);
$this->assertSame(Command::FAILURE, $status);
$this->assertStringContainsString('datastore unavailable', $this->tester->getDisplay());
}
}
@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
namespace KTXT\Unit\Console\Tenant;
use KTXC\Console\Tenant\TenantDeleteCommand;
use KTXC\Models\Tenant\DomainCollection;
use KTXC\Models\Tenant\TenantObject;
use KTXC\Service\TenantService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
class TenantDeleteCommandTest extends TestCase
{
private TenantService&MockObject $tenantService;
private CommandTester $tester;
protected function setUp(): void
{
$this->tenantService = $this->createMock(TenantService::class);
$this->tester = new CommandTester(
new TenantDeleteCommand($this->tenantService, new NullLogger())
);
}
private function givenTenant(?array $domains = ['acme.test', 'acme.example']): TenantObject
{
$tenant = (new TenantObject())
->setId('6a5d84498ac04b41fa0c43a2')
->setIdentifier('acme')
->setLabel('Acme Inc')
->setEnabled(true);
if ($domains !== null) {
$tenant->setDomains(new DomainCollection($domains));
}
$this->tenantService->method('fetchById')->with('acme')->willReturn($tenant);
return $tenant;
}
public function testFailsWhenTenantNotFound(): void
{
$this->tenantService->method('fetchById')->willReturn(null);
$this->tenantService->expects($this->never())->method('destroy');
$status = $this->tester->execute(['identifier' => 'missing']);
$this->assertSame(Command::FAILURE, $status);
$this->assertStringContainsString('not found', $this->tester->getDisplay());
}
public function testForceDeletesWithoutPrompt(): void
{
$tenant = $this->givenTenant();
$this->tenantService->expects($this->once())->method('destroy')->with($tenant);
$status = $this->tester->execute(['identifier' => 'acme', '--force' => true]);
$this->assertSame(Command::SUCCESS, $status);
$this->assertStringContainsString('deleted successfully', $this->tester->getDisplay());
}
public function testDeletesWhenConfirmationMatchesPrimaryDomain(): void
{
$tenant = $this->givenTenant();
$this->tenantService->expects($this->once())->method('destroy')->with($tenant);
$this->tester->setInputs(['acme.test']);
$status = $this->tester->execute(['identifier' => 'acme'], ['interactive' => true]);
$this->assertSame(Command::SUCCESS, $status);
$this->assertStringContainsString("Type 'acme.test' to confirm deletion", $this->tester->getDisplay());
$this->assertStringContainsString('deleted successfully', $this->tester->getDisplay());
}
public function testCancelsWhenConfirmationDoesNotMatch(): void
{
$this->givenTenant();
$this->tenantService->expects($this->never())->method('destroy');
$this->tester->setInputs(['wrong.test']);
$status = $this->tester->execute(['identifier' => 'acme'], ['interactive' => true]);
$this->assertSame(Command::FAILURE, $status);
$this->assertStringContainsString('Confirmation did not match', $this->tester->getDisplay());
}
public function testConfirmationFallsBackToIdentifierWhenTenantHasNoDomains(): void
{
$tenant = $this->givenTenant(null);
$this->tenantService->expects($this->once())->method('destroy')->with($tenant);
$this->tester->setInputs(['acme']);
$status = $this->tester->execute(['identifier' => 'acme'], ['interactive' => true]);
$this->assertSame(Command::SUCCESS, $status);
$this->assertStringContainsString("Type 'acme' to confirm deletion", $this->tester->getDisplay());
}
public function testCancelsWhenRunNonInteractivelyWithoutForce(): void
{
$this->givenTenant();
$this->tenantService->expects($this->never())->method('destroy');
$status = $this->tester->execute(['identifier' => 'acme'], ['interactive' => false]);
$this->assertSame(Command::FAILURE, $status);
}
public function testFailsWhenDestroyThrows(): void
{
$this->givenTenant();
$this->tenantService
->method('destroy')
->willThrowException(new \RuntimeException('datastore unavailable'));
$status = $this->tester->execute(['identifier' => 'acme', '--force' => true]);
$this->assertSame(Command::FAILURE, $status);
$this->assertStringContainsString('datastore unavailable', $this->tester->getDisplay());
}
}
@@ -0,0 +1,93 @@
<?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());
}
}
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace KTXT\Unit\Service;
use KTXC\Models\Tenant\DomainCollection;
use KTXC\Models\Tenant\TenantObject;
use KTXC\Service\TenantService;
use KTXC\Stores\TenantStore;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class TenantServiceTest extends TestCase
{
private TenantStore&MockObject $store;
private TenantService $service;
protected function setUp(): void
{
$this->store = $this->createMock(TenantStore::class);
$this->service = new TenantService($this->store);
}
public function testDepositRejectsTenantWithoutDomains(): void
{
$this->store->expects($this->never())->method('deposit');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('at least one domain');
$this->service->deposit(new TenantObject());
}
public function testDepositRejectsTenantWithEmptyDomainCollection(): void
{
$tenant = (new TenantObject())->setDomains(new DomainCollection([]));
$this->store->expects($this->never())->method('deposit');
$this->expectException(\InvalidArgumentException::class);
$this->service->deposit($tenant);
}
public function testDepositPassesTenantWithDomainsToStore(): void
{
$tenant = (new TenantObject())
->setIdentifier('acme')
->setDomains(new DomainCollection(['acme.test']));
$this->store->expects($this->once())->method('deposit')->with($tenant)->willReturn($tenant);
$this->assertSame($tenant, $this->service->deposit($tenant));
}
public function testListDelegatesToStore(): void
{
$tenants = ['a' => new TenantObject()];
$this->store->expects($this->once())->method('list')->willReturn($tenants);
$this->assertSame($tenants, $this->service->list());
}
public function testDestroyDelegatesToStore(): void
{
$tenant = new TenantObject();
$this->store->expects($this->once())->method('destroy')->with($tenant);
$this->service->destroy($tenant);
}
}