Strengthen settings gateway tests
This commit is contained in:
@@ -8,44 +8,29 @@ use MintyPHP\Repository\Settings\SettingRepositoryInterface;
|
||||
use MintyPHP\Repository\Tenant\TenantRepositoryInterface;
|
||||
use MintyPHP\Service\Settings\SettingsDefaultsGateway;
|
||||
use MintyPHP\Service\Settings\SettingsMetadataGateway;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SettingsDefaultsGatewayTest extends TestCase
|
||||
{
|
||||
public function testSetDefaultTenantIdRejectsUnknownTenant(): void
|
||||
#[DataProvider('unknownReferenceProvider')]
|
||||
public function testSettersRejectUnknownReferences(string $setter, int $id): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->never())->method('set');
|
||||
|
||||
$tenantRepository = $this->createMock(TenantRepositoryInterface::class);
|
||||
$tenantRepository->expects($this->once())->method('find')->with(99)->willReturn(null);
|
||||
|
||||
$gateway = $this->newGateway($settings, $tenantRepository);
|
||||
$this->assertFalse($gateway->setDefaultTenantId(99));
|
||||
}
|
||||
|
||||
public function testSetDefaultRoleIdRejectsUnknownRole(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->never())->method('set');
|
||||
|
||||
$roleRepository = $this->createMock(RoleRepositoryInterface::class);
|
||||
$roleRepository->expects($this->once())->method('find')->with(55)->willReturn(null);
|
||||
|
||||
$gateway = $this->newGateway($settings, null, $roleRepository);
|
||||
$this->assertFalse($gateway->setDefaultRoleId(55));
|
||||
}
|
||||
|
||||
public function testSetDefaultDepartmentIdRejectsUnknownDepartment(): void
|
||||
{
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->expects($this->never())->method('set');
|
||||
|
||||
$departmentRepository = $this->createMock(DepartmentRepositoryInterface::class);
|
||||
$departmentRepository->expects($this->once())->method('find')->with(77)->willReturn(null);
|
||||
$gateway = $this->newGateway($settings, $tenantRepository, $roleRepository, $departmentRepository);
|
||||
|
||||
$gateway = $this->newGateway($settings, null, null, $departmentRepository);
|
||||
$this->assertFalse($gateway->setDefaultDepartmentId(77));
|
||||
match ($setter) {
|
||||
'setDefaultTenantId' => $tenantRepository->expects($this->once())->method('find')->with($id)->willReturn(null),
|
||||
'setDefaultRoleId' => $roleRepository->expects($this->once())->method('find')->with($id)->willReturn(null),
|
||||
'setDefaultDepartmentId' => $departmentRepository->expects($this->once())->method('find')->with($id)->willReturn(null),
|
||||
};
|
||||
|
||||
$this->assertFalse($gateway->$setter($id));
|
||||
}
|
||||
|
||||
public function testSettersPersistValidIds(): void
|
||||
@@ -81,6 +66,23 @@ class SettingsDefaultsGatewayTest extends TestCase
|
||||
$this->assertTrue($gateway->setDefaultDepartmentId(null));
|
||||
}
|
||||
|
||||
#[DataProvider('getterNormalizationProvider')]
|
||||
public function testGettersNormalizeNonPositiveValuesToNull(
|
||||
string $storedKey,
|
||||
string $getter,
|
||||
?string $storedValue,
|
||||
?int $expected
|
||||
): void {
|
||||
$settings = $this->createMock(SettingRepositoryInterface::class);
|
||||
$settings->method('getValue')->willReturnCallback(
|
||||
static fn (string $key): ?string => $key === $storedKey ? $storedValue : null
|
||||
);
|
||||
|
||||
$gateway = $this->newGateway($settings);
|
||||
|
||||
$this->assertSame($expected, $gateway->$getter());
|
||||
}
|
||||
|
||||
private function newGateway(
|
||||
SettingRepositoryInterface $settingRepository,
|
||||
?TenantRepositoryInterface $tenantRepository = null,
|
||||
@@ -98,4 +100,23 @@ class SettingsDefaultsGatewayTest extends TestCase
|
||||
$departmentRepository
|
||||
);
|
||||
}
|
||||
|
||||
public static function unknownReferenceProvider(): array
|
||||
{
|
||||
return [
|
||||
'unknown tenant' => ['setDefaultTenantId', 99],
|
||||
'unknown role' => ['setDefaultRoleId', 55],
|
||||
'unknown department' => ['setDefaultDepartmentId', 77],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getterNormalizationProvider(): array
|
||||
{
|
||||
return [
|
||||
'default tenant positive id' => ['default_tenant_id', 'getDefaultTenantId', '5', 5],
|
||||
'default tenant zero becomes null' => ['default_tenant_id', 'getDefaultTenantId', '0', null],
|
||||
'default role negative becomes null' => ['default_role_id', 'getDefaultRoleId', '-1', null],
|
||||
'default department missing stays null' => ['default_department_id', 'getDefaultDepartmentId', null, null],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user