|
| 1 | +<?php |
|
8000
2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Messenger\Bridge\Doctrine\Tests\Transport; |
| 13 | + |
| 14 | +use Doctrine\DBAL\Configuration; |
| 15 | +use Doctrine\DBAL\Connection; |
| 16 | +use Doctrine\DBAL\DriverManager; |
| 17 | +use Doctrine\DBAL\Schema\Column; |
| 18 | +use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory; |
| 19 | +use Doctrine\DBAL\Schema\Sequence; |
| 20 | +use Doctrine\DBAL\Schema\Table; |
| 21 | +use Doctrine\DBAL\Tools\DsnParser; |
| 22 | +use Doctrine\DBAL\Types\Type; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | +use Symfony\Component\Messenger\Bridge\Doctrine\Transport\PostgreSqlConnection; |
| 25 | + |
| 26 | +/** |
| 27 | + * This test checks on a postgres connection whether the doctrine asset filter works as expected |
| 28 | + * |
| 29 | + * @requires extension pdo_pgsql |
| 30 | + * |
| 31 | + * @group integration |
| 32 | + */ |
| 33 | +class DoctrinePostgreSqlFilterIntegrationTest extends TestCase |
| 34 | +{ |
| 35 | + private Connection $driverConnection; |
| 36 | + |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + if (!$host = getenv('POSTGRES_HOST')) { |
| 40 | + $this->markTestSkipped('Missing POSTGRES_HOST env variable'); |
| 41 | + } |
| 42 | + |
| 43 | + $url = "pdo-pgsql://postgres:password@$host"; |
| 44 | + $params = (new DsnParser())->parse($url); |
| 45 | + $config = new Configuration(); |
| 46 | + if (class_exists(DefaultSchemaManagerFactory::class)) { |
| 47 | + $config->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); |
| 48 | + } |
| 49 | + |
| 50 | + $this->driverConnection = DriverManager::getConnection($params, $config); |
| 51 | + |
| 52 | + $this->createAssets(); |
| 53 | + } |
| 54 | + |
| 55 | + protected function tearDown(): void |
| 56 | + { |
| 57 | + $this->removeAssets(); |
| 58 | + |
| 59 | + $this->driverConnection->close(); |
| 60 | + } |
| 61 | + |
| 62 | + public function testFilterAssets(): void |
| 63 | + { |
| 64 | + $schemaManager = $this->driverConnection->createSchemaManager(); |
| 65 | + |
| 66 | + $this->assertFalse($schemaManager->tableExists('queue_table')); |
| 67 | + $this->assertTrue($schemaManager->tableExists('app_table')); |
| 68 | + $this->assertTrue($this->hasSequence('app_table_id')); |
| 69 | + |
| 70 | + $connection = new PostgreSqlConnection(['table_name' => 'queue_table'], $this->driverConnection); |
| 71 | + $connection->setup(); |
| 72 | + |
| 73 | + $schemaManager = $this->driverConnection->createSchemaManager(); |
| 74 | + |
| 75 | + $this->assertTrue($schemaManager->tableExists('queue_table')); |
| 76 | + $this->assertTrue($schemaManager->tableExists('app_table')); |
| 77 | + $this->assertTrue($this->hasSequence('app_table_id')); |
| 78 | + } |
| 79 | + |
| 80 | + private function createAssets(): void |
| 81 | + { |
| 82 | + $this->removeAssets(); |
| 83 | + |
| 84 | + $schemaManager = $this->driverConnection->createSchemaManager(); |
| 85 | + $schemaManager->createTable(new Table('app_table', [new Column('id', Type::getType('integer'))])); |
| 86 | + $schemaManager->createSequence(new Sequence('app_table_id')); |
| 87 | + } |
| 88 | + |
| 89 | + private function removeAssets(): void |
| 90 | + { |
| 91 | + $schemaManager = $this->driverConnection->createSchemaManager(); |
| 92 | + |
| 93 | + if ($schemaManager->tableExists('queue_table')) { |
| 94 | + $schemaManager->dropTable('queue_table'); |
| 95 | + } |
| 96 | + |
| 97 | + if ($schemaManager->tableExists('app_table')) { |
| 98 | + $schemaManager->dropTable('app_table'); |
| 99 | + } |
| 100 | + |
| 101 | + if ($this->hasSequence('app_table_id')) { |
| 102 | + $schemaManager->dropSequence('app_table_id'); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private function hasSequence(string $name): bool |
| 107 | + { |
| 108 | + $schemaManager = $this->driverConnection->createSchemaManager(); |
| 109 | + |
| 110 | + $sequences = $schemaManager->listSequences(); |
| 111 | + foreach ($sequences as $sequence) { |
| 112 | + if ($sequence->getName() === $name) { |
| 113 | + return true; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return false; |
| 118 | + } |
| 119 | +} |
0 commit comments