Skip to content

Commit 818a59b

Browse files
committed
Simplify testing callbacks
1 parent ce015ec commit 818a59b

3 files changed

Lines changed: 23 additions & 30 deletions

File tree

tests/ConnectionTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ public function testGetRemoteAddress()
2929

3030
$servConn = new Connection($server->master, $loop);
3131

32-
$mock = $this->createCallableMock();
33-
$mock
34-
->expects($this->once())
35-
->method('__invoke')
36-
->with($method->invokeArgs($servConn, array(stream_socket_get_name($master->getValue($server), false))))
37-
;
32+
$mock = $this->expectCallableOnceWith(
33+
$method->invokeArgs($servConn, array(stream_socket_get_name($master->getValue($server), false)))
34+
);
3835

3936
$server->on('connection', function ($conn) use ($mock) {
4037
$mock($conn->getRemoteAddress());

tests/ServerTest.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testConnectionWithManyClients()
6464
* @covers React\EventLoop\StreamSelectLoop::tick
6565
* @covers React\Socket\Connection::handleData
6666
*/
67-
public function testDataWithNoData()
67+
public function testDataEventWillNotBeEmittedWhenClientSendsNoData()
6868
{
6969
$client = stream_socket_client('tcp://localhost:'.$this->port);
7070

@@ -81,17 +81,13 @@ public function testDataWithNoData()
8181
* @covers React\EventLoop\StreamSelectLoop::tick
8282
* @covers React\Socket\Connection::handleData
8383
*/
84-
public function testData()
84+
public function testDataWillBeEmittedWithDataClientSends()
8585
{
8686
$client = stream_socket_client('tcp://localhost:'.$this->port);
8787

8888
fwrite($client, "foo\n");
8989

90-
$mock = $this->createCallableMock();
91-
$mock
92-
->expects($this->once())
93-
->method('__invoke')
94-
->with("foo\n");
90+
$mock = $this->expectCallableOnceWith("foo\n");
9591

9692
$this->server->on('connection', function ($conn) use ($mock) {
9793
$conn->on('data', $mock);
@@ -101,23 +97,16 @@ public function testData()
10197
}
10298

10399
/**
104-
* Test data sent from python language
105-
*
106100
* @covers React\EventLoop\StreamSelectLoop::tick
107101
* @covers React\Socket\Connection::handleData
108102
*/
109-
public function testDataSentFromPy()
103+
public function testDataWillBeEmittedEvenWhenClientShutsDownAfterSending()
110104
{
111105
$client = stream_socket_client('tcp://localhost:' . $this->port);
112106
fwrite($client, "foo\n");
113107
stream_socket_shutdown($client, STREAM_SHUT_WR);
114108

115-
$mock = $this->createCallableMock();
116-
$mock
117-
->expects($this->once())
118-
->method('__invoke')
119-
->with("foo\n");
120-
109+
$mock = $this->expectCallableOnceWith("foo\n");
121110

122111
$this->server->on('connection', function ($conn) use ($mock) {
123112
$conn->on('data', $mock);
@@ -126,17 +115,13 @@ public function testDataSentFromPy()
126115
$this->loop->tick();
127116
}
128117

129-
public function testFragmentedMessage()
118+
public function testDataWillBeFragmentedToBufferSize()
130119
{
131120
$client = stream_socket_client('tcp://localhost:' . $this->port);
132121

133122
fwrite($client, "Hello World!\n");
134123

135-
$mock = $this->createCallableMock();
136-
$mock
137-
->expects($this->once())
138-
->method('__invoke')
139-
->with("He");
124+
$mock = $this->expectCallableOnceWith("He");
140125

141126
$this->server->on('connection', function ($conn) use ($mock) {
142127
$conn->bufferSize = 2;
@@ -149,7 +134,7 @@ public function testFragmentedMessage()
149134
/**
150135
* @covers React\EventLoop\StreamSelectLoop::tick
151136
*/
152-
public function testDisconnectWithoutDisconnect()
137+
public function testConnectionDoesNotEndWhenClientDoesNotClose()
153138
{
154139
$client = stream_socket_client('tcp://localhost:'.$this->port);
155140

@@ -166,7 +151,7 @@ public function testDisconnectWithoutDisconnect()
166151
* @covers React\EventLoop\StreamSelectLoop::tick
167152
* @covers React\Socket\Connection::end
168153
*/
169-
public function testDisconnect()
154+
public function testConnectionDoesEndWhenClientCloses()
170155
{
171156
$client = stream_socket_client('tcp://localhost:'.$this->port);
172157

tests/TestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ protected function expectCallableOnce()
2424
return $mock;
2525
}
2626

27+
protected function expectCallableOnceWith($value)
28+
{
29+
$mock = $this->createCallableMock();
30+
$mock
31+
->expects($this->once())
32+
->method('__invoke')
33+
->with($value);
34+
35+
return $mock;
36+
}
37+
2738
protected function expectCallableNever()
2839
{
2940
$mock = $this->createCallableMock();

0 commit comments

Comments
 (0)