SlideShare a Scribd company logo
1 of 92
Download to read offline
Behavior-Driven Development
with Zend Framework 2

Zend Framework Day – Turin, Italy – 07/02/2014
DAVID CONTAVALLI

@mauipipe

2
CLEAN CODE ADEPT
BDD FANATIC

3
CLEAN CODE ADEPT
BDD FANATIC

4
WHAT IS BDD?
TDD Evolution

6
SAME RULES
1.

7

You are not allowed to write any production code
unless it is to make a failing unit test pass.
SAME RULES
1.

2.

8

You are not allowed to write any production code
unless it is to make a failing unit test pass.
You are not allowed to write any more of a unit test
than is sufficient to fail; and compilation failures are
failures.
SAME RULES
1.

2.

You are not allowed to write any more of a unit test
than is sufficient to fail; and compilation failures are
failures.

3.

9

You are not allowed to write any production code
unless it is to make a failing unit test pass.

You are not allowed to write any more production code
than is sufficient to pass the one failing unit test.
WHAT’S THE DIFFERENCE?
TDD STARTS FROM COMPONENTS
CREATE THE TEST
private $calculator;
function setUp(){
$this->calculator = new Calculator();
}
function testSumTwoPositive(){
$expectedResult = 8;
$result = $this->calculator(6,2);
assertEquals($expectedResult,$result);
}

12
13
WRITE QUICK, TEST VALIDATING CODE

class Calculator{
public function sum($num1, $num2){
return 8;
}
}

14
15
REFACTOR

class Calculator{
public function sum($num1, $num2){
$sum = $num1 + $num2;
return $sum;
}
}

16
17
BDD STARTS FROM AN EXAMPLE
SPECIFICATION BY EXAMPLE

Scenario: Add two number
Given I fill number1 field with 2
And I fill number2 field with 6
When I press Add button
Then the result should be 8

19
A COMMON LANGUAGE

STAKEHOLDER
20
A COMMON LANGUAGE

DEVELOPER
STAKEHOLDER
21
A COMMON LANGUAGE

DEVELOPER
STAKEHOLDER
22

USER
LIVING DOCUMENTATION
HOW IS STORY CODE RELATED?
GHERKIN

Scenario: Add two number
Given I fill number1 field with 2
And I fill number2 field with 6
When I press Add button
Then the result should be 8

25
Interpreted in many languages

26
GHERKIN

27
GHERKIN

28
WHAT IS BEHAT?
Scenario: Add two numbers
Given I fill number1 field with value of 2
TRANSLATE

/**
* @Given /^I fill ([^’’]*) with (d+)$/
**/
public function iFieldFieldWith($number, $fieldName)
{
throw new PendingException();
}
31
EXECUTION

32
USERS WILL NOT READ STORIES
THEY WILL USE AN UI
BROWSER
HOW TO TEST BROWSER WITH BEHAT?

36
HERE IT COMES, MINK!
GHERKIN

+

38
Web Acceptance Test

GOUTTE
• Headless Browser
• No Javascript
• Really fast

39
Web Acceptance Test

GOUTTE
• Headless Browser
• No Javascript
• Really fast

40

SELENIUM
• Can test Javascript
• Really slow
• Use Firefox as emulator
Web Acceptance Test

GOUTTE
• Headless Browser
• No Javascript
• Really fast

41

SELENIUM
• Can test Javascript
• Really slow
• Use Firefox as emulator
Zombie.js
• Can test Javascript
• Medium
• Use Firefox as emulator
Web Acceptance Test

GOUTTE
• Headless Browser
• No Javascript
• Really fast

42

SELENIUM
• Can test Javascript
• Really slow
• Use Firefox as emulator
Zombie.js
• Can test Javascript
• Medium
• Use Firefox as emulator

SAHI
• Can test Javascript
• slow
• Emulate every Browser
Scenario: Add two numbers
Given I fill number1 field with 2
And I fill number2 field with 6
When I press Add button
Then the result should be 8
/**
* @When /^I press ‘’(/[^’’]*)’’ button$/
**/
public function iPressButton($buttonName)
{
$this->pressButton($buttonName);
}

43
How to install....

+

44
"require-dev " :{
.........
"behat/behat" : "2.4.*@stable",
"behat/mink": "1.5.0",
"behat/mink-extension":"*",
"behat/mink-browserkit-driver":"dev-master",
"behat/mink-goutte-driver":"*",
"phpspec/phpspec":" 2.0.*@dev"
}
45
Differs from documentation
because there is a fix for ZF2
multicheckbox selection

"require-dev " :{
.........
"behat/behat" : "2.4.*@stable",
"behat/mink": "1.5.0",
"behat/mink-extension":"*",
"behat/mink-browserkit-driver":"dev-master",
"behat/mink-goutte-driver":"*",
"phpspec/phpspec":" 2.0.*@dev"
}
46
Mink Setup
behat.yml
default :
extensions :
BehatMinkExtensionExtension :
base_url : 'http://example.com'
goutte : ~

47
+
+
48
Static method
private static app;
/**
* @BeforeSuite
**/
public static function initializeZf(){
If(self::$zendApp === null){
$path = __DIR__ .’/../../config/application.config.php’;
self::app = ZendMvcApplication::init(require $path);
}
}
49
ZF2 EXTENSION
"require-dev":{
................
"mvlabs/zf2behat-extension":"dev-master"
},
51
Zf2 Behat Extension
behat.yml
default :
extensions :
MvLabsZf2ExtensionZf2Extension :
config : config/application.config.php
module :

52
Zf2 Behat Extension

run from console

bin/behat --init ‘’module name’’

53
Feature Context
Implements Zf2AwareContextInterface
class FeatureContext extends MinkContext implements
Zf2AwareContextInterface{
private $zf2MvcApplication;
.........
public function setZf2App(Application $zf2MvcApplication) {
$this->zf2MvcApplication = $zf2MvcApplication;
}

}

54
WE STILL NEED TO TEST
OUR COMPONENTS
GHERKIN

+

56
WHY PHPSPEC?

57
BDD BASED ON BEHAVIOUR
DESCRIPTION
Robert C. Martin
PHPUnit focus on testing code
private $calculator;

function setUp(){
$this->calculator = new Calculator();
}
function testSumTwoPositive(){
$expectedResult = 8;
$result = $this->calculator(6,2);
assertEquals($expectedResult,$result);
}
59
PHPSpec focus on code behavior
function let(){
$this->shouldBeConstructed();
}

function it_sum_two_positive_number(){
$this->sum(2,6)->shouldReturn(8);
}

60
Subject under Specification
"It’s this unexisting object, on which you’re
calling unexisting methods and assuming future
outcomes. Most important thing? There could
be only one SUS in specification"
Kostantin Kudryashov

61
Developer Tool Vs Testing Framework

62
Autogenerates classes & methods
bin/phpspec desc ‘’ApplicationControllerIndexController’’

63
PROMOTES CLEAN CODE
Demeter Law's Violation
function let(){
$this->shouldBeConstructed();
}
function it_sum_two_positive_number(){
$this->getPlayer()->getSword()->shouldBeInstanceOf(‘Sword’);
}

65
Impossible
function let(){
$this->shouldBeConstructed();
}
function it_sum_two_positive_number(){
$this->getPlayer()->getSword()->shouldBeInstanceOf(‘Sword’);
}

66
Add PHPSpec to Zf2

+

67
"require-dev":{
...........
"phpspec/phpspec" : "2.0.*@dev"
},
"config": {
"bin-dir": "bin/"
},
"autoload": {
"psr-0" {
"Application" : " module/Application/src"
}
}
68
Add a single module

’’require-dev’’:{
"require-dev":{
...........
...
"phpspec/phpspec" : "2.0.*@dev"
"phpspec/phpspec":" 2.0.*@dev"
},
},
"config": {
"config": {
"bin-dir": "bin/"
"bin-dir": "bin«
},
},
"autoload": { {
"autoload":
"psr-0" { {
"psr-0":
"Application" : " module/Application/src"
"Application": "module/Application/src"
} }
}}
69
Add more modules

"require-dev":{
...........
"phpspec/phpspec" : "2.0.*@dev"
},
"config": {
"bin-dir": "bin/"
},
"autoload": {
"psr-0" {
"Application" : " module/Application/src",
"Calculator" : "module/Calculator/src"
}
}
70
Create phpspec.yml in project root
formatter.name : progresssuites
Application:
namespace : Application
spec_prefix : Spec
src_path : 'module/Application/src/'
spec_path : 'module/Application/'
ModuleDemo:
namespace : ModuleDemo
spec_prefix : Spec
src_path : 'module/ModuleDemo/src/'
spec_path : 'module/ModuleDemo/'
71
What to test with PHPSpec?

72
What to test with PHPSpec?
1.Model Logic

73
What to test with PHPSpec?
1.Model Logic
2.Factories

74
What to test with PHPSpec?
1.Model Logic
2.Factories

3.Validation

75
Behat bad practices
1. Verbose Stories

2. Using Mink to test REST calls
3. Testing every possible usages combination
4. Fixture loading within Context

76
WHY TESTING?
IT’S A TREND
RELAX
SOMEONE WILL READ YOUR CODE
IT COULD BE YOU SOME DAY
YOUR COLLEAGUES
OR
84
A VIOLENT PSYCHOPATH WHO
KNOWS WHERE YOU LIVE
Thank you for your attention

David Contavalli
@mauipipe
QUESTIONS?
@mauipipe
mauipipe@gmail.com
Some readings

90
Credits
https://www.flickr.com/photos/42788859@N00/318947873
https://www.flickr.com/photos/79811974@N08/8895959339/
https://www.flickr.com/photos/wingedwolf/5471047557/
https://www.flickr.com/photos/21112928@N07/2922128673/
https://www.flickr.com/photos/23408922@N07/8220573257/
https://www.flickr.com/photos/11956371@N07/4146284063/
http://www.flickr.com/photos/chemicalbrother/2540855983/
http://www.flickr.com/photos/slworking/5757370044/
http://www.flickr.com/photos/ter-burg/5807937726/
http://www.flickr.com/photos/thyagohills/5023536434/
http://www.flickr.com/photos/jeremybrooks/2175042537/
http://www.flickr.com/photos/bowmanlibrary/941844481/
http://www.flickr.com/photos/webhamster/2476756607/
http://www.flickr.com/photos/nebirdsplus/5835963068/
http://www.flickr.com/photos/mistaboos/4348381987/
http://www.flickr.com/photos/moofbong/4207382992/
http://www.flickr.com/photos/ryanh/43936630/
http://www.flickr.com/photos/nathangibbs/98592171/
http://www.flickr.com/photos/71894657@N00/2553948289/
http://www.flickr.com/photos/ahia/3168219760/
http://www.flickr.com/photos/smileham/3559228586/
http://www.flickr.com/photos/kk/3834592792/
http://www.flickr.com/photos/enoughproject/5776533975/
http://www.flickr.com/photos/_flood_/8067625282/
http://www.flickr.com/photos/drooo/3114233333/
http://www.flickr.com/photos/84143785@N00/3559757811

91
David Contavalli
@mauipipe

More Related Content

What's hot

Jenkins Evolutions - JEEConf 2012
Jenkins Evolutions - JEEConf 2012Jenkins Evolutions - JEEConf 2012
Jenkins Evolutions - JEEConf 2012
Anton Arhipov
 

What's hot (20)

PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
 
Droidcon ES '16 - How to fail going offline
Droidcon ES '16 - How to fail going offlineDroidcon ES '16 - How to fail going offline
Droidcon ES '16 - How to fail going offline
 
The Screenplay Pattern: Better Interactions for Better Automation
The Screenplay Pattern: Better Interactions for Better AutomationThe Screenplay Pattern: Better Interactions for Better Automation
The Screenplay Pattern: Better Interactions for Better Automation
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop Applications
 
vJUG - The JavaFX Ecosystem
vJUG - The JavaFX EcosystemvJUG - The JavaFX Ecosystem
vJUG - The JavaFX Ecosystem
 
Idiomatic gradle plugin writing
Idiomatic gradle plugin writingIdiomatic gradle plugin writing
Idiomatic gradle plugin writing
 
Basic Gradle Plugin Writing
Basic Gradle Plugin WritingBasic Gradle Plugin Writing
Basic Gradle Plugin Writing
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
 
Architecting your GWT applications with GWT-Platform - Lesson 02
Architecting your GWT applications with GWT-Platform - Lesson 02Architecting your GWT applications with GWT-Platform - Lesson 02
Architecting your GWT applications with GWT-Platform - Lesson 02
 
Legacy Dependency Kata v2.0
Legacy Dependency Kata v2.0Legacy Dependency Kata v2.0
Legacy Dependency Kata v2.0
 
Legacy Code Kata v3.0
Legacy Code Kata v3.0Legacy Code Kata v3.0
Legacy Code Kata v3.0
 
Test-Driven Development in React with Cypress
Test-Driven Development in React with CypressTest-Driven Development in React with Cypress
Test-Driven Development in React with Cypress
 
Супер быстрая автоматизация тестирования на iOS
Супер быстрая автоматизация тестирования на iOSСупер быстрая автоматизация тестирования на iOS
Супер быстрая автоматизация тестирования на iOS
 
Jenkins Evolutions - JEEConf 2012
Jenkins Evolutions - JEEConf 2012Jenkins Evolutions - JEEConf 2012
Jenkins Evolutions - JEEConf 2012
 
One commit, one release. Continuously delivering a Symfony project.
One commit, one release. Continuously delivering a Symfony project.One commit, one release. Continuously delivering a Symfony project.
One commit, one release. Continuously delivering a Symfony project.
 
Continous Delivering a PHP application
Continous Delivering a PHP applicationContinous Delivering a PHP application
Continous Delivering a PHP application
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
Gradle 3.0: Unleash the Daemon!
Gradle 3.0: Unleash the Daemon!Gradle 3.0: Unleash the Daemon!
Gradle 3.0: Unleash the Daemon!
 

Similar to Behavioural Driven Development in Zf2

Testing of javacript
Testing of javacriptTesting of javacript
Testing of javacript
Lei Kang
 

Similar to Behavioural Driven Development in Zf2 (20)

Let your tests drive your code
Let your tests drive your codeLet your tests drive your code
Let your tests drive your code
 
3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API - 3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API -
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
 
Testing of javacript
Testing of javacriptTesting of javacript
Testing of javacript
 
Real world Webapp
Real world WebappReal world Webapp
Real world Webapp
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStorm
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)
 
Increase testability with code seams
Increase testability with code seamsIncrease testability with code seams
Increase testability with code seams
 
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
 
Coding Naked 2023
Coding Naked 2023Coding Naked 2023
Coding Naked 2023
 
不只自動化而且更敏捷的Android開發工具 gradle mopcon
不只自動化而且更敏捷的Android開發工具 gradle mopcon不只自動化而且更敏捷的Android開發工具 gradle mopcon
不只自動化而且更敏捷的Android開發工具 gradle mopcon
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroJoomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
 
Agile mobile
Agile mobileAgile mobile
Agile mobile
 
Up and Running with Angular
Up and Running with AngularUp and Running with Angular
Up and Running with Angular
 
Commencer avec le TDD
Commencer avec le TDDCommencer avec le TDD
Commencer avec le TDD
 
Codeception introduction and use in Yii
Codeception introduction and use in YiiCodeception introduction and use in Yii
Codeception introduction and use in Yii
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

Behavioural Driven Development in Zf2