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
- * @package Weather
- */
- class WeatherTest extends TestCase
- {
- /**
- * test $type
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- 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.');
- }
- /**
- * test $format
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- 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()
- {
- // json
- $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('深圳'));
- // xml
- $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')); // 当调用 get 方法时会抛出异常。
- $w = \Mockery::mock(Weather::class, ['mock-key'])->makePartial();
- $w->allows()->getHttpClient()->andReturn($client);
- // 接着需要断言调用时会产生异常。
- $this->expectException(HttpException::class);
- $this->expectExceptionMessage('request timeout');
- $w->getWeather('深圳');
- }
- /**
- * 测试是否获取到了client对象
- */
- public function testGetHttpClient()
- {
- $w = new Weather('mock-key');
- // 断言返回结果为 GuzzleHttp\ClientInterface 实例
- $this->assertInstanceOf(ClientInterface::class, $w->getHttpClient());
- }
- /**
- * 测试是否参数设置成功
- */
- public function testSetGuzzleOptions()
- {
- $w = new Weather('mock-key');
- // 设置参数前,timeout 为 null
- $this->assertNull($w->getHttpClient()->getConfig('timeout'));
- // 设置参数
- $w->setGuzzleOptions(['timeout' => 5000]);
- // 设置参数后,timeout 为 5000
- $this->assertSame(5000, $w->getHttpClient()->getConfig('timeout'));
- }
- }
|