1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\ServiceBundle\Tests\Expectation; |
4
|
|
|
|
5
|
|
|
use Overwatch\ServiceBundle\Expectation\ToPingExpectation; |
6
|
|
|
use phpmock\phpunit\PHPMock; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* ToPingExpectationTest |
10
|
|
|
*/ |
11
|
|
|
class ToPingExpectationTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
use PHPMock; |
14
|
|
|
|
15
|
|
|
private $expectation; |
16
|
|
|
|
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
$this->createSocketMocks(); |
20
|
|
|
|
21
|
|
|
$this->expectation = new ToPingExpectation([ |
22
|
|
|
'timeout' => 2, |
23
|
|
|
'unsatisfactory' => 1 |
24
|
|
|
]); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testExpectation() |
28
|
|
|
{ |
29
|
|
|
$this->createSocketReadMock(); |
30
|
|
|
|
31
|
|
|
$this->assertContains( |
32
|
|
|
'Pinged in ', |
33
|
|
|
$this->expectation->run('8.8.8.8') |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationUnsatisfactoryException |
39
|
|
|
* @expectedExceptionMessage above the unsatisfactory threshold |
40
|
|
|
*/ |
41
|
|
|
public function testExpectationUnsatisfactory() |
42
|
|
|
{ |
43
|
|
|
$expectation = new ToPingExpectation([ |
44
|
|
|
'timeout' => 2, |
45
|
|
|
'unsatisfactory' => 0.2 |
46
|
|
|
]); |
47
|
|
|
$this->createSocketReadMock('delayed'); |
48
|
|
|
$expectation->run('8.8.8.8'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
53
|
|
|
* @expectedExceptionMessage failed to respond in the timeout threshold |
54
|
|
|
*/ |
55
|
|
|
public function testExpectationFailed() |
56
|
|
|
{ |
57
|
|
|
$this->createSocketReadMock('failed'); |
58
|
|
|
$this->expectation->run('8.8.8.8'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
private function createSocketMocks() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
foreach ([ |
64
|
|
|
'socket_create', |
65
|
|
|
'socket_set_option', |
66
|
|
|
'socket_connect', |
67
|
|
|
'socket_send', |
68
|
|
|
'socket_close' |
69
|
|
|
] as $func) { |
70
|
|
|
$mock = $this->getFunctionMock('Overwatch\ServiceBundle\Expectation', $func); |
71
|
|
|
$mock->expects($this->once())->willReturn(true); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function createSocketReadMock($type = 'normal') |
76
|
|
|
{ |
77
|
|
|
$mock = $this->getFunctionMock('Overwatch\ServiceBundle\Expectation', 'socket_read'); |
78
|
|
|
|
79
|
|
|
switch ($type) { |
80
|
|
|
case 'normal': |
81
|
|
|
$mock->expects($this->once())->willReturn(true); |
82
|
|
|
break; |
83
|
|
|
|
84
|
|
|
case 'delayed': |
85
|
|
|
$mock->expects($this->once())->willReturnCallback(function() { |
86
|
|
|
usleep(210000); |
87
|
|
|
return true; |
88
|
|
|
}); |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
case 'failed': |
92
|
|
|
$mock->expects($this->once())->willReturn(false); |
93
|
|
|
break; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.