5
5
namespace App \Infrastructure \Http ;
6
6
7
7
use App \Domain \CannotGetCurrentTemperature ;
8
- use GuzzleHttp \Client ;
9
- use GuzzleHttp \Exception \RequestException ;
10
- use GuzzleHttp \Handler \MockHandler ;
11
- use GuzzleHttp \HandlerStack ;
12
- use GuzzleHttp \Promise \PromiseInterface ;
13
- use GuzzleHttp \Psr7 \Request ;
14
- use GuzzleHttp \Psr7 \Response ;
15
- use PHPUnit \Framework \TestCase ;
16
- use Prophecy \PhpUnit \ProphecyTrait ;
17
- use Psr \Http \Message \ResponseInterface ;
18
- use Throwable ;
19
-
20
- class RestWeatherProviderTest extends TestCase
21
- {
22
- use ProphecyTrait;
8
+ use Symfony \Bundle \FrameworkBundle \Test \KernelTestCase ;
9
+ use WireMock \Client \WireMock ;
23
10
24
- private MockHandler $ guzzleMockHandler ;
25
- private string $ apiKey ;
11
+ class RestWeatherProviderTest extends KernelTestCase
12
+ {
26
13
private RestWeatherProvider $ provider ;
14
+ private WireMock $ wireMock ;
15
+ private string $ currentConditionUri ;
27
16
28
17
protected function setUp (): void
29
18
{
30
- $ this -> guzzleMockHandler = new MockHandler ();
31
- $ guzzleClient = new Client ([ ' handler ' => new HandlerStack ( $ this -> guzzleMockHandler )]);
32
-
33
- $ this -> apiKey = ' key ' ;
34
- $ this ->provider = new RestWeatherProvider (
35
- $ guzzleClient ,
36
- $ this -> apiKey ,
37
- new CurrentConditionDeserializer ()
38
- ) ;
19
+ self :: bootKernel ();
20
+
21
+ $ this -> provider = self :: $ container -> get (RestWeatherProvider::class);
22
+
23
+ $ this ->wireMock = self :: $ container -> get (WireMock::class);
24
+ self :: assertTrue ( $ this -> wireMock -> isAlive (), ' Wiremock should be alive ' );
25
+
26
+ $ accuweatherApiKey = self :: $ container -> getParameter ( ' accuweather.apikey ' );
27
+ $ this -> currentConditionUri = ' /currentconditions/v1/623?apikey= ' . $ accuweatherApiKey ;
39
28
}
40
29
41
30
public function testTemperatureIsExtractedFrom200Response ()
42
31
{
43
32
// Given (Arrange)
44
- $ this ->givenAccuWeatherResponseIs (new Response (200 , ['Content-Type ' => 'application/json ' ], <<<EOD
33
+ $ this ->wireMock ->stubFor (WireMock::get (WireMock::urlEqualTo ($ this ->currentConditionUri ))
34
+ ->willReturn (WireMock::aResponse ()
35
+ ->withHeader ('Content-Type ' , 'application/json ' )
36
+ ->withBody (<<<EOD
45
37
[{
46
38
"LocalObservationDateTime": "2020-10-17T17:50:00+02:00",
47
39
"EpochTime": 1602949800,
@@ -65,23 +57,21 @@ public function testTemperatureIsExtractedFrom200Response()
65
57
"MobileLink": "http://m.accuweather.com/en/fr/paris/623/current-weather/623?lang=en-us",
66
58
"Link": "http://www.accuweather.com/en/fr/paris/623/current-weather/623?lang=en-us"
67
59
}]
68
- EOD ));
60
+ EOD ))) ;
69
61
70
62
// When (Act)
71
63
$ result = $ this ->provider ->getCurrentCelciusTemperature ();
72
64
73
65
// Then (Assert)
74
66
self ::assertSame (37.2 , $ result );
75
-
76
- $ this ->thenRequestSentShouldHaveBeen ('GET ' , 'currentconditions/v1/623?apikey= ' .$ this ->apiKey );
77
67
}
78
68
79
69
public function test404ResponseIsConvertedToDomainException ()
80
70
{
81
71
// Given (Arrange)
82
- $ this ->givenAccuWeatherResponseIs (RequestException:: create (
83
- new Request ( ' GET ' , ' uri ' ),
84
- new Response (404 )
72
+ $ this ->wireMock -> stubFor (WireMock:: get (WireMock:: urlEqualTo ( $ this -> currentConditionUri ))
73
+ -> willReturn (WireMock:: aResponse ()
74
+ -> withStatus (404 )
85
75
));
86
76
87
77
// Then (Assert)
@@ -94,29 +84,15 @@ public function test404ResponseIsConvertedToDomainException()
94
84
public function testInvalidBodyIsConvertedToDomainException ()
95
85
{
96
86
// Given (Arrange)
97
- $ this ->givenAccuWeatherResponseIs (
98
- new Response (200 , ['Content-Type ' => 'application/json ' ], 'invalid body ' )
99
- );
87
+ $ this ->wireMock ->stubFor (WireMock::get (WireMock::urlEqualTo ($ this ->currentConditionUri ))
88
+ ->willReturn (WireMock::aResponse ()
89
+ ->withHeader ('Content-Type ' , 'application/json ' )
90
+ ->withBody ('invalid body ' )));
100
91
101
92
// Then (Assert)
102
93
$ this ->expectException (CannotGetCurrentTemperature::class);
103
94
104
95
// When (Act)
105
96
$ this ->provider ->getCurrentCelciusTemperature ();
106
97
}
107
-
108
- /**
109
- * @param ResponseInterface|Throwable|PromiseInterface|callable $response
110
- */
111
- private function givenAccuWeatherResponseIs ($ response )
112
- {
113
- $ this ->guzzleMockHandler ->append ($ response );
114
- }
115
-
116
- private function thenRequestSentShouldHaveBeen (string $ method , string $ uri )
117
- {
118
- $ requestSent = $ this ->guzzleMockHandler ->getLastRequest ();
119
- self ::assertSame ($ method , $ requestSent ->getMethod ());
120
- self ::assertSame ($ uri , (string ) $ requestSent ->getUri ());
121
- }
122
98
}
0 commit comments