5
5
use Illuminate \Console \OutputStyle ;
6
6
use Illuminate \Contracts \Console \Kernel ;
7
7
use Illuminate \Contracts \Container \Container ;
8
+ use Illuminate \Contracts \Support \Arrayable ;
8
9
use Illuminate \Support \Arr ;
9
10
use Mockery ;
10
11
use Mockery \Exception \NoMatchingExpectationException ;
11
12
use PHPUnit \Framework \TestCase as PHPUnitTestCase ;
12
13
use Symfony \Component \Console \Input \ArrayInput ;
13
14
use Symfony \Component \Console \Output \BufferedOutput ;
15
+ use Symfony \Component \Console \Helper \Table ;
14
16
15
17
class PendingCommand
16
18
{
@@ -131,6 +133,27 @@ public function expectsOutput($output)
131
133
return $ this ;
132
134
}
133
135
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
+
134
157
/**
135
158
* Assert that the command has the given exit code.
136
159
*
@@ -261,8 +284,10 @@ protected function mockConsoleOutput()
261
284
private function createABufferedOutputMock ()
262
285
{
263
286
$ mock = Mockery::mock (BufferedOutput::class.'[doWrite] ' )
264
- ->shouldAllowMockingProtectedMethods ()
265
- ->shouldIgnoreMissing ();
287
+ ->shouldAllowMockingProtectedMethods ()
288
+ ->shouldIgnoreMissing ();
289
+
290
+ $ this ->applyOutputTableExpectations ($ mock );
266
291
267
292
foreach ($ this ->test ->expectedOutput as $ i => $ output ) {
268
293
$ mock ->shouldReceive ('doWrite ' )
@@ -277,6 +302,36 @@ private function createABufferedOutputMock()
277
302
return $ mock ;
278
303
}
279
304
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
+
280
335
/**
281
336
* Handle the object's destruction.
282
337
*
0 commit comments