Skip to content

Commit

Permalink
bug #50761 [DoctrineBridge] Ignore invalid stores in `LockStoreSchema…
Browse files Browse the repository at this point in the history
…Listener` raised by `StoreFactory` (alexandre-daubois)

This PR was merged into the 6.3 branch.

Discussion
----------

[DoctrineBridge] Ignore invalid stores in `LockStoreSchemaListener` raised by `StoreFactory`

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | _NA_
| License       | MIT
| Doc PR        | _NA_

In our test environment, we're setting our `LOCK_DSN` env var at `null`. This works well in 6.2 but not in 6.3 anymore.

The version 6.3 of DoctrineBridge is bringing this BC break. Indeed, lock stores are being injected in `Symfony\Bridge\Doctrine\SchemaListener\LockStoreSchemaListener`. At runtime, the `StoreFactory` is used to instantiate stores when they are needed in `postGenerateSchema`.

Unfortunately, our `LOCK_DSN=null` doesn't match any case in `StoreFactory`, which has the effect to throw `Symfony\Component\Lock\Exception\InvalidArgumentException`, which is not catched in `LockStoreSchemaListener`.

This PR takes care of this: at this time, `LockStoreSchemaListener` only deals with `DoctrineDbalStore`. I think it is safe to ignore any instantiation problem that may occur in the `StoreFactory`.

Commits
-------

0acd403 [DoctrineBridge] Ignore invalid stores in `LockStoreSchemaListener` raised by `StoreFactory`
  • Loading branch information
nicolas-grekas committed Sep 29, 2023
2 parents 04c7ccf + 0acd403 commit 69ef02e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Doctrine\SchemaListener;

use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\Store\DoctrineDbalStore;

Expand All @@ -28,12 +29,20 @@ public function postGenerateSchema(GenerateSchemaEventArgs $event): void
{
$connection = $event->getEntityManager()->getConnection();

foreach ($this->stores as $store) {
if (!$store instanceof DoctrineDbalStore) {
continue;
$storesIterator = new \ArrayIterator($this->stores);
while ($storesIterator->valid()) {
try {
$store = $storesIterator->current();
if (!$store instanceof DoctrineDbalStore) {
continue;
}

$store->configureSchema($event->getSchema(), $this->getIsSameDatabaseChecker($connection));
} catch (InvalidArgumentException) {
// no-op
}

$store->configureSchema($event->getSchema(), $this->getIsSameDatabaseChecker($connection));
$storesIterator->next();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\SchemaListener\LockStoreSchemaListener;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Store\DoctrineDbalStore;

class LockStoreSchemaListenerTest extends TestCase
Expand All @@ -39,4 +40,20 @@ public function testPostGenerateSchemaLockPdo()
$subscriber = new LockStoreSchemaListener([$lockStore]);
$subscriber->postGenerateSchema($event);
}

public function testPostGenerateSchemaWithInvalidLockStore()
{
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->expects($this->once())
->method('getConnection')
->willReturn($this->createMock(Connection::class));
$event = new GenerateSchemaEventArgs($entityManager, new Schema());

$subscriber = new LockStoreSchemaListener((static function (): \Generator {
yield $this->createMock(DoctrineDbalStore::class);

throw new InvalidArgumentException('Unsupported Connection');
})());
$subscriber->postGenerateSchema($event);
}
}

0 comments on commit 69ef02e

Please sign in to comment.