Tag Archives: testing

Testing Warnings in PHPUnit 9+

Testing warnings thrown via trigger_error throws a deprecation warning in PHPUnit 9+:

Expecting E_WARNING and E_USER_WARNING is deprecated and will no longer be possible in PHPUnit 10

Testing E_USER_WARNING and E_WARNINGM will no longer be possible, in favor of thrown exceptions. None of the usual convertWarningsToExceptions configuration tricks work.

Here’s a snippet that will help:

$errored = null;
set_error_handler(function($errno, $errstr, ...$args) use (&$errored) {
    $errored = [$errno, $errstr, $args];
    restore_error_handler();
});
call_error_triggering_function();
$this->assertNotNull($errored, 'did not trigger any warning');                                                                             
[$errno, $errstr, $args] = $errored;
$this->assertEquals(E_USER_WARNING, $errno, 'did not trigger an E_USER_WARNING');

The fact that we’re not even going to be seeing warnings, deprecations in PHPUnit 10 is appalling!

https://phpunit.de/announcements/phpunit-10.html



Testing Race Conditions in WordPress

WordPress is not thread-safe.

I’ve spoken about this, and even started work on a plugin called WP_Lock that will aim to introduce some thread-safety into core to address the occasional TOCTOU bug under high load (and concurrency). For example ticket #44568 is an easy-to-reproduce complaint about concurrent REST API access 😉

Testing Concurrency Issues in WordPress with PHPUnit

If you thought writing thread-safe code in WordPress plugins is hard, unit testing the code for concurrency issues is even harder. One of the ways I found works best is by utilizing the PCNTL module in PHP to fork and test critical sections.

Continue reading



WordCamp Russia 2013

I took part in WordCamp Russia 2013 this year, which was organized by my brother (with huge help from a handful of volunteers and the WordPress Foundation). This was the first ever WordCamp in Russia.

My talk was on testing automation in custom WordPress code, which covered some basics of unit tests using PHPUnit, system tests using CasperJS.

The talk is in Russian, but English subtitiles are available. Also slides and code. To view all other talks visit https://wordpress.tv/event/wordcamp-russia-2013/.

Konstantin wrote about the event in much detail.