BDD For Zend Framework With PHPSpec
-
Upload
marcello-duarte -
Category
Technology
-
view
5.166 -
download
1
description
Transcript of BDD For Zend Framework With PHPSpec
![Page 1: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/1.jpg)
BDD for with
PHPSpec
29th October 2011
Marcello Duarte
Saturday, 29 October 2011
![Page 2: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/2.jpg)
whoami
Marcello Duarte
Head of Training @ Ibuildings UK
Lead developer @ PHPSpec
Twitter @_md
Marcello Duarte
Head of Training @ Ibuildings UK
Lead developer @ PHPSpec
Twitter @_md
Saturday, 29 October 2011
![Page 3: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/3.jpg)
In the beginningthere was...
TDD
credits: http://www.flickr.com/photos/improveit/1574023621
Saturday, 29 October 2011
![Page 4: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/4.jpg)
They saw that it was good
Saturday, 29 October 2011
![Page 5: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/5.jpg)
$thou->shalt->test$thou->shalt->test
credits: http://www.flickr.com/photos/36829113@N05/3392940179/
Saturday, 29 October 2011
![Page 6: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/6.jpg)
BDD
credits: http://www.flickr.com/photos/psd/424257767/
Saturday, 29 October 2011
![Page 7: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/7.jpg)
BDD
A way of teaching TDD
credits: http://www.flickr.com/photos/psd/424257767/
Saturday, 29 October 2011
![Page 8: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/8.jpg)
What to test?
Where to begin?
How do I name my test?
How much to test in one go?
What to call my test??Saturday, 29 October 2011
![Page 9: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/9.jpg)
credits: http://www.flickr.com/photos/psd/424257767/
BDD
Saturday, 29 October 2011
![Page 10: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/10.jpg)
Offers a common language
credits: http://www.flickr.com/photos/psd/424257767/
BDD
Saturday, 29 October 2011
![Page 11: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/11.jpg)
user developer
business
Saturday, 29 October 2011
![Page 12: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/12.jpg)
behavior
user developer
business
Saturday, 29 October 2011
![Page 13: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/13.jpg)
Sapir-Whorf hypothesis
© M
anusc
ripts
and A
rchiv
es,
Yale
Univ
ersi
ty L
ibra
ry.
Saturday, 29 October 2011
![Page 14: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/14.jpg)
Language to express truth
vs...
Saturday, 29 October 2011
![Page 15: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/15.jpg)
Language to discover truth
Saturday, 29 October 2011
![Page 16: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/16.jpg)
Language influence thought
Saturday, 29 October 2011
![Page 17: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/17.jpg)
credits: http://www.flickr.com/photos/psd/424257767/
BDD
Saturday, 29 October 2011
![Page 18: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/18.jpg)
A way to discoverwhat is useful to deliver
credits: http://www.flickr.com/photos/psd/424257767/
BDD
Saturday, 29 October 2011
![Page 19: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/19.jpg)
Saturday, 29 October 2011
![Page 20: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/20.jpg)
BDD Outside in
Gherkin
Behat
PHPSpec
Saturday, 29 October 2011
![Page 21: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/21.jpg)
Gherkin
Saturday, 29 October 2011
![Page 22: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/22.jpg)
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"
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"
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"
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
![Page 23: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/23.jpg)
behat.org
Saturday, 29 October 2011
![Page 24: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/24.jpg)
Saturday, 29 October 2011
![Page 25: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/25.jpg)
words matter
Saturday, 29 October 2011
![Page 26: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/26.jpg)
$this->assertTrue(...
Saturday, 29 October 2011
![Page 27: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/27.jpg)
$this->assertTrue(...
What am I going to test?
Saturday, 29 October 2011
![Page 28: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/28.jpg)
$report = new Report;$this->assertTrue($report instanceof Report);
Saturday, 29 October 2011
![Page 29: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/29.jpg)
$report = new Report;$this->assertTrue($report instanceof Report);//
Saturday, 29 October 2011
![Page 30: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/30.jpg)
$employee->should->...
Saturday, 29 October 2011
![Page 31: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/31.jpg)
$employee->should->...
What is the expected behavior?
Saturday, 29 October 2011
![Page 32: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/32.jpg)
Use Gherkin and Behat for specifying scenarios
Use PHPSpec for specifying classes∫
Saturday, 29 October 2011
![Page 33: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/33.jpg)
expressiveness
Saturday, 29 October 2011
![Page 34: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/34.jpg)
$employee->should->reportTo($manager);
$this->assertTrue($employee->reportsTo($manager));
Saturday, 29 October 2011
![Page 35: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/35.jpg)
phpspec.net
Saturday, 29 October 2011
![Page 36: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/36.jpg)
$ sudo pear channel-discover pear.phpspec.net$ sudo pear install --alldeps phpspec/PHPSpec
Installing
Saturday, 29 October 2011
![Page 37: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/37.jpg)
“Specification, not verification” (Uncle Bob)
$this->assertEquals(0, $result);
becomes$result->should->be(0);
Saturday, 29 October 2011
![Page 38: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/38.jpg)
class CalculatorTest
becomesclass DescribeCalculator
class CalculatorTest
becomesclass DescribeCalculator
Saturday, 29 October 2011
![Page 39: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/39.jpg)
function testAddWithNoArguments()
becomesfunction itReturnsZeroWithNoArguments()
Saturday, 29 October 2011
![Page 40: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/40.jpg)
All together
class DescribeStringCalculator extends \PHPSpec\Context{
function itReturnsZeroWithNoArguments() { $calculator = $this->spec(new StringCalculator); $result = $calculator->add();
$result->should->be(0); }
}
Saturday, 29 October 2011
![Page 41: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/41.jpg)
Hooks
before()after()
beforeAll()afterAll()
Saturday, 29 October 2011
![Page 42: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/42.jpg)
Setting initial state with before hook
class DescribeStringCalculator extends \PHPSpec\Context{ function before() { $this->calculator = $this->spec(new StringCalculator); }
function itReturnsZeroWithNoArguments() { $result = $this->calculator->add(); $result->should->be(0); }}
Saturday, 29 October 2011
![Page 43: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/43.jpg)
class DescribeStringCalculator extends \PHPSpec\Context{ 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
![Page 44: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/44.jpg)
Formatters
progressdocumentation
html
coming soon:junit
Saturday, 29 October 2011
![Page 45: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/45.jpg)
$ 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 seconds4 examples, 1 failure, 1 pending
Progress Formatter
Saturday, 29 October 2011
![Page 46: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/46.jpg)
HTML Formatter
Saturday, 29 October 2011
![Page 47: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/47.jpg)
Documentation Formatter
Saturday, 29 October 2011
![Page 48: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/48.jpg)
Matchers
Saturday, 29 October 2011
![Page 49: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/49.jpg)
be($match)equal($match)
beEqualTo($match)beAnInstanceOf($match)
beEmpty()beFalse()
beGreaterThan($match)beGreaterThanOrEqualTo($match)
Saturday, 29 October 2011
![Page 50: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/50.jpg)
And more matchers...
Saturday, 29 October 2011
![Page 51: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/51.jpg)
beInteger()beLessThan($match)
beLessThanOrEqualTo($match) beNull()
beString()beTrue()
throwException($match)
Saturday, 29 October 2011
![Page 52: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/52.jpg)
Predicate Matchers
Saturday, 29 October 2011
![Page 53: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/53.jpg)
$cell = $this->spec(new Cell);$cell->should->beAlive();
class Cell{ protected $alive = true;
public function isAlive() { return $this->alive; } ...}
$cell = $this->spec(new Cell);$cell->should->beAlive();
class Cell{ protected $alive = true;
public function isAlive() { return $this->alive; } ...}
Saturday, 29 October 2011
![Page 54: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/54.jpg)
$newNode = $this->spec(new Node);$newNode->shouldNot->haveChildren();
class Node{ protected $children = array();
public function hasChildren() { return count($this->children) > 0; } ...}
$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
![Page 55: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/55.jpg)
Custom Matchers
Saturday, 29 October 2011
![Page 56: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/56.jpg)
\PHPSpec\Matcher\define('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 \PHPSpec\Context{ ... function itAddsNewCourses() { $john = new Supervisor("John Smith"); $john->addToTeam($this->supportEngineer); $this->supportEngineer->should->reportTo($john); }}
\PHPSpec\Matcher\define('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 \PHPSpec\Context{ ... function itAddsNewCourses() { $john = new Supervisor("John Smith"); $john->addToTeam($this->supportEngineer); $this->supportEngineer->should->reportTo($john); }}
Saturday, 29 October 2011
![Page 57: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/57.jpg)
PHPSpec &
Saturday, 29 October 2011
![Page 58: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/58.jpg)
Tool
framework.zend.com
Saturday, 29 October 2011
![Page 59: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/59.jpg)
$ sudo pear channel-discover pear.zfcampus.org$ sudo pear install zfcampus/zf$ zf create config$ vi ~/.zf.ini
Installing
Saturday, 29 October 2011
![Page 60: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/60.jpg)
.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"
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
![Page 61: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/61.jpg)
$ zf create project callconfCreating project at /var/www/callconfNote: This command created a web project, for more information setting up your VHOST, please see docs/README
create a project
Saturday, 29 October 2011
![Page 62: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/62.jpg)
$ cd callconf$ zf generate phpspec create spec create spec/SpecHelper.php create spec/.phpspec create spec/models create spec/views create spec/controllers
initialize PHPSpec
Saturday, 29 October 2011
![Page 63: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/63.jpg)
$ 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
initialize Behat
Saturday, 29 October 2011
![Page 64: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/64.jpg)
Saturday, 29 October 2011
![Page 65: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/65.jpg)
Views
Saturday, 29 October 2011
![Page 66: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/66.jpg)
why specify the view?
Saturday, 29 October 2011
![Page 67: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/67.jpg)
Controller and model to the point
Ensure we are focused on what matters
Sustainable pace
Saturday, 29 October 2011
![Page 68: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/68.jpg)
create a view spec
Saturday, 29 October 2011
![Page 69: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/69.jpg)
$ zf create view-spec add CallForPapers
create a view spec
Saturday, 29 October 2011
![Page 70: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/70.jpg)
$ zf create view-spec add CallForPapers Creating a view script in location /var/www/callconf/application/views/scripts/call-for-papers/add.phtmlCreating a spec at /var/www/callconf/spec/views/call-for-papers/AddSpec.php
create a view spec
Saturday, 29 October 2011
![Page 71: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/71.jpg)
Saturday, 29 October 2011
![Page 72: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/72.jpg)
Spec created by default<?php
namespace CallForPapers;
require_once __DIR__ . '/../../SpecHelper.php';
use \PHPSpec\Context\Zend\View as ViewContext;
class DescribeAdd extends ViewContext{ function itRendersTheDefaultContent() { $this->render(); $this->rendered->should->contain('CallForPapers'); $this->rendered->should->contain('add'); }}
<?php
namespace CallForPapers;
require_once __DIR__ . '/../../SpecHelper.php';
use \PHPSpec\Context\Zend\View as ViewContext;
class DescribeAdd extends ViewContext{ function itRendersTheDefaultContent() { $this->render(); $this->rendered->should->contain('CallForPapers'); $this->rendered->should->contain('add'); }}
<?php
namespace CallForPapers;
require_once __DIR__ . '/../../SpecHelper.php';
use \PHPSpec\Context\Zend\View as ViewContext;
class DescribeAdd extends ViewContext{ function itRendersTheDefaultContent() { $this->render(); $this->rendered->should->contain('CallForPapers'); $this->rendered->should->contain('add'); }}
Saturday, 29 October 2011
![Page 73: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/73.jpg)
what behaviors can we describe in the view spec?
Saturday, 29 October 2011
![Page 74: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/74.jpg)
Variables we need assigned
What content was rendered(We can use selectors)
Saturday, 29 October 2011
![Page 75: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/75.jpg)
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
![Page 76: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/76.jpg)
Controllers
Saturday, 29 October 2011
![Page 77: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/77.jpg)
create a controller spec
Saturday, 29 October 2011
![Page 78: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/78.jpg)
$ zf create controller-spec CallForPapers add,create
create a controller spec
Saturday, 29 October 2011
![Page 79: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/79.jpg)
$ zf create controller-spec CallForPapers add,createCreating a controller at /private/var/www/callconf/application/controllers/CallForPapersController.phpCreating an add action method in controller CallForPapersCreating an create action method in controller CallForPapersCreating a spec at /private/var/www/callconf/spec/controllers/CallForPapersSpec.php
create a controller spec
Saturday, 29 October 2011
![Page 80: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/80.jpg)
Saturday, 29 October 2011
![Page 81: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/81.jpg)
Spec created by default
<?php
require_once __DIR__ . '/../SpecHelper.php';
class DescribeCallForPapers extends \PHPSpec\Context\Zend\Controller{ function itShouldBeSuccessfulToGetAdd() { $this->get('call-for-papers/add'); $this->response->should->beSuccess(); }
}
<?php
require_once __DIR__ . '/../SpecHelper.php';
class DescribeCallForPapers extends \PHPSpec\Context\Zend\Controller{ function itShouldBeSuccessfulToGetAdd() { $this->get('call-for-papers/add'); $this->response->should->beSuccess(); }
}
Saturday, 29 October 2011
![Page 82: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/82.jpg)
what behaviors do we want todescribe in the controller spec?
Saturday, 29 October 2011
![Page 83: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/83.jpg)
How do we want to route to its actions
What view variables we need assigned
What view we want rendered
Saturday, 29 October 2011
![Page 84: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/84.jpg)
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' ); }
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
![Page 85: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/85.jpg)
Models
Saturday, 29 October 2011
![Page 86: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/86.jpg)
create a model spec
Saturday, 29 October 2011
![Page 87: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/87.jpg)
$ zf create model-spec Speaker name:string,email:string
create a model spec
Saturday, 29 October 2011
![Page 88: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/88.jpg)
$ zf create model-spec Speaker name:string,email:stringCreating a model at /private/var/www/callconf/application/models/Speaker.phpCreating a db table at /private/var/www/callconf/application/models/DbTable/Speakers.phpCreating a mapper at /private/var/www/callconf/application/models/SpeakerMapper.phpCreating a spec at /private/var/www/callconf/spec/models/SpeakerSpec.phpCreating migration scripts at /private/var/www/callconf/db/migrate/001-CreateSpeakersTable.phpUpdating project profile '/private/var/www/callconf/.zfproject.xml'
create a model spec
Saturday, 29 October 2011
![Page 89: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/89.jpg)
Saturday, 29 October 2011
![Page 90: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/90.jpg)
Spec created by default<?php
require_once __DIR__ . '/../SpecHelper.php';
use Application_Model_Speaker as Speaker;
class DescribeSpeaker extends \PHPSpec\Context{ 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
![Page 91: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/91.jpg)
what behaviors can wedescribe in the model spec?
Saturday, 29 October 2011
![Page 92: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/92.jpg)
Business logic
Validation
Spying results from data source operations
Saturday, 29 October 2011
![Page 93: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/93.jpg)
Business Logic
class DescribeSpeaker extends \PHPSpec\Context{ function before() { $this->validAttributes = array( 'name' => 'Marcello Duarte', 'email' => '[email protected]', 'diet_restriction' => 'vegetarian', ); $this->speaker = $this->spec(Speaker::create($this->validAttributes)); } function itGetsExtraRatingPointsForTalkIfVegetarian() { $this->speaker->should->haveExtraPoints(); }}
class DescribeSpeaker extends \PHPSpec\Context{ function before() { $this->validAttributes = array( 'name' => 'Marcello Duarte', 'email' => '[email protected]', 'diet_restriction' => 'vegetarian', ); $this->speaker = $this->spec(Speaker::create($this->validAttributes)); } function itGetsExtraRatingPointsForTalkIfVegetarian() { $this->speaker->should->haveExtraPoints(); }}
Saturday, 29 October 2011
![Page 94: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/94.jpg)
Business Logic
class Speaker{ //... other methods
function hasExtraPoints() { return stripos($this->getDietRestrictions(), 'vegetarian') !== false; }}
class Speaker{ //... other methods
function hasExtraPoints() { return stripos($this->getDietRestrictions(), 'vegetarian') !== false; }}
Saturday, 29 October 2011
![Page 95: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/95.jpg)
Real database hits?
Saturday, 29 October 2011
![Page 96: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/96.jpg)
Avoid
Sometimes, for confidence
When testing data access objects
Saturday, 29 October 2011
![Page 97: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/97.jpg)
Dependency chains
Saturday, 29 October 2011
![Page 98: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/98.jpg)
Dependencies can be hard to manage
class DescribeEvent extends \PHPSpec\Context{ 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
![Page 99: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/99.jpg)
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
![Page 100: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/100.jpg)
Object Mother
Saturday, 29 October 2011
![Page 101: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/101.jpg)
Dependencies can be hard to manage
class DescribeEvent extends \PHPSpec\Context{ function itDoesSomethingWhenYouHaveSpeakerAllocated() { $exampleEvent = ExampleEvent::newWithSimultaneousSpeakers();
// specify expected event behavior }}
Saturday, 29 October 2011
![Page 102: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/102.jpg)
Code duplication
Too many methods
Saturday, 29 October 2011
![Page 103: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/103.jpg)
Test Data Builder
Saturday, 29 October 2011
![Page 104: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/104.jpg)
Is created with save “empty” objects
Has a fluent interface
Has a build method
Saturday, 29 October 2011
![Page 105: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/105.jpg)
Dependencies can be hard to manage
class DescribeEvent extends \PHPSpec\Context{ function itDoesSomethingWhenYouHaveSpeakerAllocated() { $eventBuilder = new EventBuilder(); $organizerBuilder = new OrganizerBuilder();
$event = $eventBuilder->withOrganizer( $organizerBuilder->withOrganization()->build() )->withConflictingSpeakers() ->build();
// specify expected event behavior }}
Saturday, 29 October 2011
![Page 106: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/106.jpg)
phactory.org
Saturday, 29 October 2011
![Page 107: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/107.jpg)
installing
Saturday, 29 October 2011
![Page 108: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/108.jpg)
$ sudo pear channel-discover pearhub.org
installing
Saturday, 29 October 2011
![Page 109: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/109.jpg)
$ sudo pear channel-discover pearhub.org$ sudo pear install pearhub/Phactory
installing
Saturday, 29 October 2011
![Page 110: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/110.jpg)
Needs a Pdo connection
Get from default adapter
Saturday, 29 October 2011
![Page 111: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/111.jpg)
protected function _initPhactory() { Phactory::setConnection( Zend_Db_Table_Abstract::getDefaultAdapter()); return Phactory::getConnection(); }
Create a connection
Saturday, 29 October 2011
![Page 112: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/112.jpg)
// spec/factories.php
Phactory::define('speaker', array( 'name' => 'John Smith', 'email' => '[email protected]'));
Define table blueprints
Saturday, 29 October 2011
![Page 113: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/113.jpg)
// in one of my specs
$ben = Phactory::create('speaker', array('name' => 'Rowan'));$rowan = Phactory::create('speaker', array('name' => 'Ben'));
// Phactory_Row objectsecho $ben->name // prints Ben
Create objects
Saturday, 29 October 2011
![Page 114: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/114.jpg)
Questions?
223
Saturday, 29 October 2011
![Page 115: BDD For Zend Framework With PHPSpec](https://reader035.fdocuments.us/reader035/viewer/2022062405/554f650eb4c905c8088b4cd5/html5/thumbnails/115.jpg)
224
Thank you!
http://joind.in/4318
http://slidesha.re/tcGM93
Marcello Duarte
@_md
is hiring. Come talk to me.
Saturday, 29 October 2011