How to set the current screen in a WordPress PHPUnit test

Even the many AI tools out there could say how to set the global $current_screen variable in order to test things like:

if ( get_current_screen()->action === 'add' ) {
    // ... do something
}

Because WP_Screen is final and there’s no hooks, firing up a new screen seemed impossible.

Fortunately there is a set_current_screen() method, though what to pass for the first parameters (hookname) wasn’t clear. To set the current screen as editing a post, use:

set_current_screen( 'edit-post' )

and to fake that we’re adding a new one:

set_current_screen( 'post-new' )