SlideShare ist ein Scribd-Unternehmen logo
1 von 50
Downloaden Sie, um offline zu lesen
Behavior Driven
Development and
Laravel
1
About Me
• Marcus Moore
• San Diego, CA
• Diego Dev Group
• San Diego PHP User Group co-organizer
2
Overview
• My history with Behavior Driven Development
• Behavior Driven Development
• Behavior Driven Development and Laravel
3
My History with BDD
• When I worked at a small school
• Gathering requirements was easy!
4
My History with BDD
• Working for an agency...
• Requirement gathering got harder...
• Many clients with many communication styles
• "Who has solved this?"
5
Behavior Driven Development
• Extension of Test Driven Development (TDD)
• BDD works closely with Unit Testing
• BDD is not about testing
• Helps you build the right thing
6
Communication is hard
• Ambiguities arise
• Assumptions are made
• Rework becomes inevitable
7
BDD's Goals
• Close communication gap between all stakeholders using
real examples
• Establish a shared understanding of the desired outcome
• Eliminate "rework"
8
Three Steps
1. Discovery
2. Formulation
3. Automation
9
Discovery
• Most important part!
• Enables stakeholders to have focused conversations
• Fills knowledge gaps
• Develops an understanding of how the software should
function using real examples
10
Methods of Discovery
• Discovery Workshops
• Example Mapping
• Event Storming
• etc...
11
Discovery Workshops
• AKA the Three Amigos
• Short and frequent meetings
• Different people with different perspectives
12
Three Amigos
1. Business Person (Product Owner)
• Determines "what"
• Expresses the user stories
2. Developer
• Determines "how"
• Looks for details and potential roadblocks
3. Quality Assurance
• Come up with the "what if's"
• Tries to determine what will break
13
Discovery Results
• Complete shared understanding
• Concrete examples
14
Formulation
• Documentation of the concrete examples
• Written in natural language syntax
• Readable by humans and software
• .feature files
• Gherkin
15
Gherkin
• Tells the Story via a narrative
• Easy for non-technical people to read
16
Feature: Requesting rides
As a verified user
I want to request a ride
So that I can get to my destination on time
Rule: Unverified users cannot request rides
Rule: User cannot request ride for more than 5 people
Scenario: A user requests a ride for 4 people
Given I am a verified user
And there is a van with 5 seats nearby
And there is a car with 3 seats next to me
When I request a ride for 4 people
Then I should see I am being connected to the van
17
Given - Set the state of the world
Given I am a verified user
And there is a van with 5 seats nearby
And there is a car with 3 seats next to me
18
When - Interact with the application
When I request a ride for 4 people
19
Then - Check the outcome of the interaction
Then I should see I am being connected to the van
20
Feature: Requesting rides
As a verified user
I want to request a ride
So that I can get to my destination on time
Rule: Unverified users cannot request rides
Rule: User cannot request ride for more than 5 people
Scenario: A user requests a ride for 4 people
Given I am a verified user
And there is a van with 5 seats nearby
And there is a car with 3 seats next to me
When I request a ride for 4 people
Then I should see I am being connected to the van
21
Formulation Results
• Common terminology solidified
• Shared understanding is documented in feature files
• New people can understand the application
22
Automation
• Start to implement the features with BDD and TDD
• Double-Loop Workflow
• Implement the features with Behat
23
Behat
• The go-to BDD Framework for PHP
• Implementation of Cucumber
24
Laravel and Behat
25
Installation and Setup
• Install via Composer
• vendor/bin/behat !"init
• Add your feature files to /features
• vendor/bin/behat !"append-snippets
26
Feature: Requesting rides
As a verified user
I want to request a ride
So that I can get to my destination on time
Rule: Unverified users cannot request rides
Rule: User cannot request ride for more than 5 people
Scenario: A user requests a ride for 4 people
Given I am a verified user
And there is a van with 5 seats nearby
And there is a car with 3 seats next to me
When I request a ride for 4 people
Then I should see I am being connected to the van
27
/**
* @Given I am a verified user
"#
public function iAmAVerifiedUser()
{
throw new PendingException();
}
28
/**
* @Given I am a verified user
"#
public function iAmAVerifiedUser()
{
$this"$actingUser = factory(User"%class)
"$state('verified')
"$create();
$this"$userLocation = [
'latitude' "& '32.7',
'longitude' "& '-117.1',
];
}
29
class FeatureContext implements Context
{
public function !"construct()
{
}
}
30
class BaseContext extends TestCase implements Context
{
use CreatesApplication;
public function !"construct()
{
putenv('APP_ENV=testing');
parent!#!"construct();
$this!$setUp();
$this!$withoutExceptionHandling();
}
/** @BeforeScenario !%
public function before(BeforeScenarioScope $scope)
{
$this!$artisan('migrate:fresh');
}
}
31
/**
* @Given I am a verified user
"#
public function iAmAVerifiedUser()
{
$this"$actingUser = factory(User"%class)
"$state('verified')
"$create();
$this"$userLocation = [
'latitude' "& '32.7',
'longitude' "& '-117.1',
];
}
32
/**
* @And there is a van with :arg1 seats nearby
!"
public function thereIsAVanWithSeatsNearby($arg1)
{
throw new PendingException();
}
33
And there is a van with 5 seats nearby
34
/**
* @And there is a van with :arg1 seats nearby
!"
public function thereIsAVanWithSeatsNearby($arg1)
{
throw new PendingException();
}
35
/**
* @And there is a van with :numberOfSeats seats nearby
!"
public function thereIsAVanWithSeatsNearby($numberOfSeats)
{
$this!#createVan($numberOfSeats, $distanceFromUser = 3);
}
36
/**
* @And there is a car with :numberOfSeats seats next to me
!"
public function thereIsACarWithSeatsNextToMe($numberOfSeats)
{
$this!#createCar($numberOfSeats, $distanceFromUser = 1);
}
37
$this!"createVan($numberOfSeats, $distanceFromUser = 3);
$this!"createCar($numberOfSeats, $distanceFromUser = 1);
38
Scenario: A user requests a ride for 4 people
Given I am a verified user
And there is a van with 5 seats nearby
And there is a car with 3 seats next to me
When I request a ride for 4 people
Then I should see I am being connected to the van
39
Scenario: A user requests a ride for 4 people
Given I am a verified user
And there is a "van" with "5" seats "nearby"
And there is a "car" with "3" seats "next to me"
When I request a ride for 4 people
Then I should see I am being connected to the van
40
/**
* @Given there is a :arg1 with :arg2 seats :arg3
!"
public function thereIsAWithSeats($arg1, $arg2, $arg3)
{
throw new PendingException();
}
41
/**
* @Given there is a :typeOfVehicle with :numberOfSeats seats :distanceFromUser
!"
public function thereIsAWithSeatsNearby($typeOfVehicle, $numberOfSeats, $distanceFromUser)
{
$distance = 3;
if ($distanceFromUser !!# 'next to me') {
$distance = 1;
}
$this!$vehicles[$typeOfVehicle] = $this!$createVehicle($typeOfVehicle, $numberOfSeats, $distance);
}
42
/**
* @When I request a ride for :numberOfPeople people
!"
public function iRequestARideForPeople($numberOfPeople)
{
$this!#response = $this!#actingAs($this!#actingUser)
!#json(
'POST',
'/api/get-ride',
[
'location' !% $this!#userLocation,
'number_of_seats' !% $numberOfPeople
]
);
}
43
!" Controller does some work to match user and driver
!" Good time to TDD a service!
return response()!#json([
'message' !$ $closestVehicle!#driver . ' will pick you up soon!',
]);
44
/**
* @Then I should see I am being connected to the van
!"
public function iShouldSeeIAmBeingConnectedToTheVan()
{
$messageFromResponse = $this!#response!#decodeResponseJson('message');
$nameOfVanDriver = $this!#vehicles['van']['driver'];
$this!#assertTrue(Str!$contains($messageFromResponse, $nameOfVanDriver));
}
45
Repeat the cycle for the rest of the scenarios...
46
Also... Mink
47
Automation Results
• Real-world examples drove the development of the
application
• The application is tested on multiple levels
48
So...
1. Discovery
2. Formulation
3. Automation
4.
! "
49
Thank you
• bit.ly/laracon-au-bdd
• twitter.com/marcusamoore
50

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Introducción a UML y Diagrama de Casos de Uso
Introducción a UML y Diagrama de Casos de UsoIntroducción a UML y Diagrama de Casos de Uso
Introducción a UML y Diagrama de Casos de Uso
 
Modul PBO Bab-08 - Java GUI
Modul PBO Bab-08 - Java GUIModul PBO Bab-08 - Java GUI
Modul PBO Bab-08 - Java GUI
 
Diagram erd restaurant
Diagram erd restaurantDiagram erd restaurant
Diagram erd restaurant
 
WEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScriptWEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScript
 
SQL Joins With Examples | Edureka
SQL Joins With Examples | EdurekaSQL Joins With Examples | Edureka
SQL Joins With Examples | Edureka
 
AngularJS
AngularJS AngularJS
AngularJS
 
Rpl 09 - spesifikasi formal
Rpl   09 - spesifikasi  formalRpl   09 - spesifikasi  formal
Rpl 09 - spesifikasi formal
 
E-R diagram & SQL
E-R diagram & SQLE-R diagram & SQL
E-R diagram & SQL
 
Blm1 bilg.mimari
Blm1 bilg.mimariBlm1 bilg.mimari
Blm1 bilg.mimari
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
Database object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab AssignmentDatabase object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab Assignment
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
 
Kd3 penggabungan data sql
Kd3 penggabungan data sqlKd3 penggabungan data sql
Kd3 penggabungan data sql
 
Diagramas UML
Diagramas UMLDiagramas UML
Diagramas UML
 
ADO .NET Entity framework
ADO .NET Entity frameworkADO .NET Entity framework
ADO .NET Entity framework
 
Pemrograman Web - Request Get dan Post
Pemrograman Web - Request Get dan PostPemrograman Web - Request Get dan Post
Pemrograman Web - Request Get dan Post
 
Principais diagramas da UML
Principais diagramas da UMLPrincipais diagramas da UML
Principais diagramas da UML
 
Entorno de Programacion
Entorno de ProgramacionEntorno de Programacion
Entorno de Programacion
 

Kürzlich hochgeladen

Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 

Kürzlich hochgeladen (20)

Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 

Behavior Driven Development and Laravel

  • 2. About Me • Marcus Moore • San Diego, CA • Diego Dev Group • San Diego PHP User Group co-organizer 2
  • 3. Overview • My history with Behavior Driven Development • Behavior Driven Development • Behavior Driven Development and Laravel 3
  • 4. My History with BDD • When I worked at a small school • Gathering requirements was easy! 4
  • 5. My History with BDD • Working for an agency... • Requirement gathering got harder... • Many clients with many communication styles • "Who has solved this?" 5
  • 6. Behavior Driven Development • Extension of Test Driven Development (TDD) • BDD works closely with Unit Testing • BDD is not about testing • Helps you build the right thing 6
  • 7. Communication is hard • Ambiguities arise • Assumptions are made • Rework becomes inevitable 7
  • 8. BDD's Goals • Close communication gap between all stakeholders using real examples • Establish a shared understanding of the desired outcome • Eliminate "rework" 8
  • 9. Three Steps 1. Discovery 2. Formulation 3. Automation 9
  • 10. Discovery • Most important part! • Enables stakeholders to have focused conversations • Fills knowledge gaps • Develops an understanding of how the software should function using real examples 10
  • 11. Methods of Discovery • Discovery Workshops • Example Mapping • Event Storming • etc... 11
  • 12. Discovery Workshops • AKA the Three Amigos • Short and frequent meetings • Different people with different perspectives 12
  • 13. Three Amigos 1. Business Person (Product Owner) • Determines "what" • Expresses the user stories 2. Developer • Determines "how" • Looks for details and potential roadblocks 3. Quality Assurance • Come up with the "what if's" • Tries to determine what will break 13
  • 14. Discovery Results • Complete shared understanding • Concrete examples 14
  • 15. Formulation • Documentation of the concrete examples • Written in natural language syntax • Readable by humans and software • .feature files • Gherkin 15
  • 16. Gherkin • Tells the Story via a narrative • Easy for non-technical people to read 16
  • 17. Feature: Requesting rides As a verified user I want to request a ride So that I can get to my destination on time Rule: Unverified users cannot request rides Rule: User cannot request ride for more than 5 people Scenario: A user requests a ride for 4 people Given I am a verified user And there is a van with 5 seats nearby And there is a car with 3 seats next to me When I request a ride for 4 people Then I should see I am being connected to the van 17
  • 18. Given - Set the state of the world Given I am a verified user And there is a van with 5 seats nearby And there is a car with 3 seats next to me 18
  • 19. When - Interact with the application When I request a ride for 4 people 19
  • 20. Then - Check the outcome of the interaction Then I should see I am being connected to the van 20
  • 21. Feature: Requesting rides As a verified user I want to request a ride So that I can get to my destination on time Rule: Unverified users cannot request rides Rule: User cannot request ride for more than 5 people Scenario: A user requests a ride for 4 people Given I am a verified user And there is a van with 5 seats nearby And there is a car with 3 seats next to me When I request a ride for 4 people Then I should see I am being connected to the van 21
  • 22. Formulation Results • Common terminology solidified • Shared understanding is documented in feature files • New people can understand the application 22
  • 23. Automation • Start to implement the features with BDD and TDD • Double-Loop Workflow • Implement the features with Behat 23
  • 24. Behat • The go-to BDD Framework for PHP • Implementation of Cucumber 24
  • 26. Installation and Setup • Install via Composer • vendor/bin/behat !"init • Add your feature files to /features • vendor/bin/behat !"append-snippets 26
  • 27. Feature: Requesting rides As a verified user I want to request a ride So that I can get to my destination on time Rule: Unverified users cannot request rides Rule: User cannot request ride for more than 5 people Scenario: A user requests a ride for 4 people Given I am a verified user And there is a van with 5 seats nearby And there is a car with 3 seats next to me When I request a ride for 4 people Then I should see I am being connected to the van 27
  • 28. /** * @Given I am a verified user "# public function iAmAVerifiedUser() { throw new PendingException(); } 28
  • 29. /** * @Given I am a verified user "# public function iAmAVerifiedUser() { $this"$actingUser = factory(User"%class) "$state('verified') "$create(); $this"$userLocation = [ 'latitude' "& '32.7', 'longitude' "& '-117.1', ]; } 29
  • 30. class FeatureContext implements Context { public function !"construct() { } } 30
  • 31. class BaseContext extends TestCase implements Context { use CreatesApplication; public function !"construct() { putenv('APP_ENV=testing'); parent!#!"construct(); $this!$setUp(); $this!$withoutExceptionHandling(); } /** @BeforeScenario !% public function before(BeforeScenarioScope $scope) { $this!$artisan('migrate:fresh'); } } 31
  • 32. /** * @Given I am a verified user "# public function iAmAVerifiedUser() { $this"$actingUser = factory(User"%class) "$state('verified') "$create(); $this"$userLocation = [ 'latitude' "& '32.7', 'longitude' "& '-117.1', ]; } 32
  • 33. /** * @And there is a van with :arg1 seats nearby !" public function thereIsAVanWithSeatsNearby($arg1) { throw new PendingException(); } 33
  • 34. And there is a van with 5 seats nearby 34
  • 35. /** * @And there is a van with :arg1 seats nearby !" public function thereIsAVanWithSeatsNearby($arg1) { throw new PendingException(); } 35
  • 36. /** * @And there is a van with :numberOfSeats seats nearby !" public function thereIsAVanWithSeatsNearby($numberOfSeats) { $this!#createVan($numberOfSeats, $distanceFromUser = 3); } 36
  • 37. /** * @And there is a car with :numberOfSeats seats next to me !" public function thereIsACarWithSeatsNextToMe($numberOfSeats) { $this!#createCar($numberOfSeats, $distanceFromUser = 1); } 37
  • 38. $this!"createVan($numberOfSeats, $distanceFromUser = 3); $this!"createCar($numberOfSeats, $distanceFromUser = 1); 38
  • 39. Scenario: A user requests a ride for 4 people Given I am a verified user And there is a van with 5 seats nearby And there is a car with 3 seats next to me When I request a ride for 4 people Then I should see I am being connected to the van 39
  • 40. Scenario: A user requests a ride for 4 people Given I am a verified user And there is a "van" with "5" seats "nearby" And there is a "car" with "3" seats "next to me" When I request a ride for 4 people Then I should see I am being connected to the van 40
  • 41. /** * @Given there is a :arg1 with :arg2 seats :arg3 !" public function thereIsAWithSeats($arg1, $arg2, $arg3) { throw new PendingException(); } 41
  • 42. /** * @Given there is a :typeOfVehicle with :numberOfSeats seats :distanceFromUser !" public function thereIsAWithSeatsNearby($typeOfVehicle, $numberOfSeats, $distanceFromUser) { $distance = 3; if ($distanceFromUser !!# 'next to me') { $distance = 1; } $this!$vehicles[$typeOfVehicle] = $this!$createVehicle($typeOfVehicle, $numberOfSeats, $distance); } 42
  • 43. /** * @When I request a ride for :numberOfPeople people !" public function iRequestARideForPeople($numberOfPeople) { $this!#response = $this!#actingAs($this!#actingUser) !#json( 'POST', '/api/get-ride', [ 'location' !% $this!#userLocation, 'number_of_seats' !% $numberOfPeople ] ); } 43
  • 44. !" Controller does some work to match user and driver !" Good time to TDD a service! return response()!#json([ 'message' !$ $closestVehicle!#driver . ' will pick you up soon!', ]); 44
  • 45. /** * @Then I should see I am being connected to the van !" public function iShouldSeeIAmBeingConnectedToTheVan() { $messageFromResponse = $this!#response!#decodeResponseJson('message'); $nameOfVanDriver = $this!#vehicles['van']['driver']; $this!#assertTrue(Str!$contains($messageFromResponse, $nameOfVanDriver)); } 45
  • 46. Repeat the cycle for the rest of the scenarios... 46
  • 48. Automation Results • Real-world examples drove the development of the application • The application is tested on multiple levels 48
  • 49. So... 1. Discovery 2. Formulation 3. Automation 4. ! " 49
  • 50. Thank you • bit.ly/laracon-au-bdd • twitter.com/marcusamoore 50