Skip to content

Commit

Permalink
Simplify GameServer’s interface
Browse files Browse the repository at this point in the history
  • Loading branch information
koraktor committed Nov 5, 2015
1 parent 8aa4f5b commit 343991c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 71 deletions.
27 changes: 4 additions & 23 deletions lib/SteamCondenser/Servers/GameServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,6 @@ public function initialize() {
$this->updateChallengeNumber();
}

/**
* Receives a response from the server
*
* @return SteamPacket The response packet replied by the server
*/
protected function getReply() {
return $this->socket->getReply();
}

/**
* Sends the specified request to the server and handles the returned
* response
Expand Down Expand Up @@ -302,9 +293,8 @@ protected function handleResponseForRequest($requestType, $repeatOnFailure = tru
throw new SteamCondenserException('Called with wrong request type.');
}

$this->sendRequest($requestPacket);

$responsePacket = $this->getReply();
$this->socket->send($requestPacket);
$responsePacket = $this->socket->getReply();

if($responsePacket instanceof S2AINFOBasePacket) {
$this->infoHash = $responsePacket->getInfo();
Expand Down Expand Up @@ -361,15 +351,6 @@ abstract public function rconAuth($password);
*/
abstract public function rconExec($command);

/**
* Sends a request packet to the server
*
* @param SteamPacket $requestData The request packet to send to the server
*/
protected function sendRequest(SteamPacket $requestData) {
$this->socket->send($requestData);
}

/**
* Sends a A2S_SERVERQUERY_GETCHALLENGE request to the server and updates
* the challenge number used to communicate with this server
Expand Down Expand Up @@ -397,9 +378,9 @@ public function updateChallengeNumber() {
* @see initialize()
*/
public function updatePing() {
$this->sendRequest(new A2SINFOPacket());
$this->socket->send(new A2SINFOPacket());
$startTime = microtime(true);
$this->getReply();
$this->socket->getReply();
$endTime = microtime(true);
$this->ping = intval(round(($endTime - $startTime) * 1000));

Expand Down
84 changes: 36 additions & 48 deletions tests/SteamCondenser/servers/GameServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,10 @@ abstract class TestableGameServer extends GameServer {

public $socket;

public function getReply() {
return parent::getReply();
}

public function handleResponseForRequest($request, $repeatOnFailure = true) {
parent::handleResponseForRequest($request, $repeatOnFailure);
}

public function sendRequest(Packets\SteamPacket $packet) {
parent::sendRequest($packet);
}

}

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

public function testGetReply() {
$packet = $this->getMockForAbstractClass('\SteamCondenser\Servers\Packets\SteamPacket', ['']);

$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->disableOriginalConstructor()->setMethods(['getReply'])->getMock();
$socket->expects($this->once())->method('getReply')->will($this->returnValue($packet));
$server = $this->getMockForAbstractClass('\SteamCondenser\Servers\TestableGameServer', ['127.0.0.1']);
$server->socket = $socket;

$this->assertEquals($packet, $server->getReply());
}

public function testSendRequest() {
$packet = $this->getMockForAbstractClass('\SteamCondenser\Servers\Packets\SteamPacket', ['']);
public function testUpdatePing() {
$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
$socket->expects($this->once())->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SINFOPacket'));
$socket->expects($this->once())->method('getReply')->will($this->returnCallback(function() { usleep(50000); }));

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

$server->sendRequest($packet);
}

public function testUpdatePing() {
$server = $this->serverBuilder->setMethods(['getReply', 'initSocket', 'rconAuth', 'rconExec', 'sendRequest'])->getMock();
$server->expects($this->once())->method('sendRequest')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SINFOPacket'));
$server->expects($this->once())->method('getReply')->will($this->returnCallback(function() { usleep(50000); }));

$server->updatePing();
$this->assertAttributeGreaterThanOrEqual(50, 'ping', $server);
}
Expand Down Expand Up @@ -248,68 +221,83 @@ public function testGetPlayerInfoFromGoldSrcAuthenticated() {
}

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

$packet = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2CCHALLENGEPacket')->disableOriginalConstructor()->setMethods(['getChallengeNumber'])->getMock();
$packet->expects($this->once())->method('getChallengeNumber')->will($this->returnValue(1234));
$server->expects($this->once())->method('getReply')->will($this->returnValue($packet));

$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
$socket->expects($this->once())->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SPLAYERPacket'));
$socket->expects($this->once())->method('getReply')->will($this->returnValue($packet));
$server->socket = $socket;

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

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

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

$packet = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2AINFOBasePacket')->disableOriginalConstructor()->setMethods(['getInfo'])->getMock();
$packet->expects($this->once())->method('getInfo')->will($this->returnValue(['test' => 'test']));
$server->expects($this->once())->method('getReply')->will($this->returnValue($packet));

$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
$socket->expects($this->once())->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SINFOPacket'));
$socket->expects($this->once())->method('getReply')->will($this->returnValue($packet));
$server->socket = $socket;

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

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

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

$packet = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2APLAYERPacket')->disableOriginalConstructor()->setMethods(['getPlayerHash'])->getMock();
$packet->expects($this->once())->method('getPlayerHash')->will($this->returnValue(['test' => 'test']));
$server->expects($this->once())->method('getReply')->will($this->returnValue($packet));

$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
$socket->expects($this->once())->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SPLAYERPacket'));
$socket->expects($this->once())->method('getReply')->will($this->returnValue($packet));
$server->socket = $socket;

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

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

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

$packet = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2ARULESPacket')->disableOriginalConstructor()->setMethods(['getRulesArray'])->getMock();
$packet->expects($this->once())->method('getRulesArray')->will($this->returnValue(['test' => 'test']));
$server->expects($this->once())->method('getReply')->will($this->returnValue($packet));

$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
$socket->expects($this->once())->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SRULESPacket'));
$socket->expects($this->once())->method('getReply')->will($this->returnValue($packet));
$server->socket = $socket;

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

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

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

$packet1 = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2CCHALLENGEPacket')->disableOriginalConstructor()->setMethods(['getChallengeNumber'])->getMock();
$packet1->expects($this->once())->method('getChallengeNumber')->will($this->returnValue(1234));
$server->expects($this->at(1))->method('getReply')->will($this->returnValue($packet1));
$packet2 = $this->getMockBuilder('\SteamCondenser\Servers\Packets\S2APLAYERPacket')->disableOriginalConstructor()->setMethods(['getPlayerHash'])->getMock();
$packet2->expects($this->once())->method('getPlayerHash')->will($this->returnValue(['test' => 'test']));
$server->expects($this->at(3))->method('getReply')->will($this->returnValue($packet2));

$socket = $this->getMockBuilder('\SteamCondenser\Servers\Sockets\SteamSocket')->setMethods(['getReply', 'send'])->disableOriginalConstructor()->getMock();
$socket->expects($this->exactly(2))->method('send')->with($this->isInstanceOf('\SteamCondenser\Servers\Packets\A2SPLAYERPacket'));
$socket->expects($this->at(1))->method('getReply')->will($this->returnValue($packet1));
$socket->expects($this->at(3))->method('getReply')->will($this->returnValue($packet2));
$server->socket = $socket;

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

Expand Down

0 comments on commit 343991c

Please sign in to comment.