WeatherTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Chuckpac\Weather\Tests;
  3. use Chuckpac\Weather\Weather;
  4. use Chuckpac\Weather\Exceptions\InvalidArgumentException;
  5. use Chuckpac\Weather\Exceptions\HttpException;
  6. use PHPUnit\Framework\TestCase;
  7. use GuzzleHttp\Psr7\Response;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\ClientInterface;
  10. use Mockery\Matcher\AnyArgs;
  11. /**
  12. * Class WeatherTest
  13. * @package Weather
  14. */
  15. class WeatherTest extends TestCase
  16. {
  17. /**
  18. * test $type
  19. * @throws \GuzzleHttp\Exception\GuzzleException
  20. */
  21. public function testGetWeatherWithInvalidType(){
  22. $w = new Weather('key123');
  23. //断言会抛出参数错误类
  24. $this->expectException(InvalidArgumentException::class);
  25. //断言会抛出此信息
  26. $this->expectExceptionMessage('Invalid type value(base/all): foo');
  27. $w->getWeather('江苏','foo');
  28. $this->fail('Failed to assert getWeather throw exception with invalid argument.');
  29. }
  30. /**
  31. * test $format
  32. * @throws \GuzzleHttp\Exception\GuzzleException
  33. */
  34. public function testGetWeatherWithInvalidFormat(){
  35. $w = new Weather('key123');
  36. //断言会抛出参数错误类
  37. $this->expectException(InvalidArgumentException::class);
  38. //断言会抛出此信息
  39. $this->expectExceptionMessage('Invalid response format: array');
  40. $w->getWeather('江苏','json','array');
  41. $this->fail('Failed to assert getWeather throw exception with invalid argument.');
  42. }
  43. /**
  44. * 模拟测试获取天气方法
  45. */
  46. public function testGetWeather()
  47. {
  48. // json
  49. $response = new Response(200, [], '{"success": true}');
  50. $client = \Mockery::mock(Client::class);
  51. $client->allows()->get('https://restapi.amap.com/v3/weather/weatherInfo', [
  52. 'query' => [
  53. 'key' => 'mock-key',
  54. 'city' => '深圳',
  55. 'output' => 'json',
  56. 'extensions' => 'base',
  57. ],
  58. ])->andReturn($response);
  59. $w = \Mockery::mock(Weather::class, ['mock-key'])->makePartial();
  60. $w->allows()->getHttpClient()->andReturn($client);
  61. $this->assertSame(['success' => true], $w->getWeather('深圳'));
  62. // xml
  63. $response = new Response(200, [], '<hello>content</hello>');
  64. $client = \Mockery::mock(Client::class);
  65. $client->allows()->get('https://restapi.amap.com/v3/weather/weatherInfo', [
  66. 'query' => [
  67. 'key' => 'mock-key',
  68. 'city' => '深圳',
  69. 'extensions' => 'all',
  70. 'output' => 'xml',
  71. ],
  72. ])->andReturn($response);
  73. $w = \Mockery::mock(Weather::class, ['mock-key'])->makePartial();
  74. $w->allows()->getHttpClient()->andReturn($client);
  75. $this->assertSame('<hello>content</hello>', $w->getWeather('深圳', 'all', 'xml'));
  76. }
  77. /**
  78. * 测试获取天气接口异常
  79. */
  80. public function testGetWeatherWithGuzzleRuntimeException()
  81. {
  82. $client = \Mockery::mock(Client::class);
  83. $client->allows()
  84. ->get(new AnyArgs()) // 由于上面的用例已经验证过参数传递,所以这里就不关心参数了。
  85. ->andThrow(new \Exception('request timeout')); // 当调用 get 方法时会抛出异常。
  86. $w = \Mockery::mock(Weather::class, ['mock-key'])->makePartial();
  87. $w->allows()->getHttpClient()->andReturn($client);
  88. // 接着需要断言调用时会产生异常。
  89. $this->expectException(HttpException::class);
  90. $this->expectExceptionMessage('request timeout');
  91. $w->getWeather('深圳');
  92. }
  93. /**
  94. * 测试是否获取到了client对象
  95. */
  96. public function testGetHttpClient()
  97. {
  98. $w = new Weather('mock-key');
  99. // 断言返回结果为 GuzzleHttp\ClientInterface 实例
  100. $this->assertInstanceOf(ClientInterface::class, $w->getHttpClient());
  101. }
  102. /**
  103. * 测试是否参数设置成功
  104. */
  105. public function testSetGuzzleOptions()
  106. {
  107. $w = new Weather('mock-key');
  108. // 设置参数前,timeout 为 null
  109. $this->assertNull($w->getHttpClient()->getConfig('timeout'));
  110. // 设置参数
  111. $w->setGuzzleOptions(['timeout' => 5000]);
  112. // 设置参数后,timeout 为 5000
  113. $this->assertSame(5000, $w->getHttpClient()->getConfig('timeout'));
  114. }
  115. }