Skip to content

Commit

Permalink
Merge pull request #6 from smurfy/symfony
Browse files Browse the repository at this point in the history
Added example for symfony
  • Loading branch information
shyim authored Mar 15, 2024
2 parents bfbaf8a + ecaedab commit f6f40a5
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down

0 comments on commit f6f40a5

Please sign in to comment.