Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Start describing controller

copy gist to spec/ControllerSpecification.php
$> bin/phpspec describe Bossa/Bundle/TrainingBundle/Controller/CoursesController
$> cat spec/Bossa/Bundle/TrainingBundle/Controller/CoursesController.php
$> bin/phpspec run
$> vim spec/Bossa/Bundle/TrainingBundle/Controller/CoursesController.php
$> bin/phpspec run
$> vim src/Bossa/Bundle/TrainingBundle/Controller/CoursesController.php

We have a failing scenario, and we feel we need to add some code. It's time
to introduce our first spec. We install phpspec and tell it we want to
describe our controller. PHPSpec will create the controller specification
and it will generates the required code for us. We will then just add the
relevant behaviour we are expecting and make it green.'
  • Loading branch information...
commit 8e5f73d93d7e85c1c05ac564d6d032558c905cd1 1 parent 9558d96
@everzet authored
View
2  composer.json
@@ -2,7 +2,7 @@
"name": "symfony/framework-standard-edition",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
- "psr-0": { "": "src/" }
+ "psr-0": { "": "src/", "spec": "." }
},
"require": {
"php": ">=5.3.3",
View
14 spec/Bossa/Bundle/TrainingBundle/Controller/CoursesController.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace spec\Bossa\Bundle\TrainingBundle\Controller;
+
+use PHPSpec2\Specification;
+
+class CoursesController extends \spec\ControllerSpecification
+{
+ function it_should_respond_to_list_action_call()
+ {
+ $response = $this->coursesController->listAction();
+ $response->getStatusCode()->shouldReturn(200);
+ }
+}
View
101 spec/ControllerSpecification.php
@@ -0,0 +1,101 @@
+<?php
+
+namespace spec;
+
+use Symfony\Component\HttpFoundation\Response;
+
+use PHPSpec2\Specification;
+use PHPSpec2\Mocker\Mockery\ExpectationProxy;
+use PHPSpec2\Matcher\MatcherInterface;
+use Symfony\Component\DependencyInjection\ContainerAwareInterface;
+
+abstract class ControllerSpecification implements Specification
+{
+ /**
+ * @param Prophet $container mock of Symfony\Component\DependencyInjection\ContainerInterface
+ * @param Prophet $doctrine mock of Doctrine\Bundle\DoctrineBundle\Registry
+ * @param Prophet $manager mock of Doctrine\Common\Persistence\ObjectManager
+ * @param Prophet $templating mock of Symfony\Component\Templating\EngineInterface
+ * @param Prophet $repository mock of Doctrine\ORM\EntityRepository
+ */
+ function described_with($container, $doctrine, $manager, $templating, $repository)
+ {
+ if (!$this->object->getProphetSubject() instanceof ContainerAwareInterface) {
+ return;
+ }
+
+ $container->get('doctrine')->willReturn($doctrine);
+ $container->get('templating')->willReturn($templating);
+
+ $doctrine->getManager()->willReturn($manager);
+ $manager->getRepository()->willReturn($repository);
+ $repository->findAll()->willReturn(array());
+
+ $expectation = $templating->renderResponse()->willReturn(new Response());
+
+ $this->object->setContainer($container);
+ $this->object->getProphetMatchers()->add(new TemplateRenderMatcher($expectation));
+
+ $this->controller = $this->object;
+ }
+}
+
+class TemplateRenderMatcher implements MatcherInterface
+{
+ private $expectation;
+ private $controller;
+
+ public function __construct(ExpectationProxy $expectation)
+ {
+ $this->expectation = $expectation;
+ }
+
+ public function supports($name, $subject, array $arguments)
+ {
+ return 'render' === $name;
+ }
+
+ public function positiveMatch($name, $subject, array $arguments)
+ {
+ if (2 == count($arguments)) {
+ $arguments[] = null;
+ } elseif (1 == count($arguments)) {
+ $arguments[] = array();
+ $arguments[] = null;
+ }
+
+ $this->controller = $subject;
+ $this->expectation->withArguments($arguments);
+ $this->expectation->shouldBeCalled();
+
+ return $this;
+ }
+
+ public function negativeMatch($name, $subject, array $arguments)
+ {
+ if (2 == count($arguments)) {
+ $arguments[] = null;
+ } elseif (1 == count($arguments)) {
+ $arguments[] = array();
+ $arguments[] = null;
+ }
+
+ $this->controller = $subject;
+ $this->expectation->withArguments($arguments);
+ $this->expectation->shouldNotBeCalled();
+
+ return $this;
+ }
+
+ public function duringAction($name, array $arguments = array())
+ {
+ call_user_func_array(
+ array($this->controller, $name.'Action'), $arugments
+ );
+ }
+
+ public function getPriority()
+ {
+ return 0;
+ }
+}
View
13 src/Bossa/Bundle/TrainingBundle/Controller/CoursesController.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Bossa\Bundle\TrainingBundle\Controller;
+
+use Symfony\Component\HttpFoundation\Response;
+
+class CoursesController
+{
+ public function listAction()
+ {
+ return new Response();
+ }
+}

3 comments on commit 8e5f73d

@jrosell

How did you grenerate this spec files? What commands do you use? Where I can find gist file?

@everzet
Owner

@jrosell those are written, not generated. You can read about PhpSpec here: http://www.phpspec.net

@jrosell

Ok. I think I have some versions problems. I useing phpspec 2.0 instead of phpspec2

Please sign in to comment.
Something went wrong with that request. Please try again.