-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from boekkooi/doctrine
Doctrine tweaking
- Loading branch information
Showing
8 changed files
with
198 additions
and
17 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
4 changes: 2 additions & 2 deletions
4
...yInjection/CommandHandlerCompilerPass.php → ...Injection/Compiler/CommandHandlerPass.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
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,40 @@ | ||
<?php | ||
namespace League\Tactician\Bundle\DependencyInjection\Compiler; | ||
|
||
use League\Tactician\Doctrine\ORM\TransactionMiddleware; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* This compiler pass registers doctrine entity manager middleware | ||
*/ | ||
class DoctrineMiddlewarePass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!class_exists(TransactionMiddleware::class) || !$container->hasParameter('doctrine.entity_managers')) { | ||
return; | ||
} | ||
|
||
$entityManagers = $container->getParameter('doctrine.entity_managers'); | ||
if (empty($entityManagers)) { | ||
return; | ||
} | ||
|
||
foreach ($entityManagers as $name => $serviceId) { | ||
$container->setDefinition( | ||
sprintf('tactician.middleware.doctrine.%s', $name), | ||
new Definition(TransactionMiddleware::class, [ new Reference($serviceId) ]) | ||
); | ||
} | ||
|
||
$defaultEntityManager = $container->getParameter('doctrine.default_entity_manager'); | ||
$container->setAlias('tactician.middleware.doctrine', sprintf('tactician.middleware.doctrine.%s', $defaultEntityManager)); | ||
} | ||
} | ||
|
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
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
133 changes: 133 additions & 0 deletions
133
Tests/DependencyInjection/Compiler/DoctrineMiddlewarePassTest.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,133 @@ | ||
<?php | ||
|
||
namespace League\Tactician\Bundle\Tests\DependencyInjection\Compiler; | ||
|
||
use League\Tactician\Doctrine\ORM\TransactionMiddleware; | ||
use Mockery\MockInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use League\Tactician\Bundle\DependencyInjection\Compiler\DoctrineMiddlewarePass; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class DoctrineMiddlewarePassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ContainerBuilder|MockInterface | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* @var DoctrineMiddlewarePass | ||
*/ | ||
protected $compiler; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->container = \Mockery::mock(ContainerBuilder::class); | ||
|
||
$this->compiler = new DoctrineMiddlewarePass(); | ||
} | ||
|
||
public function testProcess() | ||
{ | ||
if (!class_exists(TransactionMiddleware::class)) { | ||
$this->markTestSkipped('"league/tactician-doctrine" is not installed'); | ||
} | ||
|
||
$this->container->shouldReceive('hasParameter') | ||
->with('doctrine.entity_managers') | ||
->once() | ||
->andReturn(true); | ||
|
||
$this->container->shouldReceive('getParameter') | ||
->with('doctrine.entity_managers') | ||
->once() | ||
->andReturn([ | ||
'default' => 'doctrine.orm.default_entity_manager', | ||
'second' => 'doctrine.orm.second_entity_manager', | ||
]); | ||
|
||
$this->container->shouldReceive('getParameter') | ||
->with('doctrine.default_entity_manager') | ||
->once() | ||
->andReturn('default'); | ||
|
||
$this->container->shouldReceive('setDefinition') | ||
->andReturnUsing(function($name, Definition $def) { | ||
\PHPUnit_Framework_Assert::assertEquals('tactician.middleware.doctrine.default', $name); | ||
|
||
\PHPUnit_Framework_Assert::assertEquals(TransactionMiddleware::class, $def->getClass()); | ||
\PHPUnit_Framework_Assert::assertCount(1, $def->getArguments()); | ||
\PHPUnit_Framework_Assert::assertInstanceOf(Reference::class, $def->getArgument(0)); | ||
\PHPUnit_Framework_Assert::assertEquals('doctrine.orm.default_entity_manager', (string)$def->getArgument(0)); | ||
}) | ||
->once(); | ||
|
||
$this->container->shouldReceive('setDefinition') | ||
->andReturnUsing(function($name, Definition $def) { | ||
\PHPUnit_Framework_Assert::assertEquals('tactician.middleware.doctrine.second', $name); | ||
|
||
\PHPUnit_Framework_Assert::assertEquals(TransactionMiddleware::class, $def->getClass()); | ||
\PHPUnit_Framework_Assert::assertCount(1, $def->getArguments()); | ||
\PHPUnit_Framework_Assert::assertInstanceOf(Reference::class, $def->getArgument(0)); | ||
\PHPUnit_Framework_Assert::assertEquals('doctrine.orm.second_entity_manager', (string)$def->getArgument(0)); | ||
}) | ||
->once(); | ||
|
||
$this->container->shouldReceive('setDefinition') | ||
->with('tactician.middleware.doctrine.second') | ||
->once(); | ||
|
||
$this->container->shouldReceive('setAlias') | ||
->once() | ||
->with('tactician.middleware.doctrine', 'tactician.middleware.doctrine.default'); | ||
|
||
$this->compiler->process($this->container); | ||
} | ||
|
||
public function testDoNotProcessWhenThereAreNoEntityManagers() | ||
{ | ||
if (!class_exists(TransactionMiddleware::class)) { | ||
$this->markTestSkipped('"league/tactician-doctrine" is not installed'); | ||
} | ||
|
||
$this->container->shouldReceive('hasParameter') | ||
->with('doctrine.entity_managers') | ||
->once() | ||
->andReturn(false); | ||
|
||
$this->container->shouldNotReceive('getParameter') | ||
->withAnyArgs(); | ||
|
||
$this->container->shouldNotReceive('setDefinition') | ||
->withAnyArgs(); | ||
|
||
$this->container->shouldNotReceive('setAlias') | ||
->withAnyArgs(); | ||
|
||
$this->compiler->process($this->container); | ||
} | ||
|
||
public function testDoNotProcessWhenTacticianDoctrineIsNotInstalled() | ||
{ | ||
if (class_exists(TransactionMiddleware::class)) { | ||
$this->markTestSkipped('"league/tactician-doctrine" is installed'); | ||
} | ||
|
||
$this->container->shouldReceive('hasParameter') | ||
->with('doctrine.entity_managers') | ||
->andReturn(true); | ||
|
||
$this->container->shouldNotReceive('getParameter') | ||
->withAnyArgs(); | ||
|
||
$this->container->shouldNotReceive('setDefinition') | ||
->withAnyArgs(); | ||
|
||
$this->container->shouldNotReceive('setAlias') | ||
->withAnyArgs(); | ||
|
||
$this->compiler->process($this->container); | ||
} | ||
} |
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