From 9a2e8e21ee8df64a36dddee8d6a0b6c91b790cc8 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Sat, 2 Nov 2013 17:51:35 +0000 Subject: [PATCH] Replaced custom container with a stub. --- features/describing_a_controller.feature | 4 +- .../Symfony2Extension/ExtensionSpec.php | 5 +- .../ContainerInitializerMaintainerSpec.php | 29 ++++++++++-- .../ContainerInjectorMaintainerSpec.php | 4 +- .../Specification/ContainerSpec.php | 47 ------------------- src/PhpSpec/Symfony2Extension/Extension.php | 2 +- .../ContainerInitializerMaintainer.php | 46 ++++++++++++++++-- .../Specification/Container.php | 26 ---------- .../Specification/ControllerBehavior.php | 8 ++-- 9 files changed, 82 insertions(+), 89 deletions(-) delete mode 100644 spec/PhpSpec/Symfony2Extension/Specification/ContainerSpec.php delete mode 100644 src/PhpSpec/Symfony2Extension/Specification/Container.php diff --git a/features/describing_a_controller.feature b/features/describing_a_controller.feature index 6a17650..c7b637b 100644 --- a/features/describing_a_controller.feature +++ b/features/describing_a_controller.feature @@ -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('/'); @@ -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'); diff --git a/spec/PhpSpec/Symfony2Extension/ExtensionSpec.php b/spec/PhpSpec/Symfony2Extension/ExtensionSpec.php index 8638195..a8cebb9 100644 --- a/spec/PhpSpec/Symfony2Extension/ExtensionSpec.php +++ b/spec/PhpSpec/Symfony2Extension/ExtensionSpec.php @@ -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 @@ -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) diff --git a/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainerSpec.php b/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainerSpec.php index 8a55f1c..fe36965 100644 --- a/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainerSpec.php +++ b/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainerSpec.php @@ -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 { @@ -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); } @@ -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); } diff --git a/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInjectorMaintainerSpec.php b/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInjectorMaintainerSpec.php index 5c1f78f..89eb0cb 100644 --- a/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInjectorMaintainerSpec.php +++ b/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInjectorMaintainerSpec.php @@ -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 { @@ -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); diff --git a/spec/PhpSpec/Symfony2Extension/Specification/ContainerSpec.php b/spec/PhpSpec/Symfony2Extension/Specification/ContainerSpec.php deleted file mode 100644 index 7a1d339..0000000 --- a/spec/PhpSpec/Symfony2Extension/Specification/ContainerSpec.php +++ /dev/null @@ -1,47 +0,0 @@ -wrappedObject = $wrappedObject; - } - - public function getWrappedObject() - { - return $this->wrappedObject; - } -} - -class ContainerSpec extends ObjectBehavior -{ - function it_is_a_symfony_container() - { - $this->shouldHaveType('Symfony\Component\DependencyInjection\Container'); - } - - function it_unwraps_a_service_if_wrapped(\stdClass $service) - { - $this->set('my_service', new Collaborator($service)); - - $this->get('my_service')->shouldBe($service); - } - - function it_returns_a_service_if_not_wrapped(\stdClass $service) - { - $this->set('my_service', $service); - - $this->get('my_service')->shouldBe($service); - } -} diff --git a/src/PhpSpec/Symfony2Extension/Extension.php b/src/PhpSpec/Symfony2Extension/Extension.php index d5159bc..9b8ea62 100644 --- a/src/PhpSpec/Symfony2Extension/Extension.php +++ b/src/PhpSpec/Symfony2Extension/Extension.php @@ -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')); } ); diff --git a/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainer.php b/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainer.php index 2c276dc..e4fa096 100644 --- a/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainer.php +++ b/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainer.php @@ -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 @@ -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(); } /** diff --git a/src/PhpSpec/Symfony2Extension/Specification/Container.php b/src/PhpSpec/Symfony2Extension/Specification/Container.php deleted file mode 100644 index 3245a92..0000000 --- a/src/PhpSpec/Symfony2Extension/Specification/Container.php +++ /dev/null @@ -1,26 +0,0 @@ -getWrappedObject(); - } - - return $service; - } -} diff --git a/src/PhpSpec/Symfony2Extension/Specification/ControllerBehavior.php b/src/PhpSpec/Symfony2Extension/Specification/ControllerBehavior.php index dd7a34f..ef7721a 100644 --- a/src/PhpSpec/Symfony2Extension/Specification/ControllerBehavior.php +++ b/src/PhpSpec/Symfony2Extension/Specification/ControllerBehavior.php @@ -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;