This document introduces PHPSpec, a BDD framework for PHP inspired by RSpec. It discusses the principles of behavior-driven development (BDD) and test-driven development (TDD), and how PHPSpec implements a domain-specific language (DSL) that allows developers to write specifications in a natural language style. The document provides examples of how to write PHPSpec specifications using its matchers and contexts, run the specifications, and handle pending and failing tests. It also briefly discusses related tools like mocks, screen scraping with Goutte, and how BDD fits into the development cycle.
27. Predicate Matcher
$cell = $this->spec(new Cell);
$cell->should->beAlive();
class Cell
{
protected $alive = true;
public function isAlive() {
return $this->alive;
}
...
}
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. Lets run our specs
# StringCalculatorSpec.php
<?php
class DescribeStringCalculator
extends PHPSpec_Context {
public function itReturnsZeroWithNoArgument()
{
$this->spec(StringCalculator::add())->should->be(0);
}
}
30. Lets run our specs
$ phpspec StringCalculatorSpec.php -c
.
Finished in 0.055689 seconds
1 examples, 0 failures
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
44. Goutte
Screen scraping
Web crawling library for PHP
Goutte does not start the browser
45. // Create a Goutte Client
use 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');
46. The BDD Cycle
1. Write your stories
2. Use Gherkin/Behat to
describe the behaviour of your apps
3. Use PHPSpec to
describe the behaviour of your classes
47. Thank you!
Marcello Duarte
@_md
is hiring. Come talk to me.