Skip to content

Commit

Permalink
Merge pull request #11 from boekkooi/doctrine
Browse files Browse the repository at this point in the history
Doctrine tweaking
  • Loading branch information
rosstuck committed Jun 9, 2015
2 parents adad4ce + 3b4f8bd commit 093afc5
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ php:
- 7.0
- hhvm

env:
- ''
- 'DOCTRINE="false"'

matrix:
allow_failures:
- php: 7.0
include:
- php: 5.5
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
- php: 5.5
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest" DOCTRINE="false"'

before_script:
- travis_retry composer self-update
- if [ "${DOCTRINE}" == "false" ]; then composer remove league/tactician-doctrine --dev --no-update; fi
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction

script:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
namespace League\Tactician\Bundle\DependencyInjection;
namespace League\Tactician\Bundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* This compiler pass maps Handler DI tags to specific commands
*/
class CommandHandlerCompilerPass implements CompilerPassInterface
class CommandHandlerPass implements CompilerPassInterface
{
/**
* You can modify the container here before it is dumped to PHP code.
Expand Down
40 changes: 40 additions & 0 deletions DependencyInjection/Compiler/DoctrineMiddlewarePass.php
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));
}
}

5 changes: 0 additions & 5 deletions Resources/config/services/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ services:
arguments:
- @?validator

tactician.middleware.doctrine:
class: League\Tactician\Doctrine\ORM\TransactionMiddleware
arguments:
- @doctrine.orm.entity_manager

# The standard Handler method name inflectors
tactician.handler.method_name_inflector.handle:
class: League\Tactician\Handler\MethodNameInflector\HandleInflector
Expand Down
6 changes: 4 additions & 2 deletions TacticianBundle.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php namespace League\Tactician\Bundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use League\Tactician\Bundle\DependencyInjection\CommandHandlerCompilerPass;
use League\Tactician\Bundle\DependencyInjection\Compiler\CommandHandlerPass;
use League\Tactician\Bundle\DependencyInjection\Compiler\DoctrineMiddlewarePass;
use League\Tactician\Bundle\DependencyInjection\TacticianExtension;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -10,7 +11,8 @@ class TacticianBundle extends Bundle
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new CommandHandlerCompilerPass());
$container->addCompilerPass(new DoctrineMiddlewarePass());
$container->addCompilerPass(new CommandHandlerPass());
}

public function getContainerExtension()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace League\Tactician\Bundle\Tests\DependencyInjection;
namespace League\Tactician\Bundle\Tests\DependencyInjection\Compiler;

use Mockery\MockInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use League\Tactician\Bundle\DependencyInjection\CommandHandlerCompilerPass;
use League\Tactician\Bundle\DependencyInjection\Compiler\CommandHandlerPass;

class CommandHandlerCompilerPassTest extends \PHPUnit_Framework_TestCase
class CommandHandlerPassTest extends \PHPUnit_Framework_TestCase
{

/**
Expand All @@ -16,7 +16,7 @@ class CommandHandlerCompilerPassTest extends \PHPUnit_Framework_TestCase
protected $container;

/**
* @var CommandHandlerCompilerPass
* @var CommandHandlerPass
*/
protected $compiler;

Expand All @@ -25,7 +25,7 @@ protected function setUp()
parent::setUp();
$this->container = \Mockery::mock(ContainerBuilder::class);

$this->compiler = new CommandHandlerCompilerPass();
$this->compiler = new CommandHandlerPass();
}

public function testProcess()
Expand Down
133 changes: 133 additions & 0 deletions Tests/DependencyInjection/Compiler/DoctrineMiddlewarePassTest.php
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);
}
}
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
"require" : {
"php": ">=5.5",
"league/tactician": "^0.6",
"league/tactician-doctrine": "^0.6",
"symfony/framework-bundle": "~2.3"
},
"suggest": {
"league/tactician-doctrine": "For doctrine transaction middleware"
},
"autoload" : {
"psr-4" : {
"League\\Tactician\\Bundle\\" : ""
Expand All @@ -47,9 +49,11 @@
"require-dev": {
"symfony/validator": "~2.3",
"phpunit/phpunit": "~4.5",
"mockery/mockery": "0.9.*",
"mockery/mockery": "~0.9.4",
"scrutinizer/ocular": "~1.1",
"matthiasnoback/symfony-config-test": "~1.0",
"matthiasnoback/symfony-dependency-injection-test": "^0.7"
"matthiasnoback/symfony-dependency-injection-test": "^0.7",

"league/tactician-doctrine": "^0.6"
}
}

0 comments on commit 093afc5

Please sign in to comment.