diff --git a/features/bootstrap/PhpSpecContext.php b/features/bootstrap/PhpSpecContext.php index 9b1e43d..be6281a 100644 --- a/features/bootstrap/PhpSpecContext.php +++ b/features/bootstrap/PhpSpecContext.php @@ -22,7 +22,7 @@ public function createWorkDir() // paths between scenarios. $this->workDir = sys_get_temp_dir().'/PhpSpecSymfony2Extension/'; - mkdir($this->workDir, 0777, true); + @mkdir($this->workDir, 0777, true); chdir($this->workDir); } @@ -39,12 +39,19 @@ public function removeWorkDir() */ public function theSymfonyExtendsionIsEnabled() { - $phpspecyml = <<<YML + $yml = <<<YML extensions: - - PhpSpec\Symfony2Extension\Extension + - PhpSpec\Symfony2Extension\Extension YML; + file_put_contents($this->workDir.'phpspec.yml', $yml); + } - file_put_contents($this->workDir.'phpspec.yml', $phpspecyml); + /** + * @Given /^(?:|the )Symfony extension is enabled with:$/ + */ + public function theSymfonyExtendsionIsEnabledWith(PyStringNode $yml = null) + { + file_put_contents($this->workDir.'phpspec.yml', $yml); } /** @@ -83,7 +90,7 @@ public function iDescribeThe($class) */ public function iWroteSpecInThe($file, PyStringNode $string) { - mkdir(dirname($file), 0777, true); + @mkdir(dirname($file), 0777, true); file_put_contents($file, $string->getRaw()); diff --git a/features/describing_a_controller.feature b/features/describing_a_controller.feature index 46e9373..f6d491e 100644 --- a/features/describing_a_controller.feature +++ b/features/describing_a_controller.feature @@ -14,10 +14,10 @@ Feature: Describing a controller namespace Scenario1\Bundle\DemoBundle\Spec\Controller; - use PhpSpec\Symfony2Extension\Specification\ControllerBehavior; + use PhpSpec\ObjectBehavior; use Prophecy\Argument; - class UserControllerSpec extends ControllerBehavior + class UserControllerSpec extends ObjectBehavior { function it_is_container_aware() { @@ -52,10 +52,10 @@ Feature: Describing a controller namespace Scenario5\Bundle\DemoBundle\Spec\Controller; - use PhpSpec\Symfony2Extension\Specification\ControllerBehavior; + use PhpSpec\ObjectBehavior; use Prophecy\Argument; - class UserControllerSpec extends ControllerBehavior + class UserControllerSpec extends ObjectBehavior { function it_should_respond_to_the_list_action_call() { @@ -94,15 +94,17 @@ Feature: Describing a controller namespace Scenario6\Bundle\DemoBundle\Spec\Controller; - use PhpSpec\Symfony2Extension\Specification\ControllerBehavior; + use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Symfony\Component\Routing\Router; + use Symfony\Component\DependencyInjection\ContainerInterface; - class UserControllerSpec extends ControllerBehavior + class UserControllerSpec extends ObjectBehavior { - function it_should_redirect_to_the_homepage(Router $router) + function it_should_redirect_to_the_homepage(Router $router, ContainerInterface $container) { - $this->container->set('router', $router); + $this->setContainer($container); + $container->get('router')->willReturn($router); $router->generate('homepage')->willReturn('/'); @@ -144,15 +146,17 @@ Feature: Describing a controller namespace Scenario7\Bundle\DemoBundle\Spec\Controller; - use PhpSpec\Symfony2Extension\Specification\ControllerBehavior; + use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Symfony\Component\Templating\EngineInterface; + use Symfony\Component\DependencyInjection\ContainerInterface; - class UserControllerSpec extends ControllerBehavior + class UserControllerSpec extends ObjectBehavior { - function it_should_render_list_of_users(EngineInterface $templating) + function it_should_render_list_of_users(EngineInterface $templating, ContainerInterface $container) { - $this->container->set('templating', $templating); + $this->setContainer($container); + $container->get('templating')->willReturn($templating); $this->shouldRender('Scenario7UserBundle:User:list.html.twig', array('users' => array())) ->duringAction('list'); diff --git a/features/initialize_default_collaborators.feature b/features/initialize_default_collaborators.feature new file mode 100644 index 0000000..699c354 --- /dev/null +++ b/features/initialize_default_collaborators.feature @@ -0,0 +1,55 @@ +Feature: Initialize default collaborators + As a Developer + I want default collaborators to be preconfigured + In order to avoid complex let methods + + Background: + Given the Symfony extension is enabled with: + """ + extensions: + - PhpSpec\Symfony2Extension\Extension + + symfony2_extension.common-collaborators: + container: { service_container: ~ } + router: ~ + templating: ~ + request: ~ + session: ~ + doctrine: ~ + """ + + Scenario: Controller spec has access to common collaborators + Given I wrote a spec in the "src/Scenario8/Bundle/DemoBundle/Spec/Controller/UserControllerSpec.php": + """ + <?php + + namespace Scenario8\Bundle\DemoBundle\Spec\Controller; + + use PhpSpec\ObjectBehavior; + use Prophecy\Argument; + + class UserControllerSpec extends ObjectBehavior + { + function its_generateUrl_generates_urls($container) + { + $this->setContainer($container); + $this->generateUrl('homepage')->shouldReturn('homepage'); // preconfigured router! + } + } + + """ + And I wrote a class in the "src/Scenario8/Bundle/DemoBundle/Controller/UserController.php": + """ + <?php + + namespace Scenario8\Bundle\DemoBundle\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\Controller; + + class UserController extends Controller + { + } + + """ + When I run phpspec + Then I should see "1 example (1 passed)" diff --git a/features/provide_default_collaborators.feature b/features/provide_default_collaborators.feature new file mode 100644 index 0000000..7a3644e --- /dev/null +++ b/features/provide_default_collaborators.feature @@ -0,0 +1,51 @@ +Feature: Provide default collaborators + As a Developer + I want to avoid configuring framework mocks every time by hand + In order to have a working preconfigured set of mocks + + Background: + Given the Symfony extension is enabled with: + """ + extensions: + - PhpSpec\Symfony2Extension\Extension + + symfony2_extension.common-collaborators: + container: { service_container: ~ } + router: Symfony\Component\Routing\RouterInterface + """ + + Scenario: Controller spec has access to common collaborators + Given I wrote a spec in the "src/Scenario7/Bundle/DemoBundle/Spec/Controller/UserControllerSpec.php": + """ + <?php + + namespace Scenario7\Bundle\DemoBundle\Spec\Controller; + + use PhpSpec\ObjectBehavior; + use Prophecy\Argument; + + class UserControllerSpec extends ObjectBehavior + { + function its_generateUrl_generates_urls($container) + { + $this->setContainer($container); + $this->generateUrl('homepage')->shouldReturn('homepage'); + } + } + + """ + And I wrote a class in the "src/Scenario7/Bundle/DemoBundle/Controller/UserController.php": + """ + <?php + + namespace Scenario7\Bundle\DemoBundle\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\Controller; + + class UserController extends Controller + { + } + + """ + When I run phpspec + Then I should see "1 example (1 passed)" diff --git a/phpspec.yml b/phpspec.yml new file mode 100644 index 0000000..2e84dde --- /dev/null +++ b/phpspec.yml @@ -0,0 +1,14 @@ +extensions: + - PhpSpec\Symfony2Extension\Extension + +symfony2_extension.common-collaborators: + # append: false # should we support append ? (ie: provide defaults) + container: { service_container: ~ } + router: ~ + templating: ~ + request: ~ + session: ~ + doctrine: ~ + em: ~ + repository: ~ + #formFactory: { form.factory: FormFactoryInterface } diff --git a/spec/PhpSpec/Symfony2Extension/ExtensionSpec.php b/spec/PhpSpec/Symfony2Extension/ExtensionSpec.php index 8638195..7213f0a 100644 --- a/spec/PhpSpec/Symfony2Extension/ExtensionSpec.php +++ b/spec/PhpSpec/Symfony2Extension/ExtensionSpec.php @@ -7,6 +7,9 @@ use PhpSpec\ObjectBehavior; use PhpSpec\ServiceContainer; use Prophecy\Argument; +use PhpSpec\Wrapper\Unwrapper; +use PhpSpec\Symfony2Extension\Runner\Collaborator\FactoryInterface; +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerFactory; class ExtensionSpec extends ObjectBehavior { @@ -16,6 +19,9 @@ function let(ServiceContainer $container) { $container->setShared(Argument::cetera())->willReturn(); $container->addConfigurator(Argument::any())->willReturn(); + $container->getByPrefix(Argument::any())->willReturn(array()); + $container->setParam(Argument::cetera())->willReturn(); + $container->getParam(Argument::cetera())->willReturn(); } function it_is_a_phpspec_extension() @@ -45,18 +51,29 @@ 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, FactoryInterface $defaultFactory, InitializerFactory $factory) { $container->setShared( - 'runner.maintainers.container_initializer', - $this->service('PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerInitializerMaintainer', $container) + 'runner.maintainers.common_collaborators', + $this->service('PhpSpec\Symfony2Extension\Runner\Maintainer\CommonCollaboratorsMaintainer', $container) )->shouldBeCalled(); $container->setShared( - 'runner.maintainers.container_injector', - $this->service('PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerInjectorMaintainer', $container) + 'collaborator_factory.default', + $this->service('PhpSpec\Symfony2Extension\Runner\Collaborator\DefaultFactory', $container) )->shouldBeCalled(); + $container->setShared( + 'collaborator_factory', + $this->service('PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerFactory', $container) + )->shouldBeCalled(); + + $container->getByPrefix('collaborator.initializers')->willReturn(array()); + $container->getParam('symfony2_extension.common-collaborators', array())->willReturn(array()); + $container->get('collaborator_factory')->willReturn($factory); + $container->get('collaborator_factory.default')->willReturn($defaultFactory); + $container->get('unwrapper')->willReturn($unwrapper); + $this->load($container); } diff --git a/spec/PhpSpec/Symfony2Extension/Runner/Collaborator/DefaultFactorySpec.php b/spec/PhpSpec/Symfony2Extension/Runner/Collaborator/DefaultFactorySpec.php new file mode 100644 index 0000000..538a9f9 --- /dev/null +++ b/spec/PhpSpec/Symfony2Extension/Runner/Collaborator/DefaultFactorySpec.php @@ -0,0 +1,27 @@ +<?php + +namespace spec\PhpSpec\Symfony2Extension\Runner\Collaborator; + +use PhpSpec\ObjectBehavior; +use Prophecy\Argument; +use PhpSpec\Wrapper\Unwrapper; +use Prophecy\Prophecy\ObjectProphecy; +use PhpSpec\Runner\CollaboratorManager; + +class DefaultFactorySpec extends ObjectBehavior +{ + function let(Unwrapper $unwrapper) + { + $this->beConstructedWith($unwrapper); + } + + function it_is_initializable() + { + $this->shouldHaveType('PhpSpec\Symfony2Extension\Runner\Collaborator\DefaultFactory'); + } + + function its_create_should_create_collaborators(CollaboratorManager $collaborators, ObjectProphecy $prophecy) + { + $this->create($collaborators, $prophecy, 'router', 'Symfony\Component\Routing\RouterInterface')->shouldHaveType('PhpSpec\Wrapper\Collaborator'); + } +} diff --git a/spec/PhpSpec/Symfony2Extension/Runner/Collaborator/InitializerFactorySpec.php b/spec/PhpSpec/Symfony2Extension/Runner/Collaborator/InitializerFactorySpec.php new file mode 100644 index 0000000..9885e23 --- /dev/null +++ b/spec/PhpSpec/Symfony2Extension/Runner/Collaborator/InitializerFactorySpec.php @@ -0,0 +1,38 @@ +<?php + +namespace spec\PhpSpec\Symfony2Extension\Runner\Collaborator; + +use PhpSpec\ObjectBehavior; +use Prophecy\Argument; +use PhpSpec\Symfony2Extension\Runner\Collaborator\FactoryInterface; +use Prophecy\Prophecy\ObjectProphecy; +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerInterface; +use PhpSpec\Wrapper\Collaborator; +use PhpSpec\Runner\CollaboratorManager; + +class InitializerFactorySpec extends ObjectBehavior +{ + private $closure; + + function let(FactoryInterface $factory, Collaborator $collaborator, InitializerInterface $initializer) + { + $this->beConstructedWith($factory, array( + $initializer, + )); + $factory->create(Argument::cetera())->willReturn($collaborator); + } + + function its_create_should_initialize_known_collaborators(CollaboratorManager $collaborators, ObjectProphecy $prophecy, $collaborator, $initializer) + { + $initializer->supports('router')->willReturn(true); + $initializer->initialize(Argument::cetera())->shouldBeCalled(); + $this->create($collaborators, $prophecy, 'router'); + } + + function its_create_should_not_initialize_unknown_collaborators(CollaboratorManager $collaborators, ObjectProphecy $prophecy, $collaborator, $initializer) + { + $initializer->supports('request')->willReturn(false); + $initializer->initialize(Argument::cetera())->shouldNotBeCalled(); + $this->create($collaborators, $prophecy, 'request'); + } +} diff --git a/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/CommonCollaboratorsMaintainerSpec.php b/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/CommonCollaboratorsMaintainerSpec.php new file mode 100644 index 0000000..f2973bc --- /dev/null +++ b/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/CommonCollaboratorsMaintainerSpec.php @@ -0,0 +1,21 @@ +<?php + +namespace spec\PhpSpec\Symfony2Extension\Runner\Maintainer; + +use PhpSpec\ObjectBehavior; +use Prophecy\Argument; +use PhpSpec\Wrapper\Unwrapper; +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerFactory; + +class CommonCollaboratorsMaintainerSpec extends ObjectBehavior +{ + public function let(Unwrapper $unwrapper, InitializerFactory $factory) + { + $this->beConstructedWith($unwrapper, $factory, array()); + } + + function it_is_initializable() + { + $this->shouldHaveType('PhpSpec\Symfony2Extension\Runner\Maintainer\CommonCollaboratorsMaintainer'); + } +} diff --git a/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainerSpec.php b/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainerSpec.php deleted file mode 100644 index 8a55f1c..0000000 --- a/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainerSpec.php +++ /dev/null @@ -1,68 +0,0 @@ -<?php - -namespace spec\PhpSpec\Symfony2Extension\Runner\Maintainer; - -use PhpSpec\Loader\Node\ExampleNode; -use PhpSpec\Loader\Node\SpecificationNode; -use PhpSpec\ObjectBehavior; -use PhpSpec\Runner\CollaboratorManager; -use PhpSpec\Runner\MatcherManager; -use PhpSpec\SpecificationInterface; -use PhpSpec\Symfony2Extension\Specification\ControllerBehavior; -use Prophecy\Argument; - -class UserControllerSpec extends ControllerBehavior -{ -} - -class UserSpec extends ObjectBehavior -{ -} - -class ContainerInitializerMaintainerSpec extends ObjectBehavior -{ - function let(ExampleNode $example, SpecificationNode $specification, \ReflectionClass $classReflection) - { - $example->getSpecification()->willReturn($specification); - $specification->getClassReflection()->willReturn($classReflection); - } - - function it_is_a_maintainer() - { - $this->shouldHaveType('PhpSpec\Runner\Maintainer\MaintainerInterface'); - } - - function it_is_a_container_maintainer() - { - $this->shouldHaveType('PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerMaintainer'); - } - - function it_has_an_increased_priority() - { - $this->getPriority()->shouldReturn(15); - } - - function it_supports_controller_behavior(ExampleNode $example, \ReflectionClass $classReflection) - { - $classReflection->getName()->willReturn('spec\PhpSpec\Symfony2Extension\Runner\Maintainer\UserControllerSpec'); - - $this->supports($example)->shouldReturn(true); - } - - function it_does_not_support_other_behaviors(ExampleNode $example, \ReflectionClass $classReflection) - { - $classReflection->getName()->willReturn('spec\PhpSpec\Symfony2Extension\Runner\Maintainer\UserSpec'); - - $this->supports($example)->shouldReturn(false); - } - - function it_creates_the_container(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators, \ReflectionClass $classReflection, \ReflectionProperty $property) - { - $classReflection->getProperty('container')->willReturn($property); - - $property->setAccessible(true)->shouldBeCalled(); - $property->setValue($context, Argument::type('PhpSpec\\Symfony2Extension\\Specification\\Container'))->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 deleted file mode 100644 index 5c1f78f..0000000 --- a/spec/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInjectorMaintainerSpec.php +++ /dev/null @@ -1,44 +0,0 @@ -<?php - -namespace spec\PhpSpec\Symfony2Extension\Runner\Maintainer; - -use PhpSpec\Loader\Node\ExampleNode; -use PhpSpec\Loader\Node\SpecificationNode; -use PhpSpec\ObjectBehavior; -use PhpSpec\Runner\CollaboratorManager; -use PhpSpec\Runner\MatcherManager; -use PhpSpec\Symfony2Extension\Specification\Container; -use PhpSpec\Symfony2Extension\Specification\ControllerBehavior; -use Prophecy\Argument; - -class ContainerInjectorMaintainerSpec extends ObjectBehavior -{ - function let(ExampleNode $example, SpecificationNode $specification, \ReflectionClass $classReflection) - { - $example->getSpecification()->willReturn($specification); - $specification->getClassReflection()->willReturn($classReflection); - } - - 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) - { - $classReflection->getProperty('container')->willReturn($property); - - $property->setAccessible(true)->shouldBeCalled(); - $property->getValue($context->getWrappedObject())->shouldBeCalled()->willReturn($container); - - // PhpSpec cannot handle this properly. To verify as much as we can, the line above has a shouldBeCalled() call. - // $context->setContainer($container)->shouldBeCalled(); - - $this->prepare($example, $context, $matchers, $collaborators); - } - - function it_has_a_decreased_priority() - { - $this->getPriority()->shouldReturn(5); - } -} 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 @@ -<?php - -namespace spec\PhpSpec\Symfony2Extension\Specification; - -use PhpSpec\ObjectBehavior; -use PhpSpec\Wrapper\WrapperInterface; -use Prophecy\Argument; - -/** - * We cannot use the WrapperInterface directly since getWrappedObject() cannot be stubbed. - */ -class Collaborator implements WrapperInterface -{ - private $wrappedObject = null; - - public function __construct($wrappedObject) - { - $this->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/spec/PhpSpec/Symfony2Extension/Specification/ControllerBehaviorSpec.php b/spec/PhpSpec/Symfony2Extension/Specification/ControllerBehaviorSpec.php deleted file mode 100644 index 0acb805..0000000 --- a/spec/PhpSpec/Symfony2Extension/Specification/ControllerBehaviorSpec.php +++ /dev/null @@ -1,42 +0,0 @@ -<?php - -namespace spec\PhpSpec\Symfony2Extension\Specification; - -use PhpSpec\ObjectBehavior; -use PhpSpec\Symfony2Extension\Specification\ControllerBehavior; -use PhpSpec\Wrapper\Subject; - -class AccessibleControllerBehavior extends ControllerBehavior -{ - public function getContainer() - { - return $this->container; - } -} - -class ContainerAwareSubject extends Subject -{ - public function setContainer($container) - { - } -} - -class ControllerBehaviorSpec extends ObjectBehavior -{ - function it_is_an_object_behavior() - { - $this->shouldHaveType('PhpSpec\ObjectBehavior'); - } - - function it_provides_an_internal_reference_to_the_container(ContainerAwareSubject $subject, \stdClass $container) - { - $this->beAnInstanceOf('spec\\PhpSpec\\Symfony2Extension\\Specification\\AccessibleControllerBehavior'); - $this->getWrappedObject()->setSpecificationSubject($subject->getWrappedObject()); - - $subject->setContainer($container)->shouldBeCalled(); - - $this->setContainer($container); - - $this->getContainer()->shouldBe($container); - } -} diff --git a/spec/PhpSpec/Symfony2Extension/TestControllerSpec.php b/spec/PhpSpec/Symfony2Extension/TestControllerSpec.php new file mode 100644 index 0000000..9606006 --- /dev/null +++ b/spec/PhpSpec/Symfony2Extension/TestControllerSpec.php @@ -0,0 +1,41 @@ +<?php + +namespace spec\PhpSpec\Symfony2Extension; + +use PhpSpec\ObjectBehavior; +use Prophecy\Argument; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; + +class TestControllerSpec extends ObjectBehavior +{ + public function let($container) + { + $this->setContainer($container); + } + + public function it_generates_url() + { + $this->generateUrl('homepage')->shouldReturn('homepage'); + } + + function it_renders_template_in_response() + { + $this->render('test')->shouldBeLike(new Response('test')); + } + + function it_renders_view() + { + $this->renderView('test')->shouldReturn('test'); + } + + public function it_flushes() + { + $this->flush(); + } + + public function it_finds() + { + $this->find()->shouldReturn(array()); + } +} diff --git a/src/PhpSpec/Symfony2Extension/CodeGenerator/ControllerSpecificationGenerator.php b/src/PhpSpec/Symfony2Extension/CodeGenerator/ControllerSpecificationGenerator.php index 1e06c1f..d3d9601 100644 --- a/src/PhpSpec/Symfony2Extension/CodeGenerator/ControllerSpecificationGenerator.php +++ b/src/PhpSpec/Symfony2Extension/CodeGenerator/ControllerSpecificationGenerator.php @@ -40,10 +40,10 @@ protected function getTemplate() namespace %namespace%; -use PhpSpec\Symfony2Extension\Specification\ControllerBehavior; +use PhpSpec\ObjectBehavior; use Prophecy\Argument; -class %name% extends ControllerBehavior +class %name% extends ObjectBehavior { function it_is_container_aware() { diff --git a/src/PhpSpec/Symfony2Extension/Extension.php b/src/PhpSpec/Symfony2Extension/Extension.php index d5159bc..57e95c4 100644 --- a/src/PhpSpec/Symfony2Extension/Extension.php +++ b/src/PhpSpec/Symfony2Extension/Extension.php @@ -11,17 +11,21 @@ use PhpSpec\Symfony2Extension\CodeGenerator\ControllerClassGenerator; use PhpSpec\Symfony2Extension\CodeGenerator\ControllerSpecificationGenerator; use PhpSpec\Symfony2Extension\Locator\PSR0Locator; -use PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerInitializerMaintainer; -use PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerInjectorMaintainer; -use PhpSpec\Symfony2Extension\Specification\Container; +use PhpSpec\Symfony2Extension\Runner\Maintainer\CommonCollaboratorsMaintainer; +use PhpSpec\Symfony2Extension\Runner\Collaborator\DefaultFactory; +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerFactory; +use PhpSpec\Symfony2Extension\Runner\Collaborator\Initializer; class Extension implements ExtensionInterface { /** * @param ServiceContainer $container */ - public function load(ServiceContainer $container) + public function load(ServiceContainer $container) //, array $params = array()) { + //foreach ($params as $key => $value) { + // $container->setParam('symfony2_extension.'.$key, $value); + //} $this->registerConfigurators($container); $this->registerRunnerMaintainers($container); $this->registerCodeGenerators($container); @@ -33,18 +37,55 @@ public function load(ServiceContainer $container) private function registerRunnerMaintainers(ServiceContainer $container) { $container->setShared( - 'runner.maintainers.container_initializer', + 'runner.maintainers.common_collaborators', function ($c) { - return new ContainerInitializerMaintainer(); + return new CommonCollaboratorsMaintainer( + $c->get('unwrapper'), + $c->get('collaborator_factory'), + $c->getParam('symfony2_extension.common-collaborators', array()) + ); } ); - $container->setShared( - 'runner.maintainers.container_injector', - function ($c) { - return new ContainerInjectorMaintainer(); - } - ); + $container->setShared('collaborator_factory.default', function ($c) { + return new DefaultFactory( + $c->get('unwrapper') + ); + }); + + $container->setShared('collaborator_factory', function ($c) { + return new InitializerFactory( + $c->get('collaborator_factory.default'), + $c->getByPrefix('collaborator.initializer'), + $c->getParam('symfony2_extension.common-collaborators', array()) + ); + }); + + $container->setShared('collaborator.initializer.container', function ($c) { + return new Initializer\Container( + $c->getParam('symfony2_extension.common-collaborators', array()) + ); + }); // first! + + $container->setShared('collaborator.initializer.request', function ($c) { + return new Initializer\Request; + }); + + $container->setShared('collaborator.initializer.session', function ($c) { + return new Initializer\Session; + }); + + $container->setShared('collaborator.initializer.router', function ($c) { + return new Initializer\Router; + }); + + $container->setShared('collaborator.initializer.templating', function ($c) { + return new Initializer\Templating; + }); + + $container->setShared('collaborator.initializer.doctrine', function ($c) { + return new Initializer\Doctrine; + }); } /** diff --git a/src/PhpSpec/Symfony2Extension/Runner/Collaborator/DefaultFactory.php b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/DefaultFactory.php new file mode 100644 index 0000000..78b1d37 --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/DefaultFactory.php @@ -0,0 +1,28 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Collaborator; + +use PhpSpec\Wrapper\Unwrapper; +use PhpSpec\Wrapper\Collaborator; +use Prophecy\Prophecy\ObjectProphecy; +use PhpSpec\Runner\CollaboratorManager; + +class DefaultFactory implements FactoryInterface +{ + private $unwrapper; + + public function __construct(Unwrapper $unwrapper) + { + $this->unwrapper = $unwrapper; + } + + public function create(CollaboratorManager $collaborators, ObjectProphecy $prophecy, $name, $className = null) + { + $collaborator = new Collaborator($prophecy, $this->unwrapper); + if (null !== $className) { + $collaborator->beADoubleOf($className); + } + + return $collaborator; + } +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Collaborator/FactoryInterface.php b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/FactoryInterface.php new file mode 100644 index 0000000..77ae0a4 --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/FactoryInterface.php @@ -0,0 +1,11 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Collaborator; + +use Prophecy\Prophecy\ObjectProphecy; +use PhpSpec\Runner\CollaboratorManager; + +interface FactoryInterface +{ + public function create(CollaboratorManager $collaborators, ObjectProphecy $prophecy, $name, $className = null); +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Container.php b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Container.php new file mode 100644 index 0000000..465ae29 --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Container.php @@ -0,0 +1,50 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Collaborator\Initializer; + +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerInterface; +use PhpSpec\Runner\CollaboratorManager; + +class Container implements InitializerInterface +{ + private $name; + private $commonCollaborators; + + public function __construct(array $commonCollaborators = array(), $name = 'container') + { + $this->commonCollaborators = $commonCollaborators; + $this->name = $name; + } + + public function initialize(CollaboratorManager $collaborators, $name, $className = null) + { + $container = $collaborators->get($name); + if (null === $className) { + $container->beADoubleOf('Symfony\Component\DependencyInjection\ContainerInterface'); + } + } + + public function postInitialize(CollaboratorManager $collaborators) + { + foreach ($this->commonCollaborators as $name => $config) { + list($id, $class) = $this->extractCollaboratorConfig($name, $config); + + $collaborators->get($this->name)->has($id)->willReturn(true); + $collaborators->get($this->name)->get($id)->willReturn($collaborators->get($name)); + } + } + + public function supports($name) + { + return $this->name === $name; + } + + private function extractCollaboratorConfig($name, $config) + { + if (is_array($config)) { + return each($config); + } + + return array($name, $config); + } +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Doctrine.php b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Doctrine.php new file mode 100644 index 0000000..d8a8edc --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Doctrine.php @@ -0,0 +1,77 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Collaborator\Initializer; + +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerInterface; +use PhpSpec\Runner\CollaboratorManager; +use Prophecy\Argument; + +class Doctrine implements InitializerInterface +{ + public function __construct(array $names = array()) + { + $this->names = $names ?: array( + 'doctrine', 'em', 'om', 'dm', 'repository', + ); + } + + public function initialize(CollaboratorManager $collaborators, $name, $className = null) + { + switch ($name) { + case 'doctrine': + return $this->initDoctrine($collaborators, $className); + case 'em': + case 'om': + case 'dm': + return $this->initManager($collaborators, $className); + case 'repository': + return $this->initRepository($collaborators, $className); + default: + return; + } + } + + public function postInitialize(CollaboratorManager $collaborators) + { + if ($collaborators->has('em')) { + $collaborators->get('doctrine')->getManager()->willReturn($collaborators->get('em')); + } + if ($collaborators->has('repository')) { + $collaborators->get('doctrine') + ->getRepository(Argument::type('string')) + ->willReturn($collaborators->get('repository')) + ; + } + } + + public function supports($name) + { + return in_array($name, $this->names); + } + + private function initDoctrine(CollaboratorManager $collaborators, $className) + { + $doctrine = $collaborators->get('doctrine'); + if (null === $className) { + $doctrine->beADoubleOf('Doctrine\Common\Persistence\ManagerRegistry'); + } + } + + private function initManager(CollaboratorManager $collaborators, $className) + { + $em = $collaborators->get('em'); + if (null === $className) { + $em->beADoubleOf('Doctrine\Common\Persistence\ObjectManager'); + } + } + + private function initRepository(CollaboratorManager $collaborators, $className) + { + $repository = $collaborators->get('repository'); + if (null === $className) { + $repository->beADoubleOf('Doctrine\Common\Persistence\ObjectRepository'); + } + + $repository->findAll()->willReturn(array()); + } +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Request.php b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Request.php new file mode 100644 index 0000000..dee5cb7 --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Request.php @@ -0,0 +1,36 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Collaborator\Initializer; + +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerInterface; +use PhpSpec\Runner\CollaboratorManager; +use Symfony\Component\HttpFoundation\ParameterBag; + +class Request implements InitializerInterface +{ + private $name; + + public function __construct($name = 'request') + { + $this->name = $name; + } + + public function initialize(CollaboratorManager $collaborators, $name, $className = null) + { + $request = $collaborators->get($name); + if (null === $className) { + $request->beADoubleOf('Symfony\Component\HttpFoundation\Request'); + } + $request->attributes = new ParameterBag; + } + + public function postInitialize(CollaboratorManager $collaborators) + { + + } + + public function supports($name) + { + return $this->name === $name; + } +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Router.php b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Router.php new file mode 100644 index 0000000..144f7f6 --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Router.php @@ -0,0 +1,36 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Collaborator\Initializer; + +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerInterface; +use PhpSpec\Runner\CollaboratorManager; +use Prophecy\Argument; + +class Router implements InitializerInterface +{ + private $name; + + public function __construct($name = 'router') + { + $this->name = $name; + } + + public function initialize(CollaboratorManager $collaborators, $name, $className = null) + { + $router = $collaborators->get($name); + if (null === $className) { + $router->beADoubleOf('Symfony\Component\Routing\RouterInterface'); + } + $router->generate(Argument::cetera())->willReturnArgument(); + } + + public function postInitialize(CollaboratorManager $collaborators) + { + + } + + public function supports($name) + { + return $this->name === $name; + } +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Session.php b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Session.php new file mode 100644 index 0000000..2db0423 --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Session.php @@ -0,0 +1,39 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Collaborator\Initializer; + +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerInterface; +use PhpSpec\Runner\CollaboratorManager; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; + +class Session implements InitializerInterface +{ + private $name; + + public function __construct($name = 'session') + { + $this->name = $name; + } + + public function initialize(CollaboratorManager $collaborators, $name, $className = null) + { + $session = $collaborators->get($name); + if (null === $className) { + $session->beADoubleOf('Symfony\Component\HttpFoundation\Session\Session'); + } + + $session->getFlashBag()->willReturn(new FlashBag); + } + + public function postInitialize(CollaboratorManager $collaborators) + { + if ($collaborators->has('request')) { + $collaborators->get('request')->getSession()->willReturn($collaborators->get($this->name)); + } + } + + public function supports($name) + { + return $this->name === $name; + } +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Templating.php b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Templating.php new file mode 100644 index 0000000..d877a74 --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/Initializer/Templating.php @@ -0,0 +1,41 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Collaborator\Initializer; + +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerInterface; +use PhpSpec\Runner\CollaboratorManager; +use Symfony\Component\HttpFoundation\ParameterBag; +use Symfony\Component\HttpFoundation\Response; +use Prophecy\Argument; + +class Templating implements InitializerInterface +{ + private $name; + + public function __construct($name = 'templating') + { + $this->name = $name; + } + + public function initialize(CollaboratorManager $collaborators, $name, $className = null) + { + $templating = $collaborators->get($name); + if (null === $className) { + $templating->beADoubleOf('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface'); + } + $templating->render(Argument::cetera())->willReturnArgument(); + $templating->renderResponse(Argument::cetera())->will(function($arguments) { + return new Response($arguments[0]); + }); + } + + public function postInitialize(CollaboratorManager $collaborators) + { + + } + + public function supports($name) + { + return $this->name === $name; + } +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Collaborator/InitializerFactory.php b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/InitializerFactory.php new file mode 100644 index 0000000..4329bda --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/InitializerFactory.php @@ -0,0 +1,54 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Collaborator; + +use Prophecy\Prophecy\ObjectProphecy; +use PhpSpec\Runner\CollaboratorManager; + +class InitializerFactory implements FactoryInterface +{ + private $factory; + private $initializers; + private $commonCollaborators; + + public function __construct(FactoryInterface $factory, array $initializers = array(), array $commonCollaborators = array()) + { + $this->factory = $factory; + $this->initializers = $initializers; + $this->commonCollaborators = $commonCollaborators; + } + + public function create(CollaboratorManager $collaborators, ObjectProphecy $prophecy, $name, $className = null) + { + if (!$collaborators->has($name)) { + $collaborator = $this->factory->create($collaborators, $prophecy, $name, $className); + $collaborators->set($name, $collaborator); + } + else { + $collaborator = $collaborators->get($name); + } + if ($initializer = $this->getInitializer($name)) { + $initializer->initialize($collaborators, $name, $className); + } + + return $collaborator; + } + + public function postInitialize(CollaboratorManager $collaborators) + { + foreach ($this->commonCollaborators as $name => $config) { + if ($initializer = $this->getInitializer($name)) { + $initializer->postInitialize($collaborators); + } + } + } + + private function getInitializer($name) + { + foreach ($this->initializers as $initializer) { + if ($initializer->supports($name)) { + return $initializer; + } + } + } +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Collaborator/InitializerInterface.php b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/InitializerInterface.php new file mode 100644 index 0000000..43259bd --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Collaborator/InitializerInterface.php @@ -0,0 +1,25 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Collaborator; + +use PhpSpec\Runner\CollaboratorManager; + +interface InitializerInterface +{ + /** + * add common default behavior to a collaborator + **/ + public function initialize(CollaboratorManager $collaborators, $name, $className = null); + + /** + * add more behaviors with knowledge of other collaborators + **/ + public function postInitialize(CollaboratorManager $collaborators); + + /** + * return bool if supports the collaborator name + * + * Typically, the argument name of a spec method (ex: for $request, it would be 'request' + **/ + public function supports($name); +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Maintainer/CommonCollaboratorsMaintainer.php b/src/PhpSpec/Symfony2Extension/Runner/Maintainer/CommonCollaboratorsMaintainer.php new file mode 100644 index 0000000..298a944 --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/Runner/Maintainer/CommonCollaboratorsMaintainer.php @@ -0,0 +1,77 @@ +<?php + +namespace PhpSpec\Symfony2Extension\Runner\Maintainer; + +use PhpSpec\Runner\Maintainer\MaintainerInterface; +use PhpSpec\Loader\Node\ExampleNode; +use PhpSpec\SpecificationInterface; +use PhpSpec\Runner\CollaboratorManager; +use PhpSpec\Runner\MatcherManager; +use Prophecy\Prophet; +use PhpSpec\Wrapper\Unwrapper; +use PhpSpec\Symfony2Extension\Runner\Collaborator\InitializerFactory; + +class CommonCollaboratorsMaintainer implements MaintainerInterface +{ + private $unwrapper; + private $factory; + private $commonCollaborators; + private $prophet; + + public function __construct(Unwrapper $unwrapper, InitializerFactory $factory, array $commonCollaborators = array()) + { + $this->unwrapper = $unwrapper; // @TODO avoid indirect deps + $this->factory = $factory; + + $this->commonCollaborators = $commonCollaborators; + } + + public function supports(ExampleNode $example) + { + $class = $example->getSpecification()->getResource()->getSrcClassname(); + try { + $srcRefl = new \ReflectionClass($class); + + return $srcRefl->implementsInterface('Symfony\Component\DependencyInjection\ContainerAwareInterface'); + } + catch(\Exception $e) { + return false; + } + } + + public function prepare(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators) + { + $this->prophet = new Prophet(null, $this->unwrapper, null); + + foreach ($this->commonCollaborators as $name => $service) { + list($id, $class) = $this->extractCollaboratorConfig($name, $service); + + $collaborator = $this->factory->create( + $collaborators, + $this->prophet->prophesize(), + $name, + $class + ); + } + $this->factory->postInitialize($collaborators); + } + + public function teardown(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators) + { + $this->prophet->checkPredictions(); + } + + public function getPriority() + { + return 60; // more than CollaboratorsMaintainer :/ + } + + private function extractCollaboratorConfig($name, $config) + { + if (is_array($config)) { + return each($config); + } + + return array($name, $config); + } +} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainer.php b/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainer.php deleted file mode 100644 index 2c276dc..0000000 --- a/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInitializerMaintainer.php +++ /dev/null @@ -1,34 +0,0 @@ -<?php - -namespace PhpSpec\Symfony2Extension\Runner\Maintainer; - -use PhpSpec\Loader\Node\ExampleNode; -use PhpSpec\Runner\CollaboratorManager; -use PhpSpec\Runner\MatcherManager; -use PhpSpec\SpecificationInterface; -use PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerMaintainer; -use PhpSpec\Symfony2Extension\Specification\Container; - -class ContainerInitializerMaintainer extends ContainerMaintainer -{ - /** - * @param ExampleNode $example - * @param SpecificationInterface $context - * @param MatcherManager $matchers - * @param CollaboratorManager $collaborators - */ - public function prepare(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators) - { - $containerProperty = $example->getSpecification()->getClassReflection()->getProperty('container'); - $containerProperty->setAccessible(true); - $containerProperty->setValue($context, new Container()); - } - - /** - * @return integer - */ - public function getPriority() - { - return 15; - } -} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInjectorMaintainer.php b/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInjectorMaintainer.php deleted file mode 100644 index 98aa403..0000000 --- a/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerInjectorMaintainer.php +++ /dev/null @@ -1,32 +0,0 @@ -<?php - -namespace PhpSpec\Symfony2Extension\Runner\Maintainer; - -use PhpSpec\Loader\Node\ExampleNode; -use PhpSpec\Runner\CollaboratorManager; -use PhpSpec\Runner\MatcherManager; -use PhpSpec\SpecificationInterface; - -class ContainerInjectorMaintainer extends ContainerMaintainer -{ - /** - * @param ExampleNode $example - * @param SpecificationInterface $context - * @param MatcherManager $matchers - * @param CollaboratorManager $collaborators - */ - public function prepare(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators) - { - $containerProperty = $example->getSpecification()->getClassReflection()->getProperty('container'); - $containerProperty->setAccessible(true); - $context->setContainer($containerProperty->getValue($context)); - } - - /** - * @return integer - */ - public function getPriority() - { - return 5; - } -} diff --git a/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerMaintainer.php b/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerMaintainer.php deleted file mode 100644 index 2056a74..0000000 --- a/src/PhpSpec/Symfony2Extension/Runner/Maintainer/ContainerMaintainer.php +++ /dev/null @@ -1,35 +0,0 @@ -<?php - - -namespace PhpSpec\Symfony2Extension\Runner\Maintainer; - -use PhpSpec\Loader\Node\ExampleNode; -use PhpSpec\Runner\CollaboratorManager; -use PhpSpec\Runner\Maintainer\MaintainerInterface; -use PhpSpec\Runner\MatcherManager; -use PhpSpec\SpecificationInterface; - -abstract class ContainerMaintainer implements MaintainerInterface -{ - /** - * @param ExampleNode $example - * - * @return boolean - */ - public function supports(ExampleNode $example) - { - $specClassName = $example->getSpecification()->getClassReflection()->getName(); - - return in_array('PhpSpec\\Symfony2Extension\\Specification\\ControllerBehavior', class_parents($specClassName)); - } - - /** - * @param ExampleNode $example - * @param SpecificationInterface $context - * @param MatcherManager $matchers - * @param CollaboratorManager $collaborators - */ - public function teardown(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators) - { - } -} \ No newline at end of file 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 @@ -<?php - -namespace PhpSpec\Symfony2Extension\Specification; - -use PhpSpec\Wrapper\WrapperInterface; -use Symfony\Component\DependencyInjection\Container as BaseContainer; - -class Container extends BaseContainer -{ - /** - * @param string $id - * @param integer $invalidBehavior - * - * @return mixed - */ - public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) - { - $service = parent::get($id, $invalidBehavior); - - if ($service instanceof WrapperInterface) { - return $service->getWrappedObject(); - } - - return $service; - } -} diff --git a/src/PhpSpec/Symfony2Extension/Specification/ControllerBehavior.php b/src/PhpSpec/Symfony2Extension/Specification/ControllerBehavior.php deleted file mode 100644 index dd7a34f..0000000 --- a/src/PhpSpec/Symfony2Extension/Specification/ControllerBehavior.php +++ /dev/null @@ -1,25 +0,0 @@ -<?php - -namespace PhpSpec\Symfony2Extension\Specification; - -use PhpSpec\ObjectBehavior; -use PhpSpec\Symfony2Extension\Specification\Container; -use PhpSpec\Wrapper\Subject; - -class ControllerBehavior extends ObjectBehavior -{ - /** - * @var Container|null - */ - protected $container; - - /** - * @param Container $container - */ - public function setContainer(Container $container) - { - $this->container = $container; - - $this->object->setContainer($container); - } -} diff --git a/src/PhpSpec/Symfony2Extension/TestController.php b/src/PhpSpec/Symfony2Extension/TestController.php new file mode 100644 index 0000000..e4855cb --- /dev/null +++ b/src/PhpSpec/Symfony2Extension/TestController.php @@ -0,0 +1,18 @@ +<?php + +namespace PhpSpec\Symfony2Extension; + +use Symfony\Bundle\FrameworkBundle\Controller\Controller; + +class TestController extends Controller +{ + public function flush() + { + $this->get('doctrine')->getManager()->flush(); + } + + public function find() + { + return $this->get('doctrine')->getRepository('Stuff')->findAll(); + } +}