2021-04-08 14:12:37 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-06-18 11:51:59 +03:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Util;
|
2021-04-08 14:12:37 +03:00
|
|
|
|
2022-10-22 13:46:16 +03:00
|
|
|
use PHPUnit\Framework\Assert;
|
2023-08-19 12:48:04 +03:00
|
|
|
use PHPUnit\Framework\MockObject\Generator\Generator;
|
2022-10-22 13:46:16 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-04-08 14:12:37 +03:00
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
2021-12-16 23:46:52 +03:00
|
|
|
use Symfony\Component\Console\Input\InputDefinition;
|
2021-04-08 14:34:14 +03:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2021-04-08 14:12:37 +03:00
|
|
|
|
2023-06-18 11:51:59 +03:00
|
|
|
class CliTestUtils
|
2021-04-08 14:12:37 +03:00
|
|
|
{
|
2023-06-18 11:51:59 +03:00
|
|
|
public static function createCommandMock(string $name): MockObject & Command
|
2021-04-08 14:12:37 +03:00
|
|
|
{
|
2023-06-18 11:51:59 +03:00
|
|
|
static $generator = null;
|
|
|
|
|
|
|
|
if ($generator === null) {
|
|
|
|
$generator = new Generator();
|
|
|
|
}
|
|
|
|
|
2023-10-07 11:56:04 +03:00
|
|
|
$command = $generator->testDouble(
|
2023-06-18 11:51:59 +03:00
|
|
|
Command::class,
|
2023-10-07 11:56:04 +03:00
|
|
|
mockObject: true,
|
2023-06-18 11:51:59 +03:00
|
|
|
callOriginalConstructor: false,
|
|
|
|
callOriginalClone: false,
|
|
|
|
cloneArguments: false,
|
|
|
|
allowMockingUnknownTypes: false,
|
|
|
|
);
|
2022-10-22 13:46:16 +03:00
|
|
|
$command->method('getName')->willReturn($name);
|
|
|
|
$command->method('isEnabled')->willReturn(true);
|
|
|
|
$command->method('getAliases')->willReturn([]);
|
|
|
|
$command->method('getDefinition')->willReturn(new InputDefinition());
|
|
|
|
$command->method('setApplication')->with(Assert::isInstanceOf(Application::class));
|
2021-04-08 14:12:37 +03:00
|
|
|
|
|
|
|
return $command;
|
|
|
|
}
|
2021-04-08 14:34:14 +03:00
|
|
|
|
2023-06-18 11:51:59 +03:00
|
|
|
public static function testerForCommand(Command $mainCommand, Command ...$extraCommands): CommandTester
|
2021-04-08 14:34:14 +03:00
|
|
|
{
|
|
|
|
$app = new Application();
|
|
|
|
$app->add($mainCommand);
|
|
|
|
foreach ($extraCommands as $command) {
|
|
|
|
$app->add($command);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new CommandTester($mainCommand);
|
|
|
|
}
|
2021-04-08 14:12:37 +03:00
|
|
|
}
|