From b27b15dabce52f2b5c94ace4390271f88b293c8d Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Fri, 23 Jun 2023 16:03:28 +0200 Subject: [PATCH] [DoctrineBridge] Ignore invalid stores in `LockStoreSchemaListener` raised by `StoreFactory` --- SchemaListener/LockStoreSchemaListener.php | 17 +++++++++++++---- .../LockStoreSchemaListenerTest.php | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/SchemaListener/LockStoreSchemaListener.php b/SchemaListener/LockStoreSchemaListener.php index 0902b376..5ab591d3 100644 --- a/SchemaListener/LockStoreSchemaListener.php +++ b/SchemaListener/LockStoreSchemaListener.php @@ -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; @@ -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(); } } } diff --git a/Tests/SchemaListener/LockStoreSchemaListenerTest.php b/Tests/SchemaListener/LockStoreSchemaListenerTest.php index d8d06a5f..6f23d680 100644 --- a/Tests/SchemaListener/LockStoreSchemaListenerTest.php +++ b/Tests/SchemaListener/LockStoreSchemaListenerTest.php @@ -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 @@ -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); + } }