Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

TDD with BDD in PHP and Symfony

2,871 views

Published on

Behavior-driven development: StoryBDD with Behat and SpecBDD with PhpSpec in PHP

Published in: Technology
  • Be the first to comment

TDD with BDD in PHP and Symfony

  1. 1. Kamil Adryjanek <kamil@level7systems.pl> TDD with BDD in PHP and Symfony
  2. 2. Kamil Adryjanek <kamil@level7systems.pl> Kamil AdryjanekKamil Adryjanek Head of Development at Level7Systems.pl PHP / Symfony2 trainer Blogger KamilAdryjanek.com Football amateur
  3. 3. Kamil Adryjanek <kamil@level7systems.pl> 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.
  4. 4. Kamil Adryjanek <kamil@level7systems.pl> We are hiring!We are hiring! 1)Junior PHP / Symfony2 developer 2)PHP / Symfony2 developer
  5. 5. Kamil Adryjanek <kamil@level7systems.pl> AgendaAgenda 1) Why we should write tests? 2) What is TDD? 3) What is BDD? 4) StoryBDD with Behat 5) SpecBDD with PhpSpec
  6. 6. Kamil Adryjanek <kamil@level7systems.pl> TestingTesting „There are two types of programmers: those who write tests, and those who will be writing tests.”
  7. 7. Kamil Adryjanek <kamil@level7systems.pl> TestingTesting Why should we write tests? Why don't we write tests?
  8. 8. Kamil Adryjanek <kamil@level7systems.pl> TestingTesting
  9. 9. Kamil Adryjanek <kamil@level7systems.pl> TestingTesting How to write tests?
  10. 10. Kamil Adryjanek <kamil@level7systems.pl> Test-Driven DevelopmentTest-Driven Development Tests go first...
  11. 11. Kamil Adryjanek <kamil@level7systems.pl> Test-Driven DevelopmentTest-Driven Development
  12. 12. Kamil Adryjanek <kamil@level7systems.pl> Test-Driven DevelopmentTest-Driven Development How to write tests for something that does not exist?
  13. 13. Kamil Adryjanek <kamil@level7systems.pl> The projectThe project
  14. 14. Kamil Adryjanek <kamil@level7systems.pl> The project – customer needsThe project – customer needs
  15. 15. Kamil Adryjanek <kamil@level7systems.pl> The projectThe project The first step in every project is a discussion about the behaviors of the software or feature to be built.
  16. 16. Kamil Adryjanek <kamil@level7systems.pl> 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
  17. 17. Kamil Adryjanek <kamil@level7systems.pl> BDDBDD Business Development requirements examples
  18. 18. Kamil Adryjanek <kamil@level7systems.pl> BDDBDD ● Better understanding of business ● Better ideas ● Developers can improve system ● Developers can help business
  19. 19. Kamil Adryjanek <kamil@level7systems.pl> 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.
  20. 20. Kamil Adryjanek <kamil@level7systems.pl> Evolution of TDDEvolution of TDD StoryBDD ~ functional testing SpecBDD ~ unit testing Instead of writing tests you should think of specifying behavior.
  21. 21. Kamil Adryjanek <kamil@level7systems.pl> StoryBDDStoryBDD Scenarios (examples) go first...
  22. 22. Kamil Adryjanek <kamil@level7systems.pl> StoryBDDStoryBDD StoryBDD helps ensuring that development team has understanding of business on the same level that client does
  23. 23. Kamil Adryjanek <kamil@level7systems.pl> 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
  24. 24. Kamil Adryjanek <kamil@level7systems.pl> BehatBehat Is Behat a testing tool for business?
  25. 25. Kamil Adryjanek <kamil@level7systems.pl> BehatBehat No, Behat is about communication between business and development.
  26. 26. Kamil Adryjanek <kamil@level7systems.pl> Behat - installationBehat - installation composer require --dev behat/behat composer require --dev behat/mink composer require --dev behat/mink-extension
  27. 27. Kamil Adryjanek <kamil@level7systems.pl> Behat scenarioBehat scenario Scenario: Some description of the scenario Given some context When some event Then outcome
  28. 28. Kamil Adryjanek <kamil@level7systems.pl> 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
  29. 29. Kamil Adryjanek <kamil@level7systems.pl> BehatBehat How does it work?
  30. 30. Kamil Adryjanek <kamil@level7systems.pl> Behat – context classBehat – context class
  31. 31. Kamil Adryjanek <kamil@level7systems.pl> Behat – translationBehat – translation
  32. 32. Kamil Adryjanek <kamil@level7systems.pl> FeatureFeature I want to be able to log in with username and password via login page
  33. 33. Kamil Adryjanek <kamil@level7systems.pl> Behat featureBehat feature In order to access admin dashboard As user I need to be able to log in
  34. 34. Kamil Adryjanek <kamil@level7systems.pl> Scenario 1: login formScenario 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
  35. 35. Kamil Adryjanek <kamil@level7systems.pl> Scenario 2 – unsuccessful loginScenario 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.”
  36. 36. Kamil Adryjanek <kamil@level7systems.pl> Scenario 3 – successful loginScenario 3 – successful login Scenario: successful login to admin panel Given I am on the „Login” page When I fill in "email" with: "user@example.com" 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”
  37. 37. Kamil Adryjanek <kamil@level7systems.pl> BehatBehat What about „testing” REST API?
  38. 38. Kamil Adryjanek <kamil@level7systems.pl> FeatureFeature I want to be able to register customer account via REST API
  39. 39. Kamil Adryjanek <kamil@level7systems.pl> Behat featureBehat feature In order to list my CDRs As api user I need to be able to register customer
  40. 40. Kamil Adryjanek <kamil@level7systems.pl> 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
  41. 41. Kamil Adryjanek <kamil@level7systems.pl> 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
  42. 42. Kamil Adryjanek <kamil@level7systems.pl> 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: user@example.com password: $ecret And the response status should be 201 And only following properties should exist: data links
  43. 43. Kamil Adryjanek <kamil@level7systems.pl> Bug-Driven Development?
  44. 44. Kamil Adryjanek <kamil@level7systems.pl> 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
  45. 45. Kamil Adryjanek <kamil@level7systems.pl> What about code specification?
  46. 46. Kamil Adryjanek <kamil@level7systems.pl> SpecBDDSpecBDD (Code) specifications go first...
  47. 47. Kamil Adryjanek <kamil@level7systems.pl> 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
  48. 48. Kamil Adryjanek <kamil@level7systems.pl> PhpSpecPhpSpec Create (design) simple class for storing tasks
  49. 49. Kamil Adryjanek <kamil@level7systems.pl> 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.
  50. 50. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec - installationPhpSpec - installation ● create composer.json file: ● and install: composer install
  51. 51. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – describePhpSpec – describe
  52. 52. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – runPhpSpec – run
  53. 53. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – treePhpSpec – tree
  54. 54. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – TaskCollectionSpecPhpSpec – TaskCollectionSpec
  55. 55. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – specifyPhpSpec – specify
  56. 56. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – runPhpSpec – run
  57. 57. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – runPhpSpec – run
  58. 58. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – runPhpSpec – run
  59. 59. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – TaskCollectionPhpSpec – TaskCollection
  60. 60. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – codePhpSpec – code
  61. 61. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – TaskCollectionPhpSpec – TaskCollection
  62. 62. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – specifyPhpSpec – specify
  63. 63. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – runPhpSpec – run
  64. 64. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – Task interfacePhpSpec – Task interface
  65. 65. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – runPhpSpec – run
  66. 66. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – runPhpSpec – run
  67. 67. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – codePhpSpec – code
  68. 68. Kamil Adryjanek <kamil@level7systems.pl> PhpSpec – runPhpSpec – run
  69. 69. Kamil Adryjanek <kamil@level7systems.pl> Sylius and Behat / PhpSpecSylius and Behat / PhpSpec
  70. 70. Kamil Adryjanek <kamil@level7systems.pl> Thank you!

×