Lately I’ve been using this simple setup to print stuff to the terminal during PHPUnit runs.
/**
* @var bool
*/
public $debug_on = false;
/**
* @param mixed $something
*/
public
function debug_print( $something ) {
if ( ! $this->debug_on ) {
return;
}
fwrite( STDERR, "n" . print_r( $something, true ) . "n" );
}
Use Case 1
Control debug printing by means of opening and closing “parentheses”:
$tested->debug_on = true;
//...
$tested->debug_print( $stuff );
//...
$tested->debug_on = false;
Notice that you can also control (everything or something) either from the testing or the tested code, the only difference being the use of ´$this´ instead of ´$tested´.
Use Case 2
Control debug printing by means of an automatic toggle like this one:
static $count = 0;
$this->debug_on = !$count++;
//...
$this->debug_print( $stuff );
Same considerations as above.