Skip to content

[peter279k] Complete unit testing and add some features #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
language: php
#sudo: required

sudo: false

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7
- 7.0
- 7.1
- nightly
- hhvm

matrix:
fast_finish: true
allow_failures:
- php: nightly

#addons:
# hosts:
# - api.openweathermap.org

#before_install:
# - sudo apt-get -qq update
# - sudo apt-get install -y socat
# - cat /etc/hosts
# - sudo socat TCP-LISTEN:80,fork TCP:${RTCP_HOST}:${RTCP_PORT} > /tmp/socat.log 2>&1 &
before_install:
- if [[ ! $TRAVIS_PHP_VERSION = hhvm* ]]; then phpenv config-rm xdebug.ini || echo "xdebug not available"; fi

install:
- composer install
- 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
- echo date.timezone = Europe/Berlin >> $INI_FILE
- echo memory_limit = -1 >> $INI_FILE
- composer update

script:
- phpunit --coverage-text --coverage-clover=coverage.clover
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
after_success:
# Scrutinizer
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.xml
# CodeCov
- bash <(curl -s https://codecov.io/bash)
27 changes: 23 additions & 4 deletions Cmfcmf/OpenWeatherMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ public function __construct($apiKey = '', $fetcher = null, $cache = false, $seco
}

if ($cache !== false && !($cache instanceof AbstractCache)) {
throw new \Exception('The cache class must implement the FetcherInterface!');
throw new \InvalidArgumentException('The cache class must implement the FetcherInterface!');
}
if (!is_numeric($seconds)) {
throw new \Exception('$seconds must be numeric.');
throw new \InvalidArgumentException('$seconds must be numeric.');
}
if (!isset($fetcher)) {
$fetcher = (function_exists('curl_version')) ? new CurlFetcher() : new FileGetContentsFetcher();
Expand Down Expand Up @@ -546,7 +546,7 @@ private function parseXML($answer)
// OpenWeatherMap always uses json for errors, even if one specifies xml as format.
$error = json_decode($answer, true);
if (isset($error['message'])) {
throw new OWMException($error['message'], $error['cod']);
throw new OWMException($error['message'], isset($error['cod']) ? $error['cod'] : 0);
} else {
throw new OWMException('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $answer);
}
Expand All @@ -563,9 +563,28 @@ private function parseJson($answer)
{
$json = json_decode($answer);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new OWMException('OpenWeatherMap returned an invalid json object: ' . json_last_error_msg());
throw new OWMException('OpenWeatherMap returned an invalid json object. JSON error was: ' . $this->json_last_error_msg());
}

return $json;
}

private function json_last_error_msg()
{
if (function_exists('json_last_error_msg')) {
return json_last_error_msg();
}

static $ERRORS = array(
JSON_ERROR_NONE => 'No error',
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
);

$error = json_last_error();
return isset($ERRORS[$error]) ? $ERRORS[$error] : 'Unknown error';
}
}
2 changes: 1 addition & 1 deletion Cmfcmf/OpenWeatherMap/Util/Weather.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function __toString()
*/
public function getIconUrl()
{
return str_replace("%s", $this->icon, self::$iconUrl);
return sprintf(self::$iconUrl, $this->icon);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Examples/ApiKey.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[OpenWeatherMap]
# Get an API Key from http://home.openweathermap.org/
; Get an API Key from http://home.openweathermap.org/
; DO NOT use this API key for any production!
api_key = 2f8796eefe67558dc205b09dd336d022

26 changes: 23 additions & 3 deletions Examples/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@
*/
class ExampleCache extends AbstractCache
{
protected $tmp;

public function __construct()
{
$this->tmp = sys_get_temp_dir();
}

private function urlToPath($url)
{
$tmp = sys_get_temp_dir();
$dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
$dir = $this->tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
if (!is_dir($dir)) {
mkdir($dir);
}
Expand Down Expand Up @@ -72,6 +78,18 @@ public function setCached($url, $content)
{
file_put_contents($this->urlToPath($url), $content);
}

/**
* @inheritdoc
*/
public function setTempPath($path)
{
if (!is_dir($path)) {
mkdir($path);
}

$this->tmp = $path;
}
}

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

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

$weather = $owm->getWeather('Berlin', $units, $lang);
echo "EXAMPLE 1<hr />\n\n\n";
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This library is neither maintained by OpenWeatherMap nor their official PHP API.

[![Build Status](https://travis-ci.org/cmfcmf/OpenWeatherMap-PHP-Api.png?branch=master)](https://travis-ci.org/cmfcmf/OpenWeatherMap-PHP-Api)
[![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/)
[![codecov](https://codecov.io/gh/cmfcmf/OpenWeatherMap-PHP-Api/branch/master/graph/badge.svg)](https://codecov.io/gh/cmfcmf/OpenWeatherMap-PHP-Api)
[![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)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/0addfb24-e2b4-4feb-848e-86b2078ca104/big.png)](https://insight.sensiolabs.com/projects/0addfb24-e2b4-4feb-848e-86b2078ca104)

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.0.5"
},
"autoload": {
"psr-4": {
"Cmfcmf\\": "Cmfcmf"
Expand Down
Loading