Skip to content

Commit

Permalink
[ControllerAutowire] add test, fix autowiring for whole trait
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored and Tomáš Votruba committed Nov 20, 2016
1 parent 161868a commit 8702916
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Controller/Doctrine/ControllerDoctrineTrait.php
Expand Up @@ -16,14 +16,14 @@ trait ControllerDoctrineTrait
/**
* @var ManagerRegistry
*/
protected $doctrine;
private $doctrine;

public function setDoctrine(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
}

public function getDoctrine() : ManagerRegistry
protected function getDoctrine() : ManagerRegistry
{
return $this->doctrine;
}
Expand Down
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symplify\ControllerAutowire\Contract\DependencyInjection\ControllerClassMapInterface;
use Symplify\ControllerAutowire\Controller\ControllerTrait;
use Symplify\ControllerAutowire\Controller\Doctrine\ControllerDoctrineTrait;
use Symplify\ControllerAutowire\Controller\Form\ControllerFormTrait;
use Symplify\ControllerAutowire\Controller\HttpKernel\ControllerHttpKernelTrait;
Expand All @@ -23,7 +24,7 @@
use Symplify\ControllerAutowire\Controller\Session\ControllerFlashTrait;
use Symplify\ControllerAutowire\Controller\Templating\ControllerRenderTrait;

final class AutowireControllerDependencies implements CompilerPassInterface
final class AutowireControllerDependenciesPass implements CompilerPassInterface
{
/**
* @var ControllerClassMapInterface
Expand Down Expand Up @@ -89,7 +90,7 @@ private function autowireControllerTraits(Definition $controllerDefinition)
$usedTraits = class_uses($controllerDefinition->getClass());

foreach ($this->traitsToSettersToServiceNameList as $traitClass => $setterToServiceNames) {
if (! array_key_exists($traitClass, $usedTraits)) {
if (! $this->isTraitIncluded($traitClass, $usedTraits)) {
continue;
}

Expand All @@ -102,4 +103,17 @@ private function autowireControllerTraits(Definition $controllerDefinition)
}
}
}

private function isTraitIncluded(string $traitClass, array $usedTraits) : bool
{
if (array_key_exists($traitClass, $usedTraits)) {
return true;
}

if (isset($usedTraits[ControllerTrait::class])) {
return true;
}

return false;
}
}
1 change: 1 addition & 0 deletions src/HttpKernel/Controller/ControllerResolver.php
Expand Up @@ -62,6 +62,7 @@ public function getController(Request $request)
}

list($class, $method) = $this->splitControllerClassAndMethod($controllerName);

if (! isset($this->controllerClassMap[$class])) {
return $this->controllerResolver->getController($request);
}
Expand Down
4 changes: 2 additions & 2 deletions src/SymplifyControllerAutowireBundle.php
Expand Up @@ -11,7 +11,7 @@

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symplify\ControllerAutowire\DependencyInjection\Compiler\AutowireControllerDependencies;
use Symplify\ControllerAutowire\DependencyInjection\Compiler\AutowireControllerDependenciesPass;
use Symplify\ControllerAutowire\DependencyInjection\Compiler\DecorateControllerResolverPass;
use Symplify\ControllerAutowire\DependencyInjection\Compiler\RegisterControllersPass;
use Symplify\ControllerAutowire\DependencyInjection\ControllerClassMap;
Expand All @@ -30,7 +30,7 @@ public function build(ContainerBuilder $container)
$controllerClassMap = new ControllerClassMap();

$container->addCompilerPass(new RegisterControllersPass($controllerClassMap, new ControllerFinder()));
$container->addCompilerPass(new AutowireControllerDependencies($controllerClassMap));
$container->addCompilerPass(new AutowireControllerDependenciesPass($controllerClassMap));
$container->addCompilerPass(new DecorateControllerResolverPass($controllerClassMap));
}

Expand Down
21 changes: 21 additions & 0 deletions tests/CompleteTest.php
Expand Up @@ -5,12 +5,16 @@
namespace Symplify\ControllerAutowire\Tests;

use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_Assert;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symplify\ControllerAutowire\Controller\ControllerTrait;
use Symplify\ControllerAutowire\HttpKernel\Controller\ControllerResolver;
use Symplify\ControllerAutowire\Tests\CompleteTestSource\Controller\ControllerWithParameter;
use Symplify\ControllerAutowire\Tests\CompleteTestSource\DoNotScan\SomeRegisteredController;
use Symplify\ControllerAutowire\Tests\CompleteTestSource\Scan\ContainerAwareController;
use Symplify\ControllerAutowire\Tests\CompleteTestSource\Scan\TraitAwareController;
use Symplify\ControllerAutowire\Tests\HttpKernel\Controller\ControllerFinderSource\SomeController;
use Symplify\ControllerAutowire\Tests\HttpKernel\Controller\ControllerFinderSource\SomeService;

Expand Down Expand Up @@ -72,6 +76,23 @@ public function testGetAutowiredControllerWithParameter()
$this->assertSame(__DIR__, $controller->getKernelRootDir());
}

public function testGetControllerWithTrait()
{
$request = new Request();
$request->attributes->set(
'_controller',
'symplify.controllerautowire.tests.completetestsource.scan.traitawarecontroller:someAction'
);

/** @var TraitAwareController|ControllerTrait $controller */
$controller = $this->controllerResolver->getController($request)[0];

$this->assertInstanceOf(TraitAwareController::class, $controller);

$httpKernel = PHPUnit_Framework_Assert::getObjectAttribute($controller, 'httpKernel');
$this->assertInstanceOf(HttpKernelInterface::class, $httpKernel);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
*/
Expand Down
8 changes: 1 addition & 7 deletions tests/CompleteTestSource/Scan/ContainerAwareController.php
Expand Up @@ -14,18 +14,12 @@ final class ContainerAwareController implements ContainerAwareInterface
*/
private $container;

/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* @return ContainerInterface
*/
public function getContainer()
public function getContainer() : ContainerInterface
{
return $this->container;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/CompleteTestSource/Scan/TraitAwareController.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Symplify\ControllerAutowire\Tests\CompleteTestSource\Scan;

use Symplify\ControllerAutowire\Controller\ControllerTrait;

final class TraitAwareController
{
use ControllerTrait;

public function someAction()
{
}
}
10 changes: 4 additions & 6 deletions tests/HttpKernel/Controller/ControllerFinderTest.php
Expand Up @@ -26,16 +26,14 @@ public function testFindControllersInDirs()
{
$controllers = $this->controllerFinder->findControllersInDirs([__DIR__ . '/ControllerFinderSource']);

$this->assertArrayHasKey(
'symplify.controllerautowire.tests.httpkernel.controller.controllerfindersource.somecontroller',
$controllers
);
$this->assertCount(2, $controllers);

$this->assertContains(SomeOtherController::class, $controllers);
$this->assertContains(SomeController::class, $controllers);

$this->assertArrayHasKey(
'symplify.controllerautowire.tests.httpkernel.controller.controllerfindersource.someothercontroller',
'symplify.controllerautowire.tests.httpkernel.controller.controllerfindersource.somecontroller',
$controllers
);
$this->assertContains(SomeOtherController::class, $controllers);
}
}

0 comments on commit 8702916

Please sign in to comment.