Slide 1

Slide 1 text

Writing Testable Symfony Apps SYMFONY WORLD ONLINE | JUN, 2022 @afilina

Slide 2

Slide 2 text

“Our tests are slow.“ “We can’t really write unit tests.” “Tests are hard to maintain.”

Slide 3

Slide 3 text

Anna Filina • Coding since 1997 • PHP since 2003 • Legacy archaeology • Test automation • Public speaking • Mentorship • YouTube videos

Slide 4

Slide 4 text

class MyController extends AbstractController { public function index(): Response { return $this->render('my/index.html.twig', [ 'variable' => 'value', ]); } }

Slide 5

Slide 5 text

class MyControllerTest extends TestCase { public function testIndex(): void { $controller = new MyController(); $controller->index(); } }

Slide 6

Slide 6 text

Error : Call to a member function has() on null ./vendor/symfony/framework-bundle/Controller/AbstractController.php:254 ./vendor/symfony/framework-bundle/Controller/AbstractController.php:266 ./src/Controller/MyController.php:14 ./tests/Controller/MyControllerTest.php:25

Slide 7

Slide 7 text

class MyController extends AbstractController { public function index(): Response { return $this->render('my/index.html.twig', [ 'variable' => 'value', ]); } }

Slide 8

Slide 8 text

use Twig\Environment; class MyTestableController { public function __construct(private Environment $twig) { } public function index(): Response { $html = $this->twig->render('my/index.html.twig', [ 'var' => 'value', ]); return new Response($html); } }

Slide 9

Slide 9 text

services: App\Controller\: resource: '../src/Controller/' tags: ['controller.service_arguments']

Slide 10

Slide 10 text

public function testIndex(): void { $controller = new MyTestableController( $twig = $this->createMock(Environment::class) ); $twig ->method('render') ->with('my/index.html.twig', ['var' => 'value']) ->willReturn('some content'); self::assertEquals( 'some content', $controller->index()->getContent() ); }

Slide 11

Slide 11 text

Time: 00:00.089, Memory: 10.00 MB OK (1 test, 1 assertion)

Slide 12

Slide 12 text

Unit Integration MyController Address AddressRepository routes.php

Slide 13

Slide 13 text

Unit Integration MyController Address AddressRepository routes.php

Slide 14

Slide 14 text

Unit Integration MyController Address AddressRepository routes.php

Slide 15

Slide 15 text

Unit Integration MyController Address AddressRepository routes.php

Slide 16

Slide 16 text

MyController Address AddressRepository GET /saved-addresses routes.php

Slide 17

Slide 17 text

# Behat feature file Scenario: Buyer can purchase using a credit card Given I selected a product When I submit a valid credit card Then I should see a payment receipt afilina.com/learn

Slide 18

Slide 18 text

• Routes • Service container • Security • Cache, Twig and other packages

Slide 19

Slide 19 text

UI MyController Address AddressRepository routes.php

Slide 20

Slide 20 text

UI MyController Address AddressRepository routes.php

Slide 21

Slide 21 text

UI MyController Address AddressRepository routes.php

Slide 22

Slide 22 text

AddressRepository Access database Map records to classes

Slide 23

Slide 23 text

if ($address->isBilling() && $address->getCountry() !== 'Canada') { //... }

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Doctrine Entities

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

SubscriptionRenewal Mocked PaymentProvider

Slide 31

Slide 31 text

SubscriptionRenewal Mocked PaymentProvider

Slide 32

Slide 32 text

SubscriptionRenewal Real PaymentProvider

Slide 33

Slide 33 text

SubscriptionRenewal Real PaymentProvider

Slide 34

Slide 34 text

cardToken amount description ChargeRequest

Slide 35

Slide 35 text

final class ChargeRequest { public function __construct( readonly public string $token, readonly public int $amount, readonly public string $description ) {} }

Slide 36

Slide 36 text

SubscriptionRenewal PaymentProvider

Slide 37

Slide 37 text

SubscriptionRenewal PaymentProvider

Slide 38

Slide 38 text

final class ChargeRequest { public function __construct( readonly public string $token, readonly public int $amount, readonly public string $description ) { if ($amount < 0) { throw new \DomainException('Amount cannot be lower than 0'); } } }

Slide 39

Slide 39 text

Isn’t that overkill for an edge case?

Slide 40

Slide 40 text

• Avoid extending framework classes • Use multiple test types in tandem (pyramid) • Extract business logic from infrastructure • Use small immutable classes

Slide 41

Slide 41 text

@afilina Questions?