OpenWeatherMapTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2014 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPv3 (or at your option any later version).
  9. * @package OpenWeatherMap-PHP-Api
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. namespace Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap;
  15. use \Cmfcmf\OpenWeatherMap;
  16. use Cmfcmf\OpenWeatherMap\Exception;
  17. use Cmfcmf\OpenWeatherMap\Tests\TestFetcher;
  18. class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var string
  22. */
  23. protected $apiKey;
  24. /**
  25. * @var OpenWeatherMap
  26. */
  27. protected $owm;
  28. /**
  29. * @var OpenWeatherMap
  30. */
  31. protected $openWeather;
  32. /**
  33. * @var ExampleCacheTest
  34. */
  35. protected $cache;
  36. protected function setUp()
  37. {
  38. $ini = parse_ini_file(__DIR__.'/../Examples/ApiKey.ini');
  39. $myApiKey = $ini['api_key'];
  40. $this->apiKey = $myApiKey;
  41. $this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600);
  42. $this->openWeather = new OpenWeatherMap($this->apiKey, null, false, 600);
  43. $this->cache = new ExampleCacheTest();
  44. }
  45. protected function tearDown()
  46. {
  47. $fileList = glob(__DIR__.'/temps/OpenWeatherMapPHPAPI/*');
  48. foreach ($fileList as $fileName) {
  49. @unlink($fileName);
  50. }
  51. @rmdir(__DIR__.'/temps/OpenWeatherMapPHPAPI');
  52. @rmdir(__DIR__.'/temps');
  53. }
  54. public function testApiKeyIsEmpty()
  55. {
  56. $expectApiKey = '';
  57. $weather = new OpenWeatherMap($expectApiKey, null, false, 600);
  58. $apiKey = $weather->getApiKey();
  59. $this->assertSame($expectApiKey, $apiKey);
  60. }
  61. public function testApiKeyNotNull()
  62. {
  63. $weather = $this->owm;
  64. $apiKey = $weather->getApiKey();
  65. $this->assertSame($this->apiKey, $apiKey);
  66. }
  67. public function testSecondIsZero()
  68. {
  69. $weather = new OpenWeatherMap($this->apiKey, null, false, 0);
  70. $apiKey = $weather->getApiKey();
  71. $this->assertSame($this->apiKey, $apiKey);
  72. }
  73. public function testSetApiKey()
  74. {
  75. $weather = $this->owm;
  76. $weather->setApiKey($this->apiKey);
  77. $apiKey = $weather->getApiKey();
  78. $this->assertSame($this->apiKey, $apiKey);
  79. }
  80. public function testGetApiKey()
  81. {
  82. $weather = $this->owm;
  83. $apiKey = $weather->getApiKey();
  84. $this->assertSame($this->apiKey, $apiKey);
  85. }
  86. public function testGetWeather()
  87. {
  88. $currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', '');
  89. $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather);
  90. }
  91. public function testGetWeatherGroup()
  92. {
  93. $currentWeather = $this->owm->getWeatherGroup(array('2950159'), 'imperial', 'en', '');
  94. $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
  95. $currentWeather = $this->owm->getWeatherGroup('2950159', 'imperial', 'en', '');
  96. $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
  97. }
  98. public function testGetWeatherForecast()
  99. {
  100. $days = 1;
  101. $defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
  102. $days = 16;
  103. $maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
  104. $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay);
  105. $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
  106. }
  107. public function testGetCurrentUVIndex()
  108. {
  109. $owm = $this->openWeather;
  110. $result = $owm->getCurrentUVIndex(40.7, -74.2);
  111. $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
  112. }
  113. public function testGetUVIndex()
  114. {
  115. $owm = $this->openWeather;
  116. $precisions = array('year', 'month', 'day', 'hour', 'minute', 'second');
  117. foreach ($precisions as $precision) {
  118. try {
  119. $result = $owm->getUVIndex(40.7, -74.2, new \DateTime(), $precision);
  120. } catch (Exception $e) {
  121. // OWM might not actually have data for the timespan.
  122. $this->assertSame('An error occurred: not found', $e->getMessage());
  123. }
  124. $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
  125. }
  126. }
  127. public function testGetDailyWeatherForecast()
  128. {
  129. $days = 16;
  130. $dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
  131. $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast);
  132. }
  133. public function testGetWeatherHistory()
  134. {
  135. $this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.');
  136. }
  137. public function testWasCached()
  138. {
  139. $weather = $this->owm;
  140. $result = $weather->wasCached();
  141. $this->assertFalse($result);
  142. }
  143. public function testCached()
  144. {
  145. $cache = $this->cache;
  146. $cache->setTempPath(__DIR__.'/temps');
  147. $weather = new OpenWeatherMap($this->apiKey, new TestFetcher(), $cache, 600);
  148. $currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
  149. $cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
  150. $this->assertInternalType('string', $currWeatherData);
  151. $this->assertInternalType('string', $cachedWeatherData);
  152. }
  153. public function testBuildQueryUrlParameter()
  154. {
  155. $weather = $this->owm;
  156. $queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', '');
  157. $queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', '');
  158. $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray);
  159. $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray);
  160. }
  161. public function testAbstractCache()
  162. {
  163. /** @var OpenWeatherMap\AbstractCache $sut */
  164. $sut = $this->getMockForAbstractClass('\Cmfcmf\OpenWeatherMap\AbstractCache');
  165. $this->assertNull($sut->setSeconds(10));
  166. }
  167. }