Skip to content

Commit

Permalink
Added expectsTable console assertion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stevebauman authored and taylorotwell committed Sep 3, 2020
1 parent 0933309 commit 74e1fca
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Expand Up @@ -22,6 +22,13 @@ trait InteractsWithConsole
*/
public $expectedOutput = [];

/**
* All of the expected ouput tables.
*
* @var array
*/
public $expectedTables = [];

/**
* All of the expected questions.
*
Expand Down
59 changes: 57 additions & 2 deletions src/Illuminate/Testing/PendingCommand.php
Expand Up @@ -5,12 +5,14 @@
use Illuminate\Console\OutputStyle;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Mockery;
use Mockery\Exception\NoMatchingExpectationException;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Helper\Table;

class PendingCommand
{
Expand Down Expand Up @@ -131,6 +133,27 @@ public function expectsOutput($output)
return $this;
}

/**
* Specify a table that should be printed when the command runs.
*
* @param array $headers
* @param \Illuminate\Contracts\Support\Arrayable|array $rows
* @param string $tableStyle
* @param array $columnStyles
* @return $this
*/
public function expectsTable($headers, $rows, $tableStyle = 'default', array $columnStyles = [])
{
$this->test->expectedTables[] = [
'headers' => (array) $headers,
'rows' => $rows instanceof Arrayable ? $rows->toArray() : $rows,
'tableStyle' => $tableStyle,
'columnStyles' => $columnStyles,
];

return $this;
}

/**
* Assert that the command has the given exit code.
*
Expand Down Expand Up @@ -261,8 +284,10 @@ protected function mockConsoleOutput()
private function createABufferedOutputMock()
{
$mock = Mockery::mock(BufferedOutput::class.'[doWrite]')
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();

$this->applyOutputTableExpectations($mock);

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

/**
* Apply the output table expectations to the mock.
*
* @param \Mockery\MockInterface $mock
* @return void
*/
private function applyOutputTableExpectations($mock)
{
foreach ($this->test->expectedTables as $consoleTable) {
$table = (new Table($output = new BufferedOutput))
->setHeaders($consoleTable['headers'])
->setRows($consoleTable['rows'])
->setStyle($consoleTable['tableStyle']);

foreach ($consoleTable['columnStyles'] as $columnIndex => $columnStyle) {
$table->setColumnStyle($columnIndex, $columnStyle);
}

$table->render();

$lines = array_filter(
preg_split("/\n/", $output->fetch())
);

foreach ($lines as $line) {
$this->expectsOutput($line);
}
}
}

/**
* Handle the object's destruction.
*
Expand Down

0 comments on commit 74e1fca

Please sign in to comment.