55use Illuminate \Console \OutputStyle ;
66use Illuminate \Contracts \Console \Kernel ;
77use Illuminate \Contracts \Container \Container ;
8+ use Illuminate \Contracts \Support \Arrayable ;
89use Illuminate \Support \Arr ;
910use Mockery ;
1011use Mockery \Exception \NoMatchingExpectationException ;
1112use PHPUnit \Framework \TestCase as PHPUnitTestCase ;
1213use Symfony \Component \Console \Input \ArrayInput ;
1314use Symfony \Component \Console \Output \BufferedOutput ;
15+ use Symfony \Component \Console \Helper \Table ;
1416
1517class 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