Skip to content

Commit d3a9a96

Browse files
authored
[6.x] Fixes appendRow on console table (#31469)
* [6.x] Fixes appendRow on console table. Signed-off-by: Mior Muhammad Zaki <[email protected]> * Fixes based on PR review. Signed-off-by: Mior Muhammad Zaki <[email protected]> * Fixes style. Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 3399284 commit d3a9a96

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/Illuminate/Console/Concerns/InteractsWithIO.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
99
use Symfony\Component\Console\Helper\Table;
1010
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\ConsoleOutput;
1112
use Symfony\Component\Console\Output\OutputInterface;
1213
use Symfony\Component\Console\Question\ChoiceQuestion;
1314
use Symfony\Component\Console\Question\Question;
@@ -222,7 +223,9 @@ public function choice($question, array $choices, $default = null, $attempts = n
222223
*/
223224
public function table($headers, $rows, $tableStyle = 'default', array $columnStyles = [])
224225
{
225-
$table = new Table($this->output->getOutput()->section());
226+
$output = $this->output->getOutput();
227+
228+
$table = new Table($output instanceof ConsoleOutput ? $output->section() : $output);
226229

227230
if ($rows instanceof Arrayable) {
228231
$rows = $rows->toArray();
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Console;
4+
5+
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
6+
use Orchestra\Testbench\TestCase;
7+
use Symfony\Component\Console\Exception\RuntimeException;
8+
9+
class CommandWithTableTest extends TestCase
10+
{
11+
protected function tearDown(): void
12+
{
13+
parent::tearDown();
14+
15+
$this->mockConsoleOutput = true;
16+
}
17+
18+
public function testItCanExecuteCommandWithTable()
19+
{
20+
$this->mockConsoleOutput = false;
21+
22+
$this->artisan('command:table');
23+
}
24+
25+
public function testItCantExecuteCommandWithTableAndAppendRowDueToBufferedOutput()
26+
{
27+
$this->mockConsoleOutput = false;
28+
29+
$this->expectException(RuntimeException::class);
30+
$this->expectExceptionMessage('Output should be an instance of "Symfony\Component\Console\Output\ConsoleSectionOutput" when calling "Symfony\Component\Console\Helper\Table::appendRow');
31+
32+
$this->app[ConsoleKernel::class]->command('command:table-with-append', function () {
33+
$table = $this->table([
34+
'name',
35+
], [
36+
['Taylor Otwell'],
37+
]);
38+
39+
$table->appendRow(['Mohamed Said']);
40+
});
41+
42+
$this->artisan('command:table-with-append');
43+
}
44+
45+
public function testItCanExecuteMockedCommandWithTable()
46+
{
47+
$this->mockConsoleOutput = true;
48+
49+
$this->artisan('command:table')
50+
->assertExitCode(0);
51+
}
52+
53+
protected function getEnvironmentSetUp($app)
54+
{
55+
$app[ConsoleKernel::class]->command('command:table', function () {
56+
$this->table([
57+
'name',
58+
], [
59+
['Taylor Otwell'],
60+
]);
61+
});
62+
}
63+
}

0 commit comments

Comments
 (0)