OpenWeatherMapExceptionTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\Tests\TestFetcher;
  17. class OpenWeatherMapExceptionTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected $apiKey;
  23. /**
  24. * @var OpenWeatherMap
  25. */
  26. protected $owm;
  27. protected function setUp()
  28. {
  29. $this->apiKey = 'unicorn-rainbow';
  30. $this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600);
  31. }
  32. /**
  33. * @expectedException \InvalidArgumentException
  34. */
  35. public function testCacheException()
  36. {
  37. new OpenWeatherMap($this->apiKey, null, true, 600);
  38. }
  39. /**
  40. * @expectedException \InvalidArgumentException
  41. */
  42. public function testSecondNotNumericException()
  43. {
  44. new OpenWeatherMap($this->apiKey, null, false, 'I am not numeric');
  45. }
  46. /**
  47. * @expectedException \InvalidArgumentException
  48. */
  49. public function testGetWeatherForecastException()
  50. {
  51. $days = 20;
  52. $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
  53. }
  54. /**
  55. * @expectedException \InvalidArgumentException
  56. */
  57. public function testGetDailyWeatherForecastException()
  58. {
  59. $days = 20;
  60. $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
  61. }
  62. /**
  63. * @expectedException \Cmfcmf\OpenWeatherMap\Exception
  64. */
  65. public function testGetWeatherHistoryException()
  66. {
  67. $this->owm->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), 1, 'hour', 'imperial', 'en', '');
  68. }
  69. /**
  70. * @expectedException \Cmfcmf\OpenWeatherMap\Exception
  71. */
  72. public function testGetWeatherHistoryWithEndException()
  73. {
  74. $this->owm->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), new \DateTime('now'), 'hour', 'imperial', 'en', '');
  75. }
  76. /**
  77. * @expectedException \InvalidArgumentException
  78. */
  79. public function testGetWeatherHistoryInvalidArgumentException()
  80. {
  81. $this->owm->getWeatherHistory('Berlin', new \DateTime('now'), 1, 'wrong-type', 'imperial', 'en', '');
  82. }
  83. /**
  84. * @expectedException \InvalidArgumentException
  85. */
  86. public function testGetRawDailyForecastDataInvalidArgumentException()
  87. {
  88. $this->owm->getRawDailyForecastData('Berlin', 'imperial', 'en', '', 'xml', 20);
  89. }
  90. /**
  91. * @expectedException \InvalidArgumentException
  92. */
  93. public function testGetRawWeatherHistoryException()
  94. {
  95. $this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 1, 'wrong-type', 'imperial', 'en', '');
  96. }
  97. /**
  98. * @expectedException \InvalidArgumentException
  99. */
  100. public function testGetRawWeatherHistoryWithEndDateException()
  101. {
  102. $this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 'wrong-endOrCount', 'hour', 'imperial', 'en', '');
  103. }
  104. /**
  105. * @expectedException \InvalidArgumentException
  106. * @dataProvider uvIndexExceptionDataProvider
  107. */
  108. public function testGetRawUVIndexWithQueryErrorException($lat, $lon, $dateTime, $precision)
  109. {
  110. $this->owm->getRawUVIndexData($lat, $lon, $dateTime, $precision);
  111. }
  112. /**
  113. * @expectedException \InvalidArgumentException
  114. * @dataProvider currentUVIndexExceptionDataProvider
  115. */
  116. public function testGetRawCurrentUVIndexWithQueryErrorException($lat, $lon)
  117. {
  118. $this->owm->getRawCurrentUVIndexData($lat, $lon);
  119. }
  120. /**
  121. * @expectedException \RuntimeException
  122. */
  123. public function testGetRawUVIndexWithoutApiKey()
  124. {
  125. $this->owm->setApiKey(null);
  126. $this->owm->getRawUVIndexData(1.1, 1.1, new \DateTime());
  127. }
  128. /**
  129. * @expectedException \RuntimeException
  130. */
  131. public function testGetRawCurrentUVIndexWithoutApiKey()
  132. {
  133. $this->owm->setApiKey(null);
  134. $this->owm->getRawCurrentUVIndexData(1.1, 1.1);
  135. }
  136. /**
  137. * @expectedException \InvalidArgumentException
  138. */
  139. public function testBuildQueryUrlParameterException()
  140. {
  141. $this->owm->getWeather(true, 'imperial', 'en', '');
  142. }
  143. /**
  144. * @expectedException \Cmfcmf\OpenWeatherMap\Exception
  145. */
  146. public function testParseXMLException()
  147. {
  148. $answer = 'I am not XML formatted data';
  149. $method = new \ReflectionMethod($this->owm, 'parseXML');
  150. $method->setAccessible(true);
  151. $method->invoke($this->owm, $answer);
  152. }
  153. /**
  154. * @expectedException \Cmfcmf\OpenWeatherMap\Exception
  155. */
  156. public function testParseXMLWithIsJsonException()
  157. {
  158. $answer = array('message' => 'simple json data');
  159. $answer = json_encode($answer);
  160. $method = new \ReflectionMethod($this->owm, 'parseXML');
  161. $method->setAccessible(true);
  162. $method->invoke($this->owm, $answer);
  163. }
  164. /**
  165. * @expectedException \Cmfcmf\OpenWeatherMap\Exception
  166. */
  167. public function testParseJsonException()
  168. {
  169. $answer = 'I am not a json format data';
  170. $method = new \ReflectionMethod($this->owm, 'parseJson');
  171. $method->setAccessible(true);
  172. $method->invoke($this->owm, $answer);
  173. }
  174. public function uvIndexExceptionDataProvider()
  175. {
  176. return array(
  177. array('error-query-format', 'foo', new \DateTime(), 'year'),
  178. array(5.4, 1.2, 'foo', 'month'),
  179. array(5.4, 12, 'foo', 'day'),
  180. array(5.4, 1.2, 'foo', 'bar'),
  181. );
  182. }
  183. public function currentUVIndexExceptionDataProvider()
  184. {
  185. return array(
  186. array('error-query-format', 'foo'),
  187. array(5.4, 12),
  188. array(5.4, '1.2'),
  189. );
  190. }
  191. }