tdd with phpspec2

76
TDD with phpspec2

Upload: anton-serdyuk

Post on 27-Aug-2014

1.831 views

Category:

Software


10 download

DESCRIPTION

 

TRANSCRIPT

Page 1: TDD with phpspec2

TDD with phpspec2

Page 2: TDD with phpspec2

Who I am• Anton Serdyuk

• email/jabber: [email protected]

• skype: serdyuk.anton

• twitter: @anton_serdyuk

• facebook: anton.serdyuk

Page 3: TDD with phpspec2

Overview

Page 4: TDD with phpspec2

Auto-tests forms

• Functional tests (+BDD)

• Integration tests

• Unit tests

Page 5: TDD with phpspec2

Functional tests

• Use full deployed application instance

• Test business expectations

• Use the same UI interactions as real user

Page 6: TDD with phpspec2

Functional tests with phpunit example

Page 7: TDD with phpspec2

BDD (or story BDD)

• Story-driven functional tests

• BDD ⊂ Functional tests

Page 8: TDD with phpspec2

BDD with behat example

Page 9: TDD with phpspec2

Integration tests

• Use integrated parts of applications

• Can use database, filesystem etc.

• Can test API, Doctrine repositories etc.

Page 10: TDD with phpspec2

Integration tests with PHPUnit example

Page 11: TDD with phpspec2

Unit tests

• Use small parts of code

• Can not use database

• Can not use filesystem

• Extremely fast

Page 12: TDD with phpspec2

Consider using appropriate tools for testsphpunit behat phpspec codeception

Functional + + +

Integration + +(sometimes) +

Unit + + +

Page 13: TDD with phpspec2

My personal preference

• behat for functional and API integration tests

• PHPUnit for integration tests

• phpspec for unittest

Page 14: TDD with phpspec2

phpspec basics

Page 15: TDD with phpspec2

Installation

Page 16: TDD with phpspec2

Create testfor class Wow\Hero

Page 17: TDD with phpspec2

Run testsphpspec will create classes for you

Page 18: TDD with phpspec2

Run testsgenerated class Wow\Hero

Page 19: TDD with phpspec2

Run testsRun tests again, now with generated class

Page 20: TDD with phpspec2

Matchers (assertions)Every hero has level

Page 21: TDD with phpspec2

Matchersphpspec will generate code for you

Page 22: TDD with phpspec2

Matchersphpspec will generate code for you

Page 23: TDD with phpspec2

MatchersRed

Page 24: TDD with phpspec2

MatchersGreen

Page 25: TDD with phpspec2

MatchersRefactor

Page 26: TDD with phpspec2

Matchers• Identity (return, be, equal, beEqualTo) - ===

• Comparison (beLike) - ==

• Throw (throw -> during) - exceptions

• Type (beAnInstanceOf etc.) - type

• Custom matchers

• http://www.phpspec.net/cookbook/matchers.html

Page 27: TDD with phpspec2

MatchersUse source code for more matchers

Page 28: TDD with phpspec2

Exception MatchersGenerate

Page 29: TDD with phpspec2

Exception MatchersGenerate

Page 30: TDD with phpspec2

Exception MatchersGenerate

Page 31: TDD with phpspec2

Exception MatchersRed

Page 32: TDD with phpspec2

Exception MatchersGreen

Page 33: TDD with phpspec2

MocksEvery Hero has race and class

Page 34: TDD with phpspec2

MocksGenerate

Page 35: TDD with phpspec2

MocksGenerate

Page 36: TDD with phpspec2

MocksGenerate

Page 37: TDD with phpspec2

MocksRed

Page 38: TDD with phpspec2

MocksGreen

Page 39: TDD with phpspec2

MocksGreen (whoops!)

Page 40: TDD with phpspec2

MocksGreen

Page 41: TDD with phpspec2

MocksRefactor - let() / letgo()

Page 42: TDD with phpspec2

MocksRefactor - let() / letgo()

Page 43: TDD with phpspec2

MocksRefactor

Page 44: TDD with phpspec2

Predictions

Page 45: TDD with phpspec2

PredictionsRed

Page 46: TDD with phpspec2

PredictionsGreen

Page 47: TDD with phpspec2

PredictionsRed

Page 48: TDD with phpspec2

PredictionsGreen

Page 49: TDD with phpspec2

Advanced topics

Page 50: TDD with phpspec2

Nested Object Matchers

Page 51: TDD with phpspec2

Public Property MockSometimes you want to mock public properties

Page 52: TDD with phpspec2

Promises and Predictions Order1) Promises -> 2) Predictions -> 3) Call

Page 53: TDD with phpspec2

Promises and Predictions Order1) Promises -> 2) Predictions -> 3) Call

Page 54: TDD with phpspec2

Promises and Predictions Order1) Promises -> 2) Predictions -> 3) Call

Page 55: TDD with phpspec2

Predictions Argument Wildcarding

Page 56: TDD with phpspec2

Predictions Argument Wildcarding

• Argument::exact($value)

• Argument::type($typeOrClass)

• Argument::which($method, $value)

• Argument::that(callback)

• Argument::any()

• Argument::cetera()

• https://github.com/phpspec/prophecy#arguments-wildcarding

Page 57: TDD with phpspec2

Array MocksGenerate

Page 58: TDD with phpspec2

Array MocksGenerate

Page 59: TDD with phpspec2

Array MocksRed

Page 60: TDD with phpspec2

Array MocksGreen (whoops!)

Page 61: TDD with phpspec2

Array MocksGreen

Page 62: TDD with phpspec2

Get Mock Object

Page 63: TDD with phpspec2

Get Mock Object

Page 64: TDD with phpspec2

Get Mock ObjectGenerate

Page 65: TDD with phpspec2

Get Mock ObjectGenerate (whoops!)

Page 66: TDD with phpspec2

Get Mock ObjectThis is PHP, baby

???

Page 67: TDD with phpspec2

Get Mock ObjectRight way

Page 68: TDD with phpspec2

Best PracticesIMHOs

Page 69: TDD with phpspec2

Do not use TDD in simple CRUD applications (IMHO)

• High cost

• Slow (in terms of development time)

• Zero profit

• Use functional and integration tests instead

Page 70: TDD with phpspec2

Do not mock 3rd party code

• Large complicated tests

• You actually test not how your code works with this library, but how your code works with your understanding of this library

Page 71: TDD with phpspec2

Do not mock 3rd party codeUse interfaces instead of 3rd party dependent code

Page 72: TDD with phpspec2

Consider using appropriate DI library

• symfony/di and Pimple are not well suited for TDD because of large amount of small classes

• you must define rule for every class, which is very boring

• There are some DI containers which will create most objects without explicitly defined rules by just reading constructor arguments type hints through reflection

Page 73: TDD with phpspec2

Consider using appropriate DI library

• zend-di

• Laravel IoC Container - http://laravel.com/docs/ioc

• PHP-DI - http://php-di.org/

Page 74: TDD with phpspec2

phpspec at CI

Page 75: TDD with phpspec2

phpspec junit formatter

Page 76: TDD with phpspec2

Thank you!Questions?