Skip to content

Complete the unit testing and add some features #93

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

Closed
wants to merge 5 commits into from
Closed
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: 19 additions & 17 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
language: php
#sudo: required

sudo: false

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

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:
- phpenv config-rm xdebug.ini

install:
- composer install
- mkdir -p ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d && echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- mkdir -p build/logs
- composer global require satooshi/php-coveralls:@stable --no-update
- composer global update --prefer-dist --no-interaction
- composer update --prefer-dist --no-interaction $COMPOSER_FLAGS

before_script:
- echo "zend_extension=xdebug.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

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

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
after_success:
- bash <(curl -s https://codecov.io/bash)
6 changes: 3 additions & 3 deletions Cmfcmf/OpenWeatherMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,8 @@ private function parseXML($answer)
// Invalid xml format. This happens in case OpenWeatherMap returns an error.
// 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']);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new OWMException('OpenWeatherMap returned an invalid json object: ' . json_last_error());
} else {
throw new OWMException('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $answer);
}
Expand All @@ -563,7 +563,7 @@ 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_last_error());
}

return $json;
Expand Down
9 changes: 9 additions & 0 deletions Cmfcmf/OpenWeatherMap/AbstractCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ abstract public function getCached($url);
*/
abstract public function setCached($url, $content);

/**
* Specify the cached weather data file path.
*
* @param string $path The cached file path.
*
* @return void.
*/
abstract public function setTempPath($path);

/**
* Set after how much seconds the cache shall expire.
*
Expand Down
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

20 changes: 18 additions & 2 deletions Examples/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
*/
class ExampleCache extends AbstractCache
{
protected $tmp;

private function urlToPath($url)
{
$tmp = sys_get_temp_dir();
$tmp = $this->tmp;
$dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
if (!is_dir($dir)) {
mkdir($dir);
Expand Down Expand Up @@ -72,6 +74,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 +95,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
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
"source": "https://github.com/cmfcmf/OpenWeatherMap-PHP-Api.git"
},
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.0.5"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions integTests/ApiKey.ini

This file was deleted.

44 changes: 0 additions & 44 deletions integTests/ConnectionTest.php

This file was deleted.

96 changes: 0 additions & 96 deletions integTests/CurrentWeatherTest.php

This file was deleted.

Loading