-
Notifications
You must be signed in to change notification settings - Fork 106
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 #70 from Behat/feature/service-injection
Application service injection
- Loading branch information
Showing
6 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
spec/Behat/Symfony2Extension/Context/Argument/ServiceArgumentResolverSpec.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,55 @@ | ||
<?php | ||
|
||
namespace spec\Behat\Symfony2Extension\Context\Argument; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use ReflectionClass; | ||
use stdClass; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
class ServiceArgumentResolverSpec extends ObjectBehavior | ||
{ | ||
function let(KernelInterface $kernel, ContainerInterface $container) | ||
{ | ||
$kernel->getContainer()->willReturn($container); | ||
|
||
$this->beConstructedWith($kernel); | ||
} | ||
|
||
function it_resolves_arguments_starting_from_at_sign_if_they_point_to_existing_service( | ||
ReflectionClass $reflectionClass, | ||
ContainerInterface $container | ||
) { | ||
$container->has('service')->willReturn(true); | ||
$container->get('service')->willReturn($service = new stdClass()); | ||
|
||
$this->resolveArguments($reflectionClass, array('service' => '@service'))->shouldReturn( | ||
array('service' => $service) | ||
); | ||
} | ||
|
||
function it_does_not_resolve_arguments_starting_from_at_sign_if_they_do_not_point_to_existing_service( | ||
ReflectionClass $reflectionClass, | ||
ContainerInterface $container | ||
) { | ||
$container->has('service')->willReturn(false); | ||
$container->get(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->resolveArguments($reflectionClass, array('service' => '@service'))->shouldReturn( | ||
array('service' => '@service') | ||
); | ||
} | ||
|
||
function it_does_not_resolve_arguments_not_starting_from_at_sign( | ||
ReflectionClass $reflectionClass, | ||
ContainerInterface $container | ||
) { | ||
$container->get(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->resolveArguments($reflectionClass, array('service' => 'my_service'))->shouldReturn( | ||
array('service' => 'my_service') | ||
); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
src/Behat/Symfony2Extension/Context/Argument/ServiceArgumentResolver.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,70 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Behat Symfony2Extension | ||
* | ||
* (c) Konstantin Kudryashov <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Behat\Symfony2Extension\Context\Argument; | ||
|
||
use Behat\Behat\Context\Argument\ArgumentResolver; | ||
use ReflectionClass; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
/** | ||
* Resolves service arguments using the application container. | ||
* | ||
* @author Konstantin Kudryashov <[email protected]> | ||
*/ | ||
final class ServiceArgumentResolver implements ArgumentResolver | ||
{ | ||
private $kernel; | ||
|
||
/** | ||
* Initializes resolver. | ||
* | ||
* @param KernelInterface $kernel | ||
*/ | ||
public function __construct(KernelInterface $kernel) | ||
{ | ||
$this->kernel = $kernel; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolveArguments(ReflectionClass $classReflection, array $arguments) | ||
{ | ||
$newArguments = array(); | ||
|
||
foreach ($arguments as $key => $argument) { | ||
$newArguments[$key] = $this->resolveArgument($argument); | ||
} | ||
|
||
return $newArguments; | ||
} | ||
|
||
/** | ||
* Resolves single argument using container. | ||
* | ||
* @param mixed $argument | ||
* | ||
* @return object | ||
*/ | ||
private function resolveArgument($argument) | ||
{ | ||
$container = $this->kernel->getContainer(); | ||
|
||
if (!is_string($argument) || '@' != $argument[0]) { | ||
return $argument; | ||
} | ||
|
||
$serviceId = mb_substr($argument, 1, mb_strlen($argument, 'utf8'), 'utf8'); | ||
|
||
return $container->has($serviceId) ? $container->get($serviceId) : $argument; | ||
} | ||
} |
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