Skip to content

Commit

Permalink
fill is unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Szurovecz committed Nov 2, 2014
1 parent 94cc137 commit 8898ddc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 42 deletions.
7 changes: 6 additions & 1 deletion src/predaddy/util/test/CommandSourcedFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ public function givenCommands(Command $commands)
{
$this->givenCommands = func_get_args();
$eventHandler = function (DomainEvent $event) {
$this->setAggregateId($event->aggregateId());
if ($this->getAggregateId() === null) {
$this->setAggregateId($event->aggregateId());
foreach ($this->givenCommands as $command) {
CommandPopulator::populate($this->getAggregateId()->value(), $command);
}
}
};
$this->getEventBus()->registerClosure($eventHandler);
foreach ($this->givenCommands as $command) {
Expand Down
9 changes: 7 additions & 2 deletions src/predaddy/util/test/EventSourcedFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

namespace predaddy\util\test;

use precore\util\UUID;
use predaddy\domain\AbstractDomainEvent;
use predaddy\domain\DefaultAggregateId;
use predaddy\domain\DomainEvent;
use predaddy\domain\eventsourcing\EventSourcingRepository;
use predaddy\domain\EventStore;
Expand Down Expand Up @@ -64,8 +66,11 @@ public function givenEvents(DomainEvent $events)
{
$this->given = func_get_args();
foreach ($this->given as $event) {
if (!$event->aggregateId()->equals(NullAggregateId::instance())) {
$this->setAggregateId($event->aggregateId());
if ($this->getAggregateId() === null) {
$aggregateId = $event->aggregateId() !== null && !$event->aggregateId()->equals(NullAggregateId::instance())
? $event->aggregateId()
: new DefaultAggregateId(UUID::randomUUID()->toString(), $this->getAggregateClass());
$this->setAggregateId($aggregateId);
}
if ($event instanceof AbstractDomainEvent) {
AbstractDomainEvent::initEvent($event, $this->getAggregateId(), $event->identifier());
Expand Down
33 changes: 5 additions & 28 deletions src/predaddy/util/test/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Exception;
use PHPUnit_Framework_TestCase;
use precore\lang\ObjectClass;
use precore\util\UUID;
use predaddy\commandhandling\AbstractCommand;
use predaddy\commandhandling\Command;
use predaddy\commandhandling\CommandBus;
Expand Down Expand Up @@ -119,15 +118,10 @@ abstract class Fixture implements MessageCallback
private $repository;

/**
* @var DefaultAggregateId
* @var DefaultAggregateId|null
*/
private $aggregateId;

/**
* @var array
*/
private $fillableCommands = [];

public function __construct($aggregateClass, Repository $repository = null)
{
$this->aggregateClass = $aggregateClass;
Expand All @@ -137,7 +131,7 @@ public function __construct($aggregateClass, Repository $repository = null)
$this->eventBus = new EventBus();
EventPublisher::instance()->setEventBus($this->eventBus);
$this->repository = $repository;
$this->aggregateId = new DefaultAggregateId(UUID::randomUUID()->toString(), $this->getAggregateClass());
//$this->aggregateId = new DefaultAggregateId(UUID::randomUUID()->toString(), $this->getAggregateClass());
}

/**
Expand Down Expand Up @@ -202,27 +196,13 @@ public function expectNoEvents()
*/
public function when(Command $command)
{
if ($this->aggregateId !== null && $command instanceof AbstractCommand) {
CommandPopulator::populate($this->getAggregateId()->value(), $command);
}
$this->command = $command;
return $this;
}

/**
* Sets the aggregate ID for the given command.
* Useful if we don't know the AR ID in the test case and it is not interesting either.
* Can be used both in {@link givenCommands()} and {@link when()} method parameters.
*
* Do not use it with create commands, unless the AR ID is defined by the client and the command must know it.
*
* @param AbstractCommand $command
* @return AbstractCommand the command itself, but it may be populated later
*/
public function fill(AbstractCommand $command)
{
CommandPopulator::populate($this->getAggregateId()->value(), $command);
$this->fillableCommands[] = $command;
return $command;
}

/**
* The given value must be returned by the command handler.
*
Expand Down Expand Up @@ -320,9 +300,6 @@ public function catchEvent(DomainEvent $event)
protected function setAggregateId(AggregateId $aggregateId)
{
$this->aggregateId = $aggregateId;
foreach ($this->fillableCommands as $command) {
CommandPopulator::populate($this->getAggregateId()->value(), $command);
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/predaddy/util/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $fixture
new EntryAdded(100),
new EntryAdded(200),
)
->when($fixture->fill(new AddEntry(300)))
->when(new AddEntry(300))
->expectReturnValue(600)
->expectEvents(new EntryAdded(300));
```
Expand All @@ -45,7 +45,7 @@ $fixture
new AccountCreated(),
new EntryAdded(100)
)
->when($fixture->fill(new AddEntry(-300)))
->when(new AddEntry(-300))
->expectException('\RuntimeException', 'Balance cannot be negative, entry is not allowed!');
```

Expand All @@ -57,7 +57,7 @@ $fixture
->registerAnnotatedCommandHandler(new AccountCommandHandler(new InMemoryRepository()))
->givenCommands(
new CreateAccount(),
$fixture->fill(new AddEntry(100))
new AddEntry(100)
)
->...
```
12 changes: 6 additions & 6 deletions tests/src/predaddy/domain/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function shouldIncrementUser()
$fixture
->registerAnnotatedCommandHandler(new UserCommandHandler(new InMemoryRepository()))
->givenCommands(new CreateUser())
->when($fixture->fill(new Increment()))
->when(new Increment())
->expectReturnValue(2)
->expectEvents(new IncrementedEvent());
}
Expand All @@ -39,9 +39,9 @@ public function shouldIncrementTwoTimes()
->registerAnnotatedCommandHandler(new UserCommandHandler($repository))
->givenCommands(
new CreateUser(),
$fixture->fill(new Increment())
new Increment()
)
->when($fixture->fill(new Increment()))
->when(new Increment())
->expectReturnValue(3)
->expectEvents(new IncrementedEvent());

Expand All @@ -61,10 +61,10 @@ public function shouldThrowExceptionIfIncrementedThreeTimes()
->registerAnnotatedCommandHandler(new UserCommandHandler(new InMemoryRepository()))
->givenCommands(
new CreateUser(),
$fixture->fill(new Increment()),
$fixture->fill(new Increment())
new Increment(),
new Increment()
)
->when($fixture->fill(new Increment()))
->when(new Increment())
->expectException('\RuntimeException', 'Cannot be incremented more, than twice');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function shouldIncremented()
$fixture = Fixtures::newGivenWhenThenFixture(EventSourcedUser::className());
$fixture
->givenEvents(new UserCreated())
->when($fixture->fill(new Increment()))
->when(new Increment())
->expectReturnValue(2)
->expectEvents(new IncrementedEvent());
}
Expand All @@ -51,7 +51,7 @@ public function shouldDecremented()
new UserCreated(),
new IncrementedEvent()
)
->when($fixture->fill(new Decrement()))
->when(new Decrement())
->expectEvents(new DecrementedEvent(1));
}
}

0 comments on commit 8898ddc

Please sign in to comment.