Refactor unit tests to avoid persistent state.

pull/150/head
Anthony Birkett 4 years ago
parent 0e4d7d4a6b
commit 266a2227d3

@ -11,8 +11,6 @@
"Os": "w", "Os": "w",
"Password": true, "Password": true,
"IsMod": true, "IsMod": true,
"Secure": false,
"Bots": 0,
"Mod": { "Mod": {
"Url": "", "Url": "",
"Download": "", "Download": "",
@ -20,5 +18,7 @@
"Size": 0, "Size": 0,
"ServerSide": true, "ServerSide": true,
"CustomDLL": false "CustomDLL": false
} },
"Secure": false,
"Bots": 0
} }

@ -17,85 +17,75 @@ use xPaw\SourceQuery\SourceQuery;
*/ */
final class Tests extends TestCase final class Tests extends TestCase
{ {
private TestableSocket $socket;
private SourceQuery $sourceQuery;
/**
* @throws InvalidArgumentException
*/
protected function setUp(): void
{
$this->socket = new TestableSocket(SocketType::SOURCE);
$this->sourceQuery = new SourceQuery($this->socket);
$this->sourceQuery->connect('', 2);
}
/**
* tearDown.
*/
protected function tearDown(): void
{
$this->sourceQuery->disconnect();
$this->socket = null;
$this->sourceQuery = null;
}
/** /**
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function testInvalidTimeout(): void public function testInvalidTimeout(): void
{ {
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
$SourceQuery = new SourceQuery($this->socket); $socket = new TestableSocket(SocketType::SOURCE);
$SourceQuery->connect('', 2, -1); $sourceQuery = $this->create($socket);
$sourceQuery->disconnect();
$sourceQuery->connect('', 2, -1);
} }
/** /**
* @throws InvalidArgumentException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
*/ */
public function testNotConnectedGetInfo(): void public function testNotConnectedGetInfo(): void
{ {
$this->expectException(SocketException::class); $this->expectException(SocketException::class);
$this->sourceQuery->disconnect(); $socket = new TestableSocket(SocketType::SOURCE);
$this->sourceQuery->getInfo(); $sourceQuery = $this->create($socket);
$sourceQuery->disconnect();
$sourceQuery->getInfo();
} }
/** /**
* @throws InvalidArgumentException
* @throws SocketException * @throws SocketException
*/ */
public function testNotConnectedPing(): void public function testNotConnectedPing(): void
{ {
$this->expectException(SocketException::class); $this->expectException(SocketException::class);
$this->sourceQuery->disconnect(); $socket = new TestableSocket(SocketType::SOURCE);
$this->sourceQuery->ping(); $sourceQuery = $this->create($socket);
$sourceQuery->disconnect();
$sourceQuery->ping();
} }
/** /**
* @throws InvalidArgumentException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
*/ */
public function testNotConnectedGetPlayers(): void public function testNotConnectedGetPlayers(): void
{ {
$this->expectException(SocketException::class); $this->expectException(SocketException::class);
$this->sourceQuery->disconnect(); $socket = new TestableSocket(SocketType::SOURCE);
$this->sourceQuery->getPlayers(); $sourceQuery = $this->create($socket);
$sourceQuery->disconnect();
$sourceQuery->getPlayers();
} }
/** /**
* @throws InvalidArgumentException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
*/ */
public function testNotConnectedGetRules(): void public function testNotConnectedGetRules(): void
{ {
$this->expectException(SocketException::class); $this->expectException(SocketException::class);
$this->sourceQuery->disconnect(); $socket = new TestableSocket(SocketType::SOURCE);
$this->sourceQuery->getRules(); $sourceQuery = $this->create($socket);
$sourceQuery->disconnect();
$sourceQuery->getRules();
} }
/** /**
* @throws InvalidArgumentException
* @throws SocketException * @throws SocketException
* @throws AuthenticationException * @throws AuthenticationException
* @throws InvalidPacketException * @throws InvalidPacketException
@ -103,11 +93,14 @@ final class Tests extends TestCase
public function testNotConnectedSetRconPassword(): void public function testNotConnectedSetRconPassword(): void
{ {
$this->expectException(SocketException::class); $this->expectException(SocketException::class);
$this->sourceQuery->disconnect(); $socket = new TestableSocket(SocketType::SOURCE);
$this->sourceQuery->setRconPassword('a'); $sourceQuery = $this->create($socket);
$sourceQuery->disconnect();
$sourceQuery->setRconPassword('a');
} }
/** /**
* @throws InvalidArgumentException
* @throws AuthenticationException * @throws AuthenticationException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
@ -115,11 +108,14 @@ final class Tests extends TestCase
public function testNotConnectedRcon(): void public function testNotConnectedRcon(): void
{ {
$this->expectException(SocketException::class); $this->expectException(SocketException::class);
$this->sourceQuery->disconnect(); $socket = new TestableSocket(SocketType::SOURCE);
$this->sourceQuery->rcon('a'); $sourceQuery = $this->create($socket);
$sourceQuery->disconnect();
$sourceQuery->rcon('a');
} }
/** /**
* @throws InvalidArgumentException
* @throws AuthenticationException * @throws AuthenticationException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
@ -127,7 +123,9 @@ final class Tests extends TestCase
public function testRconWithoutPassword(): void public function testRconWithoutPassword(): void
{ {
$this->expectException(SocketException::class); $this->expectException(SocketException::class);
$this->sourceQuery->rcon('a'); $socket = new TestableSocket(SocketType::SOURCE);
$sourceQuery = $this->create($socket);
$sourceQuery->rcon('a');
} }
/** /**
@ -139,17 +137,18 @@ final class Tests extends TestCase
*/ */
public function testGetInfo(string $rawInput, array $expectedOutput): void public function testGetInfo(string $rawInput, array $expectedOutput): void
{ {
if (isset($expectedOutput['IsMod'])) { $socketType = isset($expectedOutput['IsMod'])
$this->socket = new TestableSocket(SocketType::GOLDSOURCE); ? SocketType::GOLDSOURCE
$this->sourceQuery = new SourceQuery($this->socket); : SocketType::SOURCE;
$this->sourceQuery->connect('', 2);
} $socket = new TestableSocket($socketType);
$sourceQuery = $this->create($socket);
$this->socket->queue($rawInput); $socket->queue($rawInput);
$realOutput = $this->sourceQuery->getInfo(); $realOutput = $sourceQuery->getInfo();
static::assertSame($expectedOutput, $realOutput); self::assertSame($expectedOutput, $realOutput);
} }
/** /**
@ -161,6 +160,7 @@ final class Tests extends TestCase
} }
/** /**
* @throws InvalidArgumentException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
* *
@ -169,12 +169,15 @@ final class Tests extends TestCase
public function testBadGetInfo(string $data): void public function testBadGetInfo(string $data): void
{ {
$this->expectException(InvalidPacketException::class); $this->expectException(InvalidPacketException::class);
$this->socket->queue($data); $socket = new TestableSocket(SocketType::SOURCE);
$sourceQuery = $this->create($socket);
$socket->queue($data);
$this->sourceQuery->getInfo(); $sourceQuery->getInfo();
} }
/** /**
* @throws InvalidArgumentException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
* *
@ -183,12 +186,15 @@ final class Tests extends TestCase
public function testBadGetChallengeViaPlayers(string $data): void public function testBadGetChallengeViaPlayers(string $data): void
{ {
$this->expectException(InvalidPacketException::class); $this->expectException(InvalidPacketException::class);
$this->socket->queue($data); $socket = new TestableSocket(SocketType::SOURCE);
$sourceQuery = $this->create($socket);
$socket->queue($data);
$this->sourceQuery->getPlayers(); $sourceQuery->getPlayers();
} }
/** /**
* @throws InvalidArgumentException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
* *
@ -197,13 +203,16 @@ final class Tests extends TestCase
public function testBadGetPlayersAfterCorrectChallenge(string $data): void public function testBadGetPlayersAfterCorrectChallenge(string $data): void
{ {
$this->expectException(InvalidPacketException::class); $this->expectException(InvalidPacketException::class);
$this->socket->queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11"); $socket = new TestableSocket(SocketType::SOURCE);
$this->socket->queue($data); $sourceQuery = $this->create($socket);
$socket->queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11");
$socket->queue($data);
$this->sourceQuery->getPlayers(); $sourceQuery->getPlayers();
} }
/** /**
* @throws InvalidArgumentException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
* *
@ -212,10 +221,12 @@ final class Tests extends TestCase
public function testBadGetRulesAfterCorrectChallenge(string $data): void public function testBadGetRulesAfterCorrectChallenge(string $data): void
{ {
$this->expectException(InvalidPacketException::class); $this->expectException(InvalidPacketException::class);
$this->socket->queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11"); $socket = new TestableSocket(SocketType::SOURCE);
$this->socket->queue($data); $sourceQuery = $this->create($socket);
$socket->queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11");
$socket->queue($data);
$this->sourceQuery->getRules(); $sourceQuery->getRules();
} }
/** /**
@ -236,22 +247,26 @@ final class Tests extends TestCase
} }
/** /**
* @throws InvalidArgumentException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
*/ */
public function testGetChallengeTwice(): void public function testGetChallengeTwice(): void
{ {
$this->socket->queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11"); $socket = new TestableSocket(SocketType::SOURCE);
$this->socket->queue("\xFF\xFF\xFF\xFF\x45\x01\x00ayy\x00lmao\x00"); $sourceQuery = $this->create($socket);
static::assertSame(['ayy' => 'lmao'], $this->sourceQuery->getRules()); $socket->queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11");
$socket->queue("\xFF\xFF\xFF\xFF\x45\x01\x00ayy\x00lmao\x00");
$this->socket->queue("\xFF\xFF\xFF\xFF\x45\x01\x00wow\x00much\x00"); self::assertSame(['ayy' => 'lmao'], $sourceQuery->getRules());
static::assertSame(['wow' => 'much'], $this->sourceQuery->getRules());
$socket->queue("\xFF\xFF\xFF\xFF\x45\x01\x00wow\x00much\x00");
self::assertSame(['wow' => 'much'], $sourceQuery->getRules());
} }
/** /**
* @param string[] $rawInput * @param string[] $rawInput
* *
* @throws InvalidArgumentException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
* *
@ -259,27 +274,11 @@ final class Tests extends TestCase
*/ */
public function testGetRules(array $rawInput, array $expectedOutput): void public function testGetRules(array $rawInput, array $expectedOutput): void
{ {
$data = hex2bin('ffffffff4104fce20e'); $sourceQuery = $this->putDataOnSocket($rawInput);
if (!$data) {
throw new InvalidPacketException('Bad packet data');
}
$this->socket->queue($data); // Challenge.
foreach ($rawInput as $packet) { $realOutput = $sourceQuery->getRules();
$data = hex2bin($packet);
if (!$data) {
throw new InvalidPacketException('Bad packet data');
}
$this->socket->queue($data); self::assertSame($expectedOutput, $realOutput);
}
$realOutput = $this->sourceQuery->getRules();
static::assertSame($expectedOutput, $realOutput);
} }
/** /**
@ -293,6 +292,7 @@ final class Tests extends TestCase
/** /**
* @param string[] $rawInput * @param string[] $rawInput
* *
* @throws InvalidArgumentException
* @throws InvalidPacketException * @throws InvalidPacketException
* @throws SocketException * @throws SocketException
* *
@ -300,27 +300,11 @@ final class Tests extends TestCase
*/ */
public function testGetPlayers(array $rawInput, array $expectedOutput): void public function testGetPlayers(array $rawInput, array $expectedOutput): void
{ {
$data = hex2bin('ffffffff4104fce20e'); $sourceQuery = $this->putDataOnSocket($rawInput);
if (!$data) { $realOutput = $sourceQuery->getPlayers();
throw new InvalidPacketException('Bad packet data');
}
$this->socket->queue($data); // Challenge.
foreach ($rawInput as $packet) {
$data = hex2bin($packet);
if (!$data) {
throw new InvalidPacketException('Bad packet data');
}
$this->socket->queue($data);
}
$realOutput = $this->sourceQuery->getPlayers(); self::assertSame($expectedOutput, $realOutput);
static::assertSame($expectedOutput, $realOutput);
} }
/** /**
@ -332,15 +316,30 @@ final class Tests extends TestCase
} }
/** /**
* @throws InvalidArgumentException
* @throws SocketException * @throws SocketException
*/ */
public function testPing(): void public function testPing(): void
{ {
$this->socket->queue("\xFF\xFF\xFF\xFF\x6A\x00"); $socket = new TestableSocket(SocketType::SOURCE);
static::assertTrue($this->sourceQuery->ping()); $sourceQuery = $this->create($socket);
$socket->queue("\xFF\xFF\xFF\xFF\x6A\x00");
self::assertTrue($sourceQuery->ping());
$socket->queue("\xFF\xFF\xFF\xFF\xEE");
self::assertFalse($sourceQuery->ping());
}
/**
* @throws InvalidArgumentException
*/
private function create(TestableSocket $socket): SourceQuery
{
$sourceQuery = new SourceQuery($socket);
$sourceQuery->connect('', 2);
$this->socket->queue("\xFF\xFF\xFF\xFF\xEE"); return $sourceQuery;
static::assertFalse($this->sourceQuery->ping());
} }
/** /**
@ -391,4 +390,36 @@ final class Tests extends TestCase
return $dataProvider; return $dataProvider;
} }
/**
* @param string[] $rawInput
*
* @throws InvalidArgumentException
* @throws InvalidPacketException
*/
private function putDataOnSocket(array $rawInput): SourceQuery
{
$data = hex2bin('ffffffff4104fce20e');
if (!$data) {
throw new InvalidPacketException('Bad packet data');
}
$socket = new TestableSocket(SocketType::SOURCE);
$sourceQuery = $this->create($socket);
$socket->queue($data); // Challenge.
foreach ($rawInput as $packet) {
$data = hex2bin($packet);
if (!$data) {
throw new InvalidPacketException('Bad packet data');
}
$socket->queue($data);
}
return $sourceQuery;
}
} }

Loading…
Cancel
Save