Skip to content

Commit

Permalink
Add autowiring for Doctrine\DBAL\Connection (doctrine#685)
Browse files Browse the repository at this point in the history
* Add autowiring for Doctrine\DBAL\Connection

Although this is a class rather than an interface, it is the main API of
Doctrine DBAL, which has more helper methods than the driver connection
interface. So using it in typehints make sense when using these helpers.

* Add a test ensuring autowiring aliases are there.
  • Loading branch information
stof authored and fabpot committed Sep 29, 2017
1 parent 83d9d1c commit 8d77f6e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions Resources/config/dbal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<services>
<service id="Doctrine\DBAL\Driver\Connection" alias="database_connection" public="false" />
<service id="Doctrine\DBAL\Connection" alias="database_connection" public="false" />

<service id="doctrine.dbal.logger.chain" class="%doctrine.dbal.logger.chain.class%" public="false" abstract="true">
<call method="addLogger">
Expand Down
29 changes: 29 additions & 0 deletions Tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Doctrine\Bundle\DoctrineBundle\Tests\Builder\BundleConfigurationBuilder;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Proxy\AbstractProxyFactory;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Version;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -26,6 +31,30 @@

class DoctrineExtensionTest extends \PHPUnit_Framework_TestCase
{
public function testAutowiringAlias()
{
$container = $this->getContainer();
$extension = new DoctrineExtension();
$config = BundleConfigurationBuilder::createBuilderWithBaseValues()->build();

$extension->load(array($config), $container);

$expectedAliases = array(
DriverConnection::class => 'database_connection',
Connection::class => 'database_connection',
ManagerRegistry::class => 'doctrine',
ObjectManager::class => 'doctrine.orm.entity_manager',
EntityManagerInterface::class => 'doctrine.orm.entity_manager',
);

foreach ($expectedAliases as $id => $target) {
$this->assertTrue($container->hasAlias($id), sprintf('The container should have a `%s` alias for autowiring support.', $id));

$alias = $container->getAlias($id);
$this->assertEquals($target, (string) $alias, sprintf('The autowiring for `%s` should use `%s`.', $id, $target));
$this->assertFalse($alias->isPublic(), sprintf('The autowiring alias for `%s` should be private.', $id, $target));
}
}

public function testDbalGenerateDefaultConnectionConfiguration()
{
Expand Down

0 comments on commit 8d77f6e

Please sign in to comment.