PHPSpec BDD Framework

47
Introducing PHPSpec A BDD Framework

description

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

Transcript of PHPSpec BDD Framework

Page 1: PHPSpec BDD Framework

Introducing

PHPSpec

A BDD Framework

Page 2: PHPSpec BDD Framework

Marcello Duarte @_md

PHPSpec Lead DeveloperHead of Training @Agile guy

Page 3: PHPSpec BDD Framework

BDD Framework

Created by Pádraic Brady

DSL based on RSpec

What is PHPSpec?

Page 4: PHPSpec BDD Framework

Behaviour Driven Development

BDD?

Page 5: PHPSpec BDD Framework

Better way to explain TDD

BDD?

Page 6: PHPSpec BDD Framework

Write a failing test

TDD?

Page 7: PHPSpec BDD Framework

Write a failing test

Make it fail for the right reasons

TDD?

Page 8: PHPSpec BDD Framework

Write a failing testMake it fail for the right reasons

Make it pass (just about)

TDD?

Page 9: PHPSpec BDD Framework

Write a failing testMake it fail for the right reasons

Make it pass (just about)

Refactor

TDD?

Page 10: PHPSpec BDD Framework

Write a failing testMake it fail for the right reasons

Make it pass (just about)Refactor

Start again

TDD?

Page 11: PHPSpec BDD Framework

Focus on behaviour

Page 12: PHPSpec BDD Framework

Test

Specify

Page 13: PHPSpec BDD Framework

class CalculatorTest

becomes

class DescribeCalculator

Page 14: PHPSpec BDD Framework

Test Case

Context

Page 15: PHPSpec BDD Framework

class CalculatorTest extends SomeTestFramework_TestCase

becomes

class DescribeCalculator extends PHPSpec_Context

Page 16: PHPSpec BDD Framework

Test Method

Example

Page 17: PHPSpec BDD Framework

testAddWithNoArguments()

becomes

itReturnsZeroWithNoArguments()

Page 18: PHPSpec BDD Framework

Assert

Expect

Page 19: PHPSpec BDD Framework

$this->assertEquals(0, $result);

becomes

$result->should->be(0);

Page 20: PHPSpec BDD Framework

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

Page 21: PHPSpec BDD Framework

PHPSpec DSL

<?php

class DescribeStringCalculator extends PHPSpec_Context { public function itReturnsZeroWithNoArguments() { $result = $this->spec(StringCalculator::add()); $result->should->be(0); }}

# StringCalculatorSpec.php

Page 22: PHPSpec BDD Framework

$result->should->be(0)$result->shouldNot->be(42)

Page 23: PHPSpec BDD Framework

Loads of matchers...

Page 24: PHPSpec BDD Framework

be($match)equal($match)

beEqualTo($match)beAnInstanceOf($match)

beEmpty()beFalse()

beGreaterThan($match)beGreaterThanOrEqualTo($match)

Page 25: PHPSpec BDD Framework

And more matchers...

Page 26: PHPSpec BDD Framework

beInteger()beLessThan($match)

beLessThanOrEqualTo($match) beNull()

beString()beTrue()

coming soon:throwException($match)

Page 27: PHPSpec BDD Framework

Predicate Matcher

$cell = $this->spec(new Cell);$cell->should->beAlive();

class Cell{ protected $alive = true;

public function isAlive() { return $this->alive; } ...}

Page 28: PHPSpec BDD Framework

Predicate Matcher

$newNode = $this->spec(new Node);$newNode->shouldNot->haveChildren();

class Node{ protected $children = array();

public function hasChildren() { return count($this->children) > 0; } ...}

Page 29: PHPSpec BDD Framework

Lets run our specs

<?php

class DescribeStringCalculator extends PHPSpec_Context { public function itReturnsZeroWithNoArgument() { $this->spec(StringCalculator::add())->should->be(0); }}

# StringCalculatorSpec.php

Page 30: PHPSpec BDD Framework

Lets run our specs

$ phpspec StringCalculatorSpec.php -c.

Finished in 0.055689 seconds

1 examples, 0 failures

Page 31: PHPSpec BDD Framework

Pending examples

public function itReturnsZeroWithAnEmptyString(){ $this->pending();}

Page 32: PHPSpec BDD Framework

$ 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$

Page 33: PHPSpec BDD Framework

Failing examples

public function itReturnsTheBareNumber(){ $result = $this->spec(StringCalculator::add("42")); $result->should->be(42);}

Page 34: PHPSpec BDD Framework

$ 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$

Page 35: PHPSpec BDD Framework

Deliberate fail

public function itReturnsTheBareNumber(){ $this->fail("An optional message");}

Page 36: PHPSpec BDD Framework

Hooks

before()after()

beforeAll()afterAll()

Page 37: PHPSpec BDD Framework

set initial state

public function before(){ $this->calculator = new StringCalculator();}

Page 38: PHPSpec BDD Framework

Mocks

Phakehttps://github.com/mlively/Phake

Mockeryhttps://github.com/padraic/Mockery

Page 39: PHPSpec BDD Framework

BDD Outside in

Gherkin

Behat

PHPSpec

Page 40: PHPSpec BDD Framework

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

Page 41: PHPSpec BDD Framework

$ pwd/path/to/my/app$ lsfeatures src spec$ behat

Page 42: PHPSpec BDD Framework
Page 43: PHPSpec BDD Framework
Page 44: PHPSpec BDD Framework

Goutte

Screen scrapingWeb crawling library for PHP

Goutte does not start the browser

Page 45: PHPSpec BDD Framework

// 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');

Page 46: PHPSpec BDD Framework

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

Page 47: PHPSpec BDD Framework

Thank you!

Marcello Duarte@_md

is hiring. Come talk to me.