Skip to content

Commit fa3f4f7

Browse files
Alex Seriycmfcmf
authored andcommitted
Integration tests using a virtualized version of the OpenWeatherMap
service.
1 parent 86d2701 commit fa3f4f7

File tree

7 files changed

+176
-3
lines changed

7 files changed

+176
-3
lines changed

.travis.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: php
2-
sudo: false
2+
sudo: required
33

44
php:
55
- 5.3
@@ -12,10 +12,21 @@ php:
1212
matrix:
1313
fast_finish: true
1414

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:184.170.226.124:3128 > /tmp/socat.log 2>&1 &
24+
1525
install:
1626
- composer install --dev
1727

18-
script: phpunit --coverage-text --coverage-clover=coverage.clover
28+
script:
29+
- phpunit --coverage-text --coverage-clover=coverage.clover
1930

2031
after_script:
2132
- wget https://scrutinizer-ci.com/ocular.phar

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,4 @@ and license of OpenWeatherMap before using the service.**
7575
- [OpenWeatherMap.org](http://OpenWeatherMap.org)
7676
- [OpenWeatherMap.org/terms](http://OpenWeatherMap.org/terms)
7777
- [OpenWeatherMap.org/appid](http://OpenWeatherMap.org/appid)
78+

integTests/ApiKey.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[OpenWeatherMap]
2+
api_key = 50b2ae8523a458183982bf56254556dc

integTests/ConnectionTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of
11+
* OpenWeatherMap before using this class:
12+
*
13+
* @see http://www.OpenWeatherMap.org
14+
* @see http://www.OpenWeatherMap.org/terms
15+
* @see http://openweathermap.org/appid
16+
*/
17+
18+
namespace Cmfcmf\OpenWeatherMap\IntegTests;
19+
20+
use Cmfcmf\OpenWeatherMap;
21+
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
22+
23+
class ConnectionTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @var \OpenWeatherMap
27+
*/
28+
protected $owm;
29+
30+
protected function setUp()
31+
{
32+
33+
// Load the app configuration
34+
$ini = parse_ini_file(__DIR__ . '/ApiKey.ini');
35+
$apiKey = $ini['api_key'];
36+
37+
$this->owm = new OpenWeatherMap();
38+
}
39+
40+
public function testUnauthorizedAccess()
41+
{
42+
try {
43+
$weather = $this->owm->getWeather('Paris');
44+
} catch (OWMException $e) {
45+
$this->assertEquals(401, $e->getCode());
46+
$this->assertRegExp('/^Invalid API key/', $e->getMessage());
47+
}
48+
}
49+
}

integTests/CurrentWeatherTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of
11+
* OpenWeatherMap before using this class:
12+
*
13+
* @see http://www.OpenWeatherMap.org
14+
* @see http://www.OpenWeatherMap.org/terms
15+
* @see http://openweathermap.org/appid
16+
*/
17+
18+
namespace Cmfcmf\OpenWeatherMap\IntegTests;
19+
20+
use Cmfcmf\OpenWeatherMap;
21+
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
22+
23+
class CurrentWeatherTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @var \OpenWeatherMap
27+
*/
28+
protected $owm;
29+
30+
protected function setUp()
31+
{
32+
33+
// Load the app configuration
34+
$ini = parse_ini_file(__DIR__ . '/ApiKey.ini');
35+
$apiKey = $ini['api_key'];
36+
37+
$this->owm = new OpenWeatherMap();
38+
$this->owm->setApiKey($apiKey);
39+
}
40+
41+
public function testByCity()
42+
{
43+
// Default units (imperial) and language (English)
44+
$weather = $this->owm->getWeather('Paris');
45+
$this->assertEquals('49.39 F', $weather->temperature);
46+
47+
// Default language (English)
48+
$weather = $this->owm->getWeather('Atlanta', 'imperial');
49+
$this->assertEquals(38.18, $weather->temperature->getValue());
50+
$weather = $this->owm->getWeather('London', 'metric');
51+
$this->assertEquals(4.66, $weather->temperature->getValue());
52+
$this->assertEquals('&deg;C', $weather->temperature->getUnit());
53+
54+
// No defaults
55+
$weather = $this->owm->getWeather('Chicago', 'imperial', 'en');
56+
$this->assertEquals('13.88 F', $weather->temperature->getFormatted());
57+
$weather = $this->owm->getWeather('Prague', 'metric', 'en');
58+
$this->assertEquals(3.58, $weather->temperature->now->getValue());
59+
$this->assertEmpty($weather->temperature->min->getDescription());
60+
$this->assertEquals('3.58 &deg;C', $weather->temperature->max->getFormatted());
61+
}
62+
63+
public function testCityNotFound()
64+
{
65+
// City doesn't exist
66+
try {
67+
$weather = $this->owm->getWeather('InvalidCity');
68+
} catch (OWMException $e) {
69+
$this->assertEquals(404, $e->getCode());
70+
$this->assertEquals('Error: Not found city', $e->getMessage());
71+
}
72+
}
73+
74+
public function testByCityCountry()
75+
{
76+
$weather = $this->owm->getWeather('London,ON');
77+
78+
// Geo coordinates
79+
$this->assertEquals('-81.23', $weather->city->lon);
80+
$this->assertEquals('42.98', $weather->city->lat);
81+
82+
// Country
83+
$this->assertEquals('CA', $weather->city->country);
84+
}
85+
86+
public function testByCityID()
87+
{
88+
$weather = $this->owm->getWeather(4930956);
89+
$this->assertEquals('Boston', $weather->city->name);
90+
}
91+
92+
public function testByCoordinates()
93+
{
94+
$weather = $this->owm->getWeather(array('lon' => 37.62, 'lat' => 55.75));
95+
$this->assertEquals($weather->city->country, 'RU');
96+
}
97+
}

integTests/bootstrap.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
call_user_func(function () {
4+
if (!is_file($autoloadFile = __DIR__ . '/../vendor/autoload.php')) {
5+
throw new \RuntimeException('Did not find vendor/autoload.php. Did you run "composer install --dev"?');
6+
}
7+
8+
/** @noinspection PhpIncludeInspection */
9+
require_once $autoloadFile;
10+
});

phpunit.xml.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
55

66
<testsuites>
7-
<testsuite name="cmfcmf/openweathermap-php-api test suite">
7+
<testsuite name="cmfcmf/openweathermap-php-api unit test suite">
88
<directory suffix="Test.php">./tests</directory>
99
</testsuite>
10+
<testsuite name="cmfcmf/openweathermap-php-api integration test suite">
11+
<directory suffix="Test.php">./integTests</directory>
12+
</testsuite>
1013
</testsuites>
1114

1215
<filter>

0 commit comments

Comments
 (0)