From 8d01a91496faad9690dedd948947528179417aad Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Fri, 23 Jan 2026 18:37:44 +0100 Subject: [PATCH] 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. --- tests/IPinfoTest.php | 67 +++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/tests/IPinfoTest.php b/tests/IPinfoTest.php index 4258d32..0cd271f 100644 --- a/tests/IPinfoTest.php +++ b/tests/IPinfoTest.php @@ -412,32 +412,59 @@ public function testIPv6NotationsCaching() public function testResproxy() { - $tok = getenv('IPINFO_TOKEN'); - if (!$tok) { - $this->markTestSkipped('IPINFO_TOKEN env var required'); - } - - $h = new IPinfo($tok); - $ip = '175.107.211.204'; + $h = new IPinfo("test_token"); + $ip = "175.107.211.204"; + + // Create a mock stream for the response body + $mockStream = $this->createMock(\Psr\Http\Message\StreamInterface::class); + $mockStream->method("__toString")->willReturn(json_encode([ + "ip" => "175.107.211.204", + "last_seen" => "2025-01-20", + "percent_days_seen" => 0.85, + "service" => "example_service" + ])); + + $mockResponse = $this->createMock(\GuzzleHttp\Psr7\Response::class); + $mockResponse->method("getStatusCode")->willReturn(200); + $mockResponse->method("getBody")->willReturn($mockStream); + + $mockClient = $this->createMock(\GuzzleHttp\Client::class); + $mockClient->method("request")->willReturn($mockResponse); + + // Use reflection to replace the http_client + $reflectionClass = new \ReflectionClass($h); + $reflectionProperty = $reflectionClass->getProperty("http_client"); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($h, $mockClient); - // test multiple times for cache hits - for ($i = 0; $i < 5; $i++) { - $res = $h->getResproxy($ip); - $this->assertEquals($res['ip'], $ip); - $this->assertNotNull($res['last_seen']); - $this->assertNotNull($res['percent_days_seen']); - $this->assertNotNull($res['service']); - } + $res = $h->getResproxy($ip); + $this->assertEquals($res["ip"], $ip); + $this->assertEquals($res["last_seen"], "2025-01-20"); + $this->assertEquals($res["percent_days_seen"], 0.85); + $this->assertEquals($res["service"], "example_service"); } public function testResproxyEmpty() { - $tok = getenv("IPINFO_TOKEN"); - if (!$tok) { - $this->markTestSkipped("IPINFO_TOKEN env var required"); - } + $h = new IPinfo("test_token"); + + // Create a mock stream for the response body + $mockStream = $this->createMock(\Psr\Http\Message\StreamInterface::class); + $mockStream->method("__toString")->willReturn("{}"); + + $mockResponse = $this->createMock(\GuzzleHttp\Psr7\Response::class); + $mockResponse->method("getStatusCode")->willReturn(200); + $mockResponse->method("getBody")->willReturn($mockStream); + + $mockClient = $this->createMock(\GuzzleHttp\Client::class); + $mockClient->method("request")->willReturn($mockResponse); + + // Use reflection to replace the http_client + $reflectionClass = new \ReflectionClass($h); + $reflectionProperty = $reflectionClass->getProperty("http_client"); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($h, $mockClient); - $h = new IPinfo($tok); $res = $h->getResproxy("8.8.8.8"); $this->assertEquals($res, []); }