Slide 1

Slide 1 text

ADVANCED TESTING DOS' AND DON'TS SWAT.IO, @jo ife CTO, #proudDadOfAGirl, Life Long Learner Tester

Slide 2

Slide 2 text

SWAT.IO SOCIAL MEDIA MANAGEMENT FOR TEAMS ▸ 7 Developers, 8 servers, 2.5 TB of database ▸ PHP7.3, PostgreSQL, ElasticSearch, Laravel, CakePHP, Queues, Queues, Queues ▸ Lots of high volume customers ▸ 6 Mio API outgoing requests/day ▸ 0.7 Mio incoming webhooks/day ▸ Only constant: Change

Slide 3

Slide 3 text

SKILLS "We <3 to take full advantage of our abilities and talents, as well as further developing them together." ACTION "We don't default to blindly following the rules. We encourage everyone to give feedback and show initiative at any time."

Slide 4

Slide 4 text

LARAVEL @ SWAT.IO 3 REPOS, 1 BIG ONE: ▸ Tests: 7295, Assertions: 260948, Skipped: 5

Slide 5

Slide 5 text

DISCLAIMER ▸ I'm a hands-on guy, not a scientist. ▸ Don't blame me for the wrong wordings. ▸ Integration/Unit? Who cares!

Slide 6

Slide 6 text

I'M HERE FOR A show-o . AMA

Slide 7

Slide 7 text

I'M HERE FOR A discu ion.

Slide 8

Slide 8 text

I MIGHT BE HERE FOR A FLAMEWAR.

Slide 9

Slide 9 text

1. NO-

Slide 10

Slide 10 text

DEV === PROD === TEST USE SAME STACK AS IN PRODUCTION, I.E. DON'T TEST WITH SQLITE WHEN YOU'RE RUNNING PGSQL

Slide 11

Slide 11 text

2. NO-

Slide 12

Slide 12 text

USE A DEDICATED TEST DB DON'T REUSE YOUR LOCAL DEV DB

Slide 13

Slide 13 text

3. NO-

Slide 14

Slide 14 text

SETUP YOUR TEST RUNNER INSIDE PHPSTORM @group Tests that should run together

Slide 15

Slide 15 text

4. NO-

Slide 16

Slide 16 text

MOCK HTTP / EXTERNAL APIS

Slide 17

Slide 17 text

5. NO-

Slide 18

Slide 18 text

USE CARBON::SETTESTNOW() - ALL THE time!

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

6. ! - !

Slide 21

Slide 21 text

USE DATAPROVIDERS AS MUCH AS POSSIBLE!

Slide 22

Slide 22 text

7. ! - !

Slide 23

Slide 23 text

USE FACTORIES TO DEFINE YOUR FIXTURES! FOR EVERY SINGLE TEST

Slide 24

Slide 24 text

▸ we learned it the hard way. ▸ fixture_data.php ▸ fakeFactory FTW!

Slide 25

Slide 25 text

8. ! -

Slide 26

Slide 26 text

DO NOT TEST YOUR HTTP IMPLEMENTATION

Slide 27

Slide 27 text

IT'S SLOW.

Slide 28

Slide 28 text

▸ Put stuff in non-http environment classes (e.g. repositories) ▸ Test extensively your repositories (all tests) ▸ Test vaguely your HTTP environment (basic tests)

Slide 29

Slide 29 text

YOUR FUTURE YOU WILL BE THANKFUL.

Slide 30

Slide 30 text

YOUR FUTURE YOU WILL BE VERY ANGRY.

Slide 31

Slide 31 text

9. ! -

Slide 32

Slide 32 text

MY LEGACY AKA: WTF AKA: OMG!

Slide 33

Slide 33 text

ASSERT YOUR SQL

Slide 34

Slide 34 text

protected function setupSqlCountListenForQueries(): void { DB::listen(function (QueryExecuted $event) { $this->sqlQueryEvents[] = $event; }); }

Slide 35

Slide 35 text

protected function assertSqlCount(int $expectedCount, string $msg = ''): void { $numSqlQueries = \count($this->sqlQueryEvents); if ($expectedCount === $numSqlQueries) { $this->sqlCounterReset(); return; } $msg .= sprintf("Expected number of SQL statements of %d does not match the actual value of %d\nQueries:\n\n%s\n", $expectedCount, $numSqlQueries, implode("\n", array_map( function (QueryExecuted $query) { return sprintf('[%s] %s', $query->connectionName, $query->sql ); }, $this->sqlQueryEvents ) ) ); $this->assertSame($expectedCount, $numSqlQueries, $msg); }

Slide 36

Slide 36 text

$this->assertSqlCount(0); //

Slide 37

Slide 37 text

$this->dbHeavyMethod(); $this->assertSqlCount(67); // $this->sqlCounterReset();

Slide 38

Slide 38 text

$this->initCodeOfYourTestCase(); $this->sqlCounterReset(); $this->realTestStuff(); $this->assertSqlCount(5); //

Slide 39

Slide 39 text

9++. ! -

Slide 40

Slide 40 text

@LIFE_AND_DEV LEGACY AKA: WTF++ AKA: OMG++!

Slide 41

Slide 41 text

protected function assertSqlQueries(string $expectedQueries, string $msg = ''): void { $expectedQueries = trim($expectedQueries); $actualQueries = trim( implode("\n", array_map( function (QueryExecuted $query): string { // Replace any numeric literals with "fake" bind // placeholders. The framework recently optimized // whereIn queries to contain all-only integer // literals directly, which means it includes // IDs which may change during multiple test // runs, which we now manually need to normalize return preg_replace( [ // Covers integers in `WHERE IN ()` '/\d+(,|\))/', // Covers simple `WHERE x =` '/= \d+/', ], [ '?$1', '= ?', ], $query->sql) . ';'; }, $this->sqlQueryEvents ) ) ); $this->sqlCounterReset(); if (!$msg) { $msg = 'SQL queries mismatch'; } $this->assertSame($expectedQueries, $actualQueries, $msg); }

Slide 42

Slide 42 text

$this->assertSqlQueries(<<<'SQL' insert into "cron_logs" ("action", "class_name", "max_runtime", "params", "started", "trace_id") values (?, ?, ?, ?, ?, ?) returning "id"; update "cron_logs" set "finished" = ? where "id" = ?; SQL );

Slide 43

Slide 43 text

WHY?

Slide 44

Slide 44 text

WHY ASSERT SQL? ▸ Last Minute change introducing a lot of new sql statements? ▸ N+1 Problem: Gone!

Slide 45

Slide 45 text

WHY NOT ASSERT SQL? ▸ Refactoring is a PITA ▸ Example? https://github.com/DieSocialisten/Swat.io-API-V2/pull/2164/files

Slide 46

Slide 46 text

GIST LINK INCOMING! https://gist.github.com/johannesnagl/ 3bbfae042d85d8949f1cca2d1ce90539

Slide 47

Slide 47 text

Bonus!

Slide 48

Slide 48 text

PHPSTAN FIRST DEVELOPMENT STATIC ANALYSIS FTW discover bugs in your code without before ru ing it! https://github.com/phpstan/phpstan/ https://github.com/nunomaduro/larastan Different levels to jump in! (We're at 6)

Slide 49

Slide 49 text

ONE MOAR THING!

Slide 50

Slide 50 text

ASSERTMODELATTRIBUTESSAME protected function assertModelAttributesSame(Model $model, array $expected): void { $className = get_class($model); foreach ($expected as $attribute => $value) { $message = "Assert for model $className failed on attribute $attribute"; if ($value instanceof CarbonImmutable) { static::assertCarbonEqual($value, $model->$attribute, $message); } else { $this->assertSame($value, $model->$attribute, $message); } } }

Slide 51

Slide 51 text

QUESTIONS? Answers!

Slide 52

Slide 52 text

QUESTIONS? Answers! OF COURSE, WE'RE LOOKING FOR NEW team MEMBERS!