Skip to content
This repository was 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 5 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
29 changes: 16 additions & 13 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,13 +52,14 @@ 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()
function it_should_respond_to_the_list_action_call($container)
{
$this->setContainer($container);
$response = $this->listAction();
$response->shouldHaveType('Symfony\Component\HttpFoundation\Response');
$response->getStatusCode()->shouldBe(200);
Expand Down Expand Up @@ -94,15 +95,16 @@ 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;

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, $container)
{
$this->container->set('router', $router);
$this->setContainer($container);
$container->set('router', $router);

$router->generate('homepage')->willReturn('/');

Expand Down Expand Up @@ -144,15 +146,16 @@ 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;

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, $container)
{
$this->container->set('templating', $templating);
$this->setContainer($container);
$container->set('templating', $templating);

$this->shouldRender('Scenario7UserBundle:User:list.html.twig', array('users' => array()))
->duringAction('list');
Expand Down
47 changes: 47 additions & 0 deletions features/provide_default_collaborators.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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:
router: Symfony\Component\Routing\RouterInterface
"""

Scenario: Controller has access to common collaborator
Given I wrote a spec in the "src/CommonCollaborator/Spec/Controller.php":
"""
<?php

namespace CommonCollaborator\Spec;

use PhpSpec\ObjectBehavior;

class Controller extends ObjectBehavior
{
function it_has_access_to_router($container, $router) // magic!
{
$this->setContainer($container);
$this->generateUrl('homepage')->shouldHaveType('string');
}
}

"""
And I wrote a class in the "src/CommonCollaborator/Controller.php":
"""
<?php

namespace CommonCollaborator;

use Symfony\Bundle\FrameworkBundle\Controller\Controller as BaseController;

class Controller extends BaseController
{
}

"""
When I run phpspec
Then I should see "1 example (1 passed)"
7 changes: 7 additions & 0 deletions phpspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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:

Choose a reason for hiding this comment

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

is this using the new options parameters PR from phpspec/phpspec?

Copy link
Author

Choose a reason for hiding this comment

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

I reverted it until (if) the PR is (not) accepted.

Copy link
Author

Choose a reason for hiding this comment

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

it was breaking travis, cause composer was not using the PR'd version.

common-collaborators:
# append: false # should we support append ? (ie: provide defaults)
router: Symfony\Component\Routing\RouterInterface
request: Symfony\Component\HttpFoundation\Request
#formFactory: { form.factory: FormFactoryInterface }
16 changes: 11 additions & 5 deletions spec/PhpSpec/Symfony2Extension/ExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PhpSpec\ObjectBehavior;
use PhpSpec\ServiceContainer;
use Prophecy\Argument;
use PhpSpec\Wrapper\Unwrapper;
use PhpSpec\Symfony2Extension\Runner\CollaboratorFactory;

class ExtensionSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -45,18 +47,22 @@ 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, CollaboratorFactory $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',
$this->service('PhpSpec\Symfony2Extension\Runner\CollaboratorFactory', $container)
)->shouldBeCalled();

$container->getParam('symfony2_extension.common-collaborators', array())->willReturn(array());
$container->get('collaborator_factory')->willReturn($factory);
$container->get('unwrapper')->willReturn($unwrapper);

$this->load($container);
}

Expand Down
20 changes: 20 additions & 0 deletions spec/PhpSpec/Symfony2Extension/Runner/CollaboratorFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace spec\PhpSpec\Symfony2Extension\Runner;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use PhpSpec\Wrapper\Unwrapper;

class CollaboratorFactorySpec extends ObjectBehavior
{
function let(Unwrapper $unwrapper)
{
$this->beConstructedWith($unwrapper);
}

function it_is_initializable()
{
$this->shouldHaveType('PhpSpec\Symfony2Extension\Runner\CollaboratorFactory');
}
}
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\CollaboratorFactory;

class CommonCollaboratorsMaintainerSpec extends ObjectBehavior
{
public function let(Unwrapper $unwrapper, CollaboratorFactory $factory)
{
$this->beConstructedWith($unwrapper, $factory, array());
}

function it_is_initializable()
{
$this->shouldHaveType('PhpSpec\Symfony2Extension\Runner\Maintainer\CommonCollaboratorsMaintainer');
}
}

This file was deleted.

This file was deleted.

Loading