1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE file |
5
|
|
|
* that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare (strict_types = 1); |
9
|
|
|
|
10
|
|
|
namespace Component\Remote\BurzeDzisNet; |
11
|
|
|
|
12
|
|
|
use PHPUnit_Framework_TestCase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* {@see WeatherAlert} test. |
16
|
|
|
* |
17
|
|
|
* @author Krzysztof Piasecki <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class WeatherAlertTest extends PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @covers Component\Remote\BurzeDzisNet\WeatherAlert::__construct |
23
|
|
|
*/ |
24
|
|
|
public function test__construct() |
25
|
|
|
{ |
26
|
|
|
$alert = (new WeatherAlert()) |
27
|
|
|
->withAlert('storm', new Alert(1, '2015-02-12', '2015-02-13')) |
28
|
|
|
->withAlert('wind', new Alert(2, '2015-02-12', '2015-02-13')); |
29
|
|
|
$alert2 = new WeatherAlert($alert); |
30
|
|
|
$this->assertTrue($alert2->hasAlert('storm')); |
31
|
|
|
$this->assertEquals(new Alert(1, '2015-02-12', '2015-02-13'), $alert2->getAlert('storm')); |
32
|
|
|
$this->assertTrue($alert2->hasAlert('wind')); |
33
|
|
|
$this->assertEquals(new Alert(2, '2015-02-12', '2015-02-13'), $alert2->getAlert('wind')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @covers Component\Remote\BurzeDzisNet\WeatherAlert::withAlert |
38
|
|
|
*/ |
39
|
|
|
public function testWithAlert() |
40
|
|
|
{ |
41
|
|
|
$alert = (new WeatherAlert()) |
42
|
|
|
->withAlert('storm', new Alert(1, '2015-02-12', '2015-02-13')); |
43
|
|
|
$this->assertEquals($alert->getAlert('storm'), new Alert(1, '2015-02-12', '2015-02-13')); |
44
|
|
|
$alert2 = (new WeatherAlert()) |
45
|
|
|
->withAlert('wind', new Alert(2, '2015-02-12', '2015-02-13')); |
46
|
|
|
$this->assertNotSame($alert, $alert2); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers Component\Remote\BurzeDzisNet\WeatherAlert::withAlert |
51
|
|
|
* @expectedException \LogicException |
52
|
|
|
* @expectedExceptionMessage Alert storm exists |
53
|
|
|
*/ |
54
|
|
|
public function testWithAlertOutOfBoundsException() |
55
|
|
|
{ |
56
|
|
|
(new WeatherAlert()) |
57
|
|
|
->withAlert('storm', new Alert(1, '2015-02-12', '2015-02-13')) |
58
|
|
|
->withAlert('storm', new Alert(3, '2015-04-12', '2015-04-12')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @covers Component\Remote\BurzeDzisNet\WeatherAlert::getAlert |
63
|
|
|
*/ |
64
|
|
|
public function testGetAlert() |
65
|
|
|
{ |
66
|
|
|
$alert = (new WeatherAlert()) |
67
|
|
|
->withAlert('storm', new Alert(1, '2015-02-12', '2015-02-13')); |
68
|
|
|
$this->assertEquals($alert->getAlert('storm'), new Alert(1, '2015-02-12', '2015-02-13')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @covers Component\Remote\BurzeDzisNet\WeatherAlert::getAlert |
73
|
|
|
* @expectedException \OutOfBoundsException |
74
|
|
|
* @expectedExceptionMessage There is no such an alert like 'Storm' |
75
|
|
|
*/ |
76
|
|
|
public function testGetAlertOutOfBoundsException() |
77
|
|
|
{ |
78
|
|
|
$alert = (new WeatherAlert()) |
79
|
|
|
->withAlert('storm', new Alert(1, '2015-02-12', '2015-02-13')); |
80
|
|
|
$alert->getAlert('Storm'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @covers Component\Remote\BurzeDzisNet\WeatherAlert::getIterator |
85
|
|
|
*/ |
86
|
|
|
public function testGetIterator() |
87
|
|
|
{ |
88
|
|
|
$weatherAlert = (new WeatherAlert()) |
89
|
|
|
->withAlert('storm', new Alert(1, '2015-02-12', '2015-02-13')) |
90
|
|
|
->withAlert('wind', new Alert(2, '2015-02-12', '2015-02-13')); |
91
|
|
|
$iteration = []; |
92
|
|
|
foreach ($weatherAlert as $name => $alert) { |
93
|
|
|
$iteration[$name] = $alert; |
94
|
|
|
} |
95
|
|
|
$this->assertEquals(['storm' => new Alert(1, '2015-02-12', '2015-02-13'), 'wind' => new Alert(2, '2015-02-12', '2015-02-13')], |
96
|
|
|
$iteration |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @covers Component\Remote\BurzeDzisNet\WeatherAlert::toArray |
102
|
|
|
*/ |
103
|
|
|
public function testToArray() |
104
|
|
|
{ |
105
|
|
|
$alert = (new WeatherAlert()) |
106
|
|
|
->withAlert('storm', new Alert(1, '2015-02-12', '2015-02-13')) |
107
|
|
|
->withAlert('wind', new Alert(2, '2015-02-12', '2015-02-13')); |
108
|
|
|
$this->assertEquals( |
109
|
|
|
['storm' => new Alert(1, '2015-02-12', '2015-02-13'), 'wind' => new Alert(2, '2015-02-12', '2015-02-13')], |
110
|
|
|
$alert->toArray() |
111
|
|
|
); |
112
|
|
|
$alert2 = new WeatherAlert(); |
113
|
|
|
$this->assertEquals([], $alert2->toArray()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @covers Component\Remote\BurzeDzisNet\WeatherAlert::hasAlert |
118
|
|
|
*/ |
119
|
|
|
public function testHasAlert() |
120
|
|
|
{ |
121
|
|
|
$alert = new WeatherAlert(); |
122
|
|
|
$this->assertFalse($alert->hasAlert('storm')); |
123
|
|
|
$alert2 = $alert->withAlert('storm', new Alert(1, '2015-02-12', '2015-02-13')); |
124
|
|
|
$this->assertTrue($alert2->hasAlert('storm')); |
125
|
|
|
$alert3 = $alert2->withAlert('wind', new Alert(2, '2015-02-12', '2015-02-13')); |
126
|
|
|
$this->assertTrue($alert3->hasAlert('wind')); |
127
|
|
|
$this->assertTrue($alert3->hasAlert('storm')); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|