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
+ }
0 commit comments