Slide 1

Slide 1 text

Essential Testing Know-How for testing on PHP 8+ Juliette Reinders Folmer Tweet about it: @jrf_nl

Slide 2

Slide 2 text

test Test TEST

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Assessing & Improving Test Quality marktimemedia

Slide 5

Slide 5 text

Test Types Unit Tests Integration Tests E2E Tests Acceptance Tests

Slide 6

Slide 6 text

Test Requirements Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 7

Slide 7 text

Test Requirements Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 8

Slide 8 text

Have the basic setup in place [1] In composer.json: { "require-dev" : { "phpunit/phpunit": "^8.0 || ^9.0" }, "autoload": { "classmap": ["src/"] }, "autoload-dev": { "classmap": ["tests/"] } }

Slide 9

Slide 9 text

Have the basic setup in place [2] In phpunit.xml.dist: ./tests/ --generate-configuration

Slide 10

Slide 10 text

Start Small (but start somewhere)

Slide 11

Slide 11 text

Start Small (but start somewhere) assertEquals('text', $result); } }

Slide 12

Slide 12 text

Don't Use Your Own Code To Create Test Data

Slide 13

Slide 13 text

Test Requirements Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 14

Slide 14 text

assertEquals() == assertSame() === Most Common Issue

Slide 15

Slide 15 text

Available Assertions

Slide 16

Slide 16 text

Use the Right Assertion assertEquals('text', $result); $this->assertSame('text', $result); } }

Slide 17

Slide 17 text

Test Requirements Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 18

Slide 18 text

Don't Trust Code Coverage But do examine it

Slide 19

Slide 19 text

Slide 20

Slide 20 text

Enabling Code Coverage [1] ... src

Slide 21

Slide 21 text

Enabling Code Coverage [2] assertSame($out, $result); }

Slide 22

Slide 22 text

Simplify { "scripts" : { "test": [ "vendor/bin/phpunit --no-coverage" ], "coverage": [ "vendor/bin/phpunit" ], "coverage-local": [ "vendor/bin/phpunit --coverage-html ./build/coverage-html" ] } }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

PHPUnit >= 9.3 ? phpunit --migrate-configuration

Slide 25

Slide 25 text

Test Requirements Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 26

Slide 26 text

Tests Document Expectations

Slide 27

Slide 27 text

assertSame('some text', $result); $result = Foo::stripQuotes("some 'text'"); $this->assertSame("some 'text'", $result); $result = Foo::stripQuotes(false); $this->assertSame('', $result); } Don't Just Test the Happy Path

Slide 28

Slide 28 text

Allow for Testing the Unhappy Path ▪ strict_types ▪ Parameter type declarations mensatic

Slide 29

Slide 29 text

Use Data Providers

Slide 30

Slide 30 text

Test Method Data Provider /** * Test Foo::stripQuotes(). * * @dataProvider dataStripQuotes * * @param mixed $in Function input. * @param string $out Expected output. * * @return void */ public function testStripQ($in, $out) { $result = Foo::stripQuotes($in); $this->assertSame($out, $result); } /** * Data provider. * * @return array[] */ public function dataStripQuotes() { return [ ['"some text"', 'some text'], ["some 'text'", "some 'text'"], [false, ''], ]; }

Slide 31

Slide 31 text

Without Data Provider With Data Provider

Slide 32

Slide 32 text

Your Tests Are Limited By Your Own Imagination

Slide 33

Slide 33 text

Test Requirements Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 34

Slide 34 text

Test Your Tests ▪ Infection https://infection.github.io/ https://youtu.be/ADKyTlaH6e4 ▪ PHPUnitCompatibility (upcoming) ▪ PHPUnitQA (upcoming) lisaleo

Slide 35

Slide 35 text

Test Quality of WordPress Plugins & Themes marktimemedia

Slide 36

Slide 36 text

WordPress Test Suite Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 37

Slide 37 text

WordPress Test Suite Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Plugins and Themes Happy & unhappy path High code coverage, strictly measured Strict assertions Have tests

Slide 41

Slide 41 text

Running Your Tests on PHP 8.0 marktimemedia

Slide 42

Slide 42 text

PHPUnit Support v Compatible with: 10 PHP >= 7.4 (expected April 2021) 9 PHP >= 7.3 8 PHP >= 7.2 7 PHP 7.1, 7.2, 7.3 [EOL] 6 PHP 7.0, 7.1, 7.2 [EOL] 5 PHP 5.6, 7.0, 7.1 [EOL] Seemann

Slide 43

Slide 43 text

PHPUnit vs PHP PHPUnit / PHP 5.6 7.0 7.1 7.2 7.3 7.4 8.0 5.x 6.x 7.x 8.x 9.x 9.3+ 10.x void 8.5+

Slide 44

Slide 44 text

Constraints and Roadblocks » [OS] Supporting PHP 5.6 + 7.0 still required » Plugin tests often extend WP integration test suite » PHPUnit < 8.5/9.3 not compatible with PHP 8.0 - Mocking - @runInSeparateProcess » PHPUnit 7 Phar will not run on PHP 8.0 » Committed composer.lock file

Slide 45

Slide 45 text

Solution WP Core: "autoload-dev": { "files": [ "tests/includes/MockObject/Builder/NamespaceMatch.php", "tests/includes/MockObject/Builder/ParametersMatch.php", "tests/includes/MockObject/InvocationMocker.php", "tests/includes/MockObject/MockMethod.php" ], "exclude-from-classmap": [ "vendor/phpunit/…/MockObject/Builder/NamespaceMatch.php", "vendor/phpunit/…/MockObject/Builder/ParametersMatch.php", "vendor/phpunit/…/MockObject/InvocationMocker.php", "vendor/phpunit/…/MockObject/MockMethod.php" ] }, ▪ Composer, not phar ▪ Lock at PHPUnit 7.5 ▪ Downgrade to PHPUnit 5 in CI for PHP 5.6, 7.0 ▪ Use MockObject classes from PHPUnit 9.3 ▪ Separate process => separate group

Slide 46

Slide 46 text

Constraints For Public/OS projects Support multiple WP versions Support multiple PHP versions No control over run environment

Slide 47

Slide 47 text

Mock-based Tests PHPUnit 5.x composer.lock Platform - php 5.6 PHPUnit 5.x – 9.x CI: composer update … --ignore-platform-reqs Needs PHPUnit cross-version compatibility layer

Slide 48

Slide 48 text

WP Integration Tests PHPUnit 5.x composer.lock Platform - php 5.6 PHPUnit 5.x – 7.x CI: composer update … --ignore-platform-reqs Need PHPUnit cross-version compatibility layer Need custom autoload for MockObject

Slide 49

Slide 49 text

New Tooling PHPUnit Polyfills WP Test Utils Sponsored by: Alternative: Symfony Bridge * Supports PHPUnit 4.8 – 9.x, PHP 5.5 – 8.0

Slide 50

Slide 50 text

Implementing PHPUnit Polyfills In composer.json: { "require-dev" : { "phpunit/phpunit": "^5.0 || ^7.0", "yoast/phpunit-polyfills": "^0.2.0" } }

Slide 51

Slide 51 text

Implementing PHPUnit Polyfills What you get: ▪ Polyfills via traits for all new assertions and expectations in PHPUnit ▪ Helper to work round removal of assertAttribute*() methods ▪ An cross-version compatible abstract base TestCase (to get round void) which includes all polyfills ▪ A cross-version compatible TestListenerImplementation

Slide 52

Slide 52 text

Implementing PHPUnit Polyfills

Slide 53

Slide 53 text

Implementing WP Test Utils In composer.json: { "require-dev" : { "phpunit/phpunit": "^5.0 || ^7.0", "brain/monkey": "^2.6.0", "yoast/wp-test-utils": "^0.2.0" } }

Slide 54

Slide 54 text

Implementing WP Test Utils for BrainMonkey tests What you get: ▪ PHPUnit Polyfills ▪ BrainMonkey and Mockery set up and teardown ▪ Mockery tests not marked as risky ▪ Choice between BrainMonkey or opaque escape/translation stubs ▪ expectOutputContains() helper ▪ [YoastTestCase] Additional function stubs

Slide 55

Slide 55 text

Implementing WP Test Utils for BrainMonkey Based Tests

Slide 56

Slide 56 text

Implementing WP Test Utils for WP Integration tests What you get: ▪ PHPUnit Polyfills ▪ Access to all WP native test utilities ▪ expectOutputContains() helper ▪ Bootstrap utilities

Slide 57

Slide 57 text

Implementing WP Test Utils for WP Integration Based Tests [ 'plugin-name/main-file.php' ], ]; require_once dirname( __DIR__ ) . '/vendor/yoast/wp-test-utils/src/ WPIntegration/bootstrap-functions.php'; WPIntegration\bootstrap_it(); if ( ! defined( 'WP_PLUGIN_DIR' ) || file_exists( WP_PLUGIN_DIR . '/plugin- name/main-file.php' ) === false ) { echo 'ERROR: …', PHP_EOL; exit( 1 ); }

Slide 58

Slide 58 text

Implementing WP Test Utils for WP Integration Based Tests

Slide 59

Slide 59 text

Further Reading ▪ The Grumpy Programmer's Guide to Testing PHP Applications https://grumpy-learning.com/ ▪ Your Mocks Won't Save You! https://24daysindecember.net/2020/12/08/your-mocks-wont-save-you/ ▪ My Top 10 PHPUnit Tips & Tricks https://speakerdeck.com/jrf/my-top-10-phpunit-tips-and-tricks-e6ea54ce- 2515-4ea9-aacf-9bf7ab3b3141 ▪ PHPUnit Documentation https://phpunit.readthedocs.io/ ▪ Path Coverage in PHPUnit https://doug.codes/php-code-coverage

Slide 60

Slide 60 text

Tooling ▪ PHPUnit Polyfills https://packagist.org/packages/yoast/phpunit-polyfills ▪ WP Test Utils https://packagist.org/packages/yoast/wp-test-utils

Slide 61

Slide 61 text

Thanks! @jrf_nl @jrfnl Any questions ? Slides: https://speakerdeck.com/jrf