Skip to content

Commit 57489c6

Browse files
committed
Added first tests.
1 parent b485207 commit 57489c6

File tree

8 files changed

+114140
-0
lines changed

8 files changed

+114140
-0
lines changed

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
3+
before_script:
4+
- composer self-update
5+
- composer install --dev --prefer-source
6+
7+
php:
8+
- 5.3
9+
- 5.4
10+
- 5.5
11+
12+
script: phpunit
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — An 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 OpenWeatherMap before using this class.
11+
* @see http://www.OpenWeatherMap.org
12+
* @see http://www.OpenWeatherMap.org/about
13+
* @see http://www.OpenWeatherMap.org/copyright
14+
* @see http://openweathermap.org/appid
15+
*/
16+
17+
namespace Cmfcmf\OpenWeatherMap\Fetcher;
18+
19+
/**
20+
* @requires function curl_version
21+
*/
22+
class CurlFetcherTest extends \PHPUnit_Framework_TestCase
23+
{
24+
public function testInvalidUrl()
25+
{
26+
$fetcher = new CurlFetcher();
27+
28+
$content = $fetcher->fetch('http://notexisting.example.com/');
29+
30+
$this->assertSame(false, $content);
31+
}
32+
33+
public function testEmptyUrl()
34+
{
35+
$fetcher = new CurlFetcher();
36+
37+
$content = $fetcher->fetch('http://notexisting.example.com/');
38+
39+
$this->assertSame(false, $content);
40+
}
41+
42+
public function testValidUrl()
43+
{
44+
$fetcher = new CurlFetcher();
45+
46+
$content = $fetcher->fetch('http://www.example.com');
47+
48+
$this->assertContains('Example Domain', $content);
49+
}
50+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — An 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 OpenWeatherMap before using this class.
11+
* @see http://www.OpenWeatherMap.org
12+
* @see http://www.OpenWeatherMap.org/about
13+
* @see http://www.OpenWeatherMap.org/copyright
14+
* @see http://openweathermap.org/appid
15+
*/
16+
17+
namespace Cmfcmf\OpenWeatherMap\Fetcher;
18+
19+
class FileGetContentsFetcherTest extends \PHPUnit_Framework_TestCase
20+
{
21+
protected function setUp()
22+
{
23+
if (!ini_get('allow_url_fopen')) {
24+
$this->markTestSkipped('"allow_url_fopen" is set to off.');
25+
}
26+
}
27+
28+
/**
29+
* @expectedException \PHPUnit_Framework_Error_Warning
30+
*/
31+
public function testInvalidUrl()
32+
{
33+
$fetcher = new FileGetContentsFetcher();
34+
35+
$fetcher->fetch('http://notexisting.example.com/');
36+
}
37+
38+
/**
39+
* @expectedException \PHPUnit_Framework_Error_Warning
40+
*/
41+
public function testEmptyUrl()
42+
{
43+
$fetcher = new FileGetContentsFetcher();
44+
45+
$fetcher->fetch('');
46+
}
47+
48+
public function testValidUrl()
49+
{
50+
$fetcher = new FileGetContentsFetcher();
51+
52+
$content = $fetcher->fetch('http://www.example.com');
53+
54+
$this->assertContains('Example Domain', $content);
55+
}
56+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright Zikula Foundation 2014 - Zikula Application Framework
4+
*
5+
* This work is contributed to the Zikula Foundation under one or more
6+
* Contributor Agreements and licensed to You under the following license:
7+
*
8+
* @license GNU/LGPv3 (or at your option any later version).
9+
* @package OpenWeatherMap-PHP-Api
10+
*
11+
* Please see the NOTICE file distributed with this source code for further
12+
* information regarding copyright and licensing.
13+
*/
14+
15+
namespace Cmfcmf\OpenWeatherMap\Util;
16+
17+
class SunTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var Sun
21+
*/
22+
private $sun;
23+
24+
public function testSunRise()
25+
{
26+
$rise = new \DateTime('2014-01-01 08:00:00');
27+
$set = new \DateTime('2014-01-01 20:00:00');
28+
29+
$this->givenThereIsASunObject($rise, $set);
30+
31+
$this->assertSame($rise, $this->sun->rise);
32+
}
33+
34+
public function testSunSet()
35+
{
36+
$rise = new \DateTime('2014-01-01 08:00:00');
37+
$set = new \DateTime('2014-01-01 20:00:00');
38+
39+
$this->givenThereIsASunObject($rise, $set);
40+
41+
$this->assertSame($set, $this->sun->set);
42+
}
43+
44+
45+
46+
private function givenThereIsASunObject($rise, $set)
47+
{
48+
$this->sun = new Sun($rise, $set);
49+
}
50+
51+
/**
52+
* @expectedException \LogicException
53+
*/
54+
public function testSunSetBeforeSunRiseException()
55+
{
56+
$rise = new \DateTime('2014-01-01 08:00:00');
57+
$set = new \DateTime('2014-01-01 7:00:00');
58+
59+
$this->givenThereIsASunObject($rise, $set);
60+
}
61+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
/**
3+
* Copyright Zikula Foundation 2014 - Zikula Application Framework
4+
*
5+
* This work is contributed to the Zikula Foundation under one or more
6+
* Contributor Agreements and licensed to You under the following license:
7+
*
8+
* @license GNU/LGPv3 (or at your option any later version).
9+
* @package OpenWeatherMap-PHP-Api
10+
*
11+
* Please see the NOTICE file distributed with this source code for further
12+
* information regarding copyright and licensing.
13+
*/
14+
15+
namespace Cmfcmf\OpenWeatherMap\Util;
16+
17+
class UnitTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var Unit
21+
*/
22+
private $unit;
23+
24+
const POSITIVE_INT_VALUE = 23;
25+
const POSITIVE_FLOAT_VALUE = 48.23534;
26+
const NEGATIVE_INT_VALUE = -30;
27+
const NEGATIVE_FLOAT_VALUE = -93.45839;
28+
const ZERO_INT_VALUE = 0;
29+
const ZERO_FLOAT_VALUE = 0.0;
30+
31+
public function testGetValueWithPositiveIntValue()
32+
{
33+
$this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE);
34+
35+
$this->assertSame((float)self::POSITIVE_INT_VALUE, $this->unit->getValue());
36+
}
37+
38+
public function testGetValueWithPositiveFloatValue()
39+
{
40+
$this->givenThereIsAUnitWithValue(self::POSITIVE_FLOAT_VALUE);
41+
42+
$this->assertSame(self::POSITIVE_FLOAT_VALUE, $this->unit->getValue());
43+
}
44+
45+
public function testGetValueWithNegativeIntValue()
46+
{
47+
$this->givenThereIsAUnitWithValue(self::NEGATIVE_INT_VALUE);
48+
49+
$this->assertSame((float)self::NEGATIVE_INT_VALUE, $this->unit->getValue());
50+
}
51+
52+
public function testGetValueWithNegativeFloatValue()
53+
{
54+
$this->givenThereIsAUnitWithValue(self::NEGATIVE_FLOAT_VALUE);
55+
56+
$this->assertSame(self::NEGATIVE_FLOAT_VALUE, $this->unit->getValue());
57+
}
58+
59+
public function testGetValueWithZeroIntValue()
60+
{
61+
$this->givenThereIsAUnitWithValue(self::ZERO_INT_VALUE);
62+
63+
$this->assertSame((float)self::ZERO_INT_VALUE, $this->unit->getValue());
64+
}
65+
66+
public function testGetValueWithZeroFloatValue()
67+
{
68+
$this->givenThereIsAUnitWithValue(self::ZERO_FLOAT_VALUE);
69+
70+
$this->assertSame(self::ZERO_FLOAT_VALUE, $this->unit->getValue());
71+
}
72+
73+
private function givenThereIsAUnitWithValue($value, $unit = null)
74+
{
75+
$this->unit = $unit === null ? new Unit($value) : new Unit($value, $unit);
76+
}
77+
78+
public function testGetUnitWithEmptyUnit()
79+
{
80+
$this->givenThereIsAUnitWithUnit("");
81+
82+
$this->assertSame("", $this->unit->getUnit());
83+
}
84+
85+
public function testGetUnitWithStringAsUnit()
86+
{
87+
$this->givenThereIsAUnitWithUnit("Hey! I'm cmfcmf");
88+
89+
$this->assertSame("Hey! I'm cmfcmf", $this->unit->getUnit());
90+
}
91+
92+
public function testCelsiusFixture()
93+
{
94+
$this->givenThereIsAUnitWithUnit("celsius");
95+
96+
$this->assertSame("\xB0C", $this->unit->getUnit());
97+
}
98+
99+
public function testFahrenheitFixture()
100+
{
101+
$this->givenThereIsAUnitWithUnit("fahrenheit");
102+
103+
$this->assertSame("F", $this->unit->getUnit());
104+
}
105+
106+
private function givenThereIsAUnitWithUnit($unit)
107+
{
108+
$this->unit = new Unit(0, $unit);
109+
}
110+
111+
public function testGetDescriptionWithEmptyDescription()
112+
{
113+
$this->givenThereIsAUnitWithDescription("");
114+
115+
$this->assertSame("", $this->unit->getDescription());
116+
}
117+
118+
public function testGetDescriptionWithStringAsDescription()
119+
{
120+
$this->givenThereIsAUnitWithDescription("Hey! I'm cmfcmf");
121+
122+
$this->assertSame("Hey! I'm cmfcmf", $this->unit->getDescription());
123+
}
124+
125+
private function givenThereIsAUnitWithDescription($description)
126+
{
127+
$this->unit = new Unit(0, "", $description);
128+
}
129+
130+
public function testGetFormattedWithoutUnit()
131+
{
132+
$this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE);
133+
134+
$this->assertEquals(self::POSITIVE_INT_VALUE, $this->unit->getFormatted());
135+
$this->assertEquals($this->unit->getValue(), $this->unit->getFormatted());
136+
}
137+
138+
public function testGetFormattedWithUnit()
139+
{
140+
$this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE, 'K');
141+
142+
$this->assertEquals(self::POSITIVE_INT_VALUE . ' K', $this->unit->getFormatted());
143+
$this->assertEquals($this->unit->getValue() . ' ' . $this->unit->getUnit(), $this->unit->getFormatted());
144+
}
145+
146+
public function testToString()
147+
{
148+
$this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE, 'K');
149+
150+
$this->assertEquals($this->unit->getFormatted(), $this->unit);
151+
}
152+
}

0 commit comments

Comments
 (0)