behavioural driven development in zf2

92
Behavior-Driven Development with Zend Framework 2 Zend Framework Day – Turin, Italy – 07/02/2014

Upload: david-contavalli

Post on 08-May-2015

1.370 views

Category:

Technology


1 download

DESCRIPTION

A quick introduction on how to setup a zf2 project using Behat and Phpspec.

TRANSCRIPT

Page 1: Behavioural Driven Development in Zf2

Behavior-Driven Development with Zend Framework 2

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

Page 2: Behavioural Driven Development in Zf2

DAVID CONTAVALLI

@mauipipe

2

Page 3: Behavioural Driven Development in Zf2

3

CLEAN CODE ADEPT BDD FANATIC

Page 4: Behavioural Driven Development in Zf2

4

CLEAN CODE ADEPT BDD FANATIC

Page 5: Behavioural Driven Development in Zf2

WHAT IS BDD?

Page 6: Behavioural Driven Development in Zf2

TDD Evolution

6

Page 7: Behavioural Driven Development in Zf2

1. You are not allowed to write any production code

unless it is to make a failing unit test pass.

7

SAME RULES

Page 8: Behavioural Driven Development in Zf2

1. You are not allowed to write any production code

unless it is to make a failing unit test pass.

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

8

SAME RULES

Page 9: Behavioural Driven Development in Zf2

1. You are not allowed to write any production code

unless it is to make a failing unit test pass.

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

3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

9

SAME RULES

Page 10: Behavioural Driven Development in Zf2

WHAT’S THE DIFFERENCE?

Page 11: Behavioural Driven Development in Zf2

TDD STARTS FROM COMPONENTS

Page 12: Behavioural Driven Development in Zf2

12

CREATE THE TEST

private $calculator; function setUp(){ $this->calculator = new Calculator(); } function testSumTwoPositive(){ $expectedResult = 8; $result = $this->calculator(6,2); assertEquals($expectedResult,$result); }

Page 13: Behavioural Driven Development in Zf2

13

Page 14: Behavioural Driven Development in Zf2

14

WRITE QUICK, TEST VALIDATING CODE

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

Page 15: Behavioural Driven Development in Zf2

15

Page 16: Behavioural Driven Development in Zf2

16

REFACTOR

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

Page 17: Behavioural Driven Development in Zf2

17

Page 18: Behavioural Driven Development in Zf2

BDD STARTS FROM AN EXAMPLE

Page 19: Behavioural Driven Development in Zf2

19

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

Page 20: Behavioural Driven Development in Zf2

20

A COMMON LANGUAGE

STAKEHOLDER

Page 21: Behavioural Driven Development in Zf2

21

DEVELOPER

STAKEHOLDER

A COMMON LANGUAGE

Page 22: Behavioural Driven Development in Zf2

22

DEVELOPER

STAKEHOLDER USER

A COMMON LANGUAGE

Page 23: Behavioural Driven Development in Zf2

LIVING DOCUMENTATION

Page 24: Behavioural Driven Development in Zf2

HOW IS STORY CODE RELATED?

Page 25: Behavioural Driven Development in Zf2

25

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

Page 26: Behavioural Driven Development in Zf2

26

Interpreted in many languages

Page 27: Behavioural Driven Development in Zf2

27

GHERKIN

Page 28: Behavioural Driven Development in Zf2

28

GHERKIN

Page 29: Behavioural Driven Development in Zf2

WHAT IS BEHAT?

Page 30: Behavioural Driven Development in Zf2
Page 31: Behavioural Driven Development in Zf2

31

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(); }

Page 32: Behavioural Driven Development in Zf2

32

EXECUTION

Page 33: Behavioural Driven Development in Zf2

USERS WILL NOT READ STORIES

Page 34: Behavioural Driven Development in Zf2

THEY WILL USE AN UI

Page 35: Behavioural Driven Development in Zf2

BROWSER

Page 36: Behavioural Driven Development in Zf2

36

HOW TO TEST BROWSER WITH BEHAT?

Page 37: Behavioural Driven Development in Zf2

HERE IT COMES, MINK!

Page 38: Behavioural Driven Development in Zf2

38

GHERKIN

+

Page 39: Behavioural Driven Development in Zf2

39

GOUTTE • Headless Browser • No Javascript • Really fast

Web Acceptance Test

Page 40: Behavioural Driven Development in Zf2

40

GOUTTE • Headless Browser • No Javascript • Really fast

SELENIUM • Can test Javascript • Really slow • Use Firefox as emulator

Web Acceptance Test

Page 41: Behavioural Driven Development in Zf2

41

GOUTTE • Headless Browser • No Javascript • Really fast

SELENIUM • Can test Javascript • Really slow • Use Firefox as emulator

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

Web Acceptance Test

Page 42: Behavioural Driven Development in Zf2

42

GOUTTE • Headless Browser • No Javascript • Really fast

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

Web Acceptance Test

Page 43: Behavioural Driven Development in Zf2

43

/** * @When /^I press ‘’(/[^’’]*)’’ button$/ **/ public function iPressButton($buttonName) { $this->pressButton($buttonName); }

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

Page 44: Behavioural Driven Development in Zf2

44

+

How to install....

Page 45: Behavioural Driven Development in Zf2

45

"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" }

Page 46: Behavioural Driven Development in Zf2

46

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" }

Page 47: Behavioural Driven Development in Zf2

47

default : extensions : Behat\MinkExtension\Extension : base_url : 'http://example.com' goutte : ~

behat.yml

Mink Setup

Page 48: Behavioural Driven Development in Zf2

48

+

+

Page 49: Behavioural Driven Development in Zf2

49

private static app; /** * @BeforeSuite **/ public static function initializeZf(){ If(self::$zendApp === null){ $path = __DIR__ .’/../../config/application.config.php’; self::app = \Zend\Mvc\Application::init(require $path); } }

Static method

Page 50: Behavioural Driven Development in Zf2

ZF2 EXTENSION

Page 51: Behavioural Driven Development in Zf2

51

"require-dev":{ ................ "mvlabs/zf2behat-extension":"dev-master" },

Page 52: Behavioural Driven Development in Zf2

52

default : extensions : MvLabs\Zf2Extension\Zf2Extension : config : config/application.config.php module :

behat.yml

Zf2 Behat Extension

Page 53: Behavioural Driven Development in Zf2

53

bin/behat --init ‘’module name’’ run from console

Zf2 Behat Extension

Page 54: Behavioural Driven Development in Zf2

54

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

Implements Zf2AwareContextInterface

Feature Context

Page 55: Behavioural Driven Development in Zf2

WE STILL NEED TO TEST OUR COMPONENTS

Page 56: Behavioural Driven Development in Zf2

56

GHERKIN

+

Page 57: Behavioural Driven Development in Zf2

57

WHY PHPSPEC?

Page 58: Behavioural Driven Development in Zf2

BDD BASED ON BEHAVIOUR DESCRIPTION

Robert C. Martin

Page 59: Behavioural Driven Development in Zf2

59

private $calculator; function setUp(){ $this->calculator = new Calculator(); } function testSumTwoPositive(){ $expectedResult = 8; $result = $this->calculator(6,2); assertEquals($expectedResult,$result); }

PHPUnit focus on testing code

Page 60: Behavioural Driven Development in Zf2

60

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

PHPSpec focus on code behavior

Page 61: Behavioural Driven Development in Zf2

61

"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

Subject under Specification

Page 62: Behavioural Driven Development in Zf2

62

Developer Tool Vs Testing Framework

Page 63: Behavioural Driven Development in Zf2

63

bin/phpspec desc ‘’Application\Controller\IndexController’’

Autogenerates classes & methods

Page 64: Behavioural Driven Development in Zf2

PROMOTES CLEAN CODE

Page 65: Behavioural Driven Development in Zf2

65

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

Demeter Law's Violation

Page 66: Behavioural Driven Development in Zf2

66

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

Impossible

Page 67: Behavioural Driven Development in Zf2

67

+

Add PHPSpec to Zf2

Page 68: Behavioural Driven Development in Zf2

68

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

Page 69: Behavioural Driven Development in Zf2

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

69

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

Add a single module

Page 70: Behavioural Driven Development in Zf2

70

"require-dev":{ ........... "phpspec/phpspec" : "2.0.*@dev" }, "config": { "bin-dir": "bin/" }, "autoload": { "psr-0" { "Application" : " module/Application/src", "Calculator" : "module/Calculator/src" } }

Add more modules

Page 71: Behavioural Driven Development in Zf2

71

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

Create phpspec.yml in project root

Page 72: Behavioural Driven Development in Zf2

72

What to test with PHPSpec?

Page 73: Behavioural Driven Development in Zf2

73

1.Model Logic

What to test with PHPSpec?

Page 74: Behavioural Driven Development in Zf2

74

1.Model Logic

2.Factories

What to test with PHPSpec?

Page 75: Behavioural Driven Development in Zf2

75

1.Model Logic

2.Factories

3.Validation

What to test with PHPSpec?

Page 76: Behavioural Driven Development in Zf2

76

1.Verbose Stories

2.Using Mink to test REST calls

3.Testing every possible usages combination

4.Fixture loading within Context

Behat bad practices

Page 77: Behavioural Driven Development in Zf2

WHY TESTING?

Page 78: Behavioural Driven Development in Zf2

IT’S A TREND

Page 79: Behavioural Driven Development in Zf2
Page 80: Behavioural Driven Development in Zf2

RELAX

Page 81: Behavioural Driven Development in Zf2

SOMEONE WILL READ YOUR CODE

Page 82: Behavioural Driven Development in Zf2

IT COULD BE YOU SOME DAY

Page 83: Behavioural Driven Development in Zf2

YOUR COLLEAGUES

Page 84: Behavioural Driven Development in Zf2

84

OR

Page 85: Behavioural Driven Development in Zf2

A VIOLENT PSYCHOPATH WHO KNOWS WHERE YOU LIVE

Page 86: Behavioural Driven Development in Zf2
Page 87: Behavioural Driven Development in Zf2

Thank you for your attention

David Contavalli @mauipipe

Page 88: Behavioural Driven Development in Zf2

QUESTIONS?

Page 89: Behavioural Driven Development in Zf2

@mauipipe [email protected]

Page 90: Behavioural Driven Development in Zf2

90

Some readings

Page 91: Behavioural Driven Development in Zf2

Credits

91

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

Page 92: Behavioural Driven Development in Zf2

David Contavalli @mauipipe