Completed
Push — master ( 246059...760ebc )
by Oleksandr
03:20 queued 16s
created

overrideClientGetMethodAndSendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
3
namespace Savchenko\Bundle\OpenWeatherMapBundle\Tests\Api;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Psr7\Request;
8
use GuzzleHttp\Psr7\Response;
9
use Savchenko\Bundle\OpenWeatherMapBundle\Api\CurrentWeatherData;
10
11
class CurrentWeatherDataTest extends \PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @var Client|\PHPUnit_Framework_MockObject_MockObject
15
     */
16
    protected $client;
17
    /**
18
     * @var CurrentWeatherData
19
     */
20
    protected $currentWeatherData;
21
22
    /**
23
     * Get OpenWeatherMap response example
24
     *
25
     * @return string
26
     */
27
    protected function getWeatherData()
28
    {
29
        return json_encode([
30
            'coord' => [
31
                'lon' => -0.13,
32
                'lat' => 51.51,
33
            ],
34
            'weather' => [[
35
                'id' => 800,
36
                'main' => 'Clear',
37
                'description' => 'clear sky',
38
                'icon' => '01n',
39
            ]],
40
            'main' => [
41
                'temp' => 282.94,
42
                'pressure' => 1007,
43
                'humidity' => 71,
44
                'temp_min' => 281.15,
45
                'temp_max' => 285.25,
46
            ],
47
            'wind' => [
48
                'speed' => 3.6,
49
                'deg' => 100,
50
            ],
51
            'clouds' => [
52
                'all' => 0,
53
            ],
54
            'dt' => 1460493742,
55
            'sys' => [
56
                'country' => 'GB',
57
                'sunrise' => 1460437723,
58
                'sunset' => 1460487258,
59
            ],
60
            'id' => 2643743,
61
            'name' => 'London',
62
        ]);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected function setUp()
69
    {
70
        $this->client = self::getMockBuilder('GuzzleHttp\Client')
71
            ->setMethods(['get'])
72
            ->getMock();
73
74
        $this->client->method('get')
75
            ->willReturn(new Response(200, [], $this->getWeatherData()));
76
77
        $this->currentWeatherData = new CurrentWeatherData('abc', $this->client);
78
    }
79
80
    /**
81
     * Override Guzzle get() method to throw exception
82
     *
83
     * @param string $message
84
     * @param int $statusCode
85
     * @throws \InvalidArgumentException
86
     */
87
    protected function overrideClientGetMethodAndSendRequest(string $message, int $statusCode)
88
    {
89
        $this->client->method('get')
90
            ->willThrowException(
91
                new ClientException(
92
                    $message,
93
                    new Request('GET', 'http://api.openweathermap.org/data/2.5/weather'),
94
                    new Response($statusCode)
95
                )
96
            );
97
98
        $this->currentWeatherData = new CurrentWeatherData('abc', $this->client);
99
100
        $this->currentWeatherData->loadByCityName('London');
101
    }
102
103
    /**
104
     * @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\BadRequestException
105
     * @expectedExceptionMessage You doesn't set APPID parameter or it has incorrect value
106
     */
107
    public function testAppIdProblem()
108
    {
109
        $this->overrideClientGetMethodAndSendRequest(
110
            'Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.',
111
            401
112
        );
113
    }
114
115
    /**
116
     * @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\BadRequestException
117
     * @expectedExceptionMessage Data available only on commercial terms
118
     */
119
    public function testPaidFeatures()
120
    {
121
        $this->overrideClientGetMethodAndSendRequest('', 403);
122
    }
123
124
    /**
125
     * @expectedException \GuzzleHttp\Exception\ClientException
126
     */
127
    public function testCatchNonExpectedException()
128
    {
129
        $this->overrideClientGetMethodAndSendRequest('', 404);
130
    }
131
132
    public function testLoadByCityNameWithoutCountry()
133
    {
134
        $response = $this->currentWeatherData->loadByCityName('London');
135
136
        self::assertEquals('London', $response->name);
137
    }
138
139
    public function testLoadByCityNameWithCorrectCountryCode()
140
    {
141
        $response = $this->currentWeatherData->loadByCityName('London', 'GB');
142
143
        self::assertEquals('London', $response->name);
144
    }
145
146
    /**
147
     * @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\InvalidCountryCodeException
148
     * @expectedExceptionMessage You should provide ISO 3166-1 alpha-2 country code
149
     */
150
    public function testLoadByCityNameWithIncorrectCountryCode()
151
    {
152
        $this->currentWeatherData->loadByCityName('London', 'UKR');
153
    }
154
155
    public function testLoadByCityId()
156
    {
157
        $response = $this->currentWeatherData->loadByCityId('2643743');
158
159
        self::assertEquals('London', $response->name);
160
    }
161
162
    public function testLoadByGeographicCoordinates()
163
    {
164
        $response = $this->currentWeatherData->loadByGeographicCoordinates(-0.13, 51.51);
165
166
        self::assertEquals('London', $response->name);
167
    }
168
169
    public function testLoadByZipCodeWithoutCountry()
170
    {
171
        $response = $this->currentWeatherData->loadByZipCode('EC1A1AH');
172
173
        self::assertEquals('London', $response->name);
174
    }
175
176
    public function testLoadByZipCodeWithCorrectCountryCode()
177
    {
178
        $response = $this->currentWeatherData->loadByZipCode('EC1A1AH', 'GB');
179
180
        self::assertEquals('London', $response->name);
181
    }
182
183
    /**
184
     * @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\InvalidCountryCodeException
185
     * @expectedExceptionMessage You should provide ISO 3166-1 alpha-2 country code
186
     */
187
    public function testLoadByZipCodeWithIncorrectCountryCode()
188
    {
189
        $this->currentWeatherData->loadByZipCode('EC1A1AH', 'GBA');
190
    }
191
}
192