Skip to content

Commit 98e99f8

Browse files
committed
add unit testing and some features
1 parent c6429e7 commit 98e99f8

25 files changed

+1134
-328
lines changed

.travis.yml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
language: php
2-
#sudo: required
2+
3+
sudo: false
34

45
php:
56
- 5.3
67
- 5.4
78
- 5.5
89
- 5.6
9-
- 7
10-
- hhvm
10+
- 7.0
11+
- nightly
1112

1213
matrix:
1314
fast_finish: true
15+
allow_failures:
16+
- php: nightly
1417

15-
#addons:
16-
# hosts:
17-
# - api.openweathermap.org
18-
19-
#before_install:
20-
# - sudo apt-get -qq update
21-
# - sudo apt-get install -y socat
22-
# - cat /etc/hosts
23-
# - sudo socat TCP-LISTEN:80,fork TCP:${RTCP_HOST}:${RTCP_PORT} > /tmp/socat.log 2>&1 &
18+
before_install:
19+
- phpenv config-rm xdebug.ini
2420

2521
install:
26-
- composer install
22+
- mkdir -p ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d && echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
23+
- mkdir -p build/logs
24+
- composer global require satooshi/php-coveralls:@stable --no-update
25+
- composer global update --prefer-dist --no-interaction
26+
- composer update --prefer-dist --no-interaction $COMPOSER_FLAGS
27+
28+
before_script:
29+
- echo "zend_extension=xdebug.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
2730

2831
script:
29-
- phpunit --coverage-text --coverage-clover=coverage.clover
32+
- vendor/bin/phpunit
3033

31-
after_script:
32-
- wget https://scrutinizer-ci.com/ocular.phar
33-
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
34+
after_success:
35+
- bash <(curl -s https://codecov.io/bash)

Cmfcmf/OpenWeatherMap.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,8 @@ private function parseXML($answer)
545545
// Invalid xml format. This happens in case OpenWeatherMap returns an error.
546546
// OpenWeatherMap always uses json for errors, even if one specifies xml as format.
547547
$error = json_decode($answer, true);
548-
if (isset($error['message'])) {
549-
throw new OWMException($error['message'], $error['cod']);
548+
if (json_last_error() !== JSON_ERROR_NONE) {
549+
throw new OWMException('OpenWeatherMap returned an invalid json object: ' . json_last_error());
550550
} else {
551551
throw new OWMException('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $answer);
552552
}
@@ -563,7 +563,7 @@ private function parseJson($answer)
563563
{
564564
$json = json_decode($answer);
565565
if (json_last_error() !== JSON_ERROR_NONE) {
566-
throw new OWMException('OpenWeatherMap returned an invalid json object: ' . json_last_error_msg());
566+
throw new OWMException('OpenWeatherMap returned an invalid json object: ' . json_last_error());
567567
}
568568

569569
return $json;

Cmfcmf/OpenWeatherMap/AbstractCache.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ abstract public function getCached($url);
5757
*/
5858
abstract public function setCached($url, $content);
5959

60+
/**
61+
* Specify the cached weather data file path.
62+
*
63+
* @param string $path The cached file path.
64+
*
65+
* @return void.
66+
*/
67+
abstract public function setTempPath($path);
68+
6069
/**
6170
* Set after how much seconds the cache shall expire.
6271
*

Cmfcmf/OpenWeatherMap/Util/Weather.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ public function __toString()
7777
*/
7878
public function getIconUrl()
7979
{
80-
return str_replace("%s", $this->icon, $this->iconUrl);
80+
return sprintf($this->iconUrl, $this->icon);
8181
}
8282
}

Cmfcmf/OpenWeatherMap/WeatherHistory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public function __construct($weatherHistory, $query)
7575

7676
$utctz = new \DateTimeZone('UTC');
7777
foreach ($weatherHistory['list'] as $history) {
78+
if (isset($history['city'])) {
79+
continue;
80+
}
7881
if (isset($history['rain'])) {
7982
$units = array_keys($history['rain']);
8083
} else {

Examples/ApiKey.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[OpenWeatherMap]
2-
# Get an API Key from http://home.openweathermap.org/
2+
; Get an API Key from http://home.openweathermap.org/
3+
; DO NOT use this API key for any production!
34
api_key = 2f8796eefe67558dc205b09dd336d022
45

Examples/Cache.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
*/
2828
class ExampleCache extends AbstractCache
2929
{
30+
protected $tmp;
31+
3032
private function urlToPath($url)
3133
{
32-
$tmp = sys_get_temp_dir();
34+
$tmp = $this->tmp;
3335
$dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
3436
if (!is_dir($dir)) {
3537
mkdir($dir);
@@ -72,6 +74,18 @@ public function setCached($url, $content)
7274
{
7375
file_put_contents($this->urlToPath($url), $content);
7476
}
77+
78+
/**
79+
* @inheritdoc
80+
*/
81+
public function setTempPath($path)
82+
{
83+
if (!is_dir($path)) {
84+
mkdir($path);
85+
}
86+
87+
$this->tmp = $path;
88+
}
7589
}
7690

7791
// Language of data (try your own language here!):
@@ -81,7 +95,9 @@ public function setCached($url, $content)
8195
$units = 'metric';
8296

8397
// Example 1: Use your own cache implementation. Cache for 10 seconds only in this example.
84-
$owm = new OpenWeatherMap($myApiKey, null, new ExampleCache(), 10);
98+
$cache = new ExampleCache();
99+
$cache->setTempPath(__DIR__.'/temps');
100+
$owm = new OpenWeatherMap($myApiKey, null, $cache, 10);
85101

86102
$weather = $owm->getWeather('Berlin', $units, $lang);
87103
echo "EXAMPLE 1<hr />\n\n\n";

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
"source": "https://github.com/cmfcmf/OpenWeatherMap-PHP-Api.git"
1818
},
1919
"require": {
20-
"php": ">=5.3.0"
20+
"php": ">=5.3.0",
21+
"ext-curl": "*"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^4.8 || ^5.0.5"
2125
},
2226
"autoload": {
2327
"psr-4": {

composer.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integTests/ApiKey.ini

Lines changed: 0 additions & 2 deletions
This file was deleted.

integTests/ConnectionTest.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

integTests/CurrentWeatherTest.php

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)