Skip to content

Commit

Permalink
Debugger tested
Browse files Browse the repository at this point in the history
  • Loading branch information
lloc committed Dec 16, 2023
1 parent 6492161 commit a63ebc7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion includes/MslsMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ public static function init() {
*
* @param mixed $message
*/
public function debugger( $message ) {
public function debugger( $message ): void {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) {
if ( is_array( $message ) || is_object( $message ) ) {
$message = print_r( $message, true );
}

error_log( 'MSLS Debug: ' . $message );
}
}
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</coverage>
<php>
<const name="MSLS_PLUGIN_PATH" value="multisite-language-switcher/MultisiteLanguageSwitcher.php"/>
<const name="WP_DEBUG" value="true"/>
</php>
<testsuite name="Internal tests">
<directory prefix="test-" suffix=".php">./tests/</directory>
Expand Down
30 changes: 27 additions & 3 deletions tests/test-mslsmain.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,48 @@ public function get_sut() {
return new MslsMain( $options, $collection );
}

function test_get_input_array() {
public function test_get_input_array(): void {
$obj = $this->get_sut();

$this->assertIsArray( $obj->get_input_array( 0 ) );
}

function test_is_autosave_method() {
public function test_is_autosave(): void {
Functions\when( 'wp_is_post_revision' )->justReturn( true );

$obj = $this->get_sut();

$this->assertIsBool( $obj->is_autosave( 0 ) );
}

function test_verify_nonce_method() {
public function test_verify_nonce(): void {
$obj = $this->get_sut();

$this->assertFalse( $obj->verify_nonce() );
}

public function test_debugger_string(): void {
$capture = tmpfile();
$backup = ini_set('error_log', stream_get_meta_data( $capture )['uri']);

$obj = $this->get_sut();
$obj->debugger( 'Test' );

$this->assertStringContainsString( 'MSLS Debug: Test', stream_get_contents( $capture ) );

ini_set('error_log', $backup);
}

public function test_debugger_object(): void {
$capture = tmpfile();
$backup = ini_set('error_log', stream_get_meta_data( $capture )['uri']);

$obj = $this->get_sut();
$obj->debugger( (object) [ 'test' => 'msls' ] );

$this->assertStringContainsString( '[test] => msls', stream_get_contents( $capture ) );

ini_set('error_log', $backup);
}

}

0 comments on commit a63ebc7

Please sign in to comment.