Skip to content

Commit 88a4512

Browse files
committed
Fix IDN zone lookup and update tests
1 parent 4f238e0 commit 88a4512

1 file changed

Lines changed: 41 additions & 10 deletions

File tree

tests/SynologyCloudflareDDNSAgentTest.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ protected function exitWithSynologyMsg($msg = '')
1515
$this->exitMsg = $msg;
1616
throw new ExitException("Exit called with message: $msg");
1717
}
18-
18+
1919
public function callPrivateMethod($methodName, $args = [])
2020
{
2121
$reflection = new ReflectionClass($this);
2222
$method = $reflection->getMethod($methodName);
23+
$method->setAccessible(true);
2324
return $method->invokeArgs($this, $args);
2425
}
2526
}
@@ -30,13 +31,27 @@ public function testIsValidHostname()
3031
{
3132
$mockApi = $this->createMock(CloudflareAPI::class);
3233
$mockIpify = $this->createMock(Ipify::class);
33-
34+
3435
$mockApi->method('verifyToken')->willReturn(['success' => true]);
35-
36-
$mockApi->method('getZones')->willReturn(['result' => []]);
36+
37+
$mockApi->method('getZoneByName')
38+
->willReturnCallback(function ($zoneName) {
39+
if ($zoneName === 'example.com') {
40+
return [
41+
'result' => [
42+
[
43+
'id' => 'test-zone-id',
44+
'name' => 'example.com',
45+
]
46+
]
47+
];
48+
}
49+
50+
return ['result' => []];
51+
});
3752

3853
$agent = new TestableSynologyCloudflareDDNSAgent('apikey', 'example.com', '1.2.3.4', $mockApi, $mockIpify);
39-
54+
4055
$this->assertTrue($agent->callPrivateMethod('isValidHostname', ['example.com']));
4156
$this->assertTrue($agent->callPrivateMethod('isValidHostname', ['sub.example.com']));
4257
$this->assertFalse($agent->callPrivateMethod('isValidHostname', ['-example.com']));
@@ -47,27 +62,43 @@ public function testExtractHostnames()
4762
{
4863
$mockApi = $this->createMock(CloudflareAPI::class);
4964
$mockIpify = $this->createMock(Ipify::class);
65+
5066
$mockApi->method('verifyToken')->willReturn(['success' => true]);
51-
$mockApi->method('getZones')->willReturn(['result' => []]);
67+
68+
$mockApi->method('getZoneByName')
69+
->willReturnCallback(function ($zoneName) {
70+
if ($zoneName === 'example.com') {
71+
return [
72+
'result' => [
73+
[
74+
'id' => 'test-zone-id',
75+
'name' => 'example.com',
76+
]
77+
]
78+
];
79+
}
80+
81+
return ['result' => []];
82+
});
5283

5384
$agent = new TestableSynologyCloudflareDDNSAgent('apikey', 'example.com', '1.2.3.4', $mockApi, $mockIpify);
5485

5586
$input = "example.com|sub.example.com|invalid-";
5687
$expected = ['example.com', 'sub.example.com'];
57-
88+
5889
$this->assertEquals($expected, $agent->callPrivateMethod('extractHostnames', [$input]));
5990
}
60-
91+
6192
public function testConstructorAuthFailure()
6293
{
6394
$mockApi = $this->createMock(CloudflareAPI::class);
6495
$mockIpify = $this->createMock(Ipify::class);
65-
96+
6697
$mockApi->method('verifyToken')->willReturn(['success' => false]);
6798

6899
$this->expectException(ExitException::class);
69100
$this->expectExceptionMessage("Exit called with message: " . SynologyOutput::AUTH_FAILED);
70101

71102
new TestableSynologyCloudflareDDNSAgent('apikey', 'example.com', '1.2.3.4', $mockApi, $mockIpify);
72103
}
73-
}
104+
}

0 commit comments

Comments
 (0)