mirror of
https://github.com/xPaw/PHP-Source-Query.git
synced 2026-06-11 01:43:14 +02:00
Refactor unit tests to avoid persistent state.
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
+136
-105
@@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->socket->queue($rawInput);
|
$socket = new TestableSocket($socketType);
|
||||||
|
$sourceQuery = $this->create($socket);
|
||||||
|
|
||||||
$realOutput = $this->sourceQuery->getInfo();
|
$socket->queue($rawInput);
|
||||||
|
|
||||||
static::assertSame($expectedOutput, $realOutput);
|
$realOutput = $sourceQuery->getInfo();
|
||||||
|
|
||||||
|
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");
|
||||||
|
self::assertSame(['ayy' => 'lmao'], $sourceQuery->getRules());
|
||||||
|
|
||||||
$this->socket->queue("\xFF\xFF\xFF\xFF\x45\x01\x00wow\x00much\x00");
|
$socket->queue("\xFF\xFF\xFF\xFF\x45\x01\x00wow\x00much\x00");
|
||||||
static::assertSame(['wow' => 'much'], $this->sourceQuery->getRules());
|
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) {
|
$realOutput = $sourceQuery->getRules();
|
||||||
throw new InvalidPacketException('Bad packet data');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->socket->queue($data); // Challenge.
|
self::assertSame($expectedOutput, $realOutput);
|
||||||
|
|
||||||
foreach ($rawInput as $packet) {
|
|
||||||
$data = hex2bin($packet);
|
|
||||||
|
|
||||||
if (!$data) {
|
|
||||||
throw new InvalidPacketException('Bad packet data');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->socket->queue($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
$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.
|
self::assertSame($expectedOutput, $realOutput);
|
||||||
|
|
||||||
foreach ($rawInput as $packet) {
|
|
||||||
$data = hex2bin($packet);
|
|
||||||
|
|
||||||
if (!$data) {
|
|
||||||
throw new InvalidPacketException('Bad packet data');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->socket->queue($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
$realOutput = $this->sourceQuery->getPlayers();
|
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
$this->socket->queue("\xFF\xFF\xFF\xFF\xEE");
|
$socket->queue("\xFF\xFF\xFF\xFF\x6A\x00");
|
||||||
static::assertFalse($this->sourceQuery->ping());
|
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);
|
||||||
|
|
||||||
|
return $sourceQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user