diff --git a/lib/cli/table/Tabular.php b/lib/cli/table/Tabular.php index 5132e55..0675b4c 100644 --- a/lib/cli/table/Tabular.php +++ b/lib/cli/table/Tabular.php @@ -27,6 +27,7 @@ public function row( array $row ) { $output = ''; foreach ( $row as $col => $value ) { + $value = isset( $value ) ? (string) $value : ''; $value = str_replace( "\t", ' ', $value ); $split_lines = preg_split( '/\r\n|\n/', $value ); // Keep anything before the first line break on the original line diff --git a/tests/Test_Table.php b/tests/Test_Table.php index 964ec2e..26db650 100644 --- a/tests/Test_Table.php +++ b/tests/Test_Table.php @@ -267,4 +267,26 @@ public function test_preserve_trailing_tabs() { $this->assertSame( $expected, $out, 'Trailing tabs should be preserved in table output.' ); } + public function test_null_values_are_handled() { + $table = new cli\Table(); + $renderer = new cli\Table\Tabular(); + $table->setRenderer( $renderer ); + + $table->setHeaders( array( 'Field', 'Type', 'Null', 'Key', 'Default', 'Extra' ) ); + + // Add row with a null value in the middle + $table->addRow( array( 'id', 'int', 'NO', 'PRI', null, 'auto_increment' ) ); + + // Add row with a null value at the end + $table->addRow( array( 'name', 'varchar(255)', 'YES', '', 'NULL', null ) ); + + $out = $table->getDisplayLines(); + + $expected = [ + "Field\tType\tNull\tKey\tDefault\tExtra", + "id\tint\tNO\tPRI\t\tauto_increment", + "name\tvarchar(255)\tYES\t\tNULL\t", + ]; + $this->assertSame( $expected, $out, 'Null values should be safely converted to empty strings in table output.' ); + } }