|
@@ -0,0 +1,147 @@
|
|
|
|
+<?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'));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|