-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for
Debug::log_string()
. (#248)
- Loading branch information
Showing
1 changed file
with
104 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
/** | ||
* Class Debug_LogStringTest | ||
* | ||
* @package AspireUpdate | ||
*/ | ||
|
||
/** | ||
* Tests for Debug::log_string() | ||
* | ||
* These tests cause constants to be defined. | ||
* They must run in separate processes and must not preserve global state. | ||
* | ||
* @runTestsInSeparateProcesses | ||
* @preserveGlobalState disabled | ||
* | ||
* @covers \AspireUpdate\Debug::log_string | ||
*/ | ||
class Debug_LogStringTest extends Debug_UnitTestCase { | ||
/** | ||
* Test that nothing is written to the log file when debugging is disabled. | ||
*/ | ||
public function test_should_not_write_to_log_file_when_debugging_is_disabled() { | ||
define( 'AP_DEBUG', false ); | ||
define( 'AP_DEBUG_TYPES', [ 'request', 'response', 'string' ] ); | ||
|
||
AspireUpdate\Debug::log_string( 'Test log message.' ); | ||
|
||
$this->assertFileDoesNotExist( self::$log_file ); | ||
} | ||
|
||
/** | ||
* Test that nothing is written to the log file when debug types are not an array. | ||
*/ | ||
public function test_should_not_write_to_log_file_when_debug_types_are_not_an_array() { | ||
define( 'AP_DEBUG', true ); | ||
define( 'AP_DEBUG_TYPES', 'string' ); | ||
|
||
AspireUpdate\Debug::log_string( 'Test log message.' ); | ||
|
||
$this->assertFileDoesNotExist( self::$log_file ); | ||
} | ||
|
||
/** | ||
* Test that nothing is written to the log file when string debugging is disabled. | ||
*/ | ||
public function test_should_not_write_to_log_file_when_string_debugging_is_disabled() { | ||
define( 'AP_DEBUG', true ); | ||
define( 'AP_DEBUG_TYPES', [ 'request', 'response' ] ); | ||
|
||
AspireUpdate\Debug::log_string( 'Test log message.' ); | ||
|
||
$this->assertFileDoesNotExist( self::$log_file ); | ||
} | ||
|
||
/** | ||
* Test that the message is written to the log file. | ||
* | ||
* @dataProvider data_debug_types | ||
* | ||
* @param array $debug_types An array of enabled debug types. | ||
*/ | ||
public function test_should_write_to_log_file( $debug_types ) { | ||
define( 'AP_DEBUG', true ); | ||
define( 'AP_DEBUG_TYPES', $debug_types ); | ||
|
||
$message = 'Test log message.'; | ||
|
||
AspireUpdate\Debug::log_string( $message ); | ||
|
||
$this->assertFileExists( | ||
self::$log_file, | ||
'The log file was created.' | ||
); | ||
|
||
$this->assertStringContainsString( | ||
$message, | ||
file_get_contents( self::$log_file ), | ||
'The message was not logged.' | ||
); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array[] | ||
*/ | ||
public function data_debug_types() { | ||
return [ | ||
'just "string"' => [ | ||
'debug_types' => [ 'string' ], | ||
], | ||
'"string" at the start of the array"' => [ | ||
'debug_types' => [ 'string', 'request' ], | ||
], | ||
'"string" in the middle of the array"' => [ | ||
'debug_types' => [ 'request', 'string', 'response' ], | ||
], | ||
'"string" at the end of the array"' => [ | ||
'debug_types' => [ 'request', 'response', 'string' ], | ||
], | ||
]; | ||
} | ||
} |