IntroducingPHPSpecA BDD Framework
Marcello Duarte          @_mdPHPSpec Lead DeveloperHead of Training @Agile guy
What is PHPSpec? BDD Framework Created by Pádraic Brady DSL based on RSpec
BDD?Behaviour Driven Development
BDD?Better way to explain TDD
TDD?Write a failing test
TDD?       Write a failing testMake it fail for the right reasons
TDD?      Write a failing testMake it fail for the right reasons   Make it pass (just about)
TDD?      Write a failing testMake it fail for the right reasons  Make it pass (just about)            Refactor
TDD?      Write a failing testMake it fail for the right reasons  Make it pass (just about)             Refactor          ...
Focus on behaviour
TestSpecify
class CalculatorTest      becomesclass DescribeCalculator
Test CaseContext
class CalculatorTest extends   SomeTestFramework_TestCase          becomesclass DescribeCalculator extends         PHPSpec...
Test Method Example
testAddWithNoArguments()         becomesitReturnsZeroWithNoArguments()
AssertExpect
$this->assertEquals(0, $result);         becomes    $result->should->be(0);
InstallingPEAR (soon...)# pear channel-discover pear.phpspec.netAdding Channel "pear.phpspec.net" succeededDiscovery of ch...
PHPSpec DSL# StringCalculatorSpec.php<?phpclass DescribeStringCalculator  extends PHPSpec_Context {    public function itR...
$result->should->be(0)$result->shouldNot->be(42)
Loads of matchers...
be($match)         equal($match)       beEqualTo($match)    beAnInstanceOf($match)           beEmpty()           beFalse()...
And more matchers...
beInteger()    beLessThan($match)beLessThanOrEqualTo($match)         beNull()        beString()         beTrue()       com...
Predicate Matcher           $cell = $this->spec(new Cell);           $cell->should->beAlive();class Cell{    protected $al...
Predicate Matcher           $newNode = $this->spec(new Node);           $newNode->shouldNot->haveChildren();class Node{   ...
Lets run our specs# StringCalculatorSpec.php<?phpclass DescribeStringCalculator  extends PHPSpec_Context {    public funct...
Lets run our specs$ phpspec StringCalculatorSpec.php -c.Finished in 0.055689 seconds1 examples, 0 failures
Pending examplespublic function itReturnsZeroWithAnEmptyString(){    $this->pending();}
$ phpspec StringCalculatorSpec.php -c.PPending:1)string calculator returns zero with an emptystring PENDINGIncompleteFinis...
Failing examplespublic function itReturnsTheBareNumber(){    $result = $this->spec(StringCalculator::add("42"));    $resul...
$ phpspec StringCalculatorSpec.php -c.PFFailures:1)string calculator returns the bare number FAILEDexpected 42, got 0 (usi...
Deliberate failpublic function itReturnsTheBareNumber(){    $this->fail("An optional message");}
Hooks before()  after()beforeAll()afterAll()
set initial statepublic function before(){    $this->calculator = new StringCalculator();}
Mocks               Phake https://github.com/mlively/Phake              Mockeryhttps://github.com/padraic/Mockery
BDD Outside in    Gherkin    Behat   PHPSpec
Feature: Learners Feedback  In order to improve the quality of training  As an learner  I want to provide feedback for a c...
$ pwd/path/to/my/app$ lsfeatures    src   spec$ behat
Goutte        Screen scraping  Web crawling library for PHPGoutte does not start the browser
// Create a Goutte Clientuse GoutteClient;$client = new Client();// Make requests which returns a Crawler$crawler = $clien...
The BDD Cycle         1. Write your stories       2. Use Gherkin/Behat to describe the behaviour of your apps          3. ...
Thank you!    Marcello Duarte        @_mdis hiring. Come talk to me.
PHPSpec BDD Framework
PHPSpec BDD Framework
Upcoming SlideShare
Loading in …5
×

PHPSpec BDD Framework

4,209 views

Published on

Introducing PHPSpec 0.2.2 at PHPLondon, April 7th 2011, a framework written in PHP for Behaviour Driven Development.

Published in: Technology

PHPSpec BDD Framework

  1. 1. IntroducingPHPSpecA BDD Framework
  2. 2. Marcello Duarte @_mdPHPSpec Lead DeveloperHead of Training @Agile guy
  3. 3. What is PHPSpec? BDD Framework Created by Pádraic Brady DSL based on RSpec
  4. 4. BDD?Behaviour Driven Development
  5. 5. BDD?Better way to explain TDD
  6. 6. TDD?Write a failing test
  7. 7. TDD? Write a failing testMake it fail for the right reasons
  8. 8. TDD? Write a failing testMake it fail for the right reasons Make it pass (just about)
  9. 9. TDD? Write a failing testMake it fail for the right reasons Make it pass (just about) Refactor
  10. 10. TDD? Write a failing testMake it fail for the right reasons Make it pass (just about) Refactor Start again
  11. 11. Focus on behaviour
  12. 12. TestSpecify
  13. 13. class CalculatorTest becomesclass DescribeCalculator
  14. 14. Test CaseContext
  15. 15. class CalculatorTest extends SomeTestFramework_TestCase becomesclass DescribeCalculator extends PHPSpec_Context
  16. 16. Test Method Example
  17. 17. testAddWithNoArguments() becomesitReturnsZeroWithNoArguments()
  18. 18. AssertExpect
  19. 19. $this->assertEquals(0, $result); becomes $result->should->be(0);
  20. 20. InstallingPEAR (soon...)# pear channel-discover pear.phpspec.netAdding Channel "pear.phpspec.net" succeededDiscovery of channel "pear.phpspec.net" succeeded# pear install --alldeps phpspec/PHPSpec-betaGITHUB$ git clone git://github.com/phpspec/phpspec.git
  21. 21. PHPSpec DSL# StringCalculatorSpec.php<?phpclass DescribeStringCalculator extends PHPSpec_Context { public function itReturnsZeroWithNoArguments() { $result = $this->spec(StringCalculator::add()); $result->should->be(0); }}
  22. 22. $result->should->be(0)$result->shouldNot->be(42)
  23. 23. Loads of matchers...
  24. 24. be($match) equal($match) beEqualTo($match) beAnInstanceOf($match) beEmpty() beFalse() beGreaterThan($match)beGreaterThanOrEqualTo($match)
  25. 25. And more matchers...
  26. 26. beInteger() beLessThan($match)beLessThanOrEqualTo($match) beNull() beString() beTrue() coming soon: throwException($match)
  27. 27. Predicate Matcher $cell = $this->spec(new Cell); $cell->should->beAlive();class Cell{ protected $alive = true; public function isAlive() { return $this->alive; } ...}
  28. 28. Predicate Matcher $newNode = $this->spec(new Node); $newNode->shouldNot->haveChildren();class Node{ protected $children = array(); public function hasChildren() { return count($this->children) > 0; } ...}
  29. 29. Lets run our specs# StringCalculatorSpec.php<?phpclass DescribeStringCalculator extends PHPSpec_Context { public function itReturnsZeroWithNoArgument() { $this->spec(StringCalculator::add())->should->be(0); }}
  30. 30. Lets run our specs$ phpspec StringCalculatorSpec.php -c.Finished in 0.055689 seconds1 examples, 0 failures
  31. 31. Pending examplespublic function itReturnsZeroWithAnEmptyString(){ $this->pending();}
  32. 32. $ phpspec StringCalculatorSpec.php -c.PPending:1)string calculator returns zero with an emptystring PENDINGIncompleteFinished in 0.056134 seconds2 examples, 0 failures, 1 pending$
  33. 33. Failing examplespublic function itReturnsTheBareNumber(){ $result = $this->spec(StringCalculator::add("42")); $result->should->be(42);}
  34. 34. $ phpspec StringCalculatorSpec.php -c.PFFailures:1)string calculator returns the bare number FAILEDexpected 42, got 0 (using be())/Users/md/BDDTalk/StringCalculatorSpec.php:28Pending:...Finished in 0.056134 seconds3 examples, 1 failure, 1 pending$
  35. 35. Deliberate failpublic function itReturnsTheBareNumber(){ $this->fail("An optional message");}
  36. 36. Hooks before() after()beforeAll()afterAll()
  37. 37. set initial statepublic function before(){ $this->calculator = new StringCalculator();}
  38. 38. Mocks Phake https://github.com/mlively/Phake Mockeryhttps://github.com/padraic/Mockery
  39. 39. BDD Outside in Gherkin Behat PHPSpec
  40. 40. Feature: Learners Feedback In order to improve the quality of training As an learner I want to provide feedback for a course Scenario: Missed selecting the course Given I am on the feedback page And I skip selecting the course When submit the feedback Then I should be warned the feedback was not submitted Scenario: Thank you page Given I am on the feedback page And I fill all fields required When submit the feedback Then I should see a thank you message
  41. 41. $ pwd/path/to/my/app$ lsfeatures src spec$ behat
  42. 42. Goutte Screen scraping Web crawling library for PHPGoutte does not start the browser
  43. 43. // Create a Goutte Clientuse GoutteClient;$client = new Client();// Make requests which returns a Crawler$crawler = $client->request(GET, http://feedbackpage);.//Click on links:$link = $crawler->selectLink(Course)->link();$crawler = $client->click($link);// Submit forms:$form = $crawler->selectButton(Submit Feedback)->form();$crawler = $client->submit($form, array( trainer => Marcello Duarte, course => Agile PHP Developer));// Parse results$error = $crawler->filter(.errors);
  44. 44. The BDD Cycle 1. Write your stories 2. Use Gherkin/Behat to describe the behaviour of your apps 3. Use PHPSpec todescribe the behaviour of your classes
  45. 45. Thank you! Marcello Duarte @_mdis hiring. Come talk to me.

×