Slide 1

Slide 1 text

Getting the most of PhPUnit assertions by Gabriel Caruso PHPers Summit 2019

Slide 2

Slide 2 text

No content

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

Who am I? ● Backend Developer at Usabilla ● PHP Core Member ● Community(ies) member ● OSS contributor ● Refactor/Stickers/ELEPHANTS lover

Slide 8

Slide 8 text

Assertions Test Framework way to check if a result of something matches an expectation.

Slide 9

Slide 9 text

What is an Assertion in PHPUnit?

Slide 10

Slide 10 text

self::assertEquals($expected, $actual);

Slide 11

Slide 11 text

Should I use self:: or $this->?

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

self::assertEquals($expected, $actual); Assertion What you expect to be returned What was actually returned

Slide 14

Slide 14 text

self::assertTrue($actual);

Slide 15

Slide 15 text

self::assertTrue($actual); Assertion The value we are checking

Slide 16

Slide 16 text

But, what does happen inside PHPunit to judge these assertions?

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

self::assertEquals() PHPUnit's Magic?

Slide 20

Slide 20 text

Let's take a look at some PHPunit Internals Code

Slide 21

Slide 21 text

Recap What does happen under the hood to PHPUnit judge assertEquals? $expected == $actual

Slide 22

Slide 22 text

I want to implement a assertIsCountable assertion How would you do that?

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

What if we use PHPUnit's ABI for that?

Slide 25

Slide 25 text

Let's Code ‍

Slide 26

Slide 26 text

Who does use PHPunit's ABI to write custom assertions?

Slide 27

Slide 27 text

PHP Frameworks Database, HTTP, Cache, etc. - Symfony: HTTP Assertions - Laravel: Database Assertions

Slide 28

Slide 28 text

Do we just have assertEquals and assertTrue as assertions?

Slide 29

Slide 29 text

PHPUnit has 100+ assertions Booleans, comparisons, files, XML, jSON, etc.

Slide 30

Slide 30 text

How do I know if I am using the best assertion for each test?

Slide 31

Slide 31 text

$this->assertTrue($expected == $actual); Failed asserting that false is true.

Slide 32

Slide 32 text

$this->assertTrue($expected == $actual); Assertion What you expect to be returned What was actually returned Assertion

Slide 33

Slide 33 text

self::assertEquals($expected, $actual); Failed asserting that $actual matches expected $expected.

Slide 34

Slide 34 text

$this->assertFalse(in_array(‘foo’, $array); Failed asserting that true is false.

Slide 35

Slide 35 text

$this->assertFalse(in_array(‘foo’, $array)); Assertion Assertion

Slide 36

Slide 36 text

$this->assertNotContains(‘foo’, $array); Failed asserting that an array does not contains ‘foo’.

Slide 37

Slide 37 text

How can I ensure that my project always uses the best assertion?

Slide 38

Slide 38 text

Tools that you can use to automate these refactorings - FriendsOfPHP/PHP-CS-F ixer - rectorphp/rector

Slide 39

Slide 39 text

@carusogabriel Questionings? Thank you ♡