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

[WIP] common collaborators #3

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions features/bootstrap/PhpSpecContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐴

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what with that ? 🐫

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👶 maybe he means the @ handling

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah :) it's acceptable in that case imho

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

si un médecin le dit, nous buvons 👶

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha :) don't trust me.

chdir($this->workDir);
}

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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());

Expand Down
28 changes: 16 additions & 12 deletions features/describing_a_controller.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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('/');

Expand Down Expand Up @@ -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');
Expand Down
55 changes: 55 additions & 0 deletions features/initialize_default_collaborators.feature
Original file line number Diff line number Diff line change
@@ -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)"
51 changes: 51 additions & 0 deletions features/provide_default_collaborators.feature
Original file line number Diff line number Diff line change
@@ -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)"
14 changes: 14 additions & 0 deletions phpspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extensions:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the example. (to be removed)

- 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 }
27 changes: 22 additions & 5 deletions spec/PhpSpec/Symfony2Extension/ExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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');
}
}
Original file line number Diff line number Diff line change
@@ -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');
}
}
Original file line number Diff line number Diff line change
@@ -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');
}
}
Loading