BDD
design your application decoupled from framework
Dariusz Drobisz
PHPers Day Gdańsk 2019, 2.03.2019
About me
● PHP developer
● Framework agnostic enthusiast
(but Symfony lover)
● BDD practitioner
● Event Storming interested
● PHPers organiser
● working fordariusz.drobisz@gmail.com
@daryush_d
BDD
Behavior Driven
Development
What is not BDD?
● Behat
● PHPSpec
● Gherkin scenarios
● Tests
What is not BDD?
● Behat
● PHPSpec
● Gherkin scenarios
● Tests
● Brick Driven Development
What is not BDD?
● Behat
● PHPSpec
● Gherkin scenarios
● Tests
● Brick Driven Development (thx @chrisholland)
“Behaviour-driven development
is about implementing an
application by describing its
behaviour from the perspective
of its stakeholders”
- Dan North
So what is BDD?
Describing application
behaviour
So what is BDD?
Stakeholders perspective
TDDWhat BDD is?
TDD 2.0What BDD is?
Why not just TDD?
Hard to test something that doesn’t exists.
Why not just TDD?
Easier to design behaviour than test something
that you don’t have.
Why not just TDD?
Language matters…
… and helps to design.
2 levels of BDD
StoryBDD
SpecBDD
Story BDD
● Describing high level business application behaviour.
● Helps to elaborate what the needs are
● Helps communicate with business
● Helps to elaborate common language
● Gives us scenarios that can (and should) become
automated functional tests
Spec BDD
● Describing low level implementation behaviour
● To design our code
● To design how it communicate
● Helps understand what code is doing
● Helps get back to your code after while
BDD - tools
● Spec:
○ PHPSpec
○ Spec classes with examples
● Story
○ Behat
○ Scenarios written in gherkin language
○ Automation of scenarios in context classes
Problematic part
Story BDD
What's the
problem?
Mr. Story BDD?
Let’s describe
everything!
Let’s describe everything!
● every link click
● every input fill
● every automatically generated crud
● every screen generated by some admin bundle
● EVERYTHING!
**That applies also to Spec BDD**
What's the
problem?
Mr. Story BDD?
Technical
scenarios!
People think how
Not what and why
Technical scenarios!
Technical scenarios!
What's the
problem?
Mr. Story BDD?
How to automate?
How to automate?
User Interface
vs
Code level
How to automate?
With infrastructure
vs
Without infrastructure
Let’s forget!
Let’s forget that we
are developing web
application!
Let’s start solve
business problems.
Example
Design with BDD
How I do that
Feature/scenario
Given steps Let’s make some preparation
We have nothing - let’s start think up
Is it worth to spec?
Let’s implement that
Let’s store that somewhere
What is this
repository? DB one?
We don’t know.
It’s our port.
We can’t instantiate interface
Let’s have some infrastructure
The fast one!
First adapter of our port
Let’s now use this adapter
Same with another step
Date assumption
Current codebase state
Assumptions works
When steps Put some action there
Application layer introduction
Command - another port
How command looks like?
Command need to be handled - how?
Something need to
happen.
Maybe we can spec
that?
Handler spec - what we already know
Let’s think up rest
We can implement what we designed
We design something in our port
Let’s implement that in adapter
Let’s design borrowing recording
Implementation
Specs works
Get back to behat context
Then steps Check some results
Results checking
Results checking
Behat is green
Behat is green
And here we have
fully functional piece
of application
without framework
Integration Did I mention that I’m a Symfony
lover?
Application layer used in controller
Doctrine mapping
Doctrine adapter for port
Final structure
Repository
https://github.com/daryush/PHPersDay2019
Further actions
examples
● using command bus
● encapsulate transactions
● validation on framework side
● dispatching events
● integration tests
Summary
● BDD helps with design process
● framework agnostic application
is easy to integrate
● testability
● understand of business
● understand of development
process
● ability to postpone
infrastructure decisions
Questions?
Thank you
dariusz.drobisz@gmail.com
@daryush_d
https://joind.in/talk/6e31d

Phpers day 2019