Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Debug::clear() #246

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions tests/phpunit/tests/Debug/Debug_ClearTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Class Debug_ClearTest
*
* @package AspireUpdate
*/

/**
* Tests for Debug::clear()
*
* @covers \AspireUpdate\Debug::clear
*/
class Debug_ClearTest extends Debug_UnitTestCase {
/**
* Test that a WP_Error object is returned when the filesystem isn't available.
*
* @covers \AspireUpdate\Debug::init_filesystem
* @covers \AspireUpdate\Debug::verify_filesystem
*/
public function test_should_return_wp_error_when_filesystem_is_not_available() {
add_filter( 'filesystem_method', '__return_false' );

$this->assertWPError(
AspireUpdate\Debug::clear(),
'A WP_Error object was not returned.'
);

$this->assertFileDoesNotExist(
self::$log_file,
'The log file was created.'
);
}

/**
* Test that a WP_Error object is returned when the log file doesn't exist.
*
* @covers \AspireUpdate\Debug::init_filesystem
* @covers \AspireUpdate\Debug::verify_filesystem
* @covers \AspireUpdate\Debug::get_file_path
*/
public function test_should_return_wp_error_when_log_file_does_not_exist() {
$this->assertWPError(
AspireUpdate\Debug::clear(),
'A WP_Error object was not returned.'
);

$this->assertFileDoesNotExist(
self::$log_file,
'The log file was created.'
);
}

/**
* Test that a WP_Error object is returned when the log file isn't writable.
*
* @covers \AspireUpdate\Debug::init_filesystem
* @covers \AspireUpdate\Debug::verify_filesystem
* @covers \AspireUpdate\Debug::get_file_path
*/
public function test_should_return_wp_error_when_log_file_is_not_writable() {
global $wp_filesystem;

// Backup and replace the filesystem object.
$wp_filesystem = $this->get_fake_filesystem( true, true, false );

$actual = AspireUpdate\Debug::clear();

$this->assertWPError(
$actual,
'A WP_Error was not returned.'
);

$this->assertFileDoesNotExist(
self::$log_file,
'The log file was created.'
);
}

/**
* Test that the log file is cleared.
*/
public function test_should_clear_log_file() {
file_put_contents(
self::$log_file,
"First line\r\nSecond line\r\nThird line"
);

$this->assertFileExists(
self::$log_file,
'The log file was not created before testing.'
);

AspireUpdate\Debug::clear();

$this->assertFileExists(
self::$log_file,
'The log file was deleted.'
);

$this->assertSame(
'',
file_get_contents( self::$log_file ),
'The log file was not cleared.'
);
}
}
Loading