123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace Chuckpac\Weather\Tests;
- use Chuckpac\Weather\Weather;
- use Chuckpac\Weather\Exceptions\InvalidArgumentException;
- use Chuckpac\Weather\Exceptions\HttpException;
- use PHPUnit\Framework\TestCase;
- use GuzzleHttp\Psr7\Response;
- use GuzzleHttp\Client;
- use GuzzleHttp\ClientInterface;
- use Mockery\Matcher\AnyArgs;
- class WeatherTest extends TestCase
- {
-
- public function testGetWeatherWithInvalidType(){
- $w = new Weather('key123');
-
- $this->expectException(InvalidArgumentException::class);
-
- $this->expectExceptionMessage('Invalid type value(base/all): foo');
- $w->getWeather('江苏','foo');
- $this->fail('Failed to assert getWeather throw exception with invalid argument.');
- }
-
- public function testGetWeatherWithInvalidFormat(){
- $w = new Weather('key123');
-
- $this->expectException(InvalidArgumentException::class);
-
- $this->expectExceptionMessage('Invalid response format: array');
- $w->getWeather('江苏','json','array');
- $this->fail('Failed to assert getWeather throw exception with invalid argument.');
- }
-
- public function testGetWeather()
- {
-
- $response = new Response(200, [], '{"success": true}');
- $client = \Mockery::mock(Client::class);
- $client->allows()->get('https://restapi.amap.com/v3/weather/weatherInfo', [
- 'query' => [
- 'key' => 'mock-key',
- 'city' => '深圳',
- 'output' => 'json',
- 'extensions' => 'base',
- ],
- ])->andReturn($response);
- $w = \Mockery::mock(Weather::class, ['mock-key'])->makePartial();
- $w->allows()->getHttpClient()->andReturn($client);
- $this->assertSame(['success' => true], $w->getWeather('深圳'));
-
- $response = new Response(200, [], '<hello>content</hello>');
- $client = \Mockery::mock(Client::class);
- $client->allows()->get('https://restapi.amap.com/v3/weather/weatherInfo', [
- 'query' => [
- 'key' => 'mock-key',
- 'city' => '深圳',
- 'extensions' => 'all',
- 'output' => 'xml',
- ],
- ])->andReturn($response);
- $w = \Mockery::mock(Weather::class, ['mock-key'])->makePartial();
- $w->allows()->getHttpClient()->andReturn($client);
- $this->assertSame('<hello>content</hello>', $w->getWeather('深圳', 'all', 'xml'));
- }
-
- public function testGetWeatherWithGuzzleRuntimeException()
- {
- $client = \Mockery::mock(Client::class);
- $client->allows()
- ->get(new AnyArgs())
- ->andThrow(new \Exception('request timeout'));
- $w = \Mockery::mock(Weather::class, ['mock-key'])->makePartial();
- $w->allows()->getHttpClient()->andReturn($client);
-
- $this->expectException(HttpException::class);
- $this->expectExceptionMessage('request timeout');
- $w->getWeather('深圳');
- }
-
- public function testGetHttpClient()
- {
- $w = new Weather('mock-key');
-
- $this->assertInstanceOf(ClientInterface::class, $w->getHttpClient());
- }
-
- public function testSetGuzzleOptions()
- {
- $w = new Weather('mock-key');
-
- $this->assertNull($w->getHttpClient()->getConfig('timeout'));
-
- $w->setGuzzleOptions(['timeout' => 5000]);
-
- $this->assertSame(5000, $w->getHttpClient()->getConfig('timeout'));
- }
- }
|