Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automatically updating Doctrine Schema from Messenger
- Loading branch information
1 parent
c30d6f9
commit 1d7afdb
Showing
6 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...senger/Bridge/Doctrine/Tests/Transport/MessengerTransportDoctrineSchemaSubscriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Bridge\Doctrine\Tests\Transport; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineTransport; | ||
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\MessengerTransportDoctrineSchemaSubscriber; | ||
use Symfony\Component\Messenger\Transport\TransportInterface; | ||
|
||
class MessengerTransportDoctrineSchemaSubscriberTest extends TestCase | ||
{ | ||
public function testPostGenerateSchema() | ||
{ | ||
$schema = new Schema(); | ||
$dbalConnection = $this->createMock(Connection::class); | ||
$entityManager = $this->createMock(EntityManagerInterface::class); | ||
$entityManager->expects($this->once()) | ||
->method('getConnection') | ||
->willReturn($dbalConnection); | ||
$event = new GenerateSchemaEventArgs($entityManager, $schema); | ||
|
||
$doctrineTransport = $this->createMock(DoctrineTransport::class); | ||
$doctrineTransport->expects($this->once()) | ||
->method('configureSchema') | ||
->with($schema, $dbalConnection); | ||
$otherTransport = $this->createMock(TransportInterface::class); | ||
$otherTransport->expects($this->never()) | ||
->method($this->anything()); | ||
|
||
$subscriber = new MessengerTransportDoctrineSchemaSubscriber([$doctrineTransport, $otherTransport]); | ||
$subscriber->postGenerateSchema($event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ponent/Messenger/Bridge/Doctrine/Transport/MessengerTransportDoctrineSchemaSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Bridge\Doctrine\Transport; | ||
|
||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; | ||
use Doctrine\ORM\Tools\ToolEvents; | ||
use Symfony\Component\Messenger\Transport\TransportInterface; | ||
|
||
class MessengerTransportDoctrineSchemaSubscriber implements EventSubscriber | ||
{ | ||
private $transports; | ||
|
||
/** | ||
* @param iterable|TransportInterface[] $transports | ||
*/ | ||
public function __construct(iterable $transports) | ||
{ | ||
$this->transports = $transports; | ||
} | ||
|
||
public function postGenerateSchema(GenerateSchemaEventArgs $event) | ||
{ | ||
$dbalConnection = $event->getEntityManager()->getConnection(); | ||
foreach ($this->transports as $transport) { | ||
if (!$transport instanceof DoctrineTransport) { | ||
continue; | ||
} | ||
|
||
$transport->configureSchema($event->getSchema(), $dbalConnection); | ||
} | ||
} | ||
|
||
public function getSubscribedEvents(): array | ||
{ | ||
return [ToolEvents::postGenerateSchema]; | ||
} | ||
} |