From ecaedab2d29ac85a33a1470f3929391f82e01cf7 Mon Sep 17 00:00:00 2001 From: Philipp Andreas Date: Thu, 14 Mar 2024 20:52:26 +0100 Subject: [PATCH] Added example for symfony --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/README.md b/README.md index 31b3d6b..6dccfd0 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,51 @@ $container->run(); // Do something with opensearch ``` +### Use with symfony + +```yaml +# config/packages/test/services.yaml + +parameters: + 'doctrine.dbal.connection_factory.class': App\Tests\TestConnectionFactory +``` + +```php + +namespace App\Tests; + +use Doctrine\Bundle\DoctrineBundle\ConnectionFactory; +use Doctrine\Common\EventManager; +use Doctrine\DBAL\Configuration; +use Doctrine\DBAL\Tools\DsnParser; +use Testcontainer\Container\PostgresContainer; + +class TestConnectionFactory extends ConnectionFactory +{ + static $testDsn; + + public function __construct(array $typesConfig, ?DsnParser $dsnParser = null) + { + if (!$this::$testDsn) { + $psql = PostgresContainer::make('14.0', 'password'); + $psql->withPostgresDatabase('database'); + $psql->withPostgresUser('user'); + $psql->run(); + $this::$testDsn = sprintf('postgresql://user:password@%s:5432/database?serverVersion=14&charset=utf8', $psql->getAddress()); + } + parent::__construct($typesConfig, $dsnParser); + } + + + public function createConnection(array $params, ?Configuration $config = null, ?EventManager $eventManager = null, array $mappingTypes = []) + { + $params['url'] = $this::$testDsn; + return parent::createConnection($params, $config, $eventManager, $mappingTypes); + } + +} +``` + ## License [MIT](https://choosealicense.com/licenses/mit/)