Skip to content

Commit 75c176a

Browse files
authored
Merge pull request #96 from cmfcmf/peter279k-master
[peter279k] Complete unit testing and add some features
2 parents dadc126 + 5e51e4a commit 75c176a

26 files changed

+2280
-334
lines changed

.travis.yml

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
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+
- 7.0
11+
- 7.1
12+
- nightly
1013
- hhvm
1114

1215
matrix:
1316
fast_finish: true
17+
allow_failures:
18+
- php: nightly
1419

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 &
20+
before_install:
21+
- if [[ ! $TRAVIS_PHP_VERSION = hhvm* ]]; then phpenv config-rm xdebug.ini || echo "xdebug not available"; fi
2422

2523
install:
26-
- composer install
24+
- if [[ ! $TRAVIS_PHP_VERSION = hhvm* ]]; then INI_FILE=~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; else INI_FILE=/etc/hhvm/php.ini; fi
25+
- echo date.timezone = Europe/Berlin >> $INI_FILE
26+
- echo memory_limit = -1 >> $INI_FILE
27+
- composer update
2728

2829
script:
29-
- phpunit --coverage-text --coverage-clover=coverage.clover
30+
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml
3031

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

Cmfcmf/OpenWeatherMap.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ public function __construct($apiKey = '', $fetcher = null, $cache = false, $seco
119119
}
120120

121121
if ($cache !== false && !($cache instanceof AbstractCache)) {
122-
throw new \Exception('The cache class must implement the FetcherInterface!');
122+
throw new \InvalidArgumentException('The cache class must implement the FetcherInterface!');
123123
}
124124
if (!is_numeric($seconds)) {
125-
throw new \Exception('$seconds must be numeric.');
125+
throw new \InvalidArgumentException('$seconds must be numeric.');
126126
}
127127
if (!isset($fetcher)) {
128128
$fetcher = (function_exists('curl_version')) ? new CurlFetcher() : new FileGetContentsFetcher();
@@ -546,7 +546,7 @@ private function parseXML($answer)
546546
// OpenWeatherMap always uses json for errors, even if one specifies xml as format.
547547
$error = json_decode($answer, true);
548548
if (isset($error['message'])) {
549-
throw new OWMException($error['message'], $error['cod']);
549+
throw new OWMException($error['message'], isset($error['cod']) ? $error['cod'] : 0);
550550
} else {
551551
throw new OWMException('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $answer);
552552
}
@@ -563,9 +563,28 @@ 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 error was: ' . $this->json_last_error_msg());
567567
}
568568

569569
return $json;
570570
}
571+
572+
private function json_last_error_msg()
573+
{
574+
if (function_exists('json_last_error_msg')) {
575+
return json_last_error_msg();
576+
}
577+
578+
static $ERRORS = array(
579+
JSON_ERROR_NONE => 'No error',
580+
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
581+
JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
582+
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
583+
JSON_ERROR_SYNTAX => 'Syntax error',
584+
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
585+
);
586+
587+
$error = json_last_error();
588+
return isset($ERRORS[$error]) ? $ERRORS[$error] : 'Unknown error';
589+
}
571590
}

Cmfcmf/OpenWeatherMap/Util/Weather.php

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

8383
/**

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: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@
2727
*/
2828
class ExampleCache extends AbstractCache
2929
{
30+
protected $tmp;
31+
32+
public function __construct()
33+
{
34+
$this->tmp = sys_get_temp_dir();
35+
}
36+
3037
private function urlToPath($url)
3138
{
32-
$tmp = sys_get_temp_dir();
33-
$dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
39+
$dir = $this->tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
3440
if (!is_dir($dir)) {
3541
mkdir($dir);
3642
}
@@ -72,6 +78,18 @@ public function setCached($url, $content)
7278
{
7379
file_put_contents($this->urlToPath($url), $content);
7480
}
81+
82+
/**
83+
* @inheritdoc
84+
*/
85+
public function setTempPath($path)
86+
{
87+
if (!is_dir($path)) {
88+
mkdir($path);
89+
}
90+
91+
$this->tmp = $path;
92+
}
7593
}
7694

7795
// Language of data (try your own language here!):
@@ -81,7 +99,9 @@ public function setCached($url, $content)
8199
$units = 'metric';
82100

83101
// Example 1: Use your own cache implementation. Cache for 10 seconds only in this example.
84-
$owm = new OpenWeatherMap($myApiKey, null, new ExampleCache(), 10);
102+
$cache = new ExampleCache();
103+
$cache->setTempPath(__DIR__.'/temps');
104+
$owm = new OpenWeatherMap($myApiKey, null, $cache, 10);
85105

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

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This library is neither maintained by OpenWeatherMap nor their official PHP API.
77

88
[![Build Status](https://travis-ci.org/cmfcmf/OpenWeatherMap-PHP-Api.png?branch=master)](https://travis-ci.org/cmfcmf/OpenWeatherMap-PHP-Api)
99
[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/cmfcmf/OpenWeatherMap-PHP-Api/badges/quality-score.png?s=f31ca08aa8896416cf162403d34362f0a5da0966)](https://scrutinizer-ci.com/g/cmfcmf/OpenWeatherMap-PHP-Api/)
10+
[![codecov](https://codecov.io/gh/cmfcmf/OpenWeatherMap-PHP-Api/branch/master/graph/badge.svg)](https://codecov.io/gh/cmfcmf/OpenWeatherMap-PHP-Api)
1011
[![Code Coverage](https://scrutinizer-ci.com/g/cmfcmf/OpenWeatherMap-PHP-Api/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/cmfcmf/OpenWeatherMap-PHP-Api/?branch=master)
1112
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/0addfb24-e2b4-4feb-848e-86b2078ca104/big.png)](https://insight.sensiolabs.com/projects/0addfb24-e2b4-4feb-848e-86b2078ca104)
1213

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"require": {
2020
"php": ">=5.3.0"
2121
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^4.8 || ^5.0.5"
24+
},
2225
"autoload": {
2326
"psr-4": {
2427
"Cmfcmf\\": "Cmfcmf"

0 commit comments

Comments
 (0)