createMock(SettingRepositoryInterface::class); $settings->expects($this->never())->method('set'); $crypto = $this->createMock(SettingsCryptoGatewayInterface::class); $gateway = new SettingsMicrosoftGateway(new SettingsMetadataGateway($settings), $crypto); $this->assertFalse($gateway->setMicrosoftAuthority('http://login.microsoftonline.com/common/v2.0')); } public function testGetMicrosoftSharedClientSecretReturnsNullWhenDecryptFails(): void { $settings = $this->createMock(SettingRepositoryInterface::class); $settings->method('getValue')->willReturn('encrypted'); $crypto = $this->createMock(SettingsCryptoGatewayInterface::class); $crypto->method('decryptString')->willThrowException(new \RuntimeException('bad secret')); $gateway = new SettingsMicrosoftGateway(new SettingsMetadataGateway($settings), $crypto); $this->assertNull($gateway->getMicrosoftSharedClientSecret()); } public function testSetMicrosoftSharedClientSecretEncryptsAndPersists(): void { $settings = $this->createMock(SettingRepositoryInterface::class); $settings->expects($this->once()) ->method('set') ->with('microsoft_shared_client_secret_enc', 'enc-value', 'setting.microsoft_shared_client_secret_enc') ->willReturn(true); $crypto = $this->createMock(SettingsCryptoGatewayInterface::class); $crypto->expects($this->once()) ->method('encryptString') ->with('secret-value') ->willReturn('enc-value'); $gateway = new SettingsMicrosoftGateway(new SettingsMetadataGateway($settings), $crypto); $this->assertTrue($gateway->setMicrosoftSharedClientSecret('secret-value')); } public function testSetMicrosoftSharedClientSecretReturnsFalseWhenEncryptionFails(): void { $settings = $this->createMock(SettingRepositoryInterface::class); $settings->expects($this->never())->method('set'); $crypto = $this->createMock(SettingsCryptoGatewayInterface::class); $crypto->expects($this->once()) ->method('encryptString') ->with('secret-value') ->willThrowException(new \RuntimeException('encryption failed')); $gateway = new SettingsMicrosoftGateway(new SettingsMetadataGateway($settings), $crypto); $this->assertFalse($gateway->setMicrosoftSharedClientSecret('secret-value')); } public function testSetMicrosoftSharedClientSecretClearsSettingWhenEmpty(): void { $settings = $this->createMock(SettingRepositoryInterface::class); $settings->expects($this->once()) ->method('set') ->with('microsoft_shared_client_secret_enc', null, 'setting.microsoft_shared_client_secret_enc') ->willReturn(true); $crypto = $this->createMock(SettingsCryptoGatewayInterface::class); $crypto->expects($this->never())->method('encryptString'); $gateway = new SettingsMicrosoftGateway(new SettingsMetadataGateway($settings), $crypto); $this->assertTrue($gateway->setMicrosoftSharedClientSecret(' ')); } public function testGetMicrosoftAuthorityFallsBackAndTrimsTrailingSlash(): void { $settings = $this->createMock(SettingRepositoryInterface::class); $settings->method('getValue')->willReturnOnConsecutiveCalls(null, 'https://login.microsoftonline.com/common/v2.0/'); $crypto = $this->createMock(SettingsCryptoGatewayInterface::class); $gateway = new SettingsMicrosoftGateway(new SettingsMetadataGateway($settings), $crypto); $this->assertSame('https://login.microsoftonline.com/common/v2.0', $gateway->getMicrosoftAuthority()); $this->assertSame('https://login.microsoftonline.com/common/v2.0', $gateway->getMicrosoftAuthority()); } }