Skip to content
This repository was archived by the owner on Feb 14, 2021. It is now read-only.

Commit 2dd925f

Browse files
committed
Merge pull request #4 from jakzal/container-stub
Replaced custom container with a stub
2 parents 7fccbbf + 9a2e8e2 commit 2dd925f

File tree

9 files changed

+82
-89
lines changed

9 files changed

+82
-89
lines changed

features/describing_a_controller.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Feature: Describing a controller
101101
{
102102
function it_should_redirect_to_the_homepage(Router $router)
103103
{
104-
$this->container->set('router', $router);
104+
$this->container->get('router')->willReturn($router);
105105
106106
$router->generate('homepage')->willReturn('/');
107107
@@ -151,7 +151,7 @@ Feature: Describing a controller
151151
{
152152
function it_should_render_list_of_users(EngineInterface $templating)
153153
{
154-
$this->container->set('templating', $templating);
154+
$this->container->get('templating')->willReturn($templating);
155155
156156
$this->shouldRender('Scenario7UserBundle:User:list.html.twig', array('users' => array()))
157157
->duringAction('list');

spec/PhpSpec/Symfony2Extension/ExtensionSpec.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpSpec\Console\IO;
77
use PhpSpec\ObjectBehavior;
88
use PhpSpec\ServiceContainer;
9+
use PhpSpec\Wrapper\Unwrapper;
910
use Prophecy\Argument;
1011

1112
class ExtensionSpec extends ObjectBehavior
@@ -45,8 +46,10 @@ function it_registers_a_custom_locator_with_configuration(ServiceContainer $cont
4546
$configurator($container->getWrappedObject());
4647
}
4748

48-
function it_registers_runner_maintainers_for_the_container(ServiceContainer $container)
49+
function it_registers_runner_maintainers_for_the_container(ServiceContainer $container, Unwrapper $unwrapper)
4950
{
51+
$container->get('unwrapper')->willReturn($unwrapper);
52+
5053
$container->setShared(
5154
'runner.maintainers.container_initializer',
5255
$this->service('PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerInitializerMaintainer', $container)

spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainerSpec.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use PhpSpec\Runner\MatcherManager;
1010
use PhpSpec\SpecificationInterface;
1111
use PhpSpec\Symfony2Extension\Specification\ControllerBehavior;
12+
use PhpSpec\Wrapper\Unwrapper;
1213
use Prophecy\Argument;
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
1315

1416
class UserControllerSpec extends ControllerBehavior
1517
{
@@ -21,8 +23,10 @@ class UserSpec extends ObjectBehavior
2123

2224
class ContainerInitializerMaintainerSpec extends ObjectBehavior
2325
{
24-
function let(ExampleNode $example, SpecificationNode $specification, \ReflectionClass $classReflection)
26+
function let(Unwrapper $unwrapper, ExampleNode $example, SpecificationNode $specification, \ReflectionClass $classReflection)
2527
{
28+
$this->beConstructedWith($unwrapper);
29+
2630
$example->getSpecification()->willReturn($specification);
2731
$specification->getClassReflection()->willReturn($classReflection);
2832
}
@@ -56,12 +60,31 @@ function it_does_not_support_other_behaviors(ExampleNode $example, \ReflectionCl
5660
$this->supports($example)->shouldReturn(false);
5761
}
5862

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

67+
$collaborators->has('container')->willReturn(true);
68+
$collaborators->get('container')->willReturn($container);
69+
70+
$property->setAccessible(true)->shouldBeCalled();
71+
$property->setValue($context, $container)->shouldBeCalled();
72+
73+
$this->prepare($example, $context, $matchers, $collaborators);
74+
}
75+
76+
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)
77+
{
78+
$classReflection->getProperty('container')->willReturn($property);
79+
80+
$collaborators->has('container')->willReturn(false);
81+
$collaborators->set('container', Argument::type('Symfony\Component\DependencyInjection\ContainerInterface'))
82+
->will(function ($arguments, $collaborators) {
83+
$collaborators->get('container')->willReturn($arguments[1]);
84+
});
85+
6386
$property->setAccessible(true)->shouldBeCalled();
64-
$property->setValue($context, Argument::type('PhpSpec\\Symfony2Extension\\Specification\\Container'))->shouldBeCalled();
87+
$property->setValue($context, Argument::type('Symfony\Component\DependencyInjection\ContainerInterface'))->shouldBeCalled();
6588

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

spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInjectorMaintainerSpec.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use PhpSpec\ObjectBehavior;
88
use PhpSpec\Runner\CollaboratorManager;
99
use PhpSpec\Runner\MatcherManager;
10-
use PhpSpec\Symfony2Extension\Specification\Container;
1110
use PhpSpec\Symfony2Extension\Specification\ControllerBehavior;
1211
use Prophecy\Argument;
12+
use Symfony\Component\DependencyInjection\ContainerInterface;
1313

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

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

spec/PhpSpec/Symfony2Extension/Specification/ContainerSpec.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/PhpSpec/Symfony2Extension/Extension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private function registerRunnerMaintainers(ServiceContainer $container)
3535
$container->setShared(
3636
'runner.maintainers.container_initializer',
3737
function ($c) {
38-
return new ContainerInitializerMaintainer();
38+
return new ContainerInitializerMaintainer($c->get('unwrapper'));
3939
}
4040
);
4141

src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainer.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,30 @@
66
use PhpSpec\Runner\CollaboratorManager;
77
use PhpSpec\Runner\MatcherManager;
88
use PhpSpec\SpecificationInterface;
9-
use PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerMaintainer;
10-
use PhpSpec\Symfony2Extension\Specification\Container;
9+
use PhpSpec\Wrapper\Collaborator;
10+
use PhpSpec\Wrapper\Unwrapper;
11+
use Prophecy\Prophet;
1112

1213
class ContainerInitializerMaintainer extends ContainerMaintainer
1314
{
15+
/**
16+
* @var Unwrapper
17+
*/
18+
private $unwrapper;
19+
20+
/**
21+
* @var Prophet
22+
*/
23+
private $prophet;
24+
25+
/**
26+
* @param Unwrapper $unwrapper
27+
*/
28+
public function __construct(Unwrapper $unwrapper)
29+
{
30+
$this->unwrapper = $unwrapper;
31+
}
32+
1433
/**
1534
* @param ExampleNode $example
1635
* @param SpecificationInterface $context
@@ -19,9 +38,30 @@ class ContainerInitializerMaintainer extends ContainerMaintainer
1938
*/
2039
public function prepare(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators)
2140
{
41+
$this->prophet = new Prophet(null, $this->unwrapper, null);
42+
43+
if (!$collaborators->has('container')) {
44+
$container = new Collaborator($this->prophet->prophesize());
45+
$container->beADoubleOf('Symfony\Component\DependencyInjection\ContainerInterface');
46+
$collaborators->set('container', $container);
47+
}
48+
49+
$container = $collaborators->get('container');
50+
2251
$containerProperty = $example->getSpecification()->getClassReflection()->getProperty('container');
2352
$containerProperty->setAccessible(true);
24-
$containerProperty->setValue($context, new Container());
53+
$containerProperty->setValue($context, $container);
54+
}
55+
56+
/**
57+
* @param ExampleNode $example
58+
* @param SpecificationInterface $context
59+
* @param MatcherManager $matchers
60+
* @param CollaboratorManager $collaborators
61+
*/
62+
public function teardown(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators)
63+
{
64+
$this->prophet->checkPredictions();
2565
}
2666

2767
/**

src/PhpSpec/Symfony2Extension/Specification/Container.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/PhpSpec/Symfony2Extension/Specification/ControllerBehavior.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
namespace PhpSpec\Symfony2Extension\Specification;
44

55
use PhpSpec\ObjectBehavior;
6-
use PhpSpec\Symfony2Extension\Specification\Container;
76
use PhpSpec\Wrapper\Subject;
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
88

99
class ControllerBehavior extends ObjectBehavior
1010
{
1111
/**
12-
* @var Container|null
12+
* @var ContainerInterface|null
1313
*/
1414
protected $container;
1515

1616
/**
17-
* @param Container $container
17+
* @param ContainerInterface $container
1818
*/
19-
public function setContainer(Container $container)
19+
public function setContainer(ContainerInterface $container)
2020
{
2121
$this->container = $container;
2222

0 commit comments

Comments
 (0)