Skip to content

Commit 74e1fca

Browse files
stevebaumantaylorotwell
authored andcommitted
Added expectsTable console assertion
This will allow you to easily assert the expectation of a generated console table. Without this, you must determine the exact size of the tables output and assert the table structure line by line.
1 parent 0933309 commit 74e1fca

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ trait InteractsWithConsole
2222
*/
2323
public $expectedOutput = [];
2424

25+
/**
26+
* All of the expected ouput tables.
27+
*
28+
* @var array
29+
*/
30+
public $expectedTables = [];
31+
2532
/**
2633
* All of the expected questions.
2734
*

src/Illuminate/Testing/PendingCommand.php

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use Illuminate\Console\OutputStyle;
66
use Illuminate\Contracts\Console\Kernel;
77
use Illuminate\Contracts\Container\Container;
8+
use Illuminate\Contracts\Support\Arrayable;
89
use Illuminate\Support\Arr;
910
use Mockery;
1011
use Mockery\Exception\NoMatchingExpectationException;
1112
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
1213
use Symfony\Component\Console\Input\ArrayInput;
1314
use Symfony\Component\Console\Output\BufferedOutput;
15+
use Symfony\Component\Console\Helper\Table;
1416

1517
class PendingCommand
1618
{
@@ -131,6 +133,27 @@ public function expectsOutput($output)
131133
return $this;
132134
}
133135

136+
/**
137+
* Specify a table that should be printed when the command runs.
138+
*
139+
* @param array $headers
140+
* @param \Illuminate\Contracts\Support\Arrayable|array $rows
141+
* @param string $tableStyle
142+
* @param array $columnStyles
143+
* @return $this
144+
*/
145+
public function expectsTable($headers, $rows, $tableStyle = 'default', array $columnStyles = [])
146+
{
147+
$this->test->expectedTables[] = [
148+
'headers' => (array) $headers,
149+
'rows' => $rows instanceof Arrayable ? $rows->toArray() : $rows,
150+
'tableStyle' => $tableStyle,
151+
'columnStyles' => $columnStyles,
152+
];
153+
154+
return $this;
155+
}
156+
134157
/**
135158
* Assert that the command has the given exit code.
136159
*
@@ -261,8 +284,10 @@ protected function mockConsoleOutput()
261284
private function createABufferedOutputMock()
262285
{
263286
$mock = Mockery::mock(BufferedOutput::class.'[doWrite]')
264-
->shouldAllowMockingProtectedMethods()
265-
->shouldIgnoreMissing();
287+
->shouldAllowMockingProtectedMethods()
288+
->shouldIgnoreMissing();
289+
290+
$this->applyOutputTableExpectations($mock);
266291

267292
foreach ($this->test->expectedOutput as $i => $output) {
268293
$mock->shouldReceive('doWrite')
@@ -277,6 +302,36 @@ private function createABufferedOutputMock()
277302
return $mock;
278303
}
279304

305+
/**
306+
* Apply the output table expectations to the mock.
307+
*
308+
* @param \Mockery\MockInterface $mock
309+
* @return void
310+
*/
311+
private function applyOutputTableExpectations($mock)
312+
{
313+
foreach ($this->test->expectedTables as $consoleTable) {
314+
$table = (new Table($output = new BufferedOutput))
315+
->setHeaders($consoleTable['headers'])
316+
->setRows($consoleTable['rows'])
317+
->setStyle($consoleTable['tableStyle']);
318+
319+
foreach ($consoleTable['columnStyles'] as $columnIndex => $columnStyle) {
320+
$table->setColumnStyle($columnIndex, $columnStyle);
321+
}
322+
323+
$table->render();
324+
325+
$lines = array_filter(
326+
preg_split("/\n/", $output->fetch())
327+
);
328+
329+
foreach ($lines as $line) {
330+
$this->expectsOutput($line);
331+
}
332+
}
333+
}
334+
280335
/**
281336
* Handle the object's destruction.
282337
*

0 commit comments

Comments
 (0)