-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andy Fragen <[email protected]>
- Loading branch information
Showing
13 changed files
with
1,046 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
/** | ||
* Abstract base test class for \AspireUpdate\Debug. | ||
* | ||
* All \AspireUpdate\Debug unit tests should inherit from this class. | ||
*/ | ||
abstract class Debug_UnitTestCase extends WP_UnitTestCase { | ||
/** | ||
* The path to the log file. | ||
* | ||
* @var string | ||
*/ | ||
protected static $log_file; | ||
|
||
/** | ||
* Previously created filesystems. | ||
* | ||
* @var array | ||
*/ | ||
protected static $filesystems = []; | ||
|
||
/** | ||
* The original value of $wp_filesystem before any tests run. | ||
* | ||
* @var WP_Filesystem_Base|null|false False if not already set. | ||
*/ | ||
protected static $default_filesystem; | ||
|
||
/** | ||
* Gets the log file's path, and deletes if it exists before any tests run. | ||
* Backs up the default filesystem. | ||
* | ||
* @return void | ||
*/ | ||
public static function set_up_before_class() { | ||
parent::set_up_before_class(); | ||
|
||
$get_file_path = new ReflectionMethod( | ||
'AspireUpdate\Debug', | ||
'get_file_path' | ||
); | ||
|
||
$get_file_path->setAccessible( true ); | ||
self::$log_file = $get_file_path->invoke( null ); | ||
$get_file_path->setAccessible( false ); | ||
|
||
if ( file_exists( self::$log_file ) ) { | ||
unlink( self::$log_file ); | ||
} | ||
|
||
if ( isset( $GLOBALS['wp_filesystem'] ) ) { | ||
self::$default_filesystem = $GLOBALS['wp_filesystem']; | ||
} else { | ||
self::$default_filesystem = false; | ||
} | ||
} | ||
|
||
/** | ||
* Filters the filesystem method before each test runs. | ||
* | ||
* Filters are removed in the tear_down() parent method. | ||
* | ||
* @return void | ||
*/ | ||
public function set_up() { | ||
parent::set_up(); | ||
|
||
add_filter( | ||
'filesystem_method', | ||
static function () { | ||
return 'direct'; | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Delete the log file and restores the filesystem after each test runs. | ||
* | ||
* @return void | ||
*/ | ||
public function tear_down() { | ||
if ( file_exists( self::$log_file ) ) { | ||
unlink( self::$log_file ); | ||
} | ||
|
||
if ( false === self::$default_filesystem ) { | ||
unset( $GLOBALS['wp_filesystem'] ); | ||
} else { | ||
$GLOBALS['wp_filesystem'] = self::$default_filesystem; | ||
} | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
/** | ||
* Creates a fake filesystem. | ||
* | ||
* @param bool|null $exists Whether paths should exist. | ||
* Default null uses default implemenation. | ||
* @param bool|null $is_readable Whether paths should be readable. | ||
* Default null uses default implemenation. | ||
* @param bool|null $is_writable Whether paths should be writable. | ||
* Default null uses default implemenation. | ||
*/ | ||
public function get_fake_filesystem( $exists = null, $is_readable = null, $is_writable = null ) { | ||
$hash = ( null === $exists ? '-1' : (int) $exists ) . ',' . | ||
( null === $is_readable ? '-1' : (int) $is_readable ) . ',' . | ||
( null === $is_writable ? '-1' : (int) $is_writable ); | ||
|
||
if ( ! isset( self::$filesystems[ $hash ] ) ) { | ||
self::$filesystems[ $hash ] = new AP_FakeFilesystem( $exists, $is_readable, $is_writable ); | ||
} | ||
|
||
return self::$filesystems[ $hash ]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; | ||
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; | ||
|
||
class AP_FakeFilesystem extends WP_Filesystem_Direct { | ||
/** | ||
* Whether paths should exist. | ||
* | ||
* @var bool|null | ||
*/ | ||
private $exists; | ||
|
||
/** | ||
* Whether paths should be readable. | ||
* | ||
* @var bool|null | ||
*/ | ||
private $is_readable; | ||
|
||
/** | ||
* Whether paths should be writable. | ||
* | ||
* @var bool|null | ||
*/ | ||
private $is_writable; | ||
|
||
/** | ||
* Contructor. | ||
* | ||
* @param bool|null $exists Whether paths should exist. | ||
* Default null to use parent implementation. | ||
* @param bool|null $is_readable Whether paths should be readable. | ||
* Default null to use parent implementation. | ||
* @param bool|null $is_writable Whether paths should be writable. | ||
* Default null to use parent implementation. | ||
*/ | ||
public function __construct( $exists = null, $is_readable = null, $is_writable = null ) { | ||
$this->exists = $exists; | ||
$this->is_readable = $is_readable; | ||
$this->is_writable = $is_writable; | ||
} | ||
|
||
/** | ||
* Checks whether a path exists. | ||
* | ||
* @param string $path The path to check. | ||
* @return bool Whether the path exists. | ||
*/ | ||
public function exists( $path ) { | ||
if ( null === $this->exists ) { | ||
return parent::exists( $path ); | ||
} | ||
return $this->exists; | ||
} | ||
|
||
/** | ||
* Checks whether a path is readable. | ||
* | ||
* @param string $path The path to check. | ||
* @return bool Whether the path is readable. | ||
*/ | ||
public function is_readable( $path ) { | ||
if ( null === $this->is_readable ) { | ||
return parent::is_readable( $path ); | ||
} | ||
return $this->is_readable; | ||
} | ||
|
||
/** | ||
* Checks whether a path is writable. | ||
* | ||
* @param string $path The path to check. | ||
* @return bool Whether the path is writable. | ||
*/ | ||
public function is_writable( $path ) { | ||
if ( null === $this->is_writable ) { | ||
return parent::is_writable( $path ); | ||
} | ||
return $this->is_writable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
); | ||
} | ||
} |
Oops, something went wrong.