Slide 1

Slide 1 text

99% IS NOT ENOUGH

Slide 2

Slide 2 text

Sebastian Thoß Kruno Knego Sebastian Heuer Backend Lead Backend Developer Developer Advocate

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Goods and Bads • fully object-oriented • easy refactoring thanks to 100% code coverage • not enough automated acceptance tests • dependencies to legacy software (database changes, API calls)

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Code Coverage 100% != 100%

Slide 12

Slide 12 text

public function execute() { $orders = $this->orderService->getOrders($this->orderNumber); $json = $this->orderJsonMapper->map($orders); return new CommandResult($json); } Example

Slide 13

Slide 13 text

public function testExecute() { $service = $this->createMock(OrderService::class); $mapper = $this->createMock(OrderJsonMapper::class); $order1 = $this->createMock(Order::class); $order2 = $this->createMock(Order::class); $command = new GetOrdersCommand('123', $service, $mapper); $service->expects($this->once()) ->method('getOrders') ->with('123') ->willReturn([$order1, $order2]); $mapper->expects($this->once()) ->method('map') ->with([$order1, $order2]); $command->execute(); }

Slide 14

Slide 14 text

100%!

Slide 15

Slide 15 text

public function execute() { $orders = $this->orderService->getOrders($this->orderNumber); $json = $this->orderJsonMapper->map($orders); return new CommandResult(json_encode('{"foo":"bar"}')); } Breaking Stuff

Slide 16

Slide 16 text

Still 100%!

Slide 17

Slide 17 text

Test Pyramid UNIT TESTS

Slide 18

Slide 18 text

Unit Testing leads to SOLID(ish) code • Single Responsibility • Open/Closed • Liskov Substitution • Interface Segregation • Dependency Inversion

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Test Pyramid INTEGRATION TESTS UNIT TESTS

Slide 21

Slide 21 text

Class 1 Class 2 Class 3 Mock Integration Test

Slide 22

Slide 22 text

Test Pyramid ACCEPTANCE TESTS INTEGRATION TESTS UNIT TESTS

Slide 23

Slide 23 text

Class 1 Class 2 Class 3 Acceptance Test

Slide 24

Slide 24 text

Test Pyramid ACCEPTANCE TESTS INTEGRATION TESTS UNIT TESTS ? $$$ ¢

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

sendGetRequest($url); $this->assertSame(200, $result->getStatusCode()); $this->assertNotEmpty($result->getBody()); $this->assertLessThanOrEqual(100, $result->getTimeToFirstByteInMilliseconds()); }

Slide 27

Slide 27 text

Test Pyramid ACCEPTANCE TESTS INTEGRATION TESTS UNIT TESTS SMOKE TESTS $$$ ¢

Slide 28

Slide 28 text

Test Pyramid ACCEPTANCE TESTS INTEGRATION TESTS SMOKE TESTS 92% COVERAGE UNIT TESTS

Slide 29

Slide 29 text

Test Pyramid ACCEPTANCE TESTS INTEGRATION TESTS SMOKE TESTS UNIT TESTS 100% COVERAGE

Slide 30

Slide 30 text

Why 100%?

Slide 31

Slide 31 text

Why 100%? • Robustness • Feeling of security when refactoring • Fixing bugs is cheaper • Reducing amount of major bugs found in production • Broken Windows Theory

Slide 32

Slide 32 text

"Factory Denver5" by dliban licensed under CC BY-NC-ND 2 - https://flic.kr/p/73y9dg

Slide 33

Slide 33 text

"Factory Denver5" by dliban licensed under CC BY-NC-ND 2 - https://flic.kr/p/73y9dg

Slide 34

Slide 34 text

But what about… • Setters? • Factories? • Trivial Classes?

Slide 35

Slide 35 text

Tips & Tricks

Slide 36

Slide 36 text

Strict PHPUnit Config

Slide 37

Slide 37 text

Mocking public function testCalculatorMultiplies() { $multiplier = $this->getMultiplierMock(); $calculator = new Calculator($multiplier); // (…) } /** * @return PHPUnit_Framework_MockObject_MockObject|Multiplier */ private function getMultiplierMock() { return $this->createMock(Multiplier::class); }

Slide 38

Slide 38 text

@codeCoverageIgnore • errorHandlers • shutdownHandlers Be very strict about excluding code from coverage!

Slide 39

Slide 39 text

@codeCoverageIgnore Be very strict about excluding code from coverage! /** * @param mixed $targetNode * * @throws WrongInstanceException */ private function ensureIsElement($targetNode) { if (!$targetNode instanceof fDOMElement) { // @codeCoverageIgnoreStart throw new WrongInstanceException('target node is not instance of fDOMElement'); // @codeCoverageIgnoreEnd } }

Slide 40

Slide 40 text

Don't test your privates! • Uncovered private methods are probably obsolete • "If covering private methods by only testing public methods is hard, there might be another class in there struggling to get out"

Slide 41

Slide 41 text

"Who's going to pay for all that?" Relative Cost of a Bugfix 40x 80x 120x 160x Requirements Design Code Developer Tests Acceptance Tests Operations 1x 5x 10x 20x 50x 150x Source: Barry Boehm, Keynote Address, EQUITY 2007.

Slide 42

Slide 42 text

https://www.instagram.com/kartenmacherei/ Q&A https://www.facebook.com/kartenmacherei/ +49 40 468996861 [email protected] http://www.kartenmacherei.de/recruiting