diff --git a/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php b/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php index 1e491b552..d63ca5148 100644 --- a/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php +++ b/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php @@ -23,6 +23,7 @@ use Zenstruck\Foundry\Bundle\Command\StubMakeFactory; use Zenstruck\Foundry\Bundle\Command\StubMakeStory; use Zenstruck\Foundry\Instantiator; +use Zenstruck\Foundry\Persistence\PersistentObjectFactory; use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory; use Zenstruck\Foundry\Story; use Zenstruck\Foundry\Test\ORMDatabaseResetter; @@ -55,6 +56,17 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container if (true === $mergedConfig['auto_refresh_proxies']) { $container->getDefinition('.zenstruck_foundry.configuration')->addMethodCall('enableDefaultProxyAutoRefresh'); } elseif (false === $mergedConfig['auto_refresh_proxies']) { + trigger_deprecation( + 'zenstruck\foundry', + '1.37.0', + <<getDefinition('.zenstruck_foundry.configuration')->addMethodCall('disableDefaultProxyAutoRefresh'); } } diff --git a/tests/Fixtures/Entity/Cascade/Product.php b/tests/Fixtures/Entity/Cascade/Product.php index b2390e46f..c3beb36f5 100644 --- a/tests/Fixtures/Entity/Cascade/Product.php +++ b/tests/Fixtures/Entity/Cascade/Product.php @@ -82,6 +82,7 @@ public function getReview(): ?Review public function setReview(Review $review): void { $this->review = $review; + $review->setProduct($this); } public function getVariants(): Collection @@ -113,6 +114,7 @@ public function addCategory(ProductCategory $category): void { if (!$this->categories->contains($category)) { $this->categories[] = $category; + $category->addProduct($this); } } diff --git a/tests/Fixtures/Entity/Cascade/ProductCategory.php b/tests/Fixtures/Entity/Cascade/ProductCategory.php index 9946b1e46..5196282bd 100644 --- a/tests/Fixtures/Entity/Cascade/ProductCategory.php +++ b/tests/Fixtures/Entity/Cascade/ProductCategory.php @@ -54,4 +54,12 @@ public function getProducts(): Collection { return $this->products; } + + public function addProduct(Product $product): void + { + if (!$this->products->contains($product)) { + $this->products[] = $product; + $product->addCategory($this); + } + } } diff --git a/tests/Fixtures/Entity/Category.php b/tests/Fixtures/Entity/Category.php index 00af529ce..2a04df503 100644 --- a/tests/Fixtures/Entity/Category.php +++ b/tests/Fixtures/Entity/Category.php @@ -95,7 +95,7 @@ public function addSecondaryPost(Post $secondaryPost): void { if (!$this->secondaryPosts->contains($secondaryPost)) { $this->secondaryPosts[] = $secondaryPost; - $secondaryPost->setCategory($this); + $secondaryPost->setSecondaryCategory($this); } } @@ -105,7 +105,7 @@ public function removeSecondaryPost(Post $secondaryPost): void $this->secondaryPosts->removeElement($secondaryPost); // set the owning side to null (unless already changed) if ($secondaryPost->getCategory() === $this) { - $secondaryPost->setCategory(null); + $secondaryPost->setSecondaryCategory(null); } } } diff --git a/tests/Fixtures/Kernel.php b/tests/Fixtures/Kernel.php index ddaa8474f..91eea6afa 100644 --- a/tests/Fixtures/Kernel.php +++ b/tests/Fixtures/Kernel.php @@ -162,7 +162,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load } if (\getenv('USE_FOUNDRY_BUNDLE')) { - $foundryConfig = ['auto_refresh_proxies' => false]; + $foundryConfig = ['auto_refresh_proxies' => true]; if ($this->defaultMakeFactoryNamespace) { $foundryConfig['make_factory'] = ['default_namespace' => $this->defaultMakeFactoryNamespace]; } diff --git a/tests/Functional/FactoryDoctrineCascadeTest.php b/tests/Functional/FactoryDoctrineCascadeTest.php index 551290e45..23b71a84c 100644 --- a/tests/Functional/FactoryDoctrineCascadeTest.php +++ b/tests/Functional/FactoryDoctrineCascadeTest.php @@ -11,7 +11,9 @@ namespace Zenstruck\Foundry\Tests\Functional; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\HttpKernel\KernelInterface; use Zenstruck\Foundry\Instantiator; use Zenstruck\Foundry\Test\Factories; use Zenstruck\Foundry\Test\ResetDatabase; @@ -22,6 +24,7 @@ use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Review; use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Tag; use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Variant; +use Zenstruck\Foundry\Tests\Fixtures\Kernel; use function Zenstruck\Foundry\Persistence\persistent_factory; diff --git a/tests/Functional/ODMModelFactoryTest.php b/tests/Functional/ODMModelFactoryTest.php index 1245cfb29..754572761 100644 --- a/tests/Functional/ODMModelFactoryTest.php +++ b/tests/Functional/ODMModelFactoryTest.php @@ -11,6 +11,7 @@ namespace Zenstruck\Foundry\Tests\Functional; +use Zenstruck\Foundry\Factory; use Zenstruck\Foundry\Persistence\Proxy; use Zenstruck\Foundry\Tests\Fixtures\Document\ODMCategory; use Zenstruck\Foundry\Tests\Fixtures\Document\ODMComment; @@ -97,6 +98,9 @@ public function can_find_or_create_from_embedded_object(): void */ public function can_find_or_create_from_object(): void { + // must disable auto refresh proxy: otherwise doctrine would update post from db and recreate the user object. + Factory::configuration()->disableDefaultProxyAutoRefresh(); + $user = UserFactory::createOne(['name' => 'some user']); $post = PostFactory::findOrCreate($attributes = ['user' => $user->_real()]); @@ -114,6 +118,9 @@ public function can_find_or_create_from_object(): void */ public function can_find_or_create_from_proxy_of_object(): void { + // must disable auto refresh proxy: otherwise doctrine would update post from db and recreate the user object. + Factory::configuration()->disableDefaultProxyAutoRefresh(); + $user = UserFactory::createOne(['name' => 'some user']); $post = PostFactory::findOrCreate($attributes = ['user' => $user]); diff --git a/tests/Functional/ProxyTest.php b/tests/Functional/ProxyTest.php index 2e6d18139..0c62b2f32 100644 --- a/tests/Functional/ProxyTest.php +++ b/tests/Functional/ProxyTest.php @@ -136,6 +136,7 @@ public function can_force_set_and_save(): void public function can_force_set_multiple_fields(): void { $post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']); + $post->_disableAutoRefresh(); $this->assertSame('old title', $post->getTitle()); $this->assertSame('old body', $post->getBody()); @@ -248,6 +249,7 @@ public function without_auto_refresh_solves_the_auto_refresh_problem(): void public function without_auto_refresh_does_not_enable_auto_refresh_if_it_was_disabled_originally(): void { $post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']); + $post->_disableAutoRefresh(); $this->assertSame('old title', $post->getTitle()); $this->assertSame('old body', $post->getBody()); @@ -274,6 +276,7 @@ public function without_auto_refresh_does_not_enable_auto_refresh_if_it_was_disa public function without_auto_refresh_keeps_disabled_if_originally_disabled(): void { $post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']); + $post->_disableAutoRefresh(); $this->assertSame('old title', $post->getTitle()); $this->assertSame('old body', $post->getBody()); diff --git a/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php b/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php index cc6706f70..95708d167 100644 --- a/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php +++ b/tests/Unit/Bundle/DependencyInjection/ZenstruckFoundryExtensionTest.php @@ -220,6 +220,7 @@ public function cannot_configure_always_force_properties_if_using_custom_instant /** * @test + * @group legacy */ public function can_enable_auto_refresh_proxies(): void { @@ -232,6 +233,7 @@ public function can_enable_auto_refresh_proxies(): void /** * @test + * @group legacy */ public function can_disable_auto_refresh_proxies(): void {