/
UnconnectedTest.php
205 lines (188 loc) · 7.06 KB
/
UnconnectedTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace PEAR2\Net\Transmitter;
use PEAR2\Net\Transmitter\TcpClient as C;
use PEAR2\Net\Transmitter\TcpServerConnection as SC;
/**
* @group Unconnected
*/
class UnconnectedTest extends \PHPUnit_Framework_TestCase
{
/**
* @var int
*/
public static $defaultSocketTimeout;
public static function setUpBeforeClass()
{
self::$defaultSocketTimeout = ini_set('default_socket_timeout', 2);
}
public static function tearDownAfterClass()
{
ini_set('default_socket_timeout', self::$defaultSocketTimeout);
}
/**
* N/A
*
* @return void
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function testDefaultStreamTransmitterException()
{
try {
$trans = new Stream('invalid arg');
$this->fail('Transmitter initialization had to fail.');
} catch (StreamException $e) {
$this->assertSame(1, $e->getCode(), 'Improper exception code.');
}
}
public function testEmptyFilterCollection()
{
$filters = new FilterCollection();
$this->assertFalse($filters->rewind());
$this->assertFalse($filters->end());
$this->assertFalse($filters->key());
$this->assertFalse($filters->current());
$this->assertFalse($filters->valid());
$this->assertSame(0, count($filters));
}
public function testFilterCollection()
{
$filters = new FilterCollection();
$this->assertSame(0, count($filters));
$filters->append('string.toupper');
$this->assertSame(1, count($filters));
$filters->append('string.rot13');
$this->assertSame(2, count($filters));
$filters->clear();
$this->assertSame(0, count($filters));
$filters->append('string.toupper');
$this->assertSame(1, count($filters));
$this->assertSame('string.toupper', $filters->key());
$this->assertSame(array(), $filters->current());
$filters->insertBefore(-1, 'string.rot13');
$filters->insertBefore(0, 'string.base64');
$filters->insertBefore(1, 'string.tolower');
$filters->insertBefore(count($filters) + 2, 'string.quoted-printable');
$this->assertSame('string.base64', $filters->key());
$this->assertSame(array(), $filters->current());
$this->assertSame(0, $filters->getCurrentPosition());
$this->assertTrue($filters->next());
$this->assertSame('string.tolower', $filters->key());
$this->assertSame(array(), $filters->current());
$this->assertSame(1, $filters->getCurrentPosition());
$this->assertTrue($filters->next());
$this->assertSame('string.rot13', $filters->key());
$this->assertSame(array(), $filters->current());
$this->assertSame(2, $filters->getCurrentPosition());
$this->assertTrue($filters->next());
$this->assertSame('string.toupper', $filters->key());
$this->assertSame(array(), $filters->current());
$this->assertSame(3, $filters->getCurrentPosition());
$this->assertTrue($filters->next());
$this->assertSame('string.quoted-printable', $filters->key());
$this->assertSame(array(), $filters->current());
$this->assertSame(4, $filters->getCurrentPosition());
$this->assertTrue($filters->prev());
$this->assertSame('string.toupper', $filters->key());
$this->assertSame(array(), $filters->current());
$this->assertSame(3, $filters->getCurrentPosition());
$this->assertTrue($filters->rewind());
$filters->removeAt(2);
$this->assertSame('string.base64', $filters->key());
$this->assertSame(array(), $filters->current());
$this->assertSame(0, $filters->getCurrentPosition());
$this->assertTrue($filters->next());
$this->assertSame('string.tolower', $filters->key());
$this->assertSame(array(), $filters->current());
$this->assertSame(1, $filters->getCurrentPosition());
$this->assertTrue($filters->next());
$this->assertSame('string.toupper', $filters->key());
$this->assertSame(array(), $filters->current());
$this->assertSame(2, $filters->getCurrentPosition());
$this->assertTrue($filters->next());
$this->assertSame('string.quoted-printable', $filters->key());
$this->assertSame(array(), $filters->current());
$this->assertSame(3, $filters->getCurrentPosition());
}
public function testInvalidContext()
{
try {
new C(
REMOTE_HOSTNAME,
REMOTE_PORT,
false,
null,
'',
NetworkStream::CRYPTO_OFF,
fopen('php://input', 'r')
);
$this->fail('Client creation had to fail.');
} catch (SocketException $e) {
$this->assertSame(6, $e->getCode(), 'Improper exception code.');
}
}
public function testSilence()
{
$expectedCode = strpos(PHP_OS, 'WIN') === 0 ? 10061 : 111;
try {
new C(SILENT_HOSTNAME, REMOTE_PORT);
$this->fail('Client creation had to fail.');
} catch (SocketException $e) {
$this->assertSame(8, $e->getCode(), 'Improper exception code.');
$this->assertSame(
$expectedCode,
$e->getSocketErrorNumber(),
'Improper exception code.'
);
}
try {
new C(REMOTE_HOSTNAME, SILENT_PORT);
$this->fail('Client creation had to fail.');
} catch (SocketException $e) {
$this->assertSame(8, $e->getCode(), 'Improper exception code.');
$this->assertSame(
$expectedCode,
$e->getSocketErrorNumber(),
'Improper exception code.'
);
}
}
public function testInvalidClient()
{
try {
new C('@', REMOTE_PORT);
$this->fail('Client creation had to fail.');
} catch (SocketException $e) {
$this->assertSame(7, $e->getCode(), 'Improper exception code.');
$this->assertSame(
0,
$e->getSocketErrorNumber(),
'Improper exception code.'
);
}
}
public function testInvalidServer()
{
try {
new SC('not a server', 1/*h*/ * 60/*m*/ * 60/*s*/);
$this->fail('Server creation had to fail.');
} catch (SocketException $e) {
$this->assertSame(9, $e->getCode(), 'Improper exception code.');
}
}
public function testServerConnectionTimeout()
{
$hostname = strpos(LOCAL_HOSTNAME, ':') !== false
? '[' . LOCAL_HOSTNAME . ']' : LOCAL_HOSTNAME;
try {
new SC(
stream_socket_server(
"tcp://{$hostname}:" . LOCAL_PORT
),
2
);
$this->fail('Server creation had to fail.');
} catch (SocketException $e) {
$this->assertSame(10, $e->getCode(), 'Improper exception code.');
}
}
}