Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

PHP-VCR?

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

VCR?

Slide 5

Slide 5 text

Video Casset Recorder

Slide 6

Slide 6 text

How to Use

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Record Response

Slide 9

Slide 9 text

Request & Response Recorded as YML

Slide 10

Slide 10 text

Replace API-Request to PHP-VCR -> Easy way to tests HTTP Request. -> Faster than actual HTTP Request.

Slide 11

Slide 11 text

When to Use

Slide 12

Slide 12 text

Let’s say you have a legacy app

Slide 13

Slide 13 text

Legacy App

Slide 14

Slide 14 text

Replace external API with PHP-VCR

Slide 15

Slide 15 text

Legacy App now becomes testable

Slide 16

Slide 16 text

But, wait

Slide 17

Slide 17 text

Shouldn’t we use mock?

Slide 18

Slide 18 text

Desired Architecture

Slide 19

Slide 19 text

Of course we prefer mock

Slide 20

Slide 20 text

A chiken or egg situation - Loosely coupled architecture is desirable - Legacy app have a long way to achieve it

Slide 21

Slide 21 text

No tests until good architecture?

Slide 22

Slide 22 text

Yes

Slide 23

Slide 23 text

Test First

Slide 24

Slide 24 text

Create tests for the legacy app with PHP-VCR It’s will support your refactoring until you achive your desirable architecture.

Slide 25

Slide 25 text

How to Bent Wood | Ahoy! http://www.ventspleen.com/how-to-bend-wood/ It’s a jig for a legacy app

Slide 26

Slide 26 text

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.

Slide 27

Slide 27 text

Precaution

Slide 28

Slide 28 text

Needs soap extension Example setup for CircleCI 2.0

Slide 29

Slide 29 text

Headers match may be too strict Development environment & CI environment might use slightly diffrent version of UserAgent. Ignore strict headers match at this situation.

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

How PHP-VCR record Requests?

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Example

Slide 34

Slide 34 text

HTTP Request

Slide 35

Slide 35 text

HTTP Response

Slide 36

Slide 36 text

Capture HTTP Request & Replace Response

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Replaced Response

Slide 39

Slide 39 text

Summary PHP-VCR allows you to write tests for legacy Apps. Use ‘Test Fast’ approach to improve the software architecture

Slide 40

Slide 40 text

Thanks!! @hanhan1978 https://blog.hanhans.net Ryo Tomidokoro