|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class Debug_LogTest |
| 4 | + * |
| 5 | + * @package AspireUpdate |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Tests for Debug::log() |
| 10 | + * |
| 11 | + * @covers \AspireUpdate\Debug::log |
| 12 | + */ |
| 13 | +class Debug_LogTest extends Debug_UnitTestCase { |
| 14 | + /** |
| 15 | + * Test that nothing is written to the log file when the filesystem isn't available. |
| 16 | + * |
| 17 | + * @covers \AspireUpdate\Debug::init_filesystem |
| 18 | + * @covers \AspireUpdate\Debug::verify_filesystem |
| 19 | + */ |
| 20 | + public function test_should_not_write_to_log_file_when_filesystem_is_not_available() { |
| 21 | + add_filter( 'filesystem_method', '__return_false' ); |
| 22 | + |
| 23 | + AspireUpdate\Debug::log( 'Test log message.' ); |
| 24 | + |
| 25 | + $this->assertFileDoesNotExist( |
| 26 | + self::$log_file, |
| 27 | + 'The log file was created.' |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Test that the log file is created when it doesn't already exist. |
| 33 | + */ |
| 34 | + public function test_should_create_log_file_if_it_does_not_already_exist() { |
| 35 | + $this->assertFileDoesNotExist( |
| 36 | + self::$log_file, |
| 37 | + 'The log file already exists before testing.' |
| 38 | + ); |
| 39 | + |
| 40 | + $message = 'Test log message.'; |
| 41 | + |
| 42 | + AspireUpdate\Debug::log( $message ); |
| 43 | + |
| 44 | + $this->assertFileExists( |
| 45 | + self::$log_file, |
| 46 | + 'The log file was not created.' |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Test that the message is added to the log file. |
| 52 | + * |
| 53 | + * @covers \AspireUpdate\Debug::format_message |
| 54 | + */ |
| 55 | + public function test_should_add_message_to_log_file() { |
| 56 | + $this->assertFileDoesNotExist( |
| 57 | + self::$log_file, |
| 58 | + 'The log file already exists before testing.' |
| 59 | + ); |
| 60 | + |
| 61 | + $message = 'Test log message.'; |
| 62 | + |
| 63 | + AspireUpdate\Debug::log( $message ); |
| 64 | + |
| 65 | + $this->assertFileExists( |
| 66 | + self::$log_file, |
| 67 | + 'The log file was not created.' |
| 68 | + ); |
| 69 | + |
| 70 | + $this->assertStringContainsString( |
| 71 | + $message, |
| 72 | + file_get_contents( self::$log_file ), |
| 73 | + 'The message was not added.' |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test that the message is prepended to an existing log file. |
| 79 | + * |
| 80 | + * @covers \AspireUpdate\Debug::format_message |
| 81 | + */ |
| 82 | + public function test_should_add_message_to_an_existing_log_file() { |
| 83 | + $existing_content = 'An existing log file.'; |
| 84 | + file_put_contents( self::$log_file, $existing_content ); |
| 85 | + |
| 86 | + $message = 'Test log message.'; |
| 87 | + |
| 88 | + AspireUpdate\Debug::log( $message ); |
| 89 | + |
| 90 | + $this->assertFileExists( |
| 91 | + self::$log_file, |
| 92 | + 'The log file was not created.' |
| 93 | + ); |
| 94 | + |
| 95 | + $this->assertStringContainsString( |
| 96 | + "$message\n$existing_content", |
| 97 | + file_get_contents( self::$log_file ), |
| 98 | + 'The message was not prepended to the log file.' |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Test that the message is prefixed with the timestamp. |
| 104 | + * |
| 105 | + * @covers \AspireUpdate\Debug::format_message |
| 106 | + */ |
| 107 | + public function test_should_prefix_message_with_timestamp() { |
| 108 | + AspireUpdate\Debug::log( 'Test log message.' ); |
| 109 | + |
| 110 | + $this->assertFileExists( |
| 111 | + self::$log_file, |
| 112 | + 'The log file was not created.' |
| 113 | + ); |
| 114 | + |
| 115 | + $this->assertMatchesRegularExpression( |
| 116 | + '/^\[[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\]/', |
| 117 | + file_get_contents( self::$log_file ), |
| 118 | + 'The message was not prefixed with the timestamp.' |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Test that the message is prefixed with its type. |
| 124 | + * |
| 125 | + * @dataProvider data_message_types |
| 126 | + * |
| 127 | + * @covers \AspireUpdate\Debug::format_message |
| 128 | + * |
| 129 | + * @param string $type The type of message. |
| 130 | + */ |
| 131 | + public function test_should_prefix_message_with_type( $type ) { |
| 132 | + $message = 'Test log message.'; |
| 133 | + |
| 134 | + AspireUpdate\Debug::log( $message, $type ); |
| 135 | + |
| 136 | + $this->assertFileExists( |
| 137 | + self::$log_file, |
| 138 | + 'The log file was not created.' |
| 139 | + ); |
| 140 | + |
| 141 | + $this->assertStringContainsString( |
| 142 | + '[' . strtoupper( $type ) . ']: ' . $message, |
| 143 | + file_get_contents( self::$log_file ), |
| 144 | + 'The message was not prefixed with its type.' |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Data provider. |
| 150 | + * |
| 151 | + * @return array[] |
| 152 | + */ |
| 153 | + public function data_message_types() { |
| 154 | + return $this->text_array_to_dataprovider( |
| 155 | + [ |
| 156 | + 'string', |
| 157 | + 'request', |
| 158 | + 'response', |
| 159 | + 'custom', |
| 160 | + ] |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Test that array and object messages are expanded. |
| 166 | + * |
| 167 | + * @dataProvider data_arrays_and_objects |
| 168 | + * |
| 169 | + * @covers \AspireUpdate\Debug::format_message |
| 170 | + * |
| 171 | + * @param array|object $message The message. |
| 172 | + */ |
| 173 | + public function test_should_expand_array_or_object_messages( $message ) { |
| 174 | + AspireUpdate\Debug::log( $message ); |
| 175 | + |
| 176 | + $this->assertFileExists( |
| 177 | + self::$log_file, |
| 178 | + 'The log file was not created.' |
| 179 | + ); |
| 180 | + |
| 181 | + $this->assertStringContainsString( |
| 182 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 183 | + print_r( $message, true ), |
| 184 | + file_get_contents( self::$log_file ), |
| 185 | + 'The array message was not expanded.' |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Data provider. |
| 191 | + * |
| 192 | + * @return array[] |
| 193 | + */ |
| 194 | + public function data_arrays_and_objects() { |
| 195 | + return [ |
| 196 | + 'an array' => [ |
| 197 | + 'message' => [], |
| 198 | + ], |
| 199 | + 'a non-empty array' => [ |
| 200 | + 'message' => [ 'First line', 'Second line', 'Third line' ], |
| 201 | + ], |
| 202 | + 'an object with no properties' => [ |
| 203 | + 'message' => (object) [], |
| 204 | + ], |
| 205 | + 'an object with properties' => [ |
| 206 | + 'message' => (object) [ 'First line', 'Second line', 'Third line' ], |
| 207 | + ], |
| 208 | + ]; |
| 209 | + } |
| 210 | +} |
0 commit comments