65c1b5fb75
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
191 lines
6.4 KiB
PHP
191 lines
6.4 KiB
PHP
<?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 KTXC\Stores\UserAccountsStore;
|
|
use KTXC\Stores\UserRolesStore;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\NullLogger;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
#[AllowMockObjectsWithoutExpectations]
|
|
class TenantCreateCommandTest extends TestCase
|
|
{
|
|
private TenantService&MockObject $tenantService;
|
|
private UserRolesStore $rolesStore;
|
|
private UserAccountsStore $userStore;
|
|
private CommandTester $tester;
|
|
private ?TenantObject $deposited = null;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->tenantService = $this->createMock(TenantService::class);
|
|
$this->rolesStore = $this->createStub(UserRolesStore::class);
|
|
$this->rolesStore->method('createRole')->willReturn(['rid' => 'admin']);
|
|
$this->userStore = $this->createStub(UserAccountsStore::class);
|
|
$this->userStore->method('createUser')->willReturn(['uid' => 'admin']);
|
|
$this->tester = new CommandTester(
|
|
new TenantCreateCommand(
|
|
$this->tenantService,
|
|
$this->rolesStore,
|
|
$this->userStore,
|
|
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());
|
|
}
|
|
}
|