Skip to content
This repository was archived by the owner on May 31, 2026. It is now read-only.

Commit 343991c

Browse files
committed
Simplify GameServer’s interface
1 parent 8aa4f5b commit 343991c

2 files changed

Lines changed: 40 additions & 71 deletions

File tree

lib/SteamCondenser/Servers/GameServer.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,6 @@ public function initialize() {
256256
$this->updateChallengeNumber();
257257
}
258258

259-
/**
260-
* Receives a response from the server
261-
*
262-
* @return SteamPacket The response packet replied by the server
263-
*/
264-
protected function getReply() {
265-
return $this->socket->getReply();
266-
}
267-
268259
/**
269260
* Sends the specified request to the server and handles the returned
270261
* response
@@ -302,9 +293,8 @@ protected function handleResponseForRequest($requestType, $repeatOnFailure = tru
302293
throw new SteamCondenserException('Called with wrong request type.');
303294
}
304295

305-
$this->sendRequest($requestPacket);
306-
307-
$responsePacket = $this->getReply();
296+
$this->socket->send($requestPacket);
297+
$responsePacket = $this->socket->getReply();
308298

309299
if($responsePacket instanceof S2AINFOBasePacket) {
310300
$this->infoHash = $responsePacket->getInfo();
@@ -361,15 +351,6 @@ abstract public function rconAuth($password);
361351
*/
362352
abstract public function rconExec($command);
363353

364-
/**
365-
* Sends a request packet to the server
366-
*
367-
* @param SteamPacket $requestData The request packet to send to the server
368-
*/
369-
protected function sendRequest(SteamPacket $requestData) {
370-
$this->socket->send($requestData);
371-
}
372-
373354
/**
374355
* Sends a A2S_SERVERQUERY_GETCHALLENGE request to the server and updates
375356
* the challenge number used to communicate with this server
@@ -397,9 +378,9 @@ public function updateChallengeNumber() {
397378
* @see initialize()
398379
*/
399380
public function updatePing() {
400-
$this->sendRequest(new A2SINFOPacket());
381+
$this->socket->send(new A2SINFOPacket());
401382
$startTime = microtime(true);
402-
$this->getReply();
383+
$this->socket->getReply();
403384
$endTime = microtime(true);
404385
$this->ping = intval(round(($endTime - $startTime) * 1000));
405386

tests/SteamCondenser/servers/GameServerTest.php

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,10 @@ abstract class TestableGameServer extends GameServer {
2626

2727
public $socket;
2828

29-
public function getReply() {
30-
return parent::getReply();
31-
}
32-
3329
public function handleResponseForRequest($request, $repeatOnFailure = true) {
3430
parent::handleResponseForRequest($request, $repeatOnFailure);
3531
}
3632

37-
public function sendRequest(Packets\SteamPacket $packet) {
38-
parent::sendRequest($packet);
39-
}
40-
4133
}
4234

4335
class GameServerTest extends \PHPUnit_Framework_TestCase {
@@ -47,33 +39,14 @@ public function setUp() {
4739
$this->serverBuilder->disableOriginalConstructor();
4840
}
4941

50-
public function testGetReply() {
51-
$packet = $this->getMockForAbstractClass('\SteamCondenser\Servers\Packets\SteamPacket', ['']);
52-
53-
$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->disableOriginalConstructor()->setMethods(['getReply'])->getMock();
54-
$socket->expects($this->once())->method('getReply')->will($this->returnValue($packet));
55-
$server = $this->getMockForAbstractClass('\SteamCondenser\Servers\TestableGameServer', ['127.0.0.1']);
56-
$server->socket = $socket;
57-
58-
$this->assertEquals($packet, $server->getReply());
59-
}
60-
61-
public function testSendRequest() {
62-
$packet = $this->getMockForAbstractClass('\SteamCondenser\Servers\Packets\SteamPacket', ['']);
42+
public function testUpdatePing() {
43+
$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
44+
$socket->expects($this->once())->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SINFOPacket'));
45+
$socket->expects($this->once())->method('getReply')->will($this->returnCallback(function() { usleep(50000); }));
6346

64-
$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->disableOriginalConstructor()->setMethods(['getReply', 'send'])->getMock();
65-
$socket->expects($this->once())->method('send')->with($packet);
66-
$server = $this->getMockForAbstractClass('\SteamCondenser\Servers\TestableGameServer', ['127.0.0.1']);
47+
$server = $this->serverBuilder->setMethods(['initSocket', 'rconAuth', 'rconExec'])->getMock();
6748
$server->socket = $socket;
6849

69-
$server->sendRequest($packet);
70-
}
71-
72-
public function testUpdatePing() {
73-
$server = $this->serverBuilder->setMethods(['getReply', 'initSocket', 'rconAuth', 'rconExec', 'sendRequest'])->getMock();
74-
$server->expects($this->once())->method('sendRequest')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SINFOPacket'));
75-
$server->expects($this->once())->method('getReply')->will($this->returnCallback(function() { usleep(50000); }));
76-
7750
$server->updatePing();
7851
$this->assertAttributeGreaterThanOrEqual(50, 'ping', $server);
7952
}
@@ -248,68 +221,83 @@ public function testGetPlayerInfoFromGoldSrcAuthenticated() {
248221
}
249222

250223
public function testHandleChallengeRequest() {
251-
$server = $this->serverBuilder->setMethods(['initSocket', 'getReply', 'rconAuth', 'rconExec', 'sendRequest'])->getMock();
252-
$server->expects($this->once())->method('sendRequest')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SPLAYERPacket'));
224+
$server = $this->serverBuilder->setMethods(['initSocket', 'rconAuth', 'rconExec'])->getMock();
253225

254226
$packet = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2CCHALLENGEPacket')->disableOriginalConstructor()->setMethods(['getChallengeNumber'])->getMock();
255227
$packet->expects($this->once())->method('getChallengeNumber')->will($this->returnValue(1234));
256-
$server->expects($this->once())->method('getReply')->will($this->returnValue($packet));
228+
229+
$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
230+
$socket->expects($this->once())->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SPLAYERPacket'));
231+
$socket->expects($this->once())->method('getReply')->will($this->returnValue($packet));
232+
$server->socket = $socket;
257233

258234
$server->handleResponseForRequest(GameServer::REQUEST_CHALLENGE);
259235

260236
$this->assertEquals(1234, $server->challengeNumber);
261237
}
262238

263239
public function testHandleInfoRequest() {
264-
$server = $this->serverBuilder->setMethods(['initSocket', 'getReply', 'rconAuth', 'rconExec', 'sendRequest'])->getMock();
265-
$server->expects($this->once())->method('sendRequest')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SINFOPacket'));
240+
$server = $this->serverBuilder->setMethods(['initSocket', 'rconAuth', 'rconExec'])->getMock();
266241

267242
$packet = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2AINFOBasePacket')->disableOriginalConstructor()->setMethods(['getInfo'])->getMock();
268243
$packet->expects($this->once())->method('getInfo')->will($this->returnValue(['test' => 'test']));
269-
$server->expects($this->once())->method('getReply')->will($this->returnValue($packet));
244+
245+
$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
246+
$socket->expects($this->once())->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SINFOPacket'));
247+
$socket->expects($this->once())->method('getReply')->will($this->returnValue($packet));
248+
$server->socket = $socket;
270249

271250
$server->handleResponseForRequest(GameServer::REQUEST_INFO);
272251

273252
$this->assertEquals(['test' => 'test'], $server->infoHash);
274253
}
275254

276255
public function testHandlePlayersRequest() {
277-
$server = $this->serverBuilder->setMethods(['initSocket', 'getReply', 'rconAuth', 'rconExec', 'sendRequest'])->getMock();
278-
$server->expects($this->once())->method('sendRequest')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SPLAYERPacket'));
256+
$server = $this->serverBuilder->setMethods(['initSocket', 'rconAuth', 'rconExec'])->getMock();
279257

280258
$packet = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2APLAYERPacket')->disableOriginalConstructor()->setMethods(['getPlayerHash'])->getMock();
281259
$packet->expects($this->once())->method('getPlayerHash')->will($this->returnValue(['test' => 'test']));
282-
$server->expects($this->once())->method('getReply')->will($this->returnValue($packet));
260+
261+
$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
262+
$socket->expects($this->once())->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SPLAYERPacket'));
263+
$socket->expects($this->once())->method('getReply')->will($this->returnValue($packet));
264+
$server->socket = $socket;
283265

284266
$server->handleResponseForRequest(GameServer::REQUEST_PLAYER);
285267

286268
$this->assertEquals(['test' => 'test'], $server->playerHash);
287269
}
288270

289271
public function testHandleRulesRequest() {
290-
$server = $this->serverBuilder->setMethods(['initSocket', 'getReply', 'rconAuth', 'rconExec', 'sendRequest'])->getMock();
291-
$server->expects($this->once())->method('sendRequest')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SRULESPacket'));
272+
$server = $this->serverBuilder->setMethods(['initSocket', 'rconAuth', 'rconExec'])->getMock();
292273

293274
$packet = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2ARULESPacket')->disableOriginalConstructor()->setMethods(['getRulesArray'])->getMock();
294275
$packet->expects($this->once())->method('getRulesArray')->will($this->returnValue(['test' => 'test']));
295-
$server->expects($this->once())->method('getReply')->will($this->returnValue($packet));
276+
277+
$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
278+
$socket->expects($this->once())->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SRULESPacket'));
279+
$socket->expects($this->once())->method('getReply')->will($this->returnValue($packet));
280+
$server->socket = $socket;
296281

297282
$server->handleResponseForRequest(GameServer::REQUEST_RULES);
298283

299284
$this->assertEquals(['test' => 'test'], $server->rulesHash);
300285
}
301286

302287
public function testHandleUnexpectedResponse() {
303-
$server = $this->serverBuilder->setMethods(['initSocket', 'getReply', 'rconAuth', 'rconExec', 'sendRequest'])->getMock();
288+
$server = $this->serverBuilder->setMethods(['initSocket', 'rconAuth', 'rconExec'])->getMock();
304289
$server->setLogger(\SteamCondenser\getLogger(get_class($server)));
305-
$server->expects($this->exactly(2))->method('sendRequest')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SPLAYERPacket'));
306290

307291
$packet1 = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2CCHALLENGEPacket')->disableOriginalConstructor()->setMethods(['getChallengeNumber'])->getMock();
308292
$packet1->expects($this->once())->method('getChallengeNumber')->will($this->returnValue(1234));
309-
$server->expects($this->at(1))->method('getReply')->will($this->returnValue($packet1));
310293
$packet2 = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2APLAYERPacket')->disableOriginalConstructor()->setMethods(['getPlayerHash'])->getMock();
311294
$packet2->expects($this->once())->method('getPlayerHash')->will($this->returnValue(['test' => 'test']));
312-
$server->expects($this->at(3))->method('getReply')->will($this->returnValue($packet2));
295+
296+
$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
297+
$socket->expects($this->exactly(2))->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SPLAYERPacket'));
298+
$socket->expects($this->at(1))->method('getReply')->will($this->returnValue($packet1));
299+
$socket->expects($this->at(3))->method('getReply')->will($this->returnValue($packet2));
300+
$server->socket = $socket;
313301

314302
$server->handleResponseForRequest(GameServer::REQUEST_PLAYER);
315303

0 commit comments

Comments
 (0)