Skip to content

Commit

Permalink
Merge pull request phpspec#4 from jakzal/container-stub
Browse files Browse the repository at this point in the history
Replaced custom container with a stub
  • Loading branch information
jakzal committed Nov 3, 2013
2 parents 7fccbbf + 9a2e8e2 commit 2dd925f
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 89 deletions.
4 changes: 2 additions & 2 deletions features/describing_a_controller.feature
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Feature: Describing a controller
{
function it_should_redirect_to_the_homepage(Router $router)
{
$this->container->set('router', $router);
$this->container->get('router')->willReturn($router);
$router->generate('homepage')->willReturn('/');
Expand Down Expand Up @@ -151,7 +151,7 @@ Feature: Describing a controller
{
function it_should_render_list_of_users(EngineInterface $templating)
{
$this->container->set('templating', $templating);
$this->container->get('templating')->willReturn($templating);
$this->shouldRender('Scenario7UserBundle:User:list.html.twig', array('users' => array()))
->duringAction('list');
Expand Down
5 changes: 4 additions & 1 deletion spec/PhpSpec/Symfony2Extension/ExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpSpec\Console\IO;
use PhpSpec\ObjectBehavior;
use PhpSpec\ServiceContainer;
use PhpSpec\Wrapper\Unwrapper;
use Prophecy\Argument;

class ExtensionSpec extends ObjectBehavior
Expand Down Expand Up @@ -45,8 +46,10 @@ function it_registers_a_custom_locator_with_configuration(ServiceContainer $cont
$configurator($container->getWrappedObject());
}

function it_registers_runner_maintainers_for_the_container(ServiceContainer $container)
function it_registers_runner_maintainers_for_the_container(ServiceContainer $container, Unwrapper $unwrapper)
{
$container->get('unwrapper')->willReturn($unwrapper);

$container->setShared(
'runner.maintainers.container_initializer',
$this->service('PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerInitializerMaintainer', $container)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use PhpSpec\Runner\MatcherManager;
use PhpSpec\SpecificationInterface;
use PhpSpec\Symfony2Extension\Specification\ControllerBehavior;
use PhpSpec\Wrapper\Unwrapper;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\ContainerInterface;

class UserControllerSpec extends ControllerBehavior
{
Expand All @@ -21,8 +23,10 @@ class UserSpec extends ObjectBehavior

class ContainerInitializerMaintainerSpec extends ObjectBehavior
{
function let(ExampleNode $example, SpecificationNode $specification, \ReflectionClass $classReflection)
function let(Unwrapper $unwrapper, ExampleNode $example, SpecificationNode $specification, \ReflectionClass $classReflection)
{
$this->beConstructedWith($unwrapper);

$example->getSpecification()->willReturn($specification);
$specification->getClassReflection()->willReturn($classReflection);
}
Expand Down Expand Up @@ -56,12 +60,31 @@ function it_does_not_support_other_behaviors(ExampleNode $example, \ReflectionCl
$this->supports($example)->shouldReturn(false);
}

function it_creates_the_container(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators, \ReflectionClass $classReflection, \ReflectionProperty $property)
function it_sets_the_container_if_found_in_collaborators(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators, \ReflectionClass $classReflection, \ReflectionProperty $property, ContainerInterface $container)
{
$classReflection->getProperty('container')->willReturn($property);

$collaborators->has('container')->willReturn(true);
$collaborators->get('container')->willReturn($container);

$property->setAccessible(true)->shouldBeCalled();
$property->setValue($context, $container)->shouldBeCalled();

$this->prepare($example, $context, $matchers, $collaborators);
}

function it_creates_the_container_collaborator_if_it_is_not_found(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators, \ReflectionClass $classReflection, \ReflectionProperty $property, ContainerInterface $container)
{
$classReflection->getProperty('container')->willReturn($property);

$collaborators->has('container')->willReturn(false);
$collaborators->set('container', Argument::type('Symfony\Component\DependencyInjection\ContainerInterface'))
->will(function ($arguments, $collaborators) {
$collaborators->get('container')->willReturn($arguments[1]);
});

$property->setAccessible(true)->shouldBeCalled();
$property->setValue($context, Argument::type('PhpSpec\\Symfony2Extension\\Specification\\Container'))->shouldBeCalled();
$property->setValue($context, Argument::type('Symfony\Component\DependencyInjection\ContainerInterface'))->shouldBeCalled();

$this->prepare($example, $context, $matchers, $collaborators);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use PhpSpec\ObjectBehavior;
use PhpSpec\Runner\CollaboratorManager;
use PhpSpec\Runner\MatcherManager;
use PhpSpec\Symfony2Extension\Specification\Container;
use PhpSpec\Symfony2Extension\Specification\ControllerBehavior;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ContainerInjectorMaintainerSpec extends ObjectBehavior
{
Expand All @@ -24,7 +24,7 @@ function it_is_a_container_maintainer()
$this->shouldHaveType('PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerMaintainer');
}

function it_injects_the_container_into_the_subject(ExampleNode $example, ControllerBehavior $context, MatcherManager $matchers, CollaboratorManager $collaborators, \ReflectionClass $classReflection, \ReflectionProperty $property, Container $container)
function it_injects_the_container_into_the_subject(ExampleNode $example, ControllerBehavior $context, MatcherManager $matchers, CollaboratorManager $collaborators, \ReflectionClass $classReflection, \ReflectionProperty $property, ContainerInterface $container)
{
$classReflection->getProperty('container')->willReturn($property);

Expand Down
47 changes: 0 additions & 47 deletions spec/PhpSpec/Symfony2Extension/Specification/ContainerSpec.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/PhpSpec/Symfony2Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private function registerRunnerMaintainers(ServiceContainer $container)
$container->setShared(
'runner.maintainers.container_initializer',
function ($c) {
return new ContainerInitializerMaintainer();
return new ContainerInitializerMaintainer($c->get('unwrapper'));
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,30 @@
use PhpSpec\Runner\CollaboratorManager;
use PhpSpec\Runner\MatcherManager;
use PhpSpec\SpecificationInterface;
use PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerMaintainer;
use PhpSpec\Symfony2Extension\Specification\Container;
use PhpSpec\Wrapper\Collaborator;
use PhpSpec\Wrapper\Unwrapper;
use Prophecy\Prophet;

class ContainerInitializerMaintainer extends ContainerMaintainer
{
/**
* @var Unwrapper
*/
private $unwrapper;

/**
* @var Prophet
*/
private $prophet;

/**
* @param Unwrapper $unwrapper
*/
public function __construct(Unwrapper $unwrapper)
{
$this->unwrapper = $unwrapper;
}

/**
* @param ExampleNode $example
* @param SpecificationInterface $context
Expand All @@ -19,9 +38,30 @@ class ContainerInitializerMaintainer extends ContainerMaintainer
*/
public function prepare(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators)
{
$this->prophet = new Prophet(null, $this->unwrapper, null);

if (!$collaborators->has('container')) {
$container = new Collaborator($this->prophet->prophesize());
$container->beADoubleOf('Symfony\Component\DependencyInjection\ContainerInterface');
$collaborators->set('container', $container);
}

$container = $collaborators->get('container');

$containerProperty = $example->getSpecification()->getClassReflection()->getProperty('container');
$containerProperty->setAccessible(true);
$containerProperty->setValue($context, new Container());
$containerProperty->setValue($context, $container);
}

/**
* @param ExampleNode $example
* @param SpecificationInterface $context
* @param MatcherManager $matchers
* @param CollaboratorManager $collaborators
*/
public function teardown(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators)
{
$this->prophet->checkPredictions();
}

/**
Expand Down
26 changes: 0 additions & 26 deletions src/PhpSpec/Symfony2Extension/Specification/Container.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
namespace PhpSpec\Symfony2Extension\Specification;

use PhpSpec\ObjectBehavior;
use PhpSpec\Symfony2Extension\Specification\Container;
use PhpSpec\Wrapper\Subject;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ControllerBehavior extends ObjectBehavior
{
/**
* @var Container|null
* @var ContainerInterface|null
*/
protected $container;

/**
* @param Container $container
* @param ContainerInterface $container
*/
public function setContainer(Container $container)
public function setContainer(ContainerInterface $container)
{
$this->container = $container;

Expand Down

0 comments on commit 2dd925f

Please sign in to comment.