Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: assert updates are implicitly persisted #781

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading