1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\monitor\TestCase\Network; |
4
|
|
|
|
5
|
|
|
use \SimpleSAML\Module\monitor\State as State; |
6
|
|
|
use \SimpleSAML\Module\monitor\TestData as TestData; |
7
|
|
|
use \SimpleSAML\Module\monitor\TestResult as TestResult; |
8
|
|
|
|
9
|
|
|
final class ConnectUri extends \SimpleSAML\Module\monitor\TestCaseFactory |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param integer|null |
13
|
|
|
*/ |
14
|
|
|
private $timeout = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param resource|null |
18
|
|
|
*/ |
19
|
|
|
private $context = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string|null |
23
|
|
|
*/ |
24
|
|
|
private $uri = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var TestData $testData |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
protected function initialize($testData) |
32
|
|
|
{ |
33
|
|
|
$uri = $testData->getInput('uri'); |
34
|
|
|
$context = $testData->getInput('context'); |
35
|
|
|
|
36
|
|
|
if (is_null($context)) { |
37
|
|
|
$context = stream_context_create(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$timeout = $testData->getInput('timeout'); |
41
|
|
|
$timeout = is_null($timeout) ? (int)ini_get("default_socket_timeout") : $timeout; |
42
|
|
|
|
43
|
|
|
$this->setUri($uri); |
44
|
|
|
$this->setContext($context); |
45
|
|
|
$this->setTimeout($timeout); |
46
|
|
|
|
47
|
|
|
parent::initialize($testData); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* |
51
|
|
|
* @param string $uri |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
private function setUri($uri) |
56
|
|
|
{ |
57
|
|
|
assert(is_string($uri)); |
58
|
|
|
$this->uri = $uri; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/* |
62
|
|
|
* @param resource $context |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
private function setContext($context) |
67
|
|
|
{ |
68
|
|
|
assert(is_resource($context)); |
69
|
|
|
$this->context = $context; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* |
73
|
|
|
* @param integer $timeout |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
private function setTimeout($timeout) |
78
|
|
|
{ |
79
|
|
|
assert(is_int($timeout)); |
80
|
|
|
$this->timeout = $timeout; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
protected function invokeTest() |
87
|
|
|
{ |
88
|
|
|
$connection = @stream_socket_client($this->uri, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->context); |
89
|
|
|
|
90
|
|
|
$testResult = new TestResult('Network connection', $this->uri); |
91
|
|
|
|
92
|
|
|
if (is_resource($this->connection)) { |
93
|
|
|
$testResult->setOutput(['connection' => $connection]); |
94
|
|
|
$testResult->setState(State::OK); |
95
|
|
|
fclose($this->connection); |
|
|
|
|
96
|
|
|
} else { |
97
|
|
|
$testResult->setState(State::ERROR); |
98
|
|
|
$testResult->setMessage($errstr.' ('.$errno.')'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->setTestResult($testResult); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: