Slide 1

Slide 1 text

CONCURRENT TEST FRAMEWORKS Andrea Giuliano @bit_shark

Slide 2

Slide 2 text

Andrea Giuliano @bit_shark andreagiuliano.it $WHOAMI

Slide 3

Slide 3 text

PHP TEST FRAMEWORKS

Slide 4

Slide 4 text

WHICH ONE SHOULD I USE? CONCENTRATE AND ASK AGAIN

Slide 5

Slide 5 text

WHO SAID “ONE”?

Slide 6

Slide 6 text

IS THE TOOL THE PROBLEM?

Slide 7

Slide 7 text

LET’S START FROM THE BEGINNING

Slide 8

Slide 8 text

TEST DRIVEN DEVELOPMENT Red Green Refactor

Slide 9

Slide 9 text

EMERGENT DESIGN

Slide 10

Slide 10 text

TDD AS A PROBLEM

Slide 11

Slide 11 text

TDD AS A PROBLEM class VideoGalleryTest extends \PHPUnit_Framework_TestCase { public function testUploadVideo() { $videoGallery = new VideoGallery(); $videoGallery->upload('path/on/my/awesome/file.mp4'); $this->assertTrue(is_array($videoGallery->getVideos())); $this->assertCount(1, $videoGallery->getVideos()); $this->assertInstanceOf( Video::class, $videoGallery->getFirstVideo()); } }

Slide 12

Slide 12 text

TDD AS A PROBLEM class VideoGalleryTest extends \PHPUnit_Framework_TestCase { public function testUploadVideo() { $videoGallery = new VideoGallery(); $videoGallery->upload('path/on/my/awesome/file.mp4'); $this->assertTrue(is_array($videoGallery->getVideos())); $this->assertCount(1, $videoGallery->getVideos()); $this->assertInstanceOf( Video::class, $videoGallery->getFirstVideo()); } } testing internals

Slide 13

Slide 13 text

IS VERSUS DOES

Slide 14

Slide 14 text

IT’S ALL ABOUT BEHAVIOUR

Slide 15

Slide 15 text

DESCRIBING A STORY Feature: As a [role] I want [feature] So that [benefit] Scenario: scenario description Given [context] When [event] Then [outcome]

Slide 16

Slide 16 text

A DIFFERENT CYCLE Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition

Slide 17

Slide 17 text

A DIFFERENT CYCLE Describe the behaviours for ‘verbs’ and ‘nouns’ Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition

Slide 18

Slide 18 text

A DIFFERENT CYCLE Describe the behaviours for ‘verbs’ and ‘nouns’ Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods

Slide 19

Slide 19 text

A DIFFERENT CYCLE Describe the behaviours for ‘verbs’ and ‘nouns’ Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods

Slide 20

Slide 20 text

THE TOOL composer require behat/behat --dev

Slide 21

Slide 21 text

DESCRIBING A STORY Scenario: Getting the total amount for a conference ticket Given a ticket for the "PHP Day" conference priced EUR100 was added to the marketplace When a user adds the "PHP Day" ticket from the marketplace to the user's shopping cart Then the overall cart price should be EUR122

Slide 22

Slide 22 text

RUN THE STORY $ bin/behat --append-snippets

Slide 23

Slide 23 text

VERBS, NOUNS, BEHAVIOUR DISCOVERING /** * @Given a ticket for the :conference conference priced EUR:price * was added to the marketplace */ public function aTicketForTheConferencePriced($conference, $price) { $money = Money::EUR((int) $price); $ticket = Ticket::forConferencePriced($conference, $money); $this->marketPlace->add($ticket); }

Slide 24

Slide 24 text

/** * @When a user adds the ":conference" ticket * from the marketplace to the shopping cart */ public function aUserAddTheTicketToTheShoppingCart($conference) { $this->user->addTicketToCart($conference, $this->marketPlace); } VERBS, NOUNS, BEHAVIOUR DISCOVERING

Slide 25

Slide 25 text

/** * @Then the overall cart price should be EUR:price */ public function theOverallCartPriceShouldBe($price) { $money = Money::EUR((int) $price); $amount = $this->user->shoppingCart()->total(); if (!$amount->equals($money)) { throw new \Exception("Cart amount is not " . $price); } } VERBS, NOUNS, BEHAVIOUR DISCOVERING

Slide 26

Slide 26 text

DESCRIBE THE BEHAVIOUR Describe the behaviours for ‘verbs’ and ‘nouns’ Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods

Slide 27

Slide 27 text

THE TOOLS

Slide 28

Slide 28 text

DESCRIBING TICKET CREATION class TicketTest extends \PHPUnit_Framework_TestCase { /** * @test */ function should_create_a_ticket_for_conference_priced() { $price = Money::EUR(1000); $ticket = Ticket::forConferencePriced( 'An awesome conference', $price ); $this->assertInstanceOf(Ticket::class, $ticket); } }

Slide 29

Slide 29 text

OBJECT CREATION Describe the behaviours for ‘verbs’ and ‘nouns’ Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods

Slide 30

Slide 30 text

THE TICKET OBJECT class Ticket implements TicketInterface { private $event; private $price; private function __construct($event, Money $price) { $this->event = $event; $this->price = $price; } public static function forConferencePriced($conference, Money $price) { return new Ticket($conference, $price); } }

Slide 31

Slide 31 text

GOING UP Describe the behaviours for ‘verbs’ and ‘nouns’ Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods

Slide 32

Slide 32 text

SHOPPING CART SPECIFICATION class ShoppingCartSpec extends ObjectBehavior { function it_should_return_the_amount_with_vat(TicketInterface $ticket) { $ticket->price()->willReturn(Money::EUR(100.00)); $this->addTicket($ticket); $expectedMoney = Money::EUR(122.00); $this->total()->shouldBeLike($expectedMoney); } }

Slide 33

Slide 33 text

THE SHOPPING CART OBJECT class ShoppingCart { public function total() { $total = Money::EUR(0); foreach ($this->tickets as $ticket) { $ticketPriceWithVat = Money::multiply( $ticket->price(), self::VAT ); $total = Money::add($total, $ticketPriceWithVat->amount()); } return $total; } }

Slide 34

Slide 34 text

GOING UP Describe the behaviours for ‘verbs’ and ‘nouns’ Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods

Slide 35

Slide 35 text

THE GREEN SATISFACTION

Slide 36

Slide 36 text

MOVING OUTWARDLY Domain Application Presentation

Slide 37

Slide 37 text

THE FORTRESS

Slide 38

Slide 38 text

joind.in/14553 Please rate the talk

Slide 39

Slide 39 text

joind.in/14553 Please rate the talk

Slide 40

Slide 40 text

REFERENCES Assets: https://www.flickr.com/photos/levork/4966756896 https://www.flickr.com/photos/jenny-pics/7960912752 https://www.flickr.com/photos/joel_baker/12484614505 https://www.flickr.com/photos/mike-f/8628898387 https://www.flickr.com/photos/mike-f/8628898387 https://www.flickr.com/photos/michaeljzealot/6485009571 The RSpec book, Behaviour Driven Development with RSpec, Cucumber, and Friends - D. Chelimsky Konstantin Kudryashov aka Everzet - Introducing modeling by example everzet.com/post/99045129766/introducing-modelling-by-example a special thanks to Giulio De Donato aka liuggio