Skip to content

[12.x] add prompts based expectations to PendingCommand #56260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 12.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/Illuminate/Testing/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\Tappable;
use Laravel\Prompts\Note as PromptsNote;
use Laravel\Prompts\Prompt as BasePrompt;
use Laravel\Prompts\Table as PromptsTable;
use Mockery;
use Mockery\Exception\NoMatchingExpectationException;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
Expand Down Expand Up @@ -247,6 +251,108 @@ public function expectsTable($headers, $rows, $tableStyle = 'default', array $co
return $this;
}

/**
* Specify that the given Prompts info message should be contained in the command output.
*
* @return $this
*/
public function expectsPromptsInfo(string $message)
{
$this->expectOutputToContainPrompt(
new PromptsNote($message, 'info')
);

return $this;
}

/**
* Specify that the given Prompts warning message should be contained in the command output.
*
* @return $this
*/
public function expectsPromptsWarning(string $message)
{
$this->expectOutputToContainPrompt(
new PromptsNote($message, 'warning')
);

return $this;
}

/**
* Specify that the given Prompts error message should be contained in the command output.
*
* @return $this
*/
public function expectsPromptsError(string $message)
{
$this->expectOutputToContainPrompt(
new PromptsNote($message, 'error')
);

return $this;
}

/**
* Specify that the given Prompts alert message should be contained in the command output.
*
* @return $this
*/
public function expectsPromptsAlert(string $message)
{
$this->expectOutputToContainPrompt(
new PromptsNote($message, 'alert')
);

return $this;
}

/**
* Specify that the given Prompts intro message should be contained in the command output.
*
* @return $this
*/
public function expectsPromptsIntro(string $message)
{
$this->expectOutputToContainPrompt(
new PromptsNote($message, 'intro')
);

return $this;
}

/**
* Specify that the given Prompts outro message should be contained in the command output.
*
* @return $this
*/
public function expectsPromptsOutro(string $message)
{
$this->expectOutputToContainPrompt(
new PromptsNote($message, 'outro')
);

return $this;
}

/**
* Specify a Prompts table that should be printed when the command runs.
*
* @param array<int, string|array<int, string>>|Collection<int, string|array<int, string>> $headers
* @param array<int, array<int, string>>|Collection<int, array<int, string>>|null $rows
* @return $this
*
* @phpstan-param ($rows is null ? list<list<string>>|Collection<int, list<string>> : list<string|list<string>>|Collection<int, string|list<string>>) $headers
*/
public function expectsPromptsTable(array|Collection $headers, array|Collection|null $rows)
{
$this->expectOutputToContainPrompt(
new PromptsTable($headers, $rows)
);

return $this;
}

/**
* Assert that the command has the given exit code.
*
Expand Down Expand Up @@ -519,6 +625,20 @@ protected function flushExpectations()
$this->test->expectedChoices = [];
}

/**
* Render specific prompt to new output to add to expectation.
*
* @return void
*/
protected function expectOutputToContainPrompt(BasePrompt $prompt)
{
$prompt->setOutput($output = new BufferedOutput);

$prompt->display();

$this->expectsOutputToContain(trim($output->fetch()));
}

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