51
SPEC BDD IN PHP @cakper

SpecBDD in PHP

Embed Size (px)

DESCRIPTION

SPUG Meetup #4 13/06/2013 http://spug.pl/

Citation preview

Page 1: SpecBDD in PHP

SPEC BDDIN PHP

@cakper

Page 2: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

Software Engineer @SensioLabsUKSilesia PHP User Group // Spug.pl#symfony-pl // Symfonylab.pl

@CAKPER

Page 3: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

WHAT IS TEST?

Page 4: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

WHY DO WE TEST?

Page 5: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

HOW DO WE TEST?

Page 6: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

WE MAKE MISTAKESSO WE HAVE TO TEST

Page 7: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

TESTDRIVENDEVELOPMENT

Kent Beck // 2003Extreme Programming // 1999

Page 8: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #1

How to test something that does not exist?

Page 9: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #1

TEST == SpecificationDrivenDevelopment

Page 10: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #1

Communication is our Problem

Not testing itself

Page 11: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

BEHAVIOURDRIVENDEVELOPMENT

Dan North // 2006

Page 12: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

BDD

NamingConvenTionstools

Better {

Page 13: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

BDD

= TDD v2.0‘TDD DONE RIGHT’

Page 14: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #2

Writing specificationstakes time

Time is a cost

Page 15: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

you can learn it

Page 16: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

DELIBERATE PRACTICE

Pair ProgrammingCode KataCode ClubCode Retreat

Page 17: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #3

Why are you sure that your test is correct?

Page 18: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

TDD CYCLE

RED

GREENREFACTOR

Page 19: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

3 LAWS OF TDD

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.

Page 20: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #4

When and how to Refactor?

Page 21: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

‘Refactoring w/o tests = Refucktoring’

PROBLEM #4

Page 22: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

4 RULES OF SIMPLE DESING

1.Passes all the tests.

2.Express every idea we need to express.

3.Contains no duplication.

4.Minimized the number of classes, methods and other moving parts.

Page 23: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #5

How to test dependencies?

Page 24: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

DO NOT TEST...

Page 25: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

TEST DOUBLES

DUMMiesSTUBSMOCKSSPIES

Page 26: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

Mocking should be easy

very easy

Page 27: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #6

Code Coverage

Page 28: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #6

Useful to find dead code ;)

Page 29: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #7

STORY BDDvs

Spec BDD

Page 30: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

STORY BDD

description of business-targeted

application behavior

Page 31: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

SPEC BDD

specification for low-level implementation

Page 32: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PROBLEM #8

PHP UNIT

By Sebastian Bergmann©

Page 33: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PHP Spec 2

Page 34: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PHP SPEC 2

Framework Spec BDD@_md & @everzetProphecy

Page 35: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PHP SPEC 2

EASY to useTDD-Cycle orientedBehaviour focused

Page 36: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PHP SPEC 2

Test Case

Specification

Page 37: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PHP SPEC 2

Test

example

Page 38: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PHP SPEC 2

Assert

Expectation

Page 39: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

PHP SPEC 2

class MarkdownSpec extends ObjectBehavior{ function it_is_initializable() { $this->shouldHaveType('Markdown'); }}

Page 40: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

MATCHERS

Identity (===):shouldReturn()shouldBe()shouldEqual()shouldBeEqualTo()

Page 41: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

MATCHERS

Comparison (==):shouldBeLike()

Page 42: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

MATCHERS

Throw:shouldThrow(‘\Exception’)->duringSomeMethod()

Page 43: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

MATCHERS

Type:shouldBeAnInstanceOf()shouldReturnAnInstanceOf()shouldHaveType()

Page 44: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

MATCHERS

Object State:shouldHave**()

Page 45: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

TEST DOUBLES

/** * @param Markdown\Stream $stream */function it_adds_a_end_of_list_to_markup($stream){ $stream->getNextLine()->willReturn(""); $this->format(" * Hi, there", $stream) ->shouldReturn("</li></ul>");}

Page 46: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

LET & LETGOfunction let($die)

{ $die->beADoubleOf('Die'); $this->beConstructedWith($die);}

function it_live_and_let_die($die){ $this->liveAndLet()->shouldReturn($die);}

function letgo(){}

Page 47: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

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

Page 48: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

Demo

Page 49: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

Questions?

Page 50: SpecBDD in PHP

13/06/13 GliwiceSpec BDD IN PHP

WE ARE HIRING!

Software EngineerFront-end EngineerSoftware Engineer in testPRoject managerBusiness analyst

www.sensiolabs.co.ukwww.inviqa.com

Page 51: SpecBDD in PHP

THANK YOU!

@cakper