Skip to content

Commit 7b460dd

Browse files
committed
Refactor tests
1 parent 7658f54 commit 7b460dd

File tree

2 files changed

+56
-40
lines changed

2 files changed

+56
-40
lines changed

tests/Io/ChunkedDecoderTest.php

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@ public function testSimpleChunk()
2828

2929
public function testTwoChunks()
3030
{
31-
$this->parser->on('data', $this->expectCallableConsecutive(2, array('hello', 'bla')));
31+
$buffer = array();
32+
$this->parser->on('data', function ($data) use (&$buffer) {
33+
$buffer[] = $data;
34+
});
35+
3236
$this->parser->on('end', $this->expectCallableNever());
3337
$this->parser->on('close', $this->expectCallableNever());
3438

3539
$this->input->emit('data', array("5\r\nhello\r\n3\r\nbla\r\n"));
40+
41+
$this->assertEquals(array('hello', 'bla'), $buffer);
3642
}
3743

3844
public function testEnd()
@@ -46,12 +52,18 @@ public function testEnd()
4652

4753
public function testParameterWithEnd()
4854
{
49-
$this->parser->on('data', $this->expectCallableConsecutive(2, array('hello', 'bla')));
55+
$buffer = array();
56+
$this->parser->on('data', function ($data) use (&$buffer) {
57+
$buffer[] = $data;
58+
});
59+
5060
$this->parser->on('end', $this->expectCallableOnce());
5161
$this->parser->on('close', $this->expectCallableOnce());
5262
$this->parser->on('error', $this->expectCallableNever());
5363

5464
$this->input->emit('data', array("5\r\nhello\r\n3\r\nbla\r\n0\r\n\r\n"));
65+
66+
$this->assertEquals(array('hello', 'bla'), $buffer);
5567
}
5668

5769
public function testInvalidChunk()
@@ -118,7 +130,11 @@ public function testSplittedBoth()
118130

119131
public function testCompletlySplitted()
120132
{
121-
$this->parser->on('data', $this->expectCallableConsecutive(2, array('we', 'lt')));
133+
$buffer = array();
134+
$this->parser->on('data', function ($data) use (&$buffer) {
135+
$buffer[] = $data;
136+
});
137+
122138
$this->parser->on('close', $this->expectCallableNever());
123139
$this->parser->on('end', $this->expectCallableNever());
124140
$this->parser->on('error', $this->expectCallableNever());
@@ -127,25 +143,36 @@ public function testCompletlySplitted()
127143
$this->input->emit('data', array("\r\n"));
128144
$this->input->emit('data', array("we"));
129145
$this->input->emit('data', array("lt\r\n"));
146+
147+
$this->assertEquals(array('we', 'lt'), $buffer);
130148
}
131149

132150
public function testMixed()
133151
{
134-
$this->parser->on('data', $this->expectCallableConsecutive(3, array('we', 'lt', 'hello')));
152+
$buffer = array();
153+
$this->parser->on('data', function ($data) use (&$buffer) {
154+
$buffer[] = $data;
155+
});
156+
135157
$this->parser->on('close', $this->expectCallableNever());
136158
$this->parser->on('end', $this->expectCallableNever());
137159
$this->parser->on('error', $this->expectCallableNever());
138160

139161
$this->input->emit('data', array("4"));
140162
$this->input->emit('data', array("\r\n"));
141-
$this->input->emit('data', array("we"));
142-
$this->input->emit('data', array("lt\r\n"));
163+
$this->input->emit('data', array("welt\r\n"));
143164
$this->input->emit('data', array("5\r\nhello\r\n"));
165+
166+
$this->assertEquals(array('welt', 'hello'), $buffer);
144167
}
145168

146169
public function testBigger()
147170
{
148-
$this->parser->on('data', $this->expectCallableConsecutive(2, array('abcdeabcdeabcdea', 'hello')));
171+
$buffer = array();
172+
$this->parser->on('data', function ($data) use (&$buffer) {
173+
$buffer[] = $data;
174+
});
175+
149176
$this->parser->on('close', $this->expectCallableNever());
150177
$this->parser->on('end', $this->expectCallableNever());
151178
$this->parser->on('error', $this->expectCallableNever());
@@ -155,18 +182,26 @@ public function testBigger()
155182
$this->input->emit('data', array("\r\n"));
156183
$this->input->emit('data', array("abcdeabcdeabcdea\r\n"));
157184
$this->input->emit('data', array("5\r\nhello\r\n"));
185+
186+
$this->assertEquals(array('abcdeabcdeabcdea', 'hello'), $buffer);
158187
}
159188

160189
public function testOneUnfinished()
161190
{
162-
$this->parser->on('data', $this->expectCallableConsecutive(2, array('bla', 'hello')));
191+
$buffer = array();
192+
$this->parser->on('data', function ($data) use (&$buffer) {
193+
$buffer[] = $data;
194+
});
195+
163196
$this->parser->on('close', $this->expectCallableNever());
164197
$this->parser->on('end', $this->expectCallableNever());
165198
$this->parser->on('error', $this->expectCallableNever());
166199

167200
$this->input->emit('data', array("3\r\n"));
168201
$this->input->emit('data', array("bla\r\n"));
169202
$this->input->emit('data', array("5\r\nhello"));
203+
204+
$this->assertEquals(array('bla', 'hello'), $buffer);
170205
}
171206

172207
public function testChunkIsBiggerThenExpected()
@@ -326,7 +361,10 @@ public function testHexDecimalInBodyIsPotentialThreadSplitted()
326361

327362
public function testEmitSingleCharacter()
328363
{
329-
$this->parser->on('data', $this->expectCallableConsecutive(4, array('t', 'e', 's', 't')));
364+
$buffer = array();
365+
$this->parser->on('data', function ($data) use (&$buffer) {
366+
$buffer[] = $data;
367+
});
330368
$this->parser->on('close', $this->expectCallableOnce());
331369
$this->parser->on('end', $this->expectCallableOnce());
332370
$this->parser->on('error', $this->expectCallableNever());
@@ -336,6 +374,8 @@ public function testEmitSingleCharacter()
336374
foreach ($array as $character) {
337375
$this->input->emit('data', array($character));
338376
}
377+
378+
$this->assertEquals(array('t', 'e', 's', 't'), $buffer);
339379
}
340380

341381
public function testHandleError()
@@ -402,13 +442,19 @@ public function testOutputStreamCanCloseInputStream()
402442

403443
public function testLeadingZerosWillBeIgnored()
404444
{
405-
$this->parser->on('data', $this->expectCallableConsecutive(2, array('hello', 'hello world')));
445+
$buffer = array();
446+
$this->parser->on('data', function ($data) use (&$buffer) {
447+
$buffer[] = $data;
448+
});
449+
406450
$this->parser->on('error', $this->expectCallableNever());
407451
$this->parser->on('end', $this->expectCallableNever());
408452
$this->parser->on('close', $this->expectCallableNever());
409453

410454
$this->input->emit('data', array("00005\r\nhello\r\n"));
411455
$this->input->emit('data', array("0000b\r\nhello world\r\n"));
456+
457+
$this->assertEquals(['hello', 'hello world'], $buffer);
412458
}
413459

414460
public function testLeadingZerosInEndChunkWillBeIgnored()

tests/TestCase.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,6 @@ protected function expectCallableNever()
3737
return $mock;
3838
}
3939

40-
protected function expectCallableConsecutive($numberOfCalls, array $with)
41-
{
42-
$mock = $this->createCallableMock();
43-
44-
if($numberOfCalls == 2){
45-
$mock->expects($this->exactly($numberOfCalls))->method('__invoke')->withConsecutive(
46-
array($this->equalTo($with[0])),
47-
array($this->equalTo($with[1]))
48-
);
49-
}
50-
51-
if($numberOfCalls == 3){
52-
$mock->expects($this->exactly($numberOfCalls))->method('__invoke')->withConsecutive(
53-
array($this->equalTo($with[0])),
54-
array($this->equalTo($with[1])),
55-
array($this->equalTo($with[2]))
56-
);
57-
}
58-
59-
if($numberOfCalls == 4){
60-
$mock->expects($this->exactly($numberOfCalls))->method('__invoke')->withConsecutive(
61-
array($this->equalTo($with[0])),
62-
array($this->equalTo($with[1])),
63-
array($this->equalTo($with[2])),
64-
array($this->equalTo($with[3]))
65-
);
66-
}
67-
return $mock;
68-
}
69-
7040
protected function createCallableMock()
7141
{
7242
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {

0 commit comments

Comments
 (0)