Skip to content

Commit

Permalink
Add more specs to FleetJourney component
Browse files Browse the repository at this point in the history
  • Loading branch information
senghe committed Nov 26, 2023
1 parent 37b4d0a commit cb62b72
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,45 @@
namespace spec\TheGame\Application\Component\FleetJourney\Domain\Factory;

use PhpSpec\ObjectBehavior;
use TheGame\Application\Component\FleetJourney\Domain\Entity\Fleet;
use TheGame\Application\Component\FleetJourney\Domain\FleetId;
use TheGame\Application\Component\FleetJourney\Domain\ShipsGroup;
use TheGame\Application\SharedKernel\Domain\GalaxyPoint;
use TheGame\Application\SharedKernel\Domain\ResourceAmount;
use TheGame\Application\SharedKernel\Domain\ResourceId;
use TheGame\Application\SharedKernel\Domain\Resources;
use TheGame\Application\SharedKernel\UuidGeneratorInterface;

final class FleetFactorySpec extends ObjectBehavior
{
public function it_needs_being_implemented(): void
public function let(UuidGeneratorInterface $uuidGenerator): void
{
$this->beConstructedWith($uuidGenerator);
}

public function it_creates_fleet(
UuidGeneratorInterface $uuidGenerator,
): void {
$fleetId = new FleetId("393acaa4-0fdf-44ab-afa9-352b3ac8e826");
$uuidGenerator->generateNewFleetId()->willReturn($fleetId);

$shipsTakingJourney = [new ShipsGroup(
'light-fighter', 10, 50, 200,
), new ShipsGroup(
'warship', 100, 20, 5000,
)];
$stationingGalaxyPoint = new GalaxyPoint(1, 2, 3);
$resourcesLoad = new Resources();
$resourcesLoad->add(
new ResourceAmount(new ResourceId("8ddf9a4b-380d-4051-a1d7-9370ca4d7b3e"), 100),
);

$createdFleet = $this->create(
$shipsTakingJourney, $stationingGalaxyPoint, $resourcesLoad,
);
$createdFleet->shouldBeAnInstanceOf(Fleet::class);
$createdFleet->getId()->shouldReturn($fleetId);
$createdFleet->getStationingGalaxyPoint()->shouldReturn($stationingGalaxyPoint);
$createdFleet->getResourcesLoad()->shouldReturn($resourcesLoad->toScalarArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,44 @@

namespace spec\TheGame\Application\Component\FleetJourney\Domain\Factory;

use DateTimeImmutable;
use PhpSpec\ObjectBehavior;
use TheGame\Application\Component\FleetJourney\Domain\Entity\Journey;
use TheGame\Application\Component\FleetJourney\Domain\FleetId;
use TheGame\Application\Component\FleetJourney\Domain\FleetIdInterface;
use TheGame\Application\Component\FleetJourney\Domain\JourneyId;
use TheGame\Application\Component\FleetJourney\Domain\MissionType;
use TheGame\Application\SharedKernel\Domain\GalaxyPoint;
use TheGame\Application\SharedKernel\UuidGeneratorInterface;

final class JourneyFactorySpec extends ObjectBehavior
{
public function it_needs_being_implemented(): void
{
public function let(
UuidGeneratorInterface $uuidGenerator,
): void {
$this->beConstructedWith($uuidGenerator);
}

public function it_creates_journey(
UuidGeneratorInterface $uuidGenerator,
): void {
$journeyId = new JourneyId("443f9c57-b73a-4230-af53-8eaf2c33ac93");
$uuidGenerator->generateNewJourneyId()->willReturn($journeyId);

$fleetId = new FleetId("8804df77-de2d-46b2-b5f9-bffe9b695fd6");
$startGalaxyPoint = new GalaxyPoint(1, 2, 3);
$targetGalaxyPoint = new GalaxyPoint(4, 5, 6);
$journeyDuration = 500;

$createdJourney = $this->createJourney(
$fleetId, MissionType::Transport, $startGalaxyPoint, $targetGalaxyPoint, $journeyDuration,
);
$createdJourney->shouldBeAnInstanceOf(Journey::class);
$createdJourney->getStartPoint()->shouldReturn($startGalaxyPoint);
$createdJourney->getTargetPoint()->shouldReturn($targetGalaxyPoint);

$now = new DateTimeImmutable();
$createdJourney->getPlannedReachTargetAt()->getTimestamp()->shouldReturn($now->getTimestamp()+500);
$createdJourney->getPlannedReturnAt()->getTimestamp()->shouldReturn($now->getTimestamp()+1000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,121 @@
namespace spec\TheGame\Application\Component\FleetJourney\Domain;

use PhpSpec\ObjectBehavior;
use TheGame\Application\Component\FleetJourney\Domain\Exception\CannotMergeShipGroupsOfDifferentTypeException;
use TheGame\Application\Component\FleetJourney\Domain\Exception\NotEnoughShipsException;
use TheGame\Application\Component\FleetJourney\Domain\ShipsGroupInterface;

final class ShipsGroupSpec extends ObjectBehavior
{
public function it_needs_being_implemented(): void
public function let(): void
{
$type = "light-fighter";
$quantity = 10;
$speed = 35;
$unitLoadCapacity = 20;

$this->beConstructedWith($type, $quantity, $speed, $unitLoadCapacity);
}

public function it_has_type(): void
{
$this->getType()->shouldReturn("light-fighter");
}

public function it_has_quantity(): void
{
$this->getQuantity()->shouldReturn(10);
}

public function it_checks_the_correct_type(): void
{
$this->hasType("light-fighter")->shouldReturn(true);
}

public function it_checks_the_incorrect_type(): void
{
$this->hasType("warship")->shouldReturn(false);
}

public function it_checks_whether_has_more_ships_than_quantity(): void
{
$this->hasMoreShipsThan(5)->shouldReturn(true);
}

public function it_checks_whether_has_more_ships_than_a_quantity_but_has_not(): void
{
$this->hasMoreShipsThan(15)->shouldReturn(false);
}

public function it_checks_whether_has_enough_ships(): void
{
$this->hasEnoughShips(5)->shouldReturn(true);
}

public function it_checks_whether_has_enough_ships_but_has_not(): void
{
$this->hasEnoughShips(15)->shouldReturn(false);
}

public function it_merges_ships(
ShipsGroupInterface $shipsGroup,
): void {
$shipsGroup->getType()->willReturn('light-fighter');
$shipsGroup->getQuantity()->willReturn(15);
$shipsGroup->setEmpty()->shouldBeCalledOnce();

$this->merge($shipsGroup);
$this->getQuantity()->shouldReturn(25);
}

public function it_throws_exception_on_merging_ships_of_unsupported_type(
ShipsGroupInterface $shipsGroup,
): void {
$shipsGroup->getType()->willReturn('warship');

$this->shouldThrow(CannotMergeShipGroupsOfDifferentTypeException::class)->during('merge', [$shipsGroup]);
}

public function it_splits_into_second_group(): void
{
$secondGroup = $this->split(5);
$secondGroup->getType()->shouldReturn('light-fighter');
$secondGroup->getQuantity()->shouldReturn(5);
$secondGroup->getSpeed()->shouldReturn(35);
$secondGroup->getUnitLoadCapacity()->shouldReturn(20);

$this->getQuantity()->shouldReturn(5);
}

public function it_throws_exception_on_splitting_when_hasnt_enough_ships(): void
{
$this->shouldThrow(NotEnoughShipsException::class)->during('split', [15]);
}

public function it_has_speed(): void
{
$this->getSpeed()->shouldReturn(35);
}

public function it_has_load_capacity(): void
{
$this->getLoadCapacity()->shouldReturn(200);
}

public function it_has_load_capacity_of_single_ship(): void
{
$this->getUnitLoadCapacity()->shouldReturn(20);
}

public function it_sets_group_empty(): void
{
$this->setEmpty();

$this->isEmpty()->shouldReturn(true);
}

public function it_is_not_empty(): void
{
$this->isEmpty()->shouldReturn(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Fleet
private ?Journey $currentJourney = null;

/** @var array<ShipsGroupInterface> $ships */
private array $ships;
private array $ships = [];

public function __construct(
private readonly FleetIdInterface $fleetId,
Expand Down Expand Up @@ -52,7 +52,7 @@ public function merge(Fleet $fleet): void
/** @var array<ShipsGroupInterface> $ships */
public function addShips(array $ships): void
{
if ($this->currentJourney === null) {
if ($this->currentJourney !== null) {
throw new FleetAlreadyInJourneyException($this->fleetId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public function __construct(
private readonly int $duration,
) {
$this->startedAt = new DateTimeImmutable();
$this->plannedReachTargetAt = new DateTimeImmutable();
$this->plannedReturnAt = new DateTimeImmutable(
$this->plannedReachTargetAt = new DateTimeImmutable(
sprintf('+ %d seconds', $this->duration),
);
$this->plannedReturnAt = new DateTimeImmutable(
sprintf('+ %d seconds', $this->duration*2),
);
$this->reachesTargetAt = $this->plannedReachTargetAt;
$this->returnsAt = $this->plannedReturnAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getQuantity(): int

public function hasType(string $type): bool
{
return $this->type !== $type;
return $this->type === $type;
}

public function hasMoreShipsThan(int $quantity): bool
Expand Down

0 comments on commit cb62b72

Please sign in to comment.