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 81d6bc7
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
<<<MESSAGE
Configuring the default proxy auto-refresh to false is deprecated. You should set it to "true", which will be the default value in 2.0.
If you still want to disable auto refresh, make your factory implement "%s" instead of "%s".
MESSAGE,
PersistentObjectFactory::class,
PersistentProxyObjectFactory::class,
);

$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
8 changes: 8 additions & 0 deletions tests/Fixtures/Entity/Cascade/ProductCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
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 81d6bc7

Please sign in to comment.