Skip to content

Commit

Permalink
Finished writing specs for BuildingConstruction component
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinKuklinskiBitBag committed Nov 5, 2023
1 parent 78f5ba4 commit 5650054
Show file tree
Hide file tree
Showing 21 changed files with 642 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@
namespace spec\TheGame\Application\Component\BuildingConstruction\Command;

use PhpSpec\ObjectBehavior;
use TheGame\Application\SharedKernel\Domain\BuildingType;

final class CancelConstructingCommandSpec extends ObjectBehavior
{
public function it_has_planet_id(): void
public function let(): void
{
$planetId = "05FEB5A6-285B-46A8-8A3D-10280C68ECBA";
$buildingType = BuildingType::ResourceStorage->value;

$this->beConstructedWith(
$planetId,
$buildingType,
);
}

public function it_has_building_type(): void
public function it_has_planet_id(): void
{
$this->getPlanetId()->shouldReturn("05FEB5A6-285B-46A8-8A3D-10280C68ECBA");
}

public function it_has_building_type(): void
{
$this->getBuildingType()->shouldReturn(BuildingType::ResourceStorage->value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@
namespace spec\TheGame\Application\Component\BuildingConstruction\Command;

use PhpSpec\ObjectBehavior;
use TheGame\Application\SharedKernel\Domain\BuildingType;

final class FinishConstructingCommandSpec extends ObjectBehavior
{
public function it_has_planet_id(): void
public function let(): void
{
$planetId = "05FEB5A6-285B-46A8-8A3D-10280C68ECBA";
$buildingType = BuildingType::ResourceStorage->value;

$this->beConstructedWith(
$planetId,
$buildingType,
);
}

public function it_has_building_type(): void
public function it_has_planet_id(): void
{
$this->getPlanetId()->shouldReturn("05FEB5A6-285B-46A8-8A3D-10280C68ECBA");
}

public function it_has_building_type(): void
{
$this->getBuildingType()->shouldReturn(BuildingType::ResourceStorage->value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,49 @@
namespace spec\TheGame\Application\Component\BuildingConstruction\Command;

use PhpSpec\ObjectBehavior;
use TheGame\Application\SharedKernel\Domain\BuildingType;

final class StartConstructingCommandSpec extends ObjectBehavior
{
public function it_has_planet_id(): void
public function let(): void
{
$planetId = "05FEB5A6-285B-46A8-8A3D-10280C68ECBA";
$buildingType = BuildingType::ResourceStorage->value;
$resourceContextId = "68A3E097-C576-4087-9655-1ECAB8AA92F6";

$this->beConstructedWith(
$planetId,
$buildingType,
$resourceContextId,
);
}

public function it_has_planet_id(): void
{
$this->getPlanetId()->shouldReturn("05FEB5A6-285B-46A8-8A3D-10280C68ECBA");
}

public function it_has_building_type(): void
{

$this->getBuildingType()->shouldReturn(BuildingType::ResourceStorage->value);
}

public function it_has_resource_context_id(): void
{

$this->getResourceContextId()->shouldReturn("68A3E097-C576-4087-9655-1ECAB8AA92F6");
}

public function it_has_no_resource_context_id(): void
{
$planetId = "05FEB5A6-285B-46A8-8A3D-10280C68ECBA";
$buildingType = BuildingType::ResourceStorage->value;

$this->beConstructedWith(
$planetId,
$buildingType,
null,
);

$this->getResourceContextId()->shouldReturn(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,75 @@
namespace spec\TheGame\Application\Component\BuildingConstruction\CommandHandler;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use TheGame\Application\Component\Balance\Bridge\BuildingContextInterface;
use TheGame\Application\Component\BuildingConstruction\BuildingRepositoryInterface;
use TheGame\Application\Component\BuildingConstruction\Command\CancelConstructingCommand;
use TheGame\Application\Component\BuildingConstruction\Domain\Entity\Building;
use TheGame\Application\Component\BuildingConstruction\Domain\Event\BuildingConstructionHasBeenCancelledEvent;
use TheGame\Application\Component\BuildingConstruction\Domain\Exception\BuildingHasNotBeenBuiltYetFoundException;
use TheGame\Application\SharedKernel\Domain\BuildingType;
use TheGame\Application\SharedKernel\Domain\PlanetId;
use TheGame\Application\SharedKernel\Domain\ResourceRequirementsInterface;
use TheGame\Application\SharedKernel\EventBusInterface;

final class CancelConstructingCommandHandlerSpec extends ObjectBehavior
{
public function it_throws_exception_when_cant_find_aggregate(): void
{
public function let(
BuildingRepositoryInterface $buildingRepository,
BuildingContextInterface $buildingBalanceContext,
EventBusInterface $eventBus,
): void {
$this->beConstructedWith(
$buildingRepository,
$buildingBalanceContext,
$eventBus,
);
}

public function it_throws_exception_when_cant_find_aggregate(
BuildingRepositoryInterface $buildingRepository,
): void {
$planetId = "1D632422-951F-4181-A48D-5AD654260B2B";
$buildingRepository->findForPlanet(new PlanetId($planetId), BuildingType::ResourceMine)
->willReturn(null);

$command = new CancelConstructingCommand(
$planetId,
BuildingType::ResourceMine->value,
);
$this->shouldThrow(BuildingHasNotBeenBuiltYetFoundException::class)
->during('__invoke', [$command]);
}

public function it_cancels_constructing(): void
{
public function it_cancels_constructing(
BuildingRepositoryInterface $buildingRepository,
BuildingContextInterface $buildingBalanceContext,
EventBusInterface $eventBus,
Building $building,
ResourceRequirementsInterface $resourceRequirements,
): void {
$planetId = "1D632422-951F-4181-A48D-5AD654260B2B";
$buildingRepository->findForPlanet(new PlanetId($planetId), BuildingType::ResourceMine)
->willReturn($building);

$buildingBalanceContext->getResourceRequirements(5, BuildingType::ResourceMine)
->willReturn($resourceRequirements);

$building->cancelUpgrading()->shouldBeCalledOnce();
$building->getCurrentLevel()->willReturn(5);
$resourceRequirements->toScalarArray()
->willReturn([
"26E29382-BA42-4675-B643-93E9006B089B" => 500,
]);

$eventBus->dispatch(Argument::type(BuildingConstructionHasBeenCancelledEvent::class))
->shouldBeCalledOnce();

$command = new CancelConstructingCommand(
$planetId,
BuildingType::ResourceMine->value,
);
$this->__invoke($command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,78 @@

namespace spec\TheGame\Application\Component\BuildingConstruction\CommandHandler;

use PhpParser\Node\Arg;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use TheGame\Application\Component\BuildingConstruction\BuildingRepositoryInterface;
use TheGame\Application\Component\BuildingConstruction\Command\FinishConstructingCommand;
use TheGame\Application\Component\BuildingConstruction\Domain\Entity\Building;
use TheGame\Application\Component\BuildingConstruction\Domain\Event\Factory\BuildingTypeEventFactoryInterface;
use TheGame\Application\Component\BuildingConstruction\Domain\Event\ResourceStorageConstructionHasBeenFinishedEvent;
use TheGame\Application\Component\BuildingConstruction\Domain\Exception\BuildingHasNotBeenBuiltYetFoundException;
use TheGame\Application\SharedKernel\Domain\BuildingType;
use TheGame\Application\SharedKernel\Domain\PlanetId;
use TheGame\Application\SharedKernel\EventBusInterface;

final class FinishConstructingCommandHandlerSpec extends ObjectBehavior
{
public function it_throws_exception_when_aggregate_is_not_found(): void
{
public function let(
BuildingRepositoryInterface $buildingRepository,
EventBusInterface $eventBus,
BuildingTypeEventFactoryInterface $buildingTypeEventFactory,
): void {
$this->beConstructedWith(
$buildingRepository,
$eventBus,
$buildingTypeEventFactory,
);
}

public function it_throws_exception_when_aggregate_is_not_found(
BuildingRepositoryInterface $buildingRepository,
): void {
$planetId = "1D632422-951F-4181-A48D-5AD654260B2B";

$buildingRepository->findForPlanet(new PlanetId($planetId), BuildingType::ResourceStorage)
->willReturn(null);

$command = new FinishConstructingCommand(
$planetId,
BuildingType::ResourceStorage->value,
);
$this->shouldThrow(BuildingHasNotBeenBuiltYetFoundException::class)
->during('__invoke', [$command]);
}

public function it_finishes_constructing(): void
{
public function it_finishes_constructing(
BuildingRepositoryInterface $buildingRepository,
EventBusInterface $eventBus,
BuildingTypeEventFactoryInterface $buildingTypeEventFactory,
Building $building,
): void {
$planetId = "1D632422-951F-4181-A48D-5AD654260B2B";

$buildingRepository->findForPlanet(new PlanetId($planetId), BuildingType::ResourceStorage)
->willReturn($building);

$building->finishUpgrading()->shouldBeCalledOnce();

$resourceId = "73140C59-DE9C-4959-8E18-1271EB32D76A";
$event = new ResourceStorageConstructionHasBeenFinishedEvent(
$planetId,
$resourceId,
1
);
$buildingTypeEventFactory->createConstructingFinishedEvent($building)
->willReturn($event);

$eventBus->dispatch(Argument::type(ResourceStorageConstructionHasBeenFinishedEvent::class))
->shouldBeCalledOnce();

$command = new FinishConstructingCommand(
$planetId,
BuildingType::ResourceStorage->value,
);
$this->__invoke($command);
}
}
Loading

0 comments on commit 5650054

Please sign in to comment.