Skip to content

Commit

Permalink
bc: deprecate config auto_refresh_proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Mar 1, 2024
1 parent 5884bd3 commit 8e1c317
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 42 deletions.
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions src/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
32 changes: 18 additions & 14 deletions src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);

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

0 comments on commit 8e1c317

Please sign in to comment.