Skip to content

Commit 2fd6f3c

Browse files
[12.x] add prompts based expectations to PendingCommand (#56260)
* add prompts based expectations to PendingCommand * copy type definitions from Laravel\Prompts\Table to the expectation * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 20dcabd commit 2fd6f3c

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

src/Illuminate/Testing/PendingCommand.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
use Illuminate\Contracts\Container\Container;
99
use Illuminate\Contracts\Support\Arrayable;
1010
use Illuminate\Support\Arr;
11+
use Illuminate\Support\Collection;
1112
use Illuminate\Support\Traits\Conditionable;
1213
use Illuminate\Support\Traits\Macroable;
1314
use Illuminate\Support\Traits\Tappable;
15+
use Laravel\Prompts\Note as PromptsNote;
16+
use Laravel\Prompts\Prompt as BasePrompt;
17+
use Laravel\Prompts\Table as PromptsTable;
1418
use Mockery;
1519
use Mockery\Exception\NoMatchingExpectationException;
1620
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
@@ -247,6 +251,122 @@ public function expectsTable($headers, $rows, $tableStyle = 'default', array $co
247251
return $this;
248252
}
249253

254+
/**
255+
* Specify that the given Prompts info message should be contained in the command output.
256+
*
257+
* @return $this
258+
*/
259+
public function expectsPromptsInfo(string $message)
260+
{
261+
$this->expectOutputToContainPrompt(
262+
new PromptsNote($message, 'info')
263+
);
264+
265+
return $this;
266+
}
267+
268+
/**
269+
* Specify that the given Prompts warning message should be contained in the command output.
270+
*
271+
* @return $this
272+
*/
273+
public function expectsPromptsWarning(string $message)
274+
{
275+
$this->expectOutputToContainPrompt(
276+
new PromptsNote($message, 'warning')
277+
);
278+
279+
return $this;
280+
}
281+
282+
/**
283+
* Specify that the given Prompts error message should be contained in the command output.
284+
*
285+
* @return $this
286+
*/
287+
public function expectsPromptsError(string $message)
288+
{
289+
$this->expectOutputToContainPrompt(
290+
new PromptsNote($message, 'error')
291+
);
292+
293+
return $this;
294+
}
295+
296+
/**
297+
* Specify that the given Prompts alert message should be contained in the command output.
298+
*
299+
* @return $this
300+
*/
301+
public function expectsPromptsAlert(string $message)
302+
{
303+
$this->expectOutputToContainPrompt(
304+
new PromptsNote($message, 'alert')
305+
);
306+
307+
return $this;
308+
}
309+
310+
/**
311+
* Specify that the given Prompts intro message should be contained in the command output.
312+
*
313+
* @return $this
314+
*/
315+
public function expectsPromptsIntro(string $message)
316+
{
317+
$this->expectOutputToContainPrompt(
318+
new PromptsNote($message, 'intro')
319+
);
320+
321+
return $this;
322+
}
323+
324+
/**
325+
* Specify that the given Prompts outro message should be contained in the command output.
326+
*
327+
* @return $this
328+
*/
329+
public function expectsPromptsOutro(string $message)
330+
{
331+
$this->expectOutputToContainPrompt(
332+
new PromptsNote($message, 'outro')
333+
);
334+
335+
return $this;
336+
}
337+
338+
/**
339+
* Specify a Prompts table that should be printed when the command runs.
340+
*
341+
* @param array<int, string|array<int, string>>|Collection<int, string|array<int, string>> $headers
342+
* @param array<int, array<int, string>>|Collection<int, array<int, string>>|null $rows
343+
* @return $this
344+
*
345+
* @phpstan-param ($rows is null ? list<list<string>>|Collection<int, list<string>> : list<string|list<string>>|Collection<int, string|list<string>>) $headers
346+
*/
347+
public function expectsPromptsTable(array|Collection $headers, array|Collection|null $rows)
348+
{
349+
$this->expectOutputToContainPrompt(
350+
new PromptsTable($headers, $rows)
351+
);
352+
353+
return $this;
354+
}
355+
356+
/**
357+
* Render the given prompt and add the output to the expectations.
358+
*
359+
* @return void
360+
*/
361+
protected function expectOutputToContainPrompt(BasePrompt $prompt)
362+
{
363+
$prompt->setOutput($output = new BufferedOutput);
364+
365+
$prompt->display();
366+
367+
$this->expectsOutputToContain(trim($output->fetch()));
368+
}
369+
250370
/**
251371
* Assert that the command has the given exit code.
252372
*

0 commit comments

Comments
 (0)