Slide 1

Slide 1 text

TESTING LEGACY TROUBLES @konrad_126

Slide 2

Slide 2 text

LEGACY CODE? NO TESTS.

Slide 3

Slide 3 text

THE CASE OF AN IRRITATING PARAMETER

Slide 4

Slide 4 text

vatProvider->getRate($product->countryCode()); // some business logic to calculate price } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public function __construct(private VatDotComClient $vatProvider) vatProvider->getRate($product->countryCode()); 11 // some business logic to calculate price 12 } 13 } 14 $taxRate = $this->vatProvider->getRate($product->countryCode()); vatProvider->getRate($product->countryCode()); 11 12 } 13 } 14

Slide 5

Slide 5 text

Slide 6

Slide 6 text

Slide 7

Slide 7 text

Slide 8

Slide 8 text

vatProvider->getRate($product->countryCode()); // some business logic to calculate price } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public function __construct(private VatDotComClient $vatProvider) vatProvider->getRate($product->countryCode()); 11 // some business logic to calculate price 12 } 13 } 14

Slide 9

Slide 9 text

Slide 10

Slide 10 text

vatProvider->getRate($product->countryCode()); // some business logic to calculate price } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public function __construct(private VatProvider $vatProvider) vatProvider->getRate($product->countryCode()); 11 // some business logic to calculate price 12 } 13 } 14

Slide 11

Slide 11 text

rate; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 class TestProvider implements VatProvider rate; 11 } 12 } 13 public function __construct(private float $rate) rate; 11 } 12 } 13 return $this->rate;

Slide 12

Slide 12 text

Slide 13

Slide 13 text

THE CASE HIDDEN DEPENDENCY

Slide 14

Slide 14 text

Slide 15

Slide 15 text

process($payment); // ... Assert stuff } 1 2 3 4 5 6 7 8 9 10 11

Slide 16

Slide 16 text

mailer = new Mailer('api-key', 'secret'); // some code } public function process(Payment $payment) { // ... $this->mailer->send( $payment->receiver(), new PaymentNotificationMessage($payment) ); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 $this->mailer = new Mailer('api-key', 'secret'); mailer->send( 16 $payment->receiver(), 17 new PaymentNotificationMessage($payment) 18 ); 19 20 } 21 } 22 $this->mailer->send( $payment->receiver(), new PaymentNotificationMessage($payment) ); } } mailer = new Mailer('api-key', 'secret'); 8 // some code 9 } 10 11 public function process(Payment $payment) 12 { 13 // ... 14 15 16 17 18 19 20 21 22

Slide 17

Slide 17 text

mailer = new Mailer('api-key', 'secret'); } public function process(Payment $payment) { // ... } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Slide 18

Slide 18 text

mailer = $mailer; } public function process(Payment $payment) { // ... } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Parameterize Constructor (Extract Interface)

Slide 19

Slide 19 text

DESIGN?

Slide 20

Slide 20 text

dependency injection principle strategy design pattern depend on abstract not a concrete

Slide 21

Slide 21 text

MORE ADVICE?

Slide 22

Slide 22 text

THANK YOU @konrad_126