Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
BDD for        with
                                                PHPSpec



                            29th October 20...
whoami


                                   Marcello Duarte

                            Head of Training @ Ibuildings UK
...
In the beginning
                               there was...

                                            TDD
  credits: h...
They saw that it was good



Saturday, 29 October 2011
$thou->shalt->test



   credits: http://www.flickr.com/photos/36829113@N05/3392940179/
Saturday, 29 October 2011
BDD


      credits: http://www.flickr.com/photos/psd/424257767/


Saturday, 29 October 2011
BDD
                            A way of teaching TDD



      credits: http://www.flickr.com/photos/psd/424257767/


Satur...
What to test?




               H
                 Where to begin?




                ow m uch


Saturday, 29 October 20...
BDD


      credits: http://www.flickr.com/photos/psd/424257767/


Saturday, 29 October 2011
BDD
                            Offers a common language


      credits: http://www.flickr.com/photos/psd/424257767/


Sat...
business

             user                      developer




Saturday, 29 October 2011
business

             user                       developer




                            behavior

Saturday, 29 October...
Saturday, 29 October 2011
                            © Manuscripts and Archives, Yale University Library.
               ...
Language to express truth
                            vs...

Saturday, 29 October 2011
Language to discover truth



Saturday, 29 October 2011
Language influence thought



Saturday, 29 October 2011
BDD


      credits: http://www.flickr.com/photos/psd/424257767/


Saturday, 29 October 2011
BDD
                              A way to discover
                            what is useful to deliver


      credits:...
Saturday, 29 October 2011
BDD Outside in
                               Gherkin
                                Behat

                             ...
Gherkin



Saturday, 29 October 2011
Feature: Organizers can open a call for paper
     As an event organizer
     I want a way to publish a centralized cfp fo...
behat.org


Saturday, 29 October 2011
Saturday, 29 October 2011
words matter



Saturday, 29 October 2011
$this->assertTrue(...




Saturday, 29 October 2011
$this->assertTrue(...



                            What am I going to test?




Saturday, 29 October 2011
$report = new Report;
            $this->assertTrue($report instanceof Report);




Saturday, 29 October 2011
/                           /
            $report = new Report;
            $this->assertTrue($report instanceof Report);
...
$employee->should->...




Saturday, 29 October 2011
$employee->should->...



                            What is the expected behavior?




Saturday, 29 October 2011
Use Gherkin and Behat
        for specifying scenarios
                                  ∫
                             Us...
expressiveness


Saturday, 29 October 2011
$this->assertTrue($employee->reportsTo($manager));




  $employee->should->reportTo($manager);




Saturday, 29 October 2...
phpspec.net




Saturday, 29 October 2011
Installing

                  $ sudo pear channel-discover pear.phpspec.net
                  $ sudo pear install --alldep...
“Specification, not
          verification” (Uncle Bob)
                        $this->assertEquals(0, $result);

        ...
class CalculatorTest


                                  becomes
                            class DescribeCalculator




...
function testAddWithNoArguments()


                                     becomes
          function itReturnsZeroWithNoArg...
All together
     class DescribeStringCalculator extends PHPSpecContext
     {

           function itReturnsZeroWithNoArg...
Hooks

                             before()
                              after()
                            beforeAll()...
Setting initial state with before hook
     class DescribeStringCalculator extends PHPSpecContext
     {
       function b...
class DescribeStringCalculator extends PHPSpecContext
     {
       private $calculator;

          function before()
    ...
Formatters
                              progress
                            documentation
                              ...
Progress Formatter
  $ phpspec StringCalculatorSpec.php -c
  .*.F

  Pending:
    String Calculator returns the bare numbe...
HTML Formatter




Saturday, 29 October 2011
Documentation Formatter




Saturday, 29 October 2011
Matchers



Saturday, 29 October 2011
be($match)
                          equal($match)
                        beEqualTo($match)
                      beAnIns...
And more matchers...



Saturday, 29 October 2011
beInteger()
                                beLessThan($match)
                            beLessThanOrEqualTo($match)
   ...
Predicate Matchers



Saturday, 29 October 2011
$cell = $this->spec(new Cell);
                            $cell->should->beAlive();



   class Cell
   {
       protecte...
$newNode = $this->spec(new Node);
                    $newNode->shouldNot->haveChildren();



    class Node
    {
       ...
Custom Matchers



Saturday, 29 October 2011
PHPSpecMatcherdefine('reportTo', function($supervisor) {
            return array (
                'match' => function($s...
PHPSpec &


Saturday, 29 October 2011
Tool
                            framework.zend.com




Saturday, 29 October 2011
Installing
  $     sudo pear channel-discover pear.zfcampus.org
  $     sudo pear install zfcampus/zf
  $     zf create co...
.zf.ini

  php.include_path = ".:/usr/share/pear"

  basicloader.classes.1     =   "Akrabat_Tool_DatabaseSchemaProvider"
 ...
create a project
  $ zf create project callconf
  Creating project at /var/www/callconf
  Note: This command created a web...
initialize PHPSpec
  $ cd callconf
  $ zf generate             phpspec
        create              spec
        create    ...
initialize Behat
  $ zf generate behat
  +d features - place your *.feature files here
  +d features/bootstrap - place boo...
Saturday, 29 October 2011
Views



Saturday, 29 October 2011
why specify the view?




Saturday, 29 October 2011
Controller and model to the point

                            Ensure we are focused on what matters

                    ...
create a view spec




Saturday, 29 October 2011
create a view spec
  $ zf create view-spec add CallForPapers




Saturday, 29 October 2011
create a view spec
  $ zf create view-spec add CallForPapers
  Creating a view script in location /var/www/callconf/
  app...
Saturday, 29 October 2011
Spec created by default
         <?php

         namespace CallForPapers;

         require_once __DIR__ . '/../../SpecHel...
what behaviors can we
                     describe in the view spec?



Saturday, 29 October 2011
Variables we need assigned

                            What content was rendered
                             (We can use...
Assigning Variables


    function itRendersTheTalkAbstract()
    {
        $marcello = $this->mock('Speaker', array('isVe...
Controllers



Saturday, 29 October 2011
create a controller spec




Saturday, 29 October 2011
create a controller spec
  $ zf create controller-spec CallForPapers add,create




Saturday, 29 October 2011
create a controller spec
  $ zf create controller-spec CallForPapers add,create
  Creating a controller at /private/var/ww...
Saturday, 29 October 2011
Spec created by default
    <?php

    require_once __DIR__ . '/../SpecHelper.php';

    class DescribeCallForPapers exten...
what behaviors do we want to
        describe in the controller spec?



Saturday, 29 October 2011
How do we want to route to its actions

                            What view variables we need assigned

                ...
Routing and assigning
               function itShouldRouteToTheAddAction()
               {
                   $this->rou...
Models



Saturday, 29 October 2011
create a model spec




Saturday, 29 October 2011
create a model spec
  $ zf create model-spec Speaker name:string,email:string




Saturday, 29 October 2011
create a model spec
  $ zf create model-spec Speaker name:string,email:string
  Creating a model at /private/var/www/callc...
Saturday, 29 October 2011
Spec created by default
<?php

require_once __DIR__ . '/../SpecHelper.php';

use Application_Model_Speaker as Speaker;

cl...
what behaviors can we
                 describe in the model spec?



Saturday, 29 October 2011
Business logic

                                     Validation

                     Spying results from data source oper...
Business Logic

class DescribeSpeaker extends PHPSpecContext
{
  function before()
  {
    $this->validAttributes = array(...
Business Logic



class Speaker
{
  //... other methods

    function hasExtraPoints()
    {
      return stripos($this->g...
Real database hits?




Saturday, 29 October 2011
Avoid

                               Sometimes, for confidence

                            When testing data access obje...
Dependency chains




Saturday, 29 October 2011
Dependencies can be hard to manage

class DescribeEvent extends PHPSpecContext
{
    function itDoesSomethingWhenYouHaveSp...
Usually dependencies are replaced with
                                  doubles when writing specs

                     ...
Object Mother



Saturday, 29 October 2011
Dependencies can be hard to manage



class DescribeEvent extends PHPSpecContext
{
    function itDoesSomethingWhenYouHave...
Code duplication

                            Too many methods




Saturday, 29 October 2011
Test Data Builder



Saturday, 29 October 2011
Is created with save “empty” objects

                                   Has a fluent interface

                         ...
Dependencies can be hard to manage


class DescribeEvent extends PHPSpecContext
{
    function itDoesSomethingWhenYouHaveS...
phactory.org




Saturday, 29 October 2011
installing




Saturday, 29 October 2011
installing
  $ sudo pear channel-discover pearhub.org




Saturday, 29 October 2011
installing
  $ sudo pear channel-discover pearhub.org
  $ sudo pear install pearhub/Phactory




Saturday, 29 October 2011
Needs a Pdo connection

                            Get from default adapter




Saturday, 29 October 2011
Create a connection

         protected function _initPhactory()
         {
             Phactory::setConnection(
        ...
Define table blueprints

     // spec/factories.php

     Phactory::define('speaker', array(
                 'name' => 'J...
Create objects
     // in one of my specs

     $ben = Phactory::create('speaker', array('name' => 'Rowan'));
     $rowan ...
Questions?




                                         223
Saturday, 29 October 2011
Thank you!

                            http://joind.in/4318
                            http://slidesha.re/tcGM93
       ...
Upcoming SlideShare
Loading in …5
×

of

BDD For Zend Framework With PHPSpec Slide 1 BDD For Zend Framework With PHPSpec Slide 2 BDD For Zend Framework With PHPSpec Slide 3 BDD For Zend Framework With PHPSpec Slide 4 BDD For Zend Framework With PHPSpec Slide 5 BDD For Zend Framework With PHPSpec Slide 6 BDD For Zend Framework With PHPSpec Slide 7 BDD For Zend Framework With PHPSpec Slide 8 BDD For Zend Framework With PHPSpec Slide 9 BDD For Zend Framework With PHPSpec Slide 10 BDD For Zend Framework With PHPSpec Slide 11 BDD For Zend Framework With PHPSpec Slide 12 BDD For Zend Framework With PHPSpec Slide 13 BDD For Zend Framework With PHPSpec Slide 14 BDD For Zend Framework With PHPSpec Slide 15 BDD For Zend Framework With PHPSpec Slide 16 BDD For Zend Framework With PHPSpec Slide 17 BDD For Zend Framework With PHPSpec Slide 18 BDD For Zend Framework With PHPSpec Slide 19 BDD For Zend Framework With PHPSpec Slide 20 BDD For Zend Framework With PHPSpec Slide 21 BDD For Zend Framework With PHPSpec Slide 22 BDD For Zend Framework With PHPSpec Slide 23 BDD For Zend Framework With PHPSpec Slide 24 BDD For Zend Framework With PHPSpec Slide 25 BDD For Zend Framework With PHPSpec Slide 26 BDD For Zend Framework With PHPSpec Slide 27 BDD For Zend Framework With PHPSpec Slide 28 BDD For Zend Framework With PHPSpec Slide 29 BDD For Zend Framework With PHPSpec Slide 30 BDD For Zend Framework With PHPSpec Slide 31 BDD For Zend Framework With PHPSpec Slide 32 BDD For Zend Framework With PHPSpec Slide 33 BDD For Zend Framework With PHPSpec Slide 34 BDD For Zend Framework With PHPSpec Slide 35 BDD For Zend Framework With PHPSpec Slide 36 BDD For Zend Framework With PHPSpec Slide 37 BDD For Zend Framework With PHPSpec Slide 38 BDD For Zend Framework With PHPSpec Slide 39 BDD For Zend Framework With PHPSpec Slide 40 BDD For Zend Framework With PHPSpec Slide 41 BDD For Zend Framework With PHPSpec Slide 42 BDD For Zend Framework With PHPSpec Slide 43 BDD For Zend Framework With PHPSpec Slide 44 BDD For Zend Framework With PHPSpec Slide 45 BDD For Zend Framework With PHPSpec Slide 46 BDD For Zend Framework With PHPSpec Slide 47 BDD For Zend Framework With PHPSpec Slide 48 BDD For Zend Framework With PHPSpec Slide 49 BDD For Zend Framework With PHPSpec Slide 50 BDD For Zend Framework With PHPSpec Slide 51 BDD For Zend Framework With PHPSpec Slide 52 BDD For Zend Framework With PHPSpec Slide 53 BDD For Zend Framework With PHPSpec Slide 54 BDD For Zend Framework With PHPSpec Slide 55 BDD For Zend Framework With PHPSpec Slide 56 BDD For Zend Framework With PHPSpec Slide 57 BDD For Zend Framework With PHPSpec Slide 58 BDD For Zend Framework With PHPSpec Slide 59 BDD For Zend Framework With PHPSpec Slide 60 BDD For Zend Framework With PHPSpec Slide 61 BDD For Zend Framework With PHPSpec Slide 62 BDD For Zend Framework With PHPSpec Slide 63 BDD For Zend Framework With PHPSpec Slide 64 BDD For Zend Framework With PHPSpec Slide 65 BDD For Zend Framework With PHPSpec Slide 66 BDD For Zend Framework With PHPSpec Slide 67 BDD For Zend Framework With PHPSpec Slide 68 BDD For Zend Framework With PHPSpec Slide 69 BDD For Zend Framework With PHPSpec Slide 70 BDD For Zend Framework With PHPSpec Slide 71 BDD For Zend Framework With PHPSpec Slide 72 BDD For Zend Framework With PHPSpec Slide 73 BDD For Zend Framework With PHPSpec Slide 74 BDD For Zend Framework With PHPSpec Slide 75 BDD For Zend Framework With PHPSpec Slide 76 BDD For Zend Framework With PHPSpec Slide 77 BDD For Zend Framework With PHPSpec Slide 78 BDD For Zend Framework With PHPSpec Slide 79 BDD For Zend Framework With PHPSpec Slide 80 BDD For Zend Framework With PHPSpec Slide 81 BDD For Zend Framework With PHPSpec Slide 82 BDD For Zend Framework With PHPSpec Slide 83 BDD For Zend Framework With PHPSpec Slide 84 BDD For Zend Framework With PHPSpec Slide 85 BDD For Zend Framework With PHPSpec Slide 86 BDD For Zend Framework With PHPSpec Slide 87 BDD For Zend Framework With PHPSpec Slide 88 BDD For Zend Framework With PHPSpec Slide 89 BDD For Zend Framework With PHPSpec Slide 90 BDD For Zend Framework With PHPSpec Slide 91 BDD For Zend Framework With PHPSpec Slide 92 BDD For Zend Framework With PHPSpec Slide 93 BDD For Zend Framework With PHPSpec Slide 94 BDD For Zend Framework With PHPSpec Slide 95 BDD For Zend Framework With PHPSpec Slide 96 BDD For Zend Framework With PHPSpec Slide 97 BDD For Zend Framework With PHPSpec Slide 98 BDD For Zend Framework With PHPSpec Slide 99 BDD For Zend Framework With PHPSpec Slide 100 BDD For Zend Framework With PHPSpec Slide 101 BDD For Zend Framework With PHPSpec Slide 102 BDD For Zend Framework With PHPSpec Slide 103 BDD For Zend Framework With PHPSpec Slide 104 BDD For Zend Framework With PHPSpec Slide 105 BDD For Zend Framework With PHPSpec Slide 106 BDD For Zend Framework With PHPSpec Slide 107 BDD For Zend Framework With PHPSpec Slide 108 BDD For Zend Framework With PHPSpec Slide 109 BDD For Zend Framework With PHPSpec Slide 110 BDD For Zend Framework With PHPSpec Slide 111 BDD For Zend Framework With PHPSpec Slide 112 BDD For Zend Framework With PHPSpec Slide 113 BDD For Zend Framework With PHPSpec Slide 114 BDD For Zend Framework With PHPSpec Slide 115
Upcoming SlideShare
Behavioural Driven Development in Zf2
Next
Download to read offline and view in fullscreen.

11 Likes

Share

Download to read offline

BDD For Zend Framework With PHPSpec

Download to read offline

Talk delivered at PHP Barcelona Conference 2011. Using PHPSpec and Zend Tool together to demonstrate BDD in a PHP MVC context.

Related Books

Free with a 30 day trial from Scribd

See all

BDD For Zend Framework With PHPSpec

  1. 1. BDD for with PHPSpec 29th October 2011 Marcello Duarte Saturday, 29 October 2011
  2. 2. whoami Marcello Duarte Head of Training @ Ibuildings UK Lead developer @ PHPSpec Twitter @_md Saturday, 29 October 2011
  3. 3. In the beginning there was... TDD credits: http://www.flickr.com/photos/improveit/1574023621 Saturday, 29 October 2011
  4. 4. They saw that it was good Saturday, 29 October 2011
  5. 5. $thou->shalt->test credits: http://www.flickr.com/photos/36829113@N05/3392940179/ Saturday, 29 October 2011
  6. 6. BDD credits: http://www.flickr.com/photos/psd/424257767/ Saturday, 29 October 2011
  7. 7. BDD A way of teaching TDD credits: http://www.flickr.com/photos/psd/424257767/ Saturday, 29 October 2011
  8. 8. What to test? H Where to begin? ow m uch Saturday, 29 October 2011 to test i ? What to call my test? How do I name my test? n one go?
  9. 9. BDD credits: http://www.flickr.com/photos/psd/424257767/ Saturday, 29 October 2011
  10. 10. BDD Offers a common language credits: http://www.flickr.com/photos/psd/424257767/ Saturday, 29 October 2011
  11. 11. business user developer Saturday, 29 October 2011
  12. 12. business user developer behavior Saturday, 29 October 2011
  13. 13. Saturday, 29 October 2011 © Manuscripts and Archives, Yale University Library. Sapir-Whorf hypothesis
  14. 14. Language to express truth vs... Saturday, 29 October 2011
  15. 15. Language to discover truth Saturday, 29 October 2011
  16. 16. Language influence thought Saturday, 29 October 2011
  17. 17. BDD credits: http://www.flickr.com/photos/psd/424257767/ Saturday, 29 October 2011
  18. 18. BDD A way to discover what is useful to deliver credits: http://www.flickr.com/photos/psd/424257767/ Saturday, 29 October 2011
  19. 19. Saturday, 29 October 2011
  20. 20. BDD Outside in Gherkin Behat PHPSpec Saturday, 29 October 2011
  21. 21. Gherkin Saturday, 29 October 2011
  22. 22. Feature: Organizers can open a call for paper As an event organizer I want a way to publish a centralized cfp form So that it’s easier for speakers to submit Scenario: Creation form with valid attributes Given I am in on "call-for-papers/add" When I fill in the following: | event | PHPLondon Conference| | start_date | 2012-02-06 | | limit_abstract_wc | 500 | | why_you_field | 1 | | offer_hotel | 1 | | offer_travel | 0 | And I press "Create" Then I should see "The cfp was created successfully" Saturday, 29 October 2011
  23. 23. behat.org Saturday, 29 October 2011
  24. 24. Saturday, 29 October 2011
  25. 25. words matter Saturday, 29 October 2011
  26. 26. $this->assertTrue(... Saturday, 29 October 2011
  27. 27. $this->assertTrue(... What am I going to test? Saturday, 29 October 2011
  28. 28. $report = new Report; $this->assertTrue($report instanceof Report); Saturday, 29 October 2011
  29. 29. / / $report = new Report; $this->assertTrue($report instanceof Report); Saturday, 29 October 2011
  30. 30. $employee->should->... Saturday, 29 October 2011
  31. 31. $employee->should->... What is the expected behavior? Saturday, 29 October 2011
  32. 32. Use Gherkin and Behat for specifying scenarios ∫ Use PHPSpec for specifying classes Saturday, 29 October 2011
  33. 33. expressiveness Saturday, 29 October 2011
  34. 34. $this->assertTrue($employee->reportsTo($manager)); $employee->should->reportTo($manager); Saturday, 29 October 2011
  35. 35. phpspec.net Saturday, 29 October 2011
  36. 36. Installing $ sudo pear channel-discover pear.phpspec.net $ sudo pear install --alldeps phpspec/PHPSpec Saturday, 29 October 2011
  37. 37. “Specification, not verification” (Uncle Bob) $this->assertEquals(0, $result); becomes $result->should->be(0); Saturday, 29 October 2011
  38. 38. class CalculatorTest becomes class DescribeCalculator Saturday, 29 October 2011
  39. 39. function testAddWithNoArguments() becomes function itReturnsZeroWithNoArguments() Saturday, 29 October 2011
  40. 40. All together class DescribeStringCalculator extends PHPSpecContext { function itReturnsZeroWithNoArguments() { $calculator = $this->spec(new StringCalculator); $result = $calculator->add(); $result->should->be(0); } } Saturday, 29 October 2011
  41. 41. Hooks before() after() beforeAll() afterAll() Saturday, 29 October 2011
  42. 42. Setting initial state with before hook class DescribeStringCalculator extends PHPSpecContext { function before() { $this->calculator = $this->spec(new StringCalculator); } function itReturnsZeroWithNoArguments() { $result = $this->calculator->add(); $result->should->be(0); } } Saturday, 29 October 2011
  43. 43. class DescribeStringCalculator extends PHPSpecContext { private $calculator; function before() { $this->calculator = $this->spec(new StringCalculator); } function itReturnsZeroWithNoArguments() { $this->calculator->add()->should->equal(0); } function itReturnsTheBareNumber() { $this->calculator->add('42')->should->equal(42); } } Saturday, 29 October 2011
  44. 44. Formatters progress documentation html coming soon: junit Saturday, 29 October 2011
  45. 45. Progress Formatter $ phpspec StringCalculatorSpec.php -c .*.F Pending: String Calculator returns the bare number # Waiting to clarify the spec # ./spec/StringCalculatorSpec.php:19 Failures: 1) String Calculator returns the sum of space separate string expected 42, got 0 (using be()) # .spec/StringCalculatorSpec.php:28 2) StringCalculator returns the sum of any white space separated string Failure/Error: Just because Finished in 0.056134 seconds 4 examples, 1 failure, 1 pending Saturday, 29 October 2011
  46. 46. HTML Formatter Saturday, 29 October 2011
  47. 47. Documentation Formatter Saturday, 29 October 2011
  48. 48. Matchers Saturday, 29 October 2011
  49. 49. be($match) equal($match) beEqualTo($match) beAnInstanceOf($match) beEmpty() beFalse() beGreaterThan($match) beGreaterThanOrEqualTo($match) Saturday, 29 October 2011
  50. 50. And more matchers... Saturday, 29 October 2011
  51. 51. beInteger() beLessThan($match) beLessThanOrEqualTo($match) beNull() beString() beTrue() throwException($match) Saturday, 29 October 2011
  52. 52. Predicate Matchers Saturday, 29 October 2011
  53. 53. $cell = $this->spec(new Cell); $cell->should->beAlive(); class Cell { protected $alive = true; public function isAlive() { return $this->alive; } ... } Saturday, 29 October 2011
  54. 54. $newNode = $this->spec(new Node); $newNode->shouldNot->haveChildren(); class Node { protected $children = array(); public function hasChildren() { return count($this->children) > 0; } ... } Saturday, 29 October 2011
  55. 55. Custom Matchers Saturday, 29 October 2011
  56. 56. PHPSpecMatcherdefine('reportTo', function($supervisor) { return array ( 'match' => function($supportEngineer) use ($supervisor) { return $supportEngineer->reportsTo($supervisor); }, 'failure_message_for_should' => function($supportEngineer) use ($supervisor) { return "expected " . $supervisor->getName() . " to report to " . $supervisor->getName(); } ); }); class DescribeSupportEngineer extends PHPSpecContext { ... function itAddsNewCourses() { $john = new Supervisor("John Smith"); $john->addToTeam($this->supportEngineer); $this->supportEngineer->should->reportTo($john); } } Saturday, 29 October 2011
  57. 57. PHPSpec & Saturday, 29 October 2011
  58. 58. Tool framework.zend.com Saturday, 29 October 2011
  59. 59. Installing $ sudo pear channel-discover pear.zfcampus.org $ sudo pear install zfcampus/zf $ zf create config $ vi ~/.zf.ini Saturday, 29 October 2011
  60. 60. .zf.ini php.include_path = ".:/usr/share/pear" basicloader.classes.1 = "Akrabat_Tool_DatabaseSchemaProvider" basicloader.classes.2 = "PHPSpec_Context_Zend_Tool_Provider_Phpspec" basicloader.classes.3 = "PHPSpec_Context_Zend_Tool_Provider_ModelSpec" basicloader.classes.4 = "PHPSpec_Context_Zend_Tool_Provider_ViewSpec" basicloader.classes.5 = "PHPSpec_Context_Zend_Tool_Provider_ControllerSpec" basicloader.classes.6 = "PHPSpec_Context_Zend_Tool_Provider_ActionSpec" basicloader.classes.7 = "PHPSpec_Context_Zend_Tool_Provider_Behat" Saturday, 29 October 2011
  61. 61. create a project $ zf create project callconf Creating project at /var/www/callconf Note: This command created a web project, for more information setting up your VHOST, please see docs/README Saturday, 29 October 2011
  62. 62. initialize PHPSpec $ cd callconf $ zf generate phpspec create spec create spec/SpecHelper.php create spec/.phpspec create spec/models create spec/views create spec/controllers Saturday, 29 October 2011
  63. 63. initialize Behat $ zf generate behat +d features - place your *.feature files here +d features/bootstrap - place bootstrap scripts and static files here +f features/bootstrap/FeatureContext.php - place your feature related code here Saturday, 29 October 2011
  64. 64. Saturday, 29 October 2011
  65. 65. Views Saturday, 29 October 2011
  66. 66. why specify the view? Saturday, 29 October 2011
  67. 67. Controller and model to the point Ensure we are focused on what matters Sustainable pace Saturday, 29 October 2011
  68. 68. create a view spec Saturday, 29 October 2011
  69. 69. create a view spec $ zf create view-spec add CallForPapers Saturday, 29 October 2011
  70. 70. create a view spec $ zf create view-spec add CallForPapers Creating a view script in location /var/www/callconf/ application/views/scripts/call-for-papers/add.phtml Creating a spec at /var/www/callconf/spec/views/call-for- papers/AddSpec.php Saturday, 29 October 2011
  71. 71. Saturday, 29 October 2011
  72. 72. Spec created by default <?php namespace CallForPapers; require_once __DIR__ . '/../../SpecHelper.php'; use PHPSpecContextZendView as ViewContext; class DescribeAdd extends ViewContext { function itRendersTheDefaultContent() { $this->render(); $this->rendered->should->contain('CallForPapers'); $this->rendered->should->contain('add'); } } Saturday, 29 October 2011
  73. 73. what behaviors can we describe in the view spec? Saturday, 29 October 2011
  74. 74. Variables we need assigned What content was rendered (We can use selectors) Saturday, 29 October 2011
  75. 75. Assigning Variables function itRendersTheTalkAbstract() { $marcello = $this->mock('Speaker', array('isVegetarian' => true)); $this->assign('speaker', $marcello); $this->render(); $this->rendered->should->contain('diet restrictions: vegetarian'); } Saturday, 29 October 2011
  76. 76. Controllers Saturday, 29 October 2011
  77. 77. create a controller spec Saturday, 29 October 2011
  78. 78. create a controller spec $ zf create controller-spec CallForPapers add,create Saturday, 29 October 2011
  79. 79. create a controller spec $ zf create controller-spec CallForPapers add,create Creating a controller at /private/var/www/callconf/ application/controllers/CallForPapersController.php Creating an add action method in controller CallForPapers Creating an create action method in controller CallForPapers Creating a spec at /private/var/www/callconf/spec/ controllers/CallForPapersSpec.php Saturday, 29 October 2011
  80. 80. Saturday, 29 October 2011
  81. 81. Spec created by default <?php require_once __DIR__ . '/../SpecHelper.php'; class DescribeCallForPapers extends PHPSpecContextZendController { function itShouldBeSuccessfulToGetAdd() { $this->get('call-for-papers/add'); $this->response->should->beSuccess(); } } Saturday, 29 October 2011
  82. 82. what behaviors do we want to describe in the controller spec? Saturday, 29 October 2011
  83. 83. How do we want to route to its actions What view variables we need assigned What view we want rendered Saturday, 29 October 2011
  84. 84. Routing and assigning function itShouldRouteToTheAddAction() { $this->routeFor(array( 'controller' => 'call-for-papers', 'action' => 'add' ))->should->be('/call-for-papers/add'); } function itAssignsAddSubmissionFormVariable() { $this->get('/call-for-papers/add'); $this->assigns('addSubmissionForm')->should->beAnInstanceOf( 'Application_Form_AddSubmissionForm' ); } Saturday, 29 October 2011
  85. 85. Models Saturday, 29 October 2011
  86. 86. create a model spec Saturday, 29 October 2011
  87. 87. create a model spec $ zf create model-spec Speaker name:string,email:string Saturday, 29 October 2011
  88. 88. create a model spec $ zf create model-spec Speaker name:string,email:string Creating a model at /private/var/www/callconf/application/ models/Speaker.php Creating a db table at /private/var/www/callconf/ application/models/DbTable/Speakers.php Creating a mapper at /private/var/www/callconf/application/ models/SpeakerMapper.php Creating a spec at /private/var/www/callconf/spec/models/ SpeakerSpec.php Creating migration scripts at /private/var/www/callconf/db/ migrate/001-CreateSpeakersTable.php Updating project profile '/private/var/www/ callconf/.zfproject.xml' Saturday, 29 October 2011
  89. 89. Saturday, 29 October 2011
  90. 90. Spec created by default <?php require_once __DIR__ . '/../SpecHelper.php'; use Application_Model_Speaker as Speaker; class DescribeSpeaker extends PHPSpecContext { function before() { $this->validAttributes = array( 'name' => 'value for name', 'email' => 'value for email', ); } function itShouldCreateANewInstanceGivenValidAttributes() { $this->speaker = $this->spec(Speaker::create($this->validAttributes)); $this->speaker->should->beValid(); } } Saturday, 29 October 2011
  91. 91. what behaviors can we describe in the model spec? Saturday, 29 October 2011
  92. 92. Business logic Validation Spying results from data source operations Saturday, 29 October 2011
  93. 93. Business Logic class DescribeSpeaker extends PHPSpecContext { function before() { $this->validAttributes = array( 'name' => 'Marcello Duarte', 'email' => 'marcello@ibuildings.com', 'diet_restriction' => 'vegetarian', ); $this->speaker = $this->spec(Speaker::create($this->validAttributes)); } function itGetsExtraRatingPointsForTalkIfVegetarian() { $this->speaker->should->haveExtraPoints(); } } Saturday, 29 October 2011
  94. 94. Business Logic class Speaker { //... other methods function hasExtraPoints() { return stripos($this->getDietRestrictions(), 'vegetarian') !== false; } } Saturday, 29 October 2011
  95. 95. Real database hits? Saturday, 29 October 2011
  96. 96. Avoid Sometimes, for confidence When testing data access objects Saturday, 29 October 2011
  97. 97. Dependency chains Saturday, 29 October 2011
  98. 98. Dependencies can be hard to manage class DescribeEvent extends PHPSpecContext { function itDoesSomethingWhenYouHaveSpeakerAllocated() { $event = new Event( new Organizer('John Smith', new Organization('Ibuildings') ) ); $event->addSpeaker(new Speaker('Rowan'), new Slot(’10:30’), new Room('A')); $event->addSpeaker(new Speaker('Ben'), new Slot(’10:30’), new Room('B')); // specify expected behavior } } Saturday, 29 October 2011
  99. 99. Usually dependencies are replaced with doubles when writing specs We can use a framework like Mockery But if you really need the real thing Saturday, 29 October 2011
  100. 100. Object Mother Saturday, 29 October 2011
  101. 101. Dependencies can be hard to manage class DescribeEvent extends PHPSpecContext { function itDoesSomethingWhenYouHaveSpeakerAllocated() { $exampleEvent = ExampleEvent::newWithSimultaneousSpeakers(); // specify expected event behavior } } Saturday, 29 October 2011
  102. 102. Code duplication Too many methods Saturday, 29 October 2011
  103. 103. Test Data Builder Saturday, 29 October 2011
  104. 104. Is created with save “empty” objects Has a fluent interface Has a build method Saturday, 29 October 2011
  105. 105. Dependencies can be hard to manage class DescribeEvent extends PHPSpecContext { function itDoesSomethingWhenYouHaveSpeakerAllocated() { $eventBuilder = new EventBuilder(); $organizerBuilder = new OrganizerBuilder(); $event = $eventBuilder->withOrganizer( $organizerBuilder->withOrganization()->build() )->withConflictingSpeakers() ->build(); // specify expected event behavior } } Saturday, 29 October 2011
  106. 106. phactory.org Saturday, 29 October 2011
  107. 107. installing Saturday, 29 October 2011
  108. 108. installing $ sudo pear channel-discover pearhub.org Saturday, 29 October 2011
  109. 109. installing $ sudo pear channel-discover pearhub.org $ sudo pear install pearhub/Phactory Saturday, 29 October 2011
  110. 110. Needs a Pdo connection Get from default adapter Saturday, 29 October 2011
  111. 111. Create a connection protected function _initPhactory() { Phactory::setConnection( Zend_Db_Table_Abstract::getDefaultAdapter()); return Phactory::getConnection(); } Saturday, 29 October 2011
  112. 112. Define table blueprints // spec/factories.php Phactory::define('speaker', array( 'name' => 'John Smith', 'email' => 'john@smith.com')); Saturday, 29 October 2011
  113. 113. Create objects // in one of my specs $ben = Phactory::create('speaker', array('name' => 'Rowan')); $rowan = Phactory::create('speaker', array('name' => 'Ben')); // Phactory_Row objects echo $ben->name // prints Ben Saturday, 29 October 2011
  114. 114. Questions? 223 Saturday, 29 October 2011
  115. 115. Thank you! http://joind.in/4318 http://slidesha.re/tcGM93 Marcello Duarte @_md is hiring. Come talk to me. 224 Saturday, 29 October 2011
  • powerirs

    May. 18, 2016
  • stephandesouza

    Mar. 4, 2015
  • tiagojvt

    Oct. 30, 2014
  • oswald_odr

    Jun. 11, 2013
  • frozenminds

    Oct. 9, 2012
  • ftassi

    Sep. 17, 2012
  • romanyarovoy

    Apr. 3, 2012
  • sbalbalosa

    Nov. 29, 2011
  • benyan

    Nov. 20, 2011
  • jrub

    Oct. 30, 2011
  • 1ed

    Oct. 29, 2011

Talk delivered at PHP Barcelona Conference 2011. Using PHPSpec and Zend Tool together to demonstrate BDD in a PHP MVC context.

Views

Total views

7,517

On Slideshare

0

From embeds

0

Number of embeds

29

Actions

Downloads

71

Shares

0

Comments

0

Likes

11

×