PHPSpec BDD Framework
-
Upload
marcello-duarte -
Category
Technology
-
view
3.314 -
download
0
description
Transcript of PHPSpec BDD Framework
Introducing
PHPSpec
A BDD Framework
Marcello Duarte @_md
PHPSpec Lead DeveloperHead of Training @Agile guy
BDD Framework
Created by Pádraic Brady
DSL based on RSpec
What is PHPSpec?
Behaviour Driven Development
BDD?
Better way to explain TDD
BDD?
Write a failing test
TDD?
Write a failing test
Make 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
Start again
TDD?
Focus on behaviour
Test
Specify
class CalculatorTest
becomes
class DescribeCalculator
Test Case
Context
class CalculatorTest extends SomeTestFramework_TestCase
becomes
class DescribeCalculator extends PHPSpec_Context
Test Method
Example
testAddWithNoArguments()
becomes
itReturnsZeroWithNoArguments()
Assert
Expect
$this->assertEquals(0, $result);
becomes
$result->should->be(0);
Installing
PEAR (soon...)
# pear channel-discover pear.phpspec.netAdding Channel "pear.phpspec.net" succeededDiscovery of channel "pear.phpspec.net" succeeded# pear install --alldeps phpspec/PHPSpec-beta
GITHUB
$ git clone git://github.com/phpspec/phpspec.git
PHPSpec DSL
<?php
class DescribeStringCalculator extends PHPSpec_Context { public function itReturnsZeroWithNoArguments() { $result = $this->spec(StringCalculator::add()); $result->should->be(0); }}
# StringCalculatorSpec.php
$result->should->be(0)$result->shouldNot->be(42)
Loads of matchers...
be($match)equal($match)
beEqualTo($match)beAnInstanceOf($match)
beEmpty()beFalse()
beGreaterThan($match)beGreaterThanOrEqualTo($match)
And more matchers...
beInteger()beLessThan($match)
beLessThanOrEqualTo($match) beNull()
beString()beTrue()
coming soon:throwException($match)
Predicate Matcher
$cell = $this->spec(new Cell);$cell->should->beAlive();
class Cell{ protected $alive = true;
public function isAlive() { return $this->alive; } ...}
Predicate Matcher
$newNode = $this->spec(new Node);$newNode->shouldNot->haveChildren();
class Node{ protected $children = array();
public function hasChildren() { return count($this->children) > 0; } ...}
Lets run our specs
<?php
class DescribeStringCalculator extends PHPSpec_Context { public function itReturnsZeroWithNoArgument() { $this->spec(StringCalculator::add())->should->be(0); }}
# StringCalculatorSpec.php
Lets run our specs
$ phpspec StringCalculatorSpec.php -c.
Finished in 0.055689 seconds
1 examples, 0 failures
Pending examples
public function itReturnsZeroWithAnEmptyString(){ $this->pending();}
$ phpspec StringCalculatorSpec.php -c.P
Pending:
1)'string calculator returns zero with an empty string' PENDINGIncomplete
Finished in 0.056134 seconds
2 examples, 0 failures, 1 pending$
Failing examples
public function itReturnsTheBareNumber(){ $result = $this->spec(StringCalculator::add("42")); $result->should->be(42);}
$ phpspec StringCalculatorSpec.php -c.PF
Failures:
1)'string calculator returns the bare number' FAILEDexpected 42, got 0 (using be())/Users/md/BDDTalk/StringCalculatorSpec.php:28
Pending:...Finished in 0.056134 seconds
3 examples, 1 failure, 1 pending$
Deliberate fail
public function itReturnsTheBareNumber(){ $this->fail("An optional message");}
Hooks
before()after()
beforeAll()afterAll()
set initial state
public function before(){ $this->calculator = new StringCalculator();}
Mocks
Phakehttps://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 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
$ pwd/path/to/my/app$ lsfeatures src spec$ behat
Goutte
Screen scrapingWeb crawling library for PHP
Goutte does not start the browser
// Create a Goutte Client use Goutte\Client;
$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');
The BDD Cycle
1. Write your stories
2. Use Gherkin/Behat todescribe the behaviour of your apps
3. Use PHPSpec todescribe the behaviour of your classes
Thank you!
Marcello Duarte@_md
is hiring. Come talk to me.