Skip to content

Commit 6510fc8

Browse files
committed
feat: added tests for cache and logger usage assertion
1 parent edc1971 commit 6510fc8

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tests/AbstractEndpointTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Test;
4+
5+
use Nyholm\Psr7\Response;
6+
use ProgrammatorDev\OpenWeatherMap\Endpoint\AbstractEndpoint;
7+
use ProgrammatorDev\OpenWeatherMap\OpenWeatherMap;
8+
use Psr\Cache\CacheItemPoolInterface;
9+
use Psr\Log\LoggerInterface;
10+
11+
class AbstractEndpointTest extends AbstractTest
12+
{
13+
public function testAbstractEndpointWithCache()
14+
{
15+
$this->mockHttpClient->addResponse(
16+
new Response(body: '[]')
17+
);
18+
19+
$cache = $this->createMock(CacheItemPoolInterface::class);
20+
$cache->expects($this->once())->method('save');
21+
22+
$api = $this->givenApi();
23+
$api->getConfig()->setCache($cache);
24+
25+
$this->mockSendRequest($api);
26+
}
27+
28+
public function testAbstractEndpointWithLogger()
29+
{
30+
$this->mockHttpClient->addResponse(
31+
new Response(body: '[]')
32+
);
33+
34+
$logger = $this->createMock(LoggerInterface::class);
35+
$logger->expects($this->atLeastOnce())->method('info');
36+
37+
$api = $this->givenApi();
38+
$api->getConfig()->setLogger($logger);
39+
40+
$this->mockSendRequest($api);
41+
}
42+
43+
private function mockSendRequest(OpenWeatherMap $api): void
44+
{
45+
// Using ReflectionClass to be able to call the *protected* sendRequest method
46+
// (otherwise it would not be possible)
47+
$endpoint = new AbstractEndpoint($api);
48+
$reflectionClass = new \ReflectionClass($endpoint);
49+
$sendRequest = $reflectionClass->getMethod('sendRequest');
50+
$sendRequest->invokeArgs($endpoint, ['GET', 'https://mock.test']);
51+
}
52+
}

tests/GeocodingEndpointTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class GeocodingEndpointTest extends AbstractTest
1616
{
1717
// --- BY LOCATION NAME ---
18+
1819
public function testGeocodingGetByLocationName()
1920
{
2021
$this->mockHttpClient->addResponse(

0 commit comments

Comments
 (0)