Skip to content

Commit

Permalink
Fix an issue where entities are marked as persisted when they aren't
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasnoback committed Jan 29, 2019
1 parent 6bb3b82 commit 4efef98
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/TalisOrm/AggregateRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public function __construct(Connection $connection, EventDispatcher $eventDispat
public function save(Aggregate $aggregate): void
{
$this->connection->transactional(function () use ($aggregate) {
/** @var Entity[] $persistedEntities */
$persistedEntities = [];

$this->insertOrUpdate($aggregate);
$persistedEntities[] = $aggregate;

foreach ($aggregate->deletedChildEntities() as $childEntity) {
$this->connection->delete(
Expand All @@ -47,8 +51,13 @@ public function save(Aggregate $aggregate): void
foreach ($aggregate->childEntitiesByType() as $type => $childEntities) {
foreach ($childEntities as $childEntity) {
$this->insertOrUpdate($childEntity);
$persistedEntities[] = $childEntity;
}
}

foreach ($persistedEntities as $persistedObject) {
$persistedObject->markAsPersisted();
}
});

$this->eventDispatcher->dispatch($aggregate->releaseEvents());
Expand Down Expand Up @@ -159,7 +168,6 @@ private function insertOrUpdate(Entity $entity): void
$this->connection->quoteIdentifier($entity::tableName()),
$entity->state()
);
$entity->markAsPersisted();
} else {
$state = $entity->state();
if (array_key_exists(Aggregate::VERSION_COLUMN, $state)) {
Expand Down
33 changes: 33 additions & 0 deletions test/TalisOrm/AbstractAggregateRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,39 @@ public function it_saves_an_aggregate_with_its_child_entities()
);
}

/**
* @test
*/
public function entities_will_only_be_marked_as_persisted_if_all_queries_within_the_transaction_succeeded(): void
{
$aggregate = Order::create(
new OrderId('91338a57-5c9a-40e8-b5e8-803e8175c7d7', 5),
DateTimeUtil::createDateTimeImmutable('2018-10-03')
);
$aggregate->addLine(
new LineNumber(1),
new ProductId('73d46c97-a71b-4e3c-9633-bb7a8603b301', 5),
new Quantity(10)
);
// Add another line with the same number (1) - this should cause an exception when saving
$aggregate->addLine(
new LineNumber(1),
new ProductId('73d46c97-a71b-4e3c-9633-bb7a8603b301', 5),
new Quantity(10)
);
try {
$this->repository->save($aggregate);
$this->fail('Expected this to fail');
} catch (UniqueConstraintViolationException $exception) {
// this was expected
}

self::assertTrue($aggregate->isNew());
foreach ($aggregate->lines() as $line) {
self::assertTrue($line->isNew());
}
}

/**
* @test
*/
Expand Down
8 changes: 8 additions & 0 deletions test/TalisOrm/AggregateRepositoryTest/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,12 @@ public function setAggregateVersion($aggregateVersion)
Assert::integer($aggregateVersion);
$this->aggregateVersion = $aggregateVersion;
}

/**
* @return array&Line[]
*/
public function lines(): array
{
return $this->lines;
}
}

0 comments on commit 4efef98

Please sign in to comment.