diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php index c4ec0d5c361..b34777910ea 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php @@ -22,6 +22,13 @@ trait InteractsWithConsole */ public $expectedOutput = []; + /** + * All of the expected ouput tables. + * + * @var array + */ + public $expectedTables = []; + /** * All of the expected questions. * diff --git a/src/Illuminate/Testing/PendingCommand.php b/src/Illuminate/Testing/PendingCommand.php index a21f869ac49..c267e8dd872 100644 --- a/src/Illuminate/Testing/PendingCommand.php +++ b/src/Illuminate/Testing/PendingCommand.php @@ -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 { @@ -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. * @@ -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') @@ -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. *