From 8e1c31773caaa5bc49870329b9ebaec5d21d1a94 Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Fri, 1 Mar 2024 15:42:48 +0100 Subject: [PATCH] bc: deprecate config auto_refresh_proxies --- .env | 1 - composer.json | 2 +- .../DependencyInjection/Configuration.php | 1 + src/Configuration.php | 2 -- src/Proxy.php | 32 +++++++++++-------- tests/Fixtures/Kernel.php | 2 +- tests/Functional/ProxyTest.php | 23 ------------- 7 files changed, 21 insertions(+), 42 deletions(-) diff --git a/.env b/.env index afeca281..187d16b5 100644 --- a/.env +++ b/.env @@ -2,4 +2,3 @@ DATABASE_URL="mysql://root:1234@127.0.0.1:3307/foundry_test?serverVersion=5.7.42 MONGO_URL="mongodb://127.0.0.1:27018/dbName?compressors=disabled&gssapiServiceName=mongodb" USE_FOUNDRY_BUNDLE=1 TEST_MIGRATIONS=0 -MAKER_PHP_CS_FIXER_BINARY_PATH="vendor/bin/php-cs-fixer" diff --git a/composer.json b/composer.json index 12f01cde..c61ed989 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "phpunit/phpunit": "^9.5.0", "symfony/dotenv": "^5.4|^6.0|^7.0", "symfony/framework-bundle": "^5.4|^6.0|^7.0", - "symfony/maker-bundle": "^1.49", + "symfony/maker-bundle": "^1.55", "symfony/phpunit-bridge": "^5.4|^6.0|^7.0", "symfony/translation-contracts": "^2.5|^3.0" }, diff --git a/src/Bundle/DependencyInjection/Configuration.php b/src/Bundle/DependencyInjection/Configuration.php index 1b4d1ca8..8c840786 100644 --- a/src/Bundle/DependencyInjection/Configuration.php +++ b/src/Bundle/DependencyInjection/Configuration.php @@ -27,6 +27,7 @@ public function getConfigTreeBuilder(): TreeBuilder $treeBuilder->getRootNode() ->children() ->booleanNode('auto_refresh_proxies') + ->setDeprecated('zenstruck/foundry', '1.37.0', 'The option "zenstruck_foundry.auto_refresh_proxies" is deprecated, you should remove it.') ->info('Whether to auto-refresh proxies by default (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#auto-refresh)') ->defaultNull() ->end() diff --git a/src/Configuration.php b/src/Configuration.php index 1fea4bcb..50c1ae8d 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -85,8 +85,6 @@ public function defaultProxyAutoRefresh(): bool } if (null === $this->defaultProxyAutoRefresh) { - trigger_deprecation('zenstruck\foundry', '1.9', 'Not explicitly configuring the default proxy auto-refresh is deprecated and will default to "true" in 2.0. Use "zenstruck_foundry.auto_refresh_proxies" in the bundle config or TestState::enableDefaultProxyAutoRefresh()/disableDefaultProxyAutoRefresh().'); - $this->defaultProxyAutoRefresh = false; } diff --git a/src/Proxy.php b/src/Proxy.php index 9c039495..8248d248 100644 --- a/src/Proxy.php +++ b/src/Proxy.php @@ -154,22 +154,11 @@ public function _real(): object return $this->object; } - $om = $this->objectManager(); - - // only check for changes if the object is managed in the current om - if (($om instanceof EntityManagerInterface || $om instanceof DocumentManager) && $om->contains($this->object)) { - // cannot use UOW::recomputeSingleEntityChangeSet() here as it wrongly computes embedded objects as changed - $om->getUnitOfWork()->computeChangeSet($om->getClassMetadata($this->class), $this->object); - - if ( - ($om instanceof EntityManagerInterface && !empty($om->getUnitOfWork()->getEntityChangeSet($this->object))) - || ($om instanceof DocumentManager && !empty($om->getUnitOfWork()->getDocumentChangeSet($this->object)))) { - throw new \RuntimeException(\sprintf('Cannot auto refresh "%s" as there are unsaved changes. Be sure to call ->save() or disable auto refreshing (see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#auto-refresh for details).', $this->class)); - } + try { + $this->_refresh(); + } catch (\Throwable) { } - $this->_refresh(); - return $this->object; } @@ -236,6 +225,20 @@ public function _refresh(): static throw new \RuntimeException(\sprintf('Cannot refresh unpersisted object (%s).', $this->class)); } + $om = $this->objectManager(); + + // only check for changes if the object is managed in the current om + if (($om instanceof EntityManagerInterface || $om instanceof DocumentManager) && $om->contains($this->object)) { + // cannot use UOW::recomputeSingleEntityChangeSet() here as it wrongly computes embedded objects as changed + $om->getUnitOfWork()->computeChangeSet($om->getClassMetadata($this->class), $this->object); + + if ( + ($om instanceof EntityManagerInterface && !empty($om->getUnitOfWork()->getEntityChangeSet($this->object))) + || ($om instanceof DocumentManager && !empty($om->getUnitOfWork()->getDocumentChangeSet($this->object)))) { + throw new \RuntimeException(\sprintf('Cannot auto refresh "%s" as there are unsaved changes. Be sure to call ->save() or disable auto refreshing (see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#auto-refresh for details).', $this->class)); + } + } + if ($this->objectManager()->contains($this->object)) { $this->objectManager()->refresh($this->object); @@ -263,6 +266,7 @@ public function forceSet(string $property, mixed $value): static public function _set(string $property, mixed $value): static { + $this->_refresh(); $object = $this->_real(); Instantiator::forceSet($object, $property, $value, calledInternally: true); diff --git a/tests/Fixtures/Kernel.php b/tests/Fixtures/Kernel.php index 92fb4fc1..6041eb90 100644 --- a/tests/Fixtures/Kernel.php +++ b/tests/Fixtures/Kernel.php @@ -165,7 +165,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load } if (\getenv('USE_FOUNDRY_BUNDLE')) { - $foundryConfig = ['auto_refresh_proxies' => true]; + $foundryConfig = []; if ($this->defaultMakeFactoryNamespace) { $foundryConfig['make_factory'] = ['default_namespace' => $this->defaultMakeFactoryNamespace]; } diff --git a/tests/Functional/ProxyTest.php b/tests/Functional/ProxyTest.php index 0c62b2f3..50005c0c 100644 --- a/tests/Functional/ProxyTest.php +++ b/tests/Functional/ProxyTest.php @@ -151,29 +151,6 @@ public function can_force_set_multiple_fields(): void $this->assertSame('new body', $post->getBody()); } - /** - * @test - */ - public function exception_thrown_if_trying_to_autorefresh_object_with_unsaved_changes(): void - { - $post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']) - ->_enableAutoRefresh() - ; - - $this->assertSame('old title', $post->getTitle()); - $this->assertSame('old body', $post->getBody()); - - $post - ->_enableAutoRefresh() - ->_set('title', 'new title') - ; - - $this->expectException(\RuntimeException::class); - - // exception thrown because of "unsaved changes" to $post from above - $post->_set('body', 'new body'); - } - /** * @test */