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()); } }