Skip to content
This repository was archived by the owner on Feb 14, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions phpspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
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
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\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');
}
}
15 changes: 15 additions & 0 deletions spec/PhpSpec/Symfony2Extension/TestControllerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace spec\PhpSpec\Symfony2Extension;

use PhpSpec\Symfony2Extension\Specification\ControllerBehavior;
use Prophecy\Argument;

class TestControllerSpec extends ControllerBehavior
{
public function it_generates_url($container, $router)
{
$this->setContainer($container);
$this->generateUrl('homepage')->shouldReturn('test');
Copy link
Author

Choose a reason for hiding this comment

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

the actual example spec

Copy link
Author

Choose a reason for hiding this comment

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

to be removed too.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace spec\Symfony2Extension\Spec\Runner\Collaborator;

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('Php\Symfony2Extension\Runner\Collaborator\CollaboratorFactory');
}
}
22 changes: 22 additions & 0 deletions src/PhpSpec/Symfony2Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerInitializerMaintainer;
use PhpSpec\Symfony2Extension\Runner\Maintainer\ContainerInjectorMaintainer;
use PhpSpec\Symfony2Extension\Specification\Container;
use PhpSpec\Symfony2Extension\Runner\Collaborator\CollaboratorFactory;
use PhpSpec\Symfony2Extension\Runner\Maintainer\CommonCollaboratorsMaintainer;

class Extension implements ExtensionInterface
{
Expand Down Expand Up @@ -45,6 +47,26 @@ function ($c) {
return new ContainerInjectorMaintainer();
}
);

$container->setShared(
'runner.maintainers.common_collaborators',
function ($c) {
return new CommonCollaboratorsMaintainer(
$c->get('unwrapper'),
$c->get('collaborator_factory'),
array()
);
}
);

$container->setShared(
'collaborator_factory',
function ($c) {
return new CollaboratorFactory(
$c->get('unwrapper')
);
}
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace PhpSpec\Symfony2Extension\Runner\Collaborator;

use PhpSpec\Wrapper\Unwrapper;
use PhpSpec\Wrapper\Collaborator;
use Prophecy\Prophecy\ObjectProphecy;

class CollaboratorFactory
{
private $unwrapper;

public function __construct(Unwrapper $unwrapper)
{
$this->unwrapper = $unwrapper;
}

public function create(ObjectProphecy $prophecy, $className = null, array $arguments = array())
{
$collaborator = new Collaborator($prophecy, $this->unwrapper);
if (null !== $className) {
$collaborator->beADoubleOf($className);
}
if (!empty($arguments)) {
$collaborator->beConstructedWith($arguments);
}

return $collaborator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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 PhpSpec\Symfony2Extension\Runner\Collaborator\CollaboratorFactory;
use Prophecy\Prophet;
use PhpSpec\Wrapper\Unwrapper;

class CommonCollaboratorsMaintainer implements MaintainerInterface
{
private $unwrapper;
private $factory;
private $commonCollaborators;
private $commonServices;
private $prophet;

public function __construct(Unwrapper $unwrapper, CollaboratorFactory $factory, array $commonCollaborators = array(), array $commonServices = array())
{
// @TODO avoid indirect deps ?
$this->unwrapper = $unwrapper;
$this->factory = $factory;
$this->commonCollaborators = $commonCollaborators ?: array(
'router' => 'Symfony\Component\Routing\RouterInterface',
'session' => 'Symfony\Component\HttpFoundation\Session\Session',
'request' => 'Symfony\Component\HttpFoundation\Request',
//'securityContext' => 'SecurityContextIterface',
);

$this->commonServices = $commonServices ?: array(
'router' => 'router',
'session' => 'session',
'request' => 'request',
//'securityContext' => 'security.context',
);
}

public function supports(ExampleNode $example)
{
$specClassName = $example->getSpecification()->getClassReflection()->getName();

return in_array('PhpSpec\Symfony2Extension\Specification\ControllerBehavior', class_parents($specClassName));
}

public function prepare(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators)
{
$this->prophet = new Prophet(null, $this->unwrapper, null);

if (!$collaborators->has('container')) {
$container = $this->factory->create(
$this->prophet->prophesize(),
'Symfony\Component\DependencyInjection\ContainerInterface'
);
$collaborators->set('container', $container);
}

foreach ($this->commonCollaborators as $name => $className) {
if (!$collaborators->has($name)) {
$collaborator = $this->factory->create($this->prophet->prophesize(), $className);
$collaborators->set($name, $collaborator);

if ($collaborators->has('container')) {
$collaborators->get('container')->get($this->commonServices[$name])->willReturn($collaborator);
}
}
}
}

public function teardown(ExampleNode $example, SpecificationInterface $context, MatcherManager $matchers, CollaboratorManager $collaborators)
{
$this->prophet->checkPredictions();
}

public function getPriority()
{
return 60; // more than CollaboratorsMaintainer :/
}
}
9 changes: 9 additions & 0 deletions src/PhpSpec/Symfony2Extension/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpSpec\Symfony2Extension;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class TestController extends Controller
{
}