Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Encourage TDD with PHP-VCR

Encourage TDD with PHP-VCR

Test First Approach with PHP-VCR

Ryo Tomidokoro

February 27, 2019

More Decks by Ryo Tomidokoro

Other Decks in Programming


  1. Encourage TDD With PHP-VCR Ryo Tomidokoro PHP Study 2019/2/27 @hanhan1978

  2. PHP-VCR?

  3. https://php-vcr.github.io/

  4. VCR?

  5. Video Casset Recorder

  6. How to Use

  7. PHP-VCR | Record HTTP interactions while testing https://php-vcr.github.io/

  8. Record Response

  9. Request & Response Recorded as YML

  10. Replace API-Request to PHP-VCR -> Easy way to tests HTTP

    Request. -> Faster than actual HTTP Request.
  11. When to Use

  12. Let’s say you have a legacy app

  13. Legacy App

  14. Replace external API with PHP-VCR

  15. Legacy App now becomes testable

  16. But, wait

  17. Shouldn’t we use mock?

  18. Desired Architecture

  19. Of course we prefer mock

  20. A chiken or egg situation - Loosely coupled architecture is

    desirable - Legacy app have a long way to achieve it
  21. No tests until good architecture?

  22. Yes

  23. Test First

  24. Create tests for the legacy app with PHP-VCR It’s will

    support your refactoring until you achive your desirable architecture.
  25. How to Bent Wood | Ahoy! http://www.ventspleen.com/how-to-bend-wood/ It’s a jig

    for a legacy app
  26. When refactoring ends - You can choose wheather to use

    Mock or PHP-VCR - PHP-VCR is a great tool guiding you to achieve a better software architecture.
  27. Precaution

  28. Needs soap extension Example setup for CircleCI 2.0

  29. Headers match may be too strict Development environment & CI

    environment might use slightly diffrent version of UserAgent. Ignore strict headers match at this situation.
  30. curl_getinfo is not recorded (yet) https://github.com/php-vcr/php-vcr/pull/210 It’s in PR, but

    not yet merged. Using curl_getinfo, PHP-VCR will not work.
  31. How PHP-VCR record Requests?

  32. stream_wapper_register http://php.net/manual/en/function.stream-wrapper-register.php

  33. Example

  34. HTTP Request

  35. HTTP Response

  36. Capture HTTP Request & Replace Response

  37. None
  38. Replaced Response

  39. Summary PHP-VCR allows you to write tests for legacy Apps.

    Use ‘Test Fast’ approach to improve the software architecture
  40. Thanks!! @hanhan1978 https://blog.hanhans.net Ryo Tomidokoro