From a03fa80cfcae970d2a1ab8ed6776e247e067d03f Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari <2908547+adhocore@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:28:47 +0545 Subject: [PATCH] Apply fixes from StyleCI (#108) [ci skip] [skip ci] Co-authored-by: Jitendra --- src/Output/Table.php | 14 +- src/Output/Writer.php | 4 +- tests/Output/TableTest.php | 361 +++++++++++++++++++------------------ 3 files changed, 191 insertions(+), 188 deletions(-) diff --git a/src/Output/Table.php b/src/Output/Table.php index 5f9c441..19400a2 100644 --- a/src/Output/Table.php +++ b/src/Output/Table.php @@ -48,7 +48,7 @@ public function render(array $rows, array $styles = []): string $title = $body = $dash = $positions = []; [$start, $end] = $styles['head']; - $pos = 0; + $pos = 0; foreach ($head as $col => $size) { $dash[] = str_repeat('-', $size + 2); $title[] = str_pad($this->toWords($col), $size, ' '); @@ -68,11 +68,11 @@ public function render(array $rows, array $styles = []): string if (isset($styles[$line . ':' . $colNumber])) { // cell, 1:1 $style = $styles[$line . ':' . $colNumber]; - } else if (isset($styles[$col]) || isset($styles['*:' . $colNumber])) { // col, *:2 or b + } elseif (isset($styles[$col]) || isset($styles['*:' . $colNumber])) { // col, *:2 or b $style = $styles['*:' . $colNumber] ?? $styles[$col]; - } else if (isset($styles[$line . ':*'])) { // row, 2:* + } elseif (isset($styles[$line . ':*'])) { // row, 2:* $style = $styles[$line . ':*']; - } else if (isset($styles['*:*'])) { // any cell, *:* + } elseif (isset($styles['*:*'])) { // any cell, *:* $style = $styles['*:*']; } else { $style = $styles[['even', 'odd'][(int) $odd]]; @@ -121,7 +121,7 @@ protected function normalize(array $rows): array foreach ($head as $col => &$value) { $cols = array_column($rows, $col); - $cols = array_map(function($col) { + $cols = array_map(function ($col) { $col ??= ''; if (preg_match('/(\\x1b(?:.+)m)/U', $col, $matches)) { @@ -152,8 +152,8 @@ protected function normalizeStyles(array $styles): array foreach ($styles as $for => $style) { if (is_string($style) && $style !== '') { $default[$for] = ['<' . trim($style, '<> ') . '>', '']; - } else if (str_contains($for, ':') && is_callable($style)) { - $default[$for] = $style; + } elseif (str_contains($for, ':') && is_callable($style)) { + $default[$for] = $style; } } diff --git a/src/Output/Writer.php b/src/Output/Writer.php index 35bf90f..626978f 100644 --- a/src/Output/Writer.php +++ b/src/Output/Writer.php @@ -321,9 +321,9 @@ public function justify(string $first, ?string $second = null, array $options = 'sep' => $options['sep'] ?? '.', ]; - $second = (string) $second; + $second = (string) $second; $terminalWidth = $this->terminal->width() ?? 80; - $dashWidth = $terminalWidth - (strlen($first) + strlen($second)); + $dashWidth = $terminalWidth - (strlen($first) + strlen($second)); // remove left and right margins because we're going to add 1 space on each side (after/before the text). // if we don't have a second element, we just remove the left margin $dashWidth -= $second === '' ? 1 : 2; diff --git a/tests/Output/TableTest.php b/tests/Output/TableTest.php index 90f4b09..87fc6a2 100644 --- a/tests/Output/TableTest.php +++ b/tests/Output/TableTest.php @@ -35,13 +35,13 @@ public function test_render_returns_empty_string_for_empty_rows(): void public function test_render_with_single_row_and_column(): void { - $rows = [['header' => 'values']]; + $rows = [['header' => 'values']]; $expectedOutput = - "+--------+" . PHP_EOL . - "| Header |" . PHP_EOL . - "+--------+" . PHP_EOL . - "| values |" . PHP_EOL . - "+--------+"; + '+--------+' . PHP_EOL . + '| Header |' . PHP_EOL . + '+--------+' . PHP_EOL . + '| values |' . PHP_EOL . + '+--------+'; $result = $this->table->render($rows); @@ -53,17 +53,17 @@ public function test_render_with_single_column(): void $rows = [ ['name' => 'John Doe'], ['name' => 'Jane Smith'], - ['name' => 'Bob Johnson'] + ['name' => 'Bob Johnson'], ]; $expectedOutput = - "+-------------+" . PHP_EOL . - "| Name |" . PHP_EOL . - "+-------------+" . PHP_EOL . - "| John Doe |" . PHP_EOL . - "| Jane Smith |" . PHP_EOL . - "| Bob Johnson |" . PHP_EOL . - "+-------------+"; + '+-------------+' . PHP_EOL . + '| Name |' . PHP_EOL . + '+-------------+' . PHP_EOL . + '| John Doe |' . PHP_EOL . + '| Jane Smith |' . PHP_EOL . + '| Bob Johnson |' . PHP_EOL . + '+-------------+'; $result = $this->table->render($rows); @@ -75,17 +75,17 @@ public function test_render_with_multiple_rows_and_columns(): void $rows = [ ['name' => 'John Doe', 'age' => '30', 'city' => 'New York'], ['name' => 'Jane Smith', 'age' => '25', 'city' => 'Los Angeles'], - ['name' => 'Bob Johnson', 'age' => '40', 'city' => 'Chicago'] + ['name' => 'Bob Johnson', 'age' => '40', 'city' => 'Chicago'], ]; $expectedOutput = - "+-------------+-----+-------------+" . PHP_EOL . - "| Name | Age | City |" . PHP_EOL . - "+-------------+-----+-------------+" . PHP_EOL . - "| John Doe | 30 | New York |" . PHP_EOL . - "| Jane Smith | 25 | Los Angeles |" . PHP_EOL . - "| Bob Johnson | 40 | Chicago |" . PHP_EOL . - "+-------------+-----+-------------+" ; + '+-------------+-----+-------------+' . PHP_EOL . + '| Name | Age | City |' . PHP_EOL . + '+-------------+-----+-------------+' . PHP_EOL . + '| John Doe | 30 | New York |' . PHP_EOL . + '| Jane Smith | 25 | Los Angeles |' . PHP_EOL . + '| Bob Johnson | 40 | Chicago |' . PHP_EOL . + '+-------------+-----+-------------+'; $result = $this->table->render($rows); @@ -97,22 +97,22 @@ public function test_render_with_different_styles_for_odd_and_even_rows(): void $rows = [ ['name' => 'John Doe', 'age' => '30'], ['name' => 'Jane Smith', 'age' => '25'], - ['name' => 'Bob Johnson', 'age' => '40'] + ['name' => 'Bob Johnson', 'age' => '40'], ]; $styles = [ - 'odd' => 'bold', - 'even' => 'comment' + 'odd' => 'bold', + 'even' => 'comment', ]; $expectedOutput = - "+-------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+-------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "| Bob Johnson | 40 |" . PHP_EOL . - "+-------------+-----+"; + '+-------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+-------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '| Bob Johnson | 40 |' . PHP_EOL . + '+-------------+-----+'; $result = $this->table->render($rows, $styles); @@ -124,17 +124,17 @@ public function test_render_with_padded_column_content(): void $rows = [ ['name' => 'John', 'age' => '30'], ['name' => 'Jane Smith', 'age' => '25'], - ['name' => 'Bob', 'age' => '40'] + ['name' => 'Bob', 'age' => '40'], ]; $expectedOutput = - "+------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+------------+-----+" . PHP_EOL . - "| John | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "| Bob | 40 |" . PHP_EOL . - "+------------+-----+"; + '+------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+------------+-----+' . PHP_EOL . + '| John | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '| Bob | 40 |' . PHP_EOL . + '+------------+-----+'; $result = $this->table->render($rows); @@ -145,23 +145,23 @@ public function test_render_generates_correct_separators_between_header_and_body { $rows = [ ['name' => 'John Doe', 'age' => '30'], - ['name' => 'Jane Smith', 'age' => '25'] + ['name' => 'Jane Smith', 'age' => '25'], ]; $expectedOutput = - "+------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "+------------+-----+"; + '+------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '+------------+-----+'; $result = $this->table->render($rows); - $this->assertStringContainsString("+------------+-----+" . PHP_EOL, $result); - $this->assertStringContainsString("| Name | Age |" . PHP_EOL, $result); - $this->assertStringContainsString("+------------+-----+" . PHP_EOL, $result); - $this->assertEquals(3, substr_count($result, "+------------+-----+" . PHP_EOL)); + $this->assertStringContainsString('+------------+-----+' . PHP_EOL, $result); + $this->assertStringContainsString('| Name | Age |' . PHP_EOL, $result); + $this->assertStringContainsString('+------------+-----+' . PHP_EOL, $result); + $this->assertEquals(3, substr_count($result, '+------------+-----+' . PHP_EOL)); $this->assertSame($expectedOutput, trim($result)); } @@ -170,17 +170,17 @@ public function test_render_handles_missing_values_in_rows_gracefully(): void $rows = [ ['name' => 'John Doe', 'age' => '30', 'city' => 'New York'], ['name' => 'Jane Smith', 'age' => '25'], - ['name' => 'Bob Johnson', 'city' => 'Chicago'] + ['name' => 'Bob Johnson', 'city' => 'Chicago'], ]; $expectedOutput = - "+-------------+-----+----------+" . PHP_EOL . - "| Name | Age | City |" . PHP_EOL . - "+-------------+-----+----------+" . PHP_EOL . - "| John Doe | 30 | New York |" . PHP_EOL . - "| Jane Smith | 25 | |" . PHP_EOL . - "| Bob Johnson | | Chicago |" . PHP_EOL . - "+-------------+-----+----------+"; + '+-------------+-----+----------+' . PHP_EOL . + '| Name | Age | City |' . PHP_EOL . + '+-------------+-----+----------+' . PHP_EOL . + '| John Doe | 30 | New York |' . PHP_EOL . + '| Jane Smith | 25 | |' . PHP_EOL . + '| Bob Johnson | | Chicago |' . PHP_EOL . + '+-------------+-----+----------+'; $result = $this->table->render($rows); @@ -191,16 +191,16 @@ public function test_render_converts_column_names_to_words(): void { $rows = [ ['first_name' => 'John', 'last_name' => 'Doe', 'age_in_years' => '30'], - ['first_name' => 'Jane', 'last_name' => 'Smith', 'age_in_years' => '25'] + ['first_name' => 'Jane', 'last_name' => 'Smith', 'age_in_years' => '25'], ]; $expectedOutput = - "+------------+-----------+--------------+" . PHP_EOL . - "| First Name | Last Name | Age In Years |" . PHP_EOL . - "+------------+-----------+--------------+" . PHP_EOL . - "| John | Doe | 30 |" . PHP_EOL . - "| Jane | Smith | 25 |" . PHP_EOL . - "+------------+-----------+--------------+"; + '+------------+-----------+--------------+' . PHP_EOL . + '| First Name | Last Name | Age In Years |' . PHP_EOL . + '+------------+-----------+--------------+' . PHP_EOL . + '| John | Doe | 30 |' . PHP_EOL . + '| Jane | Smith | 25 |' . PHP_EOL . + '+------------+-----------+--------------+'; $result = $this->table->render($rows); @@ -224,18 +224,18 @@ public function test_render_with_custom_styles(): void ]; $expectedOutput = - "+------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "+------------+-----+"; + '+------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '+------------+-----+'; $result = $this->table->render($rows, $styles); - $this->assertStringContainsString("", $result); - $this->assertStringContainsString("", $result); - $this->assertStringContainsString("", $result); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('', $result); $this->assertSame($expectedOutput, trim($result)); } @@ -244,17 +244,17 @@ public function test_render_with_ansi_color_codes_in_cell_content(): void $rows = [ ['name' => "\033[31mJohn Doe\033[0m", 'age' => '30'], ['name' => 'Jane Smith', 'age' => "\033[32m25\033[0m"], - ['name' => "\033[34mBob Johnson\033[0m", 'age' => '40'] + ['name' => "\033[34mBob Johnson\033[0m", 'age' => '40'], ]; $expectedOutput = - "+-------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+-------------+-----+" . PHP_EOL . + '+-------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+-------------+-----+' . PHP_EOL . "| \033[31mJohn Doe\033[0m | 30 |" . PHP_EOL . "| Jane Smith | \033[32m25\033[0m |" . PHP_EOL . "| \033[34mBob Johnson\033[0m | 40 |" . PHP_EOL . - "+-------------+-----+"; + '+-------------+-----+'; $result = $this->table->render($rows); @@ -268,17 +268,17 @@ public function test_render_with_ansi_color_codes_in_cell_content_using_colors_c $rows = [ ['name' => $color->error('John Doe'), 'age' => '30'], ['name' => 'Jane Smith', 'age' => $color->ok('25')], - ['name' => $color->info('Bob Johnson'), 'age' => '40'] + ['name' => $color->info('Bob Johnson'), 'age' => '40'], ]; $expectedOutput = - "+-------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+-------------+-----+" . PHP_EOL . + '+-------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+-------------+-----+' . PHP_EOL . "| \033[0;31mJohn Doe\033[0m | 30 |" . PHP_EOL . "| Jane Smith | \033[0;32m25\033[0m |" . PHP_EOL . "| \033[0;34mBob Johnson\033[0m | 40 |" . PHP_EOL . - "+-------------+-----+"; + '+-------------+-----+'; $result = $this->table->render($rows); @@ -294,17 +294,17 @@ public function test_render_with_cell_specific_styles(): void $styles = [ 'head' => 'boldGreen', - '1:1' => 'boldRed', // Cell-specific style for first row, first column - '2:2' => 'boldBlue', // Cell-specific style for second row, second column + '1:1' => 'boldRed', // Cell-specific style for first row, first column + '2:2' => 'boldBlue', // Cell-specific style for second row, second column ]; $expectedOutput = - "+------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "+------------+-----+"; + '+------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '+------------+-----+'; $result = $this->table->render($rows, $styles); @@ -320,16 +320,16 @@ public function test_render_with_column_specific_styles(): void $styles = [ 'head' => 'boldGreen', - '*:2' => 'boldBlue', // Column-specific style for the second column + '*:2' => 'boldBlue', // Column-specific style for the second column ]; $expectedOutput = - "+------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "+------------+-----+"; + '+------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '+------------+-----+'; $result = $this->table->render($rows, $styles); @@ -346,17 +346,17 @@ public function test_render_with_row_specific_styles(): void $styles = [ 'head' => 'boldGreen', - '2:*' => 'boldRed', // Row-specific style for the second row + '2:*' => 'boldRed', // Row-specific style for the second row ]; $expectedOutput = - "+-------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+-------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "| Bob Johnson | 40 |" . PHP_EOL . - "+-------------+-----+"; + '+-------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+-------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '| Bob Johnson | 40 |' . PHP_EOL . + '+-------------+-----+'; $result = $this->table->render($rows, $styles); @@ -372,7 +372,7 @@ public function test_render_with_callable_styles(): void $styles = [ 'head' => 'boldGreen', - '1:1' => function ($val, $row, $table) { + '1:1' => function ($val, $row, $table) { return $val === 'John Doe' ? 'boldRed' : ''; }, '2:2' => function ($val, $row, $table) { @@ -381,12 +381,12 @@ public function test_render_with_callable_styles(): void ]; $expectedOutput = - "+------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "+------------+-----+"; + '+------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '+------------+-----+'; $result = $this->table->render($rows, $styles); @@ -403,7 +403,7 @@ public function test_render_with_callable_styles_using_row(): void $styles = [ 'head' => 'boldGreen', - '*:2' => function ($val, $row) { + '*:2' => function ($val, $row) { if ($val == 25) { return 'boldYellow'; } @@ -413,13 +413,13 @@ public function test_render_with_callable_styles_using_row(): void ]; $expectedOutput = - "+-------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+-------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "| Bob Johnson | 40 |" . PHP_EOL . - "+-------------+-----+"; + '+-------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+-------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '| Bob Johnson | 40 |' . PHP_EOL . + '+-------------+-----+'; $result = $this->table->render($rows, $styles); @@ -438,32 +438,34 @@ public function test_render_with_callable_styles_on_any_cell(): void $styles = [ 'head' => 'boldGreen', - '*:*' => function ($val, $row) { + '*:*' => function ($val, $row) { if ($val === 'Jane X') { return 'yellow'; } if ($val == 10) { return 'purple'; } + return $row['age'] >= 30 ? 'boldRed' : ''; }, ]; $expectedOutput = - "+-------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+-------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "| Alice Bob | 10 |" . PHP_EOL . - "| Bob Johnson | 40 |" . PHP_EOL . - "| Jane X | 50 |" . PHP_EOL . - "+-------------+-----+"; + '+-------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+-------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '| Alice Bob | 10 |' . PHP_EOL . + '| Bob Johnson | 40 |' . PHP_EOL . + '| Jane X | 50 |' . PHP_EOL . + '+-------------+-----+'; $result = $this->table->render($rows, $styles); $this->assertSame($expectedOutput, trim($result)); } + public function test_render_with_mixed_specific_styles(): void { $rows = [ @@ -474,19 +476,19 @@ public function test_render_with_mixed_specific_styles(): void $styles = [ 'head' => 'boldGreen', - '1:2' => 'boldRed', // Cell-specific style for first row, second column - '*:3' => 'boldBlue', // Column-specific style for the third column - '3:*' => 'italic', // Row-specific style for the third row + '1:2' => 'boldRed', // Cell-specific style for first row, second column + '*:3' => 'boldBlue', // Column-specific style for the third column + '3:*' => 'italic', // Row-specific style for the third row ]; $expectedOutput = - "+-------------+-----+-------------+" . PHP_EOL . - "| Name | Age | City |" . PHP_EOL . - "+-------------+-----+-------------+" . PHP_EOL . - "| John Doe | 30 | New York |" . PHP_EOL . - "| Jane Smith | 25 | Los Angeles |" . PHP_EOL . - "| Bob Johnson | 40 | Chicago |" . PHP_EOL . - "+-------------+-----+-------------+"; + '+-------------+-----+-------------+' . PHP_EOL . + '| Name | Age | City |' . PHP_EOL . + '+-------------+-----+-------------+' . PHP_EOL . + '| John Doe | 30 | New York |' . PHP_EOL . + '| Jane Smith | 25 | Los Angeles |' . PHP_EOL . + '| Bob Johnson | 40 | Chicago |' . PHP_EOL . + '+-------------+-----+-------------+'; $result = $this->table->render($rows, $styles); @@ -503,19 +505,19 @@ public function test_render_with_styles_using_column_name(): void $styles = [ 'head' => 'boldGreen', - '1:2' => 'boldRed', // Cell-specific style for first row, second column + '1:2' => 'boldRed', // Cell-specific style for first row, second column 'city' => 'boldBlue', 'name' => 'italic', ]; $expectedOutput = - "+-------------+-----+-------------+" . PHP_EOL . - "| Name | Age | City |" . PHP_EOL . - "+-------------+-----+-------------+" . PHP_EOL . - "| John Doe | 30 | New York |" . PHP_EOL . - "| Jane Smith | 25 | Los Angeles |" . PHP_EOL . - "| Bob Johnson | 40 | Chicago |" . PHP_EOL . - "+-------------+-----+-------------+"; + '+-------------+-----+-------------+' . PHP_EOL . + '| Name | Age | City |' . PHP_EOL . + '+-------------+-----+-------------+' . PHP_EOL . + '| John Doe | 30 | New York |' . PHP_EOL . + '| Jane Smith | 25 | Los Angeles |' . PHP_EOL . + '| Bob Johnson | 40 | Chicago |' . PHP_EOL . + '+-------------+-----+-------------+'; $result = $this->table->render($rows, $styles); @@ -530,12 +532,12 @@ public function test_render_with_empty_styles_array(): void ]; $expectedOutput = - "+------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "+------------+-----+"; + '+------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '+------------+-----+'; $result = $this->table->render($rows, []); @@ -551,29 +553,30 @@ public function test_render_handles_invalid_style_keys_gracefully(): void $invalidStyles = [ 'invalidKey' => 'boldRed', // Invalid style key - 'head' => 'boldGreen', + 'head' => 'boldGreen', ]; $expectedOutput = - "+------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "+------------+-----+"; + '+------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '+------------+-----+'; $result = $this->table->render($rows, $invalidStyles); $this->assertSame($expectedOutput, trim($result)); } + public function test_render_with_large_number_of_columns(): void { $columns = 100; - $rows = [ + $rows = [ array_combine( - array_map(fn($i) => "col$i", range(1, $columns)), - array_map(fn($i) => "value$i", range(1, $columns)) - ) + array_map(fn ($i) => "col$i", range(1, $columns)), + array_map(fn ($i) => "value$i", range(1, $columns)) + ), ]; $result = $this->table->render($rows); @@ -595,9 +598,9 @@ public function test_render_handles_large_number_of_rows(): void $rows = []; for ($i = 0; $i < 1000; $i++) { $rows[] = [ - 'id' => $i, - 'name' => "Name $i", - 'email' => "email$i@example.com" + 'id' => $i, + 'name' => "Name $i", + 'email' => "email$i@example.com", ]; } @@ -614,7 +617,7 @@ public function test_render_with_html_like_tags_in_cell_content(): void $rows = [ ['name' => 'John Doe', 'age' => '30'], ['name' => 'Jane Smith', 'age' => '25'], - ['name' => 'Bob Johnson', 'age' => '40'] + ['name' => 'Bob Johnson', 'age' => '40'], ]; $styles = [ @@ -624,13 +627,13 @@ public function test_render_with_html_like_tags_in_cell_content(): void ]; $expectedOutput = - "+--------------------------+-----+" . PHP_EOL . - "| Name | Age |" . PHP_EOL . - "+--------------------------+-----+" . PHP_EOL . - "| John Doe | 30 |" . PHP_EOL . - "| Jane Smith | 25 |" . PHP_EOL . - "| Bob Johnson | 40 |" . PHP_EOL . - "+--------------------------+-----+"; + '+--------------------------+-----+' . PHP_EOL . + '| Name | Age |' . PHP_EOL . + '+--------------------------+-----+' . PHP_EOL . + '| John Doe | 30 |' . PHP_EOL . + '| Jane Smith | 25 |' . PHP_EOL . + '| Bob Johnson | 40 |' . PHP_EOL . + '+--------------------------+-----+'; $result = $this->table->render($rows, $styles);