Skip to content

Commit

Permalink
Merge pull request #70 from Behat/feature/service-injection
Browse files Browse the repository at this point in the history
Application service injection
  • Loading branch information
everzet committed Sep 4, 2014
2 parents e461dc3 + f1f18c9 commit 815ec1f
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 3 deletions.
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')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function it_is_an_event_subscriber()

function it_subscribes_to_events()
{
$this->getSubscribedEvents()->shouldHaveCount(4);
$this->getSubscribedEvents()->shouldHaveCount(2);
}

function it_does_nothing_for_non_kernel_aware_contexts(Context $context)
Expand Down
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;
}
}
10 changes: 10 additions & 0 deletions src/Behat/Symfony2Extension/ServiceContainer/Symfony2Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function load(ContainerBuilder $container, array $config)
$this->loadFeatureLocator($container);
$this->loadKernel($container, $config['kernel']);
$this->loadSuiteGenerator($container, $config['context']);
$this->loadServiceArgumentResolver($container);
}

/**
Expand Down Expand Up @@ -186,4 +187,13 @@ private function loadSuiteGenerator(ContainerBuilder $container, array $config)
$definition->addTag(SuiteExtension::GENERATOR_TAG, array('priority' => 100));
$container->setDefinition('symfony2_extension.suite.generator', $definition);
}

private function loadServiceArgumentResolver(ContainerBuilder $container)
{
$definition = new Definition('Behat\Symfony2Extension\Context\Argument\ServiceArgumentResolver', array(
new Reference(self::KERNEL_ID)
));
$definition->addTag(ContextExtension::ARGUMENT_RESOLVER_TAG, array('priority' => 0));
$container->setDefinition('symfony2_extension.context.argument.service_resolver', $definition);
}
}
5 changes: 4 additions & 1 deletion testapp/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ default:
tags: '~@web'
web:
type: symfony_bundle
contexts: ['Behat\Sf2DemoBundle\Features\Context\WebContext']
contexts:
- Behat\Sf2DemoBundle\Features\Context\WebContext:
simpleArg: 'string'
session: @session
bundle: 'BehatSf2DemoBundle'
filters:
tags: '@web'
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
namespace Behat\Sf2DemoBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Symfony\Component\HttpKernel\KernelInterface;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\KernelInterface;

class WebContext extends MinkContext implements KernelAwareContext
{
private $kernel;

public function __construct(Session $session, $simpleArg)
{
}

/**
* Sets HttpKernel instance.
* This method will be automatically called by Symfony2Extension ContextInitializer.
Expand Down

0 comments on commit 815ec1f

Please sign in to comment.