Skip to content

Commit

Permalink
refactor: deprecate config auto_refresh_proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Dec 21, 2023
1 parent 73c044b commit 734a547
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ 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', 'Configuring the default proxy auto-refresh to false is deprecated and will default to "true" in 2.0.');

$container->getDefinition('.zenstruck_foundry.configuration')->addMethodCall('disableDefaultProxyAutoRefresh');
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/Entity/Cascade/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function getReview(): ?Review
public function setReview(Review $review): void
{
$this->review = $review;
$review->setProduct($this);
}

public function getVariants(): Collection
Expand Down Expand Up @@ -113,6 +114,7 @@ public function addCategory(ProductCategory $category): void
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
$category->addProduct($this);
}
}

Expand Down
11 changes: 10 additions & 1 deletion tests/Fixtures/Entity/Cascade/ProductCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Zenstruck\Foundry\Tests\Fixtures\Entity\Repository\ProductCategoryRepository;

#[ORM\Entity]
#[ORM\Entity(repositoryClass: ProductCategoryRepository::class)]
#[ORM\Table(name: 'category_cascade')]
class ProductCategory
{
Expand Down Expand Up @@ -54,4 +55,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);
}
}
}
4 changes: 2 additions & 2 deletions tests/Fixtures/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function addSecondaryPost(Post $secondaryPost): void
{
if (!$this->secondaryPosts->contains($secondaryPost)) {
$this->secondaryPosts[] = $secondaryPost;
$secondaryPost->setCategory($this);
$secondaryPost->setSecondaryCategory($this);
}
}

Expand All @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/FactoryDoctrineCascadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions tests/Functional/ODMModelFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()]);

Expand All @@ -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]);

Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -232,6 +233,7 @@ public function can_enable_auto_refresh_proxies(): void

/**
* @test
* @group legacy
*/
public function can_disable_auto_refresh_proxies(): void
{
Expand Down

0 comments on commit 734a547

Please sign in to comment.