Skip to content

Commit

Permalink
test: assert updates are implicitly persisted (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored Jan 27, 2025
1 parent ccaca15 commit 86c5aab
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/ORM/AbstractORMPersistenceStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ final public function hasChanges(object $object): bool
return false;
}

// we're cloning the UOW because computing change set has side effect
$unitOfWork = clone $em->getUnitOfWork();

// cannot use UOW::recomputeSingleEntityChangeSet() here as it wrongly computes embedded objects as changed
$em->getUnitOfWork()->computeChangeSet($em->getClassMetadata($object::class), $object);
$unitOfWork->computeChangeSet($em->getClassMetadata($object::class), $object);

return (bool) $em->getUnitOfWork()->getEntityChangeSet($object);
return (bool) $unitOfWork->getEntityChangeSet($object);
}

final public function truncate(string $class): void
Expand Down
6 changes: 2 additions & 4 deletions src/Persistence/PersistenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,8 @@ public function refresh(object &$object, bool $force = false): object

$strategy = $this->strategyFor($object::class);

if (!$force) {
if ($strategy->hasChanges($object)) {
throw RefreshObjectFailed::objectHasUnsavedChanges($object::class);
}
if ($strategy->hasChanges($object)) {
throw RefreshObjectFailed::objectHasUnsavedChanges($object::class);
}

$om = $strategy->objectManagerFor($object::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ protected function normalizeObject(object $object): object
}

try {
return $persistenceManager->refresh($object, force: true);
return $configuration->persistence()->refresh($object);
} catch (RefreshObjectFailed|VarExportLogicException) {
return $object;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Zenstruck\Foundry\Tests\Fixture\Entity\Contact;
use Zenstruck\Foundry\Tests\Fixture\Entity\Tag;

use function Zenstruck\Foundry\Persistence\refresh;
use function Zenstruck\Foundry\Persistence\unproxy;

/**
Expand Down Expand Up @@ -361,6 +362,22 @@ public function ensure_one_to_many_relations_are_not_pre_persisted(): void
}
}

/**
* @test
*/
public function assert_updates_are_implicitly_persisted(): void
{
$category = static::categoryFactory()->create();
$address = static::addressFactory()->create();

$category->setName('new name');

static::contactFactory()->create(['category' => $category, 'address' => $address]);

refresh($category);
self::assertSame('new name', $category->getName());
}

/** @test */
#[Test]
#[DataProvider('provideCascadeRelationshipsCombinations')]
Expand Down

0 comments on commit 86c5aab

Please sign in to comment.