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

tests: ensure passing entity to many-to-one works correctly #780

Merged
merged 1 commit into from
Jan 23, 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
1 change: 1 addition & 0 deletions src/Persistence/PersistenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public function refresh(object &$object, bool $force = false): object

public function isPersisted(object $object): bool
{
// prevents doctrine to use its cache and think the object is persisted
if ($this->strategyFor($object::class)->isScheduledForInsert($object)) {
return false;
}
Expand Down
21 changes: 19 additions & 2 deletions src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,29 @@ protected function normalizeObject(object $object): object

$configuration = Configuration::instance();

if (!$configuration->isPersistenceAvailable() || !$configuration->persistence()->hasPersistenceFor($object)) {
if (!$configuration->isPersistenceAvailable()) {
return $object;
}

$persistenceManager = $configuration->persistence();

if ($object instanceof Proxy) {
$proxy = $object;
$proxy->_disableAutoRefresh();
$object = $proxy->_real();
$proxy->_enableAutoRefresh();
}

if (!$persistenceManager->hasPersistenceFor($object)) {
return $object;
}

if (!$persistenceManager->isPersisted($object)) {
$persistenceManager->scheduleForInsert($object);
}

try {
return $configuration->persistence()->refresh($object, true);
return $persistenceManager->refresh($object, force: true);
} 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,43 @@ public function ensure_one_to_many_relations_are_not_pre_persisted(): void
}
}

/** @test */
#[Test]
#[DataProvider('provideCascadeRelationshipsCombinations')]
#[UsingRelationships(Category::class, ['contacts'])]
public function it_can_add_managed_entity_to_many_to_one(): void
{
$this->it_can_add_entity_to_many_to_one(
static::categoryFactory()->create()
);
}

/** @test */
#[Test]
#[DataProvider('provideCascadeRelationshipsCombinations')]
#[UsingRelationships(Category::class, ['contacts'])]
public function it_can_add_unmanaged_entity_to_many_to_one(): void
{
$this->it_can_add_entity_to_many_to_one(
static::categoryFactory()->withoutPersisting()->create()
);
}

private function it_can_add_entity_to_many_to_one(Category $category): void
{
self::assertCount(0, $category->getContacts());

$contact1 = static::contactFactory()->create(['category' => $category]);
$contact2 = static::contactFactory()->create(['category' => $category]);

static::categoryFactory()::assert()->count(1);

self::assertCount(2, $category->getContacts());

self::assertSame(unproxy($category), $contact1->getCategory());
self::assertSame(unproxy($category), $contact2->getCategory());
}

/** @return PersistentObjectFactory<Contact> */
protected static function contactFactoryWithoutCategory(): PersistentObjectFactory
{
Expand Down
Loading