From 824dc5d2a71c28a4cea09bd36afff2d4ad908c36 Mon Sep 17 00:00:00 2001 From: Daniel Maixner Date: Sun, 25 Dec 2022 16:33:18 +0100 Subject: [PATCH] Grinding 100% test coverage --- server/src/Core/DynamicFloor.php | 4 +- server/src/Core/GameFactory.php | 3 ++ server/src/Core/World.php | 6 ++- test/og/Shooting/BombTest.php | 56 +++++++++++++++++++++++++ test/og/Shooting/PlayerKillTest.php | 5 ++- test/og/Unit/BoxTest.php | 17 ++++++++ test/og/Unit/ProtocolTest.php | 65 +++++++++++++++++++++++++++++ test/og/World/WallTest.php | 33 +++++++++++++++ 8 files changed, 184 insertions(+), 5 deletions(-) diff --git a/server/src/Core/DynamicFloor.php b/server/src/Core/DynamicFloor.php index 0b5626d..1bf49fc 100644 --- a/server/src/Core/DynamicFloor.php +++ b/server/src/Core/DynamicFloor.php @@ -33,12 +33,12 @@ final class DynamicFloor extends Floor public function getHitAntiForce(Point $point): int { - return $this->hitAntiForce; + throw new GameException('Should not be here'); } public function getPlane(): string { - return 'xz'; + throw new GameException('Should not be here'); } } diff --git a/server/src/Core/GameFactory.php b/server/src/Core/GameFactory.php index 33ed4c5..243bccb 100644 --- a/server/src/Core/GameFactory.php +++ b/server/src/Core/GameFactory.php @@ -5,6 +5,9 @@ namespace cs\Core; class GameFactory { + /** + * @codeCoverageIgnore + */ public static function createDefaultCompetitive(): Game { $properties = new GameProperty(); diff --git a/server/src/Core/World.php b/server/src/Core/World.php index f3741d8..bdbbfa6 100644 --- a/server/src/Core/World.php +++ b/server/src/Core/World.php @@ -608,8 +608,9 @@ class World } /** - * @return array> * @internal + * @codeCoverageIgnore + * @return array> */ public function getWalls(): array { @@ -626,8 +627,9 @@ class World } /** - * @return array> * @internal + * @codeCoverageIgnore + * @return array> */ public function getFloors(): array { diff --git a/test/og/Shooting/BombTest.php b/test/og/Shooting/BombTest.php index 12625a3..d442953 100644 --- a/test/og/Shooting/BombTest.php +++ b/test/og/Shooting/BombTest.php @@ -3,7 +3,10 @@ namespace Test\Shooting; use cs\Core\GameState; +use cs\Core\Player; use cs\Core\Util; +use cs\Enum\BuyMenuItem; +use cs\Enum\Color; use cs\Enum\InventorySlot; use cs\Enum\RoundEndReason; use cs\Event\PlantEvent; @@ -56,4 +59,57 @@ class BombTest extends BaseTestCase ); } + protected function _testBombPlantRound(bool $shouldDefuse): void + { + $properties = $this->createNoPauseGameProperty(); + $properties->bomb_plant_time_ms = 1; + $properties->bomb_defuse_time_ms = 1600; + $properties->bomb_explode_time_ms = 1000; + + $game = $this->createTestGame(null, $properties); + $defender = new Player(2, Color::BLUE, false); + $game->addPlayer($defender); + $defender->getSight()->lookAt(180, -90); + + $roundEndEvent = null; + $game->onTick(function (GameState $state) { + if ($state->getTickId() === 1) { + $state->getPlayer(1)->equip(InventorySlot::SLOT_BOMB); + return; + } + $state->getPlayer(1)->attack(); + $state->getPlayer(2)->use(); + }); + $game->onEvents(function (array $events) use (&$roundEndEvent) { + foreach ($events as $event) { + if (!$roundEndEvent && $event instanceof RoundEndEvent) { + $roundEndEvent = $event; + } + } + }); + + if ($shouldDefuse) { + $defender->buyItem(BuyMenuItem::DEFUSE_KIT); + } + $game->start(); + + $this->assertInstanceOf(RoundEndEvent::class, $roundEndEvent); + $this->assertSame($shouldDefuse ? RoundEndReason::BOMB_DEFUSED : RoundEndReason::BOMB_EXPLODED, $roundEndEvent->reason); + if ($shouldDefuse) { + $this->assertTrue($game->getScore()->defendersIsWinning()); + $this->assertFalse($game->getScore()->attackersIsWinning()); + } else { + $this->assertTrue($game->getScore()->attackersIsWinning()); + $this->assertFalse($game->getScore()->defendersIsWinning()); + } + $this->assertFalse($game->getScore()->isTie()); + } + + public function testBombPlantDefuse(): void + { + $this->_testBombPlantRound(false); + $this->_testBombPlantRound(true); + } + + } diff --git a/test/og/Shooting/PlayerKillTest.php b/test/og/Shooting/PlayerKillTest.php index 5b55f23..7106a8c 100644 --- a/test/og/Shooting/PlayerKillTest.php +++ b/test/og/Shooting/PlayerKillTest.php @@ -48,7 +48,10 @@ class PlayerKillTest extends BaseTestCase $this->assertCount(2, $hits); $headHit = $hits[0]; $this->assertInstanceOf(HitBox::class, $headHit); - $this->assertInstanceOf(Wall::class, $hits[1]); + $wall = $hits[1]; + $this->assertInstanceOf(Wall::class, $wall); + $this->assertFalse($wall->playerWasKilled()); + $this->assertGreaterThan(0, $wall->getHitAntiForce(new Point())); $playerOne = $headHit->getPlayer(); $this->assertInstanceOf(Player::class, $playerOne); diff --git a/test/og/Unit/BoxTest.php b/test/og/Unit/BoxTest.php index 1037b44..3dacda3 100644 --- a/test/og/Unit/BoxTest.php +++ b/test/og/Unit/BoxTest.php @@ -4,6 +4,7 @@ namespace Test\Unit; use cs\Core\Box; use cs\Core\Floor; +use cs\Core\GameException; use cs\Core\Point; use cs\Core\Wall; use Test\BaseTest; @@ -58,6 +59,22 @@ class BoxTest extends BaseTest $this->assertPositionSame($point->clone()->addX($width), $rightWall->getStart()); $pointEnd = $point->clone()->addX($width)->addZ($depth)->addY($height); $this->assertPositionSame($pointEnd, $rightWall->getEnd()); + + $this->assertSame([ + 'width' => $width, + 'height' => $height, + 'depth' => $depth, + 'x' => $point->x, + 'y' => $point->y, + 'z' => $point->z, + ], $box->toArray()); + } + + public function testBoxWithoutSideThrow(): void + { + $this->expectException(GameException::class); + $this->expectExceptionMessage('Choose at least one box side'); + new Box(new Point(), 1,1,1, 0); } } diff --git a/test/og/Unit/ProtocolTest.php b/test/og/Unit/ProtocolTest.php index 207689f..707b817 100644 --- a/test/og/Unit/ProtocolTest.php +++ b/test/og/Unit/ProtocolTest.php @@ -7,6 +7,7 @@ use cs\Core\GameProperty; use cs\Core\GameState; use cs\Core\Player; use cs\Enum\Color; +use cs\Map\TestMap; use cs\Net\PlayerControl; use cs\Net\Protocol; use cs\Net\ProtocolReader; @@ -53,4 +54,68 @@ class ProtocolTest extends BaseTest $this->assertSame([], $protocol->parsePlayerControlCommands("lookAt 1")); } + public function testSerialization(): void + { + $player = new Player(1, Color::BLUE, true); + $game = new Game(new GameProperty()); + $game->loadMap(new TestMap()); + $game->addPlayer($player); + $player->getSight()->lookAt(12.45, 1.09); + $protocol = new Protocol\TextProtocol(); + + $playerSerializedExpected = [ + 'id' => 1, + 'color' => 1, + 'money' => 800, + 'item' => [ + 'id' => 2, + 'slot' => 2, + ], + 'canAttack' => false, + 'canBuy' => true, + 'canPlant' => false, + 'slots' => [ + 0 => [ + 'id' => 1, + 'slot' => 0, + ], + 2 => [ + 'id' => 2, + 'slot' => 2, + ], + 3 => [ + 'id' => 50, + 'slot' => 3, + ], + ], + 'health' => 100, + 'position' => [ + 'x' => 0, + 'y' => 0, + 'z' => 0, + ], + 'look' => [ + 'horizontal' => 12.45, + 'vertical' => 1.09, + ], + 'isAttacker' => true, + 'sight' => 180, + 'armor' => 0, + 'armorType' => 0, + 'ammo' => 12, + 'ammoReserve' => 120, + 'isReloading' => false, + ]; + $this->assertSame($playerSerializedExpected, $player->serialize()); + + $expected = [ + 'players' => [ + $playerSerializedExpected, + ], + 'events' => [], + ]; + $actual = $protocol->serialize($game->getPlayers(), []); + $this->assertSame($expected, json_decode($actual, true)); + } + } diff --git a/test/og/World/WallTest.php b/test/og/World/WallTest.php index 7bf3b48..9c4ed3e 100644 --- a/test/og/World/WallTest.php +++ b/test/og/World/WallTest.php @@ -70,6 +70,39 @@ class WallTest extends BaseTestCase $this->assertNotNull($world->checkXSideWallCollision(new Point($x, 0, 0), 12, 20)); } + public function testWallSerialization(): void + { + $wall = new Wall(new Point(1, 2, 3), true, 10, 20); + $this->assertSame([ + 's' => [ + 'x' => 1, + 'y' => 2, + 'z' => 3, + ], + 'e' =>[ + 'x' => 11, + 'y' => 22, + 'z' => 3, + ], + 'p' => 'xy', + ], $wall->toArray()); + + $wall = new Wall(new Point(1, 2, 3), false, 10, 20); + $this->assertSame([ + 's' => [ + 'x' => 1, + 'y' => 2, + 'z' => 3, + ], + 'e' =>[ + 'x' => 1, + 'y' => 22, + 'z' => 13, + ], + 'p' => 'zy', + ], $wall->toArray()); + } + public function testRampGenerate(): void { $stepDepth = 2;