70
Kamil Adryjanek <[email protected]> TDD with BDD in PHP and Symfony

TDD with BDD in PHP and Symfony

Embed Size (px)

Citation preview

Kamil Adryjanek <[email protected]>

TDD with BDD in PHP and Symfony

Kamil Adryjanek <[email protected]>

Kamil AdryjanekKamil Adryjanek

Head of Development at Level7Systems.pl

PHP / Symfony2 trainer

BloggerKamilAdryjanek.com

Football amateur

Kamil Adryjanek <[email protected]>

Level 7 Systems LtdLevel 7 Systems Ltd

Level 7 Systems delivers business and residential communication services based on the SIP technology.

VoIPstudio is a next generation communication platform tailored to a specific business requirements

Powerful SIP Trunking solution for inbound and outbound calls aimed at companies which have their own PBX.

Kamil Adryjanek <[email protected]>

We are hiring!We are hiring!

1)Junior PHP / Symfony2 developer

2)PHP / Symfony2 developer

Kamil Adryjanek <[email protected]>

AgendaAgenda

1) Why we should write tests?

2) What is TDD?

3) What is BDD?

4) StoryBDD with Behat

5) SpecBDD with PhpSpec

Kamil Adryjanek <[email protected]>

TestingTesting

„There are two types of programmers:those who write tests,

and those who will be writing tests.”

Kamil Adryjanek <[email protected]>

TestingTesting

Why should we write tests?Why don't we write tests?

Kamil Adryjanek <[email protected]>

TestingTesting

Kamil Adryjanek <[email protected]>

TestingTesting

How to write tests?

Kamil Adryjanek <[email protected]>

Test-Driven DevelopmentTest-Driven Development

Tests go first...

Kamil Adryjanek <[email protected]>

Test-Driven DevelopmentTest-Driven Development

Kamil Adryjanek <[email protected]>

Test-Driven DevelopmentTest-Driven Development

How to write tests for something that does not exist?

Kamil Adryjanek <[email protected]>

The projectThe project

Kamil Adryjanek <[email protected]>

The project – customer needsThe project – customer needs

Kamil Adryjanek <[email protected]>

The projectThe project

The first step in every project is a discussion about the behaviors of the software or feature

to be built.

Kamil Adryjanek <[email protected]>

Behaviour-Driven DevelopmentBehaviour-Driven Development

BDD is a methodology for developing software through continuous example-based

communication between developers and a business. This communication happens in a form

that both the business and developers can clearly understand - examples

Kamil Adryjanek <[email protected]>

BDDBDD

Business Development

requirements

examples

Kamil Adryjanek <[email protected]>

BDDBDD

● Better understanding of business● Better ideas● Developers can improve system● Developers can help business

Kamil Adryjanek <[email protected]>

TDD vs BDDTDD vs BDD

● TDD focuses on the DEVELOPER's opinion on how parts of the software should work

● BDD focuses on the USERs’ opinion on how they want your application to behave.

Kamil Adryjanek <[email protected]>

Evolution of TDDEvolution of TDD

StoryBDD ~ functional testingSpecBDD ~ unit testing

Instead of writing tests you should think of specifying behavior.

Kamil Adryjanek <[email protected]>

StoryBDDStoryBDD

Scenarios (examples) go first...

Kamil Adryjanek <[email protected]>

StoryBDDStoryBDD

StoryBDD helps ensuring that development team has understanding of business on the same level

that client does

Kamil Adryjanek <[email protected]>

What is Behat?What is Behat?

● Open Source framwork● official BDD tool for PHP● design tool (design by example)● inspired by Ruby’s Cucumber project● easy integration with Symfony framework

Kamil Adryjanek <[email protected]>

Behat Behat

Is Behat a testing tool for business?

Kamil Adryjanek <[email protected]>

Behat Behat

No, Behat is about communication between business and development.

Kamil Adryjanek <[email protected]>

Behat - installationBehat - installation

composer require --dev behat/behatcomposer require --dev behat/minkcomposer require --dev behat/mink-extension

Kamil Adryjanek <[email protected]>

Behat scenarioBehat scenario

Scenario: Some description of the scenario Given some context When some event Then outcome

Kamil Adryjanek <[email protected]>

Behat scenarioBehat scenario

Scenario: Some description of the scenario Given some context And more context When some event And second event occurs Then outcome And another outcome But another outcome

Kamil Adryjanek <[email protected]>

Behat Behat

How does it work?

Kamil Adryjanek <[email protected]>

Behat – context classBehat – context class

Kamil Adryjanek <[email protected]>

Behat – translationBehat – translation

Kamil Adryjanek <[email protected]>

FeatureFeature

I want to be able to log in with username and password via login page

Kamil Adryjanek <[email protected]>

Behat featureBehat feature

In order to access admin dashboardAs userI need to be able to log in

Kamil Adryjanek <[email protected]>

Scenario 1: login form Scenario 1: login form

Scenario: login form Given I am on the „homepage” page When I press „Login” Then I should be on the „Login” page And I should see „email” field And I should see „password” field And I should see „Login” button

Kamil Adryjanek <[email protected]>

Scenario 2 – unsuccessful login Scenario 2 – unsuccessful login

Scenario: Login with empty data Given I am on the „Login” page When I press „Login” Then I should still be on the „Login” page And I should see „E-mail and/or password is required.”

Kamil Adryjanek <[email protected]>

Scenario 3 – successful login Scenario 3 – successful login

Scenario: successful login to admin panel Given I am on the „Login” page When I fill in "email" with: "[email protected]" And I fill in "password" with: "$secret" And I press „Login” Then I should be on the „Dashboard” page And I should see „Admin dashboard”

Kamil Adryjanek <[email protected]>

Behat Behat

What about „testing” REST API?

Kamil Adryjanek <[email protected]>

FeatureFeature

I want to be able to register customer account via REST API

Kamil Adryjanek <[email protected]>

Behat featureBehat feature

In order to list my CDRsAs api userI need to be able to register customer

Kamil Adryjanek <[email protected]>

Scenario 1 – invalid HTTP methodScenario 1 – invalid HTTP method

Scenario: register with invalid method Given I am not authenticated When I send GET request to „/customers” Then the response status code should be 405

Kamil Adryjanek <[email protected]>

Scenario 2 – POST empty dataScenario 2 – POST empty data

Scenario: register customer with empty data Given I am not authenticated When I send POST request to „/customers” Then the response status code should be 400 And only following properties should exist: message errors And the „message” property should contain „Validation errors.” And the „errors” property should be array with „2” elements

Kamil Adryjanek <[email protected]>

Scenario 3 – successful registrationScenario 3 – successful registration

Scenario: successful customer registration Given I am not authenticated When I send POST request to „/customers” with data: email: [email protected] password: $ecret And the response status should be 201 And only following properties should exist: data links

Kamil Adryjanek <[email protected]>

Bug-Driven Development?

Kamil Adryjanek <[email protected]>

BehatBehat

1) Create scenario that will produce a „bug”

2) Run Behat scenario that will fail

3) Fix the bug / refactor

4) Go to step 2 until scenario passes

Kamil Adryjanek <[email protected]>

What about code specification?

Kamil Adryjanek <[email protected]>

SpecBDDSpecBDD

(Code) specifications go first...

Kamil Adryjanek <[email protected]>

What is PhpSpec?What is PhpSpec?

● Open Source framwork● design tool (design by specification)● allows to describe the behaviour of an object you are about to write / create

● inspired by Ruby’s RSpec project

Kamil Adryjanek <[email protected]>

PhpSpecPhpSpec

Create (design) simple class for storing tasks

Kamil Adryjanek <[email protected]>

PhpSpec - featurePhpSpec - feature

We are going to implement a class that:● will store a collection of tasks;● we can add a task to;● can be marked as done.

Kamil Adryjanek <[email protected]>

PhpSpec - installationPhpSpec - installation● create composer.json file:

● and install:composer install

Kamil Adryjanek <[email protected]>

PhpSpec – describePhpSpec – describe

Kamil Adryjanek <[email protected]>

PhpSpec – runPhpSpec – run

Kamil Adryjanek <[email protected]>

PhpSpec – treePhpSpec – tree

Kamil Adryjanek <[email protected]>

PhpSpec – TaskCollectionSpecPhpSpec – TaskCollectionSpec

Kamil Adryjanek <[email protected]>

PhpSpec – specifyPhpSpec – specify

Kamil Adryjanek <[email protected]>

PhpSpec – runPhpSpec – run

Kamil Adryjanek <[email protected]>

PhpSpec – runPhpSpec – run

Kamil Adryjanek <[email protected]>

PhpSpec – runPhpSpec – run

Kamil Adryjanek <[email protected]>

PhpSpec – TaskCollectionPhpSpec – TaskCollection

Kamil Adryjanek <[email protected]>

PhpSpec – codePhpSpec – code

Kamil Adryjanek <[email protected]>

PhpSpec – TaskCollectionPhpSpec – TaskCollection

Kamil Adryjanek <[email protected]>

PhpSpec – specifyPhpSpec – specify

Kamil Adryjanek <[email protected]>

PhpSpec – runPhpSpec – run

Kamil Adryjanek <[email protected]>

PhpSpec – Task interfacePhpSpec – Task interface

Kamil Adryjanek <[email protected]>

PhpSpec – runPhpSpec – run

Kamil Adryjanek <[email protected]>

PhpSpec – runPhpSpec – run

Kamil Adryjanek <[email protected]>

PhpSpec – codePhpSpec – code

Kamil Adryjanek <[email protected]>

PhpSpec – runPhpSpec – run

Kamil Adryjanek <[email protected]>

Sylius and Behat / PhpSpecSylius and Behat / PhpSpec

Kamil Adryjanek <[email protected]>

Thank you!