Skip to content

Commit 8d01a91

Browse files
committed
Mock resproxy API responses in tests
Use PHPUnit mocking with StreamInterface to mock the resproxy API responses instead of relying on live data that may change.
1 parent ea60986 commit 8d01a91

1 file changed

Lines changed: 47 additions & 20 deletions

File tree

tests/IPinfoTest.php

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -412,32 +412,59 @@ public function testIPv6NotationsCaching()
412412

413413
public function testResproxy()
414414
{
415-
$tok = getenv('IPINFO_TOKEN');
416-
if (!$tok) {
417-
$this->markTestSkipped('IPINFO_TOKEN env var required');
418-
}
419-
420-
$h = new IPinfo($tok);
421-
$ip = '175.107.211.204';
415+
$h = new IPinfo("test_token");
416+
$ip = "175.107.211.204";
417+
418+
// Create a mock stream for the response body
419+
$mockStream = $this->createMock(\Psr\Http\Message\StreamInterface::class);
420+
$mockStream->method("__toString")->willReturn(json_encode([
421+
"ip" => "175.107.211.204",
422+
"last_seen" => "2025-01-20",
423+
"percent_days_seen" => 0.85,
424+
"service" => "example_service"
425+
]));
426+
427+
$mockResponse = $this->createMock(\GuzzleHttp\Psr7\Response::class);
428+
$mockResponse->method("getStatusCode")->willReturn(200);
429+
$mockResponse->method("getBody")->willReturn($mockStream);
430+
431+
$mockClient = $this->createMock(\GuzzleHttp\Client::class);
432+
$mockClient->method("request")->willReturn($mockResponse);
433+
434+
// Use reflection to replace the http_client
435+
$reflectionClass = new \ReflectionClass($h);
436+
$reflectionProperty = $reflectionClass->getProperty("http_client");
437+
$reflectionProperty->setAccessible(true);
438+
$reflectionProperty->setValue($h, $mockClient);
422439

423-
// test multiple times for cache hits
424-
for ($i = 0; $i < 5; $i++) {
425-
$res = $h->getResproxy($ip);
426-
$this->assertEquals($res['ip'], $ip);
427-
$this->assertNotNull($res['last_seen']);
428-
$this->assertNotNull($res['percent_days_seen']);
429-
$this->assertNotNull($res['service']);
430-
}
440+
$res = $h->getResproxy($ip);
441+
$this->assertEquals($res["ip"], $ip);
442+
$this->assertEquals($res["last_seen"], "2025-01-20");
443+
$this->assertEquals($res["percent_days_seen"], 0.85);
444+
$this->assertEquals($res["service"], "example_service");
431445
}
432446

433447
public function testResproxyEmpty()
434448
{
435-
$tok = getenv("IPINFO_TOKEN");
436-
if (!$tok) {
437-
$this->markTestSkipped("IPINFO_TOKEN env var required");
438-
}
449+
$h = new IPinfo("test_token");
450+
451+
// Create a mock stream for the response body
452+
$mockStream = $this->createMock(\Psr\Http\Message\StreamInterface::class);
453+
$mockStream->method("__toString")->willReturn("{}");
454+
455+
$mockResponse = $this->createMock(\GuzzleHttp\Psr7\Response::class);
456+
$mockResponse->method("getStatusCode")->willReturn(200);
457+
$mockResponse->method("getBody")->willReturn($mockStream);
458+
459+
$mockClient = $this->createMock(\GuzzleHttp\Client::class);
460+
$mockClient->method("request")->willReturn($mockResponse);
461+
462+
// Use reflection to replace the http_client
463+
$reflectionClass = new \ReflectionClass($h);
464+
$reflectionProperty = $reflectionClass->getProperty("http_client");
465+
$reflectionProperty->setAccessible(true);
466+
$reflectionProperty->setValue($h, $mockClient);
439467

440-
$h = new IPinfo($tok);
441468
$res = $h->getResproxy("8.8.8.8");
442469
$this->assertEquals($res, []);
443470
}

0 commit comments

Comments
 (0)