|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class Debug_LogStringTest |
| 4 | + * |
| 5 | + * @package AspireUpdate |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Tests for Debug::log_string() |
| 10 | + * |
| 11 | + * These tests cause constants to be defined. |
| 12 | + * They must run in separate processes and must not preserve global state. |
| 13 | + * |
| 14 | + * @runTestsInSeparateProcesses |
| 15 | + * @preserveGlobalState disabled |
| 16 | + * |
| 17 | + * @covers \AspireUpdate\Debug::log_string |
| 18 | + */ |
| 19 | +class Debug_LogStringTest extends Debug_UnitTestCase { |
| 20 | + /** |
| 21 | + * Test that nothing is written to the log file when debugging is disabled. |
| 22 | + */ |
| 23 | + public function test_should_not_write_to_log_file_when_debugging_is_disabled() { |
| 24 | + define( 'AP_DEBUG', false ); |
| 25 | + define( 'AP_DEBUG_TYPES', [ 'request', 'response', 'string' ] ); |
| 26 | + |
| 27 | + AspireUpdate\Debug::log_string( 'Test log message.' ); |
| 28 | + |
| 29 | + $this->assertFileDoesNotExist( self::$log_file ); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Test that nothing is written to the log file when debug types are not an array. |
| 34 | + */ |
| 35 | + public function test_should_not_write_to_log_file_when_debug_types_are_not_an_array() { |
| 36 | + define( 'AP_DEBUG', true ); |
| 37 | + define( 'AP_DEBUG_TYPES', 'string' ); |
| 38 | + |
| 39 | + AspireUpdate\Debug::log_string( 'Test log message.' ); |
| 40 | + |
| 41 | + $this->assertFileDoesNotExist( self::$log_file ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test that nothing is written to the log file when string debugging is disabled. |
| 46 | + */ |
| 47 | + public function test_should_not_write_to_log_file_when_string_debugging_is_disabled() { |
| 48 | + define( 'AP_DEBUG', true ); |
| 49 | + define( 'AP_DEBUG_TYPES', [ 'request', 'response' ] ); |
| 50 | + |
| 51 | + AspireUpdate\Debug::log_string( 'Test log message.' ); |
| 52 | + |
| 53 | + $this->assertFileDoesNotExist( self::$log_file ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test that the message is written to the log file. |
| 58 | + * |
| 59 | + * @dataProvider data_debug_types |
| 60 | + * |
| 61 | + * @param array $debug_types An array of enabled debug types. |
| 62 | + */ |
| 63 | + public function test_should_write_to_log_file( $debug_types ) { |
| 64 | + define( 'AP_DEBUG', true ); |
| 65 | + define( 'AP_DEBUG_TYPES', $debug_types ); |
| 66 | + |
| 67 | + $message = 'Test log message.'; |
| 68 | + |
| 69 | + AspireUpdate\Debug::log_string( $message ); |
| 70 | + |
| 71 | + $this->assertFileExists( |
| 72 | + self::$log_file, |
| 73 | + 'The log file was created.' |
| 74 | + ); |
| 75 | + |
| 76 | + $this->assertStringContainsString( |
| 77 | + $message, |
| 78 | + file_get_contents( self::$log_file ), |
| 79 | + 'The message was not logged.' |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Data provider. |
| 85 | + * |
| 86 | + * @return array[] |
| 87 | + */ |
| 88 | + public function data_debug_types() { |
| 89 | + return [ |
| 90 | + 'just "string"' => [ |
| 91 | + 'debug_types' => [ 'string' ], |
| 92 | + ], |
| 93 | + '"string" at the start of the array"' => [ |
| 94 | + 'debug_types' => [ 'string', 'request' ], |
| 95 | + ], |
| 96 | + '"string" in the middle of the array"' => [ |
| 97 | + 'debug_types' => [ 'request', 'string', 'response' ], |
| 98 | + ], |
| 99 | + '"string" at the end of the array"' => [ |
| 100 | + 'debug_types' => [ 'request', 'response', 'string' ], |
| 101 | + ], |
| 102 | + ]; |
| 103 | + } |
| 104 | +} |
0 commit comments