From 03d2f556d89cde4693084be1871229ad8f121962 Mon Sep 17 00:00:00 2001 From: Colin Stewart <79332690+costdev@users.noreply.github.com> Date: Thu, 19 Dec 2024 17:54:29 +0000 Subject: [PATCH] Add tests for `Debug::log_request()`. (#249) --- .../tests/Debug/Debug_LogRequestTest.php | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 tests/phpunit/tests/Debug/Debug_LogRequestTest.php diff --git a/tests/phpunit/tests/Debug/Debug_LogRequestTest.php b/tests/phpunit/tests/Debug/Debug_LogRequestTest.php new file mode 100644 index 0000000..e16ba57 --- /dev/null +++ b/tests/phpunit/tests/Debug/Debug_LogRequestTest.php @@ -0,0 +1,104 @@ +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', 'request' ); + + AspireUpdate\Debug::log_request( 'Test log message.' ); + + $this->assertFileDoesNotExist( self::$log_file ); + } + + /** + * Test that nothing is written to the log file when request debugging is disabled. + */ + public function test_should_not_write_to_log_file_when_request_debugging_is_disabled() { + define( 'AP_DEBUG', true ); + define( 'AP_DEBUG_TYPES', [ 'response', 'string' ] ); + + AspireUpdate\Debug::log_request( '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_request( $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 "request"' => [ + 'debug_types' => [ 'request' ], + ], + '"request" at the start of the array"' => [ + 'debug_types' => [ 'request', 'response' ], + ], + '"request" in the middle of the array"' => [ + 'debug_types' => [ 'string', 'request', 'response' ], + ], + '"request" at the end of the array"' => [ + 'debug_types' => [ 'string', 'response', 'request' ], + ], + ]; + } +}