diff --git a/tests/AccountTestCase.php b/tests/AccountTestCase.php index 78dd0f8b..805e3b84 100644 --- a/tests/AccountTestCase.php +++ b/tests/AccountTestCase.php @@ -63,9 +63,12 @@ protected function setUp(): void // Set default test time to 2024-01-01 00:00:00 to ensure all tests have the same starting point. $this->travelTo(Carbon::create(2024, 1, 1, 0, 0, 0)); + // Set default server settings for all tests. + $settingsService = resolve(SettingsService::class); + $settingsService->set('economy_speed', 8); + // Set amount of planets to be created for the user because planet switching // is a part of the test suite. - $settingsService = resolve(SettingsService::class); $settingsService->set('registration_planet_amount', $this->userPlanetAmount); // Create a new user and login so we can access ingame features. @@ -235,6 +238,35 @@ protected function getNearbyForeignPlanet(): PlanetService } } + /** + * Creates a new clean planet for a foreign player in a nearby system. + * This is useful for testing interactions with a fresh hostile planet that has no history. + * + * @return PlanetService + */ + protected function getNearbyForeignCleanPlanet(): PlanetService + { + // First get a nearby foreign planet to obtain its player + $foreignPlanet = $this->getNearbyForeignPlanet(); + $foreignPlayer = $foreignPlanet->getPlayer(); + + // Get a random empty coordinate near the current planet + $coordinate = $this->getNearbyEmptyCoordinate(); + + // Create a new planet at this coordinate for the foreign player + $planetServiceFactory = resolve(PlanetServiceFactory::class); + $newPlanet = $planetServiceFactory->createAdditionalPlanetForPlayer($foreignPlayer, $coordinate); + + if ($newPlanet === null) { + $this->fail('Failed to create a new clean planet'); + } + + // Reload the foreign player to include the new planet + $foreignPlayer->load($foreignPlayer->getId()); + + return $newPlanet; + } + /** * Gets a nearby foreign moon for the current user. This is useful for testing interactions between two players. * diff --git a/tests/Feature/FleetDispatch/FleetDispatchAttackTest.php b/tests/Feature/FleetDispatch/FleetDispatchAttackTest.php index 6d2487eb..cdf264f3 100644 --- a/tests/Feature/FleetDispatch/FleetDispatchAttackTest.php +++ b/tests/Feature/FleetDispatch/FleetDispatchAttackTest.php @@ -420,6 +420,11 @@ public function testDispatchFleetRecallMissionTwiceError(): void */ public function testDispatchFleetCombatUnitsLostAndResourceGained(): void { + // Disable all resource generation in server settings to ensure we're not affected by it + // when comparing resources before and after battle. + $settingsService = resolve(SettingsService::class); + $settingsService->set('economy_speed', 0); + // Send fleet to a nearby foreign planet. // Attack with 200 light fighters, defend with 100 rocket launchers. // We expect attacker to win in +/- 4 rounds, while losing 10-50 light fighters. @@ -428,21 +433,17 @@ public function testDispatchFleetCombatUnitsLostAndResourceGained(): void $unitCollection = new UnitCollection(); $unitCollection->addUnit(ObjectService::getUnitObjectByMachineName('light_fighter'), 200); - $foreignPlanet = $this->sendMissionToOtherPlayerPlanet($unitCollection, new Resources(0, 0, 0, 0)); - - // Clear existing units from foreign planet. - $foreignPlanet->removeUnits($foreignPlanet->getShipUnits(), true); - $foreignPlanet->removeUnits($foreignPlanet->getDefenseUnits(), true); + $foreignPlanet = $this->sendMissionToOtherPlayerCleanPlanet($unitCollection, new Resources(0, 0, 0, 0)); // Give the foreign planet some units to defend itself. $foreignPlanet->addUnit('rocket_launcher', 100); + // Increase time by 24 hours to ensure the mission is done and fleets have returned. + $this->travel(24)->hours(); + // Reload application to make sure the planet is not cached. $this->reloadApplication(); - // Increase time by 2 hours to ensure the mission is done. - $this->travel(2)->hours(); - // Get amount of resources of the foreign planet before the battle. $attackerResourcesBefore = $this->planetService->getResources(); $foreignPlanetResourcesBefore = $foreignPlanet->getResources(); @@ -451,9 +452,10 @@ public function testDispatchFleetCombatUnitsLostAndResourceGained(): void $response = $this->get('/overview'); $response->assertStatus(200); - // Assert that attacker has less than 200 light fighters after battle. + // Assert that attacker has more than 0 but less than 200 light fighters after battle. $this->planetService->reloadPlanet(); - $this->assertLessThan(200, $this->planetService->getObjectAmount('light_fighter'), 'Attacker still has 150 light fighters after battle while it was expected they lost some.'); + $this->assertGreaterThan(0, $this->planetService->getObjectAmount('light_fighter'), 'Attacker has no light fighters after battle while it was expected some should have survived and returned.'); + $this->assertLessThan(200, $this->planetService->getObjectAmount('light_fighter'), 'Attacker still has 200 light fighters after battle while it was expected they lost some.'); // Assert that the defender has lost all units. $foreignPlanet->reloadPlanet(); diff --git a/tests/FleetDispatchTestCase.php b/tests/FleetDispatchTestCase.php index ea216588..d3c6c731 100644 --- a/tests/FleetDispatchTestCase.php +++ b/tests/FleetDispatchTestCase.php @@ -144,6 +144,22 @@ protected function sendMissionToOtherPlayerPlanet(UnitCollection $units, Resourc return $nearbyForeignPlanet; } + /** + * Send a fleet to a new clean planet of another player. + * + * @param UnitCollection $units + * @param Resources $resources + * @param int $assertStatus + * @return PlanetService + */ + protected function sendMissionToOtherPlayerCleanPlanet(UnitCollection $units, Resources $resources, int $assertStatus = 200): PlanetService + { + $nearbyForeignCleanPlanet = $this->getNearbyForeignCleanPlanet(); + + $this->dispatchFleet($nearbyForeignCleanPlanet->getPlanetCoordinates(), $units, $resources, PlanetType::Planet, $assertStatus); + return $nearbyForeignCleanPlanet; + } + /** * Send a fleet to a moon of another player. *