Upstream spiral/testing state of the touched files before 6e6e351854
This commit is contained in:
1 parent
0171b86dd8
commit
e8fb94b682
2 files changed
+93
No files matched your search
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Spiral\Testing\Traits;
|
||||
|
||||
use Spiral\Console\Console;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Output\BufferedOutput;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
trait InteractsWithConsole
|
||||
{
|
||||
public int $defaultVerbosityLevel = OutputInterface::VERBOSITY_NORMAL;
|
||||
|
||||
/**
|
||||
* @param string[]|string $strings
|
||||
*/
|
||||
public function assertConsoleCommandOutputContainsStrings(
|
||||
string $command,
|
||||
array $args = [],
|
||||
array|string $strings = [],
|
||||
?int $verbosityLevel = null,
|
||||
): void {
|
||||
$output = $this->runCommand($command, $args);
|
||||
|
||||
foreach ((array) $strings as $string) {
|
||||
$this->assertStringContainsString(
|
||||
$string,
|
||||
$output,
|
||||
\sprintf(
|
||||
'Console command [%s] with args [%s] does not contain string [%s]',
|
||||
$command,
|
||||
json_encode($args, JSON_THROW_ON_ERROR),
|
||||
$string,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function assertCommandRegistered(string $name): void
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->getConsole()->getApplication()->has($name),
|
||||
\sprintf('Command [%s] is not registered.', $name),
|
||||
);
|
||||
}
|
||||
|
||||
final public function runCommand(
|
||||
string $command,
|
||||
array $args = [],
|
||||
?OutputInterface $output = null,
|
||||
?int $verbosityLevel = null,
|
||||
): string {
|
||||
$input = new ArrayInput($args);
|
||||
$input->setInteractive(false);
|
||||
$output = $output ?? new BufferedOutput();
|
||||
/** @psalm-suppress ArgumentTypeCoercion */
|
||||
$output->setVerbosity($verbosityLevel ?? $this->defaultVerbosityLevel);
|
||||
|
||||
$this->getConsole()->run($command, $input, $output);
|
||||
|
||||
return $output->fetch();
|
||||
}
|
||||
|
||||
final public function getConsole(): Console
|
||||
{
|
||||
return $this->getContainer()->get(Console::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Spiral\Testing\Tests;
|
||||
|
||||
use PHPUnit\Framework\ExpectationFailedException;
|
||||
|
||||
final class ConsoleTest extends TestCase
|
||||
{
|
||||
public function testRegisteredCommand(): void
|
||||
{
|
||||
$this->assertCommandRegistered('foo');
|
||||
}
|
||||
|
||||
public function testNotRegisteredCommandShouldThrowAnException(): void
|
||||
{
|
||||
$this->expectException(ExpectationFailedException::class);
|
||||
$this->expectExceptionMessage('Command [bar] is not registered.');
|
||||
|
||||
$this->assertCommandRegistered('bar');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user