Slide 1

Slide 1 text

Behaviour Driven Development with PHPSpec Zvonimir Spajic

Slide 2

Slide 2 text

Behaviour Driven Development

Slide 3

Slide 3 text

Behaviour Driven Development technique derived from test-first development

Slide 4

Slide 4 text

Behaviour Driven Development focus on behaviour not implementation technique derived from test-first development

Slide 5

Slide 5 text

It's using examples to talk through how an application behaves... And having conversations about those examples. Dan North

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

a tool which can help you write clean and working PHP code using Behaviour Driven Development

Slide 8

Slide 8 text

a tool which can help you write clean and working PHP code using Behaviour Driven Development at the spec level

Slide 9

Slide 9 text

{ "require-dev": { "phpspec/phpspec": "^4.0" }, "config": { "bin-dir": "bin" }, "autoload": {"psr-0": {"": "src"}} }

Slide 10

Slide 10 text

requirement:

Slide 11

Slide 11 text

Dog requirement:

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

shouldHaveType(Dog::class); } }

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Slide 20

Slide 20 text

behaviour:

Slide 21

Slide 21 text

Dog can make a sound behaviour:

Slide 22

Slide 22 text

makeSound()->shouldBe('Vuf Vuf'); } }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Slide 32

Slide 32 text

Matchers describe how an object should behave

Slide 33

Slide 33 text

// Identity matchers $this->getRating()->shouldBe(5); $this->getTitle()->shouldBeEqualTo("Star Wars"); $this->getReleaseDate()->shouldReturn(233366400); $this->getDescription()->shouldEqual("Inexplicably popular children's film"); // Comparison Matcher $this->getRating()->shouldBeLike('5'); // Type Matcher $this->shouldHaveType('Movie'); $this->shouldReturnAnInstanceOf('Movie'); $this->shouldBeAnInstanceOf('Movie'); $this->shouldImplement('Movie'); // Count Matcher $this->getDirectors()->shouldHaveCount(1); // IterableContain Matcher¶ $this->getCast()->shouldContain('Jane Smith'); // Throw Matcher $this->shouldThrow('\InvalidArgumentException')->duringSetRating(-3); $this->shouldThrow('\InvalidArgumentException')->during('setRating', array(-3)); $this->shouldThrow('\InvalidArgumentException')->duringInstantiation(); // ...

Slide 34

Slide 34 text

behaviour:

Slide 35

Slide 35 text

Dog can greet a Person behaviour:

Slide 36

Slide 36 text

Stubs describe how we interact with object we query

Slide 37

Slide 37 text

name()->willReturn('Mike'); $this->greet($person)->shouldBe('Hello Mike, Vuf Vuf'); } }

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

name()->willReturn('Mike'); $this->greet($person)->shouldBe('Hello Mike, Vuf Vuf'); } }

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Slide 52

Slide 52 text

Slide 53

Slide 53 text

name() . 'Vuf Vuf'; } }

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

spec:

Slide 57

Slide 57 text

Person has a name spec:

Slide 58

Slide 58 text

beConstructedWith('Mike'); $this->name()->shouldBe('Mike'); } }

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Slide 66

Slide 66 text

name = $name; } public function name() { return $this->name; } }

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

spec:

Slide 70

Slide 70 text

Person can change it's name spec:

Slide 71

Slide 71 text

beConstructedWith('Mike'); $this->renameTo ('Mary'); $this->name()->shouldBe('Mary'); } }

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

name = $name; } public function name() { return $this->name; } public function renameTo($argument1) { // TODO: write logic here } }

Slide 77

Slide 77 text

name = $name; } public function name() { return $this->name; } public function renameTo(string $name) { $this->name = $name; } }

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

beConstructedWith('Mike'); $this->name()->shouldBe('Mike'); } function it_returns_new_name_if_renamed() { $this->beConstructedWith('Mike'); $this->renameTo ('Mary'); $this->name()->shouldBe('Mary'); } }

Slide 81

Slide 81 text

beConstructedWith('Mike'); } function it_returns_the_name_it_was_constructed_with() { $this->name()->shouldBe('Mike'); } function it_returns_new_name_if_renamed() { $this->renameTo ('Mary'); $this->name()->shouldBe('Mary'); } }

Slide 82

Slide 82 text

behaviour:

Slide 83

Slide 83 text

When a Dog greets a Person, it should be logged behaviour:

Slide 84

Slide 84 text

Mocks and Spies describe how we interact with object we command

Slide 85

Slide 85 text

beConstructedWith($logger); } // ... function it_logs_the_greeting(Named $named, Logger $logger) { $named->name()->willReturn('Mike'); $logger->log('Hello Mike, Vuf Vuf')->shouldBeCalled(); $this->greet($named); } }

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

name() . ', Vuf Vuf'; return $greeting; } }

Slide 91

Slide 91 text

logger = $logger; } public function makeSound() { return 'Vuf Vuf'; } public function greet(Named $named) { $greeting = 'Hello ' . $named->name() . ', Vuf Vuf'; $this->logger->log($greeting); return $greeting; } }

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

Resources Driving quality with PhpSpec by Ciaran McNulty phpspec: Testing... *Designing* with a Bite Introducing BDD

Slide 96

Slide 96 text

Thank You!