1 | <?php |
||
22 | class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $apiKey; |
||
28 | |||
29 | /** |
||
30 | * @var OpenWeatherMap |
||
31 | */ |
||
32 | protected $owm; |
||
33 | |||
34 | /** |
||
35 | * @var ExampleCacheTest |
||
36 | */ |
||
37 | protected $cache; |
||
38 | |||
39 | protected function setUp() |
||
40 | { |
||
41 | $this->apiKey = 'unicorn-rainbow'; |
||
42 | $this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600); |
||
43 | $this->cache = new ExampleCacheTest(); |
||
44 | } |
||
45 | |||
46 | protected function tearDown() |
||
47 | { |
||
48 | $fileList = glob(__DIR__.'/temps/OpenWeatherMapPHPAPI/*'); |
||
49 | foreach ($fileList as $fileName) { |
||
50 | @unlink($fileName); |
||
|
|||
51 | } |
||
52 | |||
53 | @rmdir(__DIR__.'/temps/OpenWeatherMapPHPAPI'); |
||
54 | @rmdir(__DIR__.'/temps'); |
||
55 | } |
||
56 | |||
57 | public function testApiKeyIsEmpty() |
||
58 | { |
||
59 | $expectApiKey = ''; |
||
60 | $weather = new OpenWeatherMap($expectApiKey, null, false, 600); |
||
61 | $apiKey = $weather->getApiKey(); |
||
62 | |||
63 | $this->assertSame($expectApiKey, $apiKey); |
||
64 | } |
||
65 | |||
66 | public function testApiKeyNotNull() |
||
67 | { |
||
68 | $weather = $this->owm; |
||
69 | $apiKey = $weather->getApiKey(); |
||
70 | |||
71 | $this->assertSame($this->apiKey, $apiKey); |
||
72 | } |
||
73 | |||
74 | public function testSecondIsZero() |
||
75 | { |
||
76 | $weather = new OpenWeatherMap($this->apiKey, null, false, 0); |
||
77 | $apiKey = $weather->getApiKey(); |
||
78 | |||
79 | $this->assertSame($this->apiKey, $apiKey); |
||
80 | } |
||
81 | |||
82 | public function testSetApiKey() |
||
83 | { |
||
84 | $weather = $this->owm; |
||
85 | $weather->setApiKey($this->apiKey); |
||
86 | $apiKey = $weather->getApiKey(); |
||
87 | |||
88 | $this->assertSame($this->apiKey, $apiKey); |
||
89 | } |
||
90 | |||
91 | public function testGetApiKey() |
||
92 | { |
||
93 | $weather = $this->owm; |
||
94 | $apiKey = $weather->getApiKey(); |
||
95 | |||
96 | $this->assertSame($this->apiKey, $apiKey); |
||
97 | } |
||
98 | |||
99 | public function testGetWeather() |
||
100 | { |
||
101 | $currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', ''); |
||
102 | |||
103 | $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather); |
||
104 | } |
||
105 | |||
106 | public function testGetWeatherGroup() |
||
107 | { |
||
108 | $currentWeather = $this->owm->getWeatherGroup('2950159', 'imperial', 'en', ''); |
||
109 | |||
110 | $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather); |
||
111 | } |
||
112 | |||
113 | public function testGetWeatherForecast() |
||
114 | { |
||
115 | $days = 1; |
||
116 | $defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
||
117 | |||
118 | $days = 16; |
||
119 | $maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
||
120 | |||
121 | $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay); |
||
122 | $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay); |
||
123 | } |
||
124 | |||
125 | public function testGetDailyWeatherForecast() |
||
126 | { |
||
127 | $days = 16; |
||
128 | $dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
||
129 | |||
130 | $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast); |
||
131 | } |
||
132 | |||
133 | public function testGetWeatherHistory() |
||
134 | { |
||
135 | // @TODO!! |
||
136 | $this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.'); |
||
137 | } |
||
138 | |||
139 | public function testWasCached() |
||
140 | { |
||
141 | $weather = $this->owm; |
||
142 | $result = $weather->wasCached(); |
||
143 | |||
144 | $this->assertFalse($result); |
||
145 | } |
||
146 | |||
147 | public function testCached() |
||
148 | { |
||
149 | $cache = $this->cache; |
||
150 | $cache->setTempPath(__DIR__.'/temps'); |
||
151 | $weather = new OpenWeatherMap($this->apiKey, new TestFetcher(), $cache, 600); |
||
152 | $currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml'); |
||
153 | $cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml'); |
||
154 | |||
155 | $this->assertInternalType('string', $currWeatherData); |
||
156 | $this->assertInternalType('string', $cachedWeatherData); |
||
157 | } |
||
158 | |||
159 | public function testBuildQueryUrlParameter() |
||
168 | |||
169 | public function testAbstractCache() |
||
174 | } |
||
175 |
If you suppress an error, we recommend checking for the error condition explicitly: