-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathInvokeSubCommandTest.php
More file actions
75 lines (59 loc) · 2.88 KB
/
InvokeSubCommandTest.php
File metadata and controls
75 lines (59 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Php\PieUnitTest\Command;
use Php\Pie\Command\InvokeSubCommand;
use Php\Pie\Util\OutputFormatterWithPrefix;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use function trim;
#[CoversClass(InvokeSubCommand::class)]
final class InvokeSubCommandTest extends TestCase
{
public function testInvokeWithNoOutputFormatterRunsSubCommand(): void
{
$inputDefinition = new InputDefinition();
$inputDefinition->addOption(new InputOption('verbose', 'v', InputOption::VALUE_NONE, 'Verbose option'));
$input = new ArrayInput(['--verbose' => true], $inputDefinition);
$output = new BufferedOutput();
$application = $this->createMock(Application::class);
$application->expects(self::once())
->method('doRun')
->willReturnCallback(static function (ArrayInput $newInput, OutputInterface $output) {
self::assertSame('foo --verbose=1', (string) $newInput);
$output->writeln('command output here');
return 0;
});
$command = $this->createMock(Command::class);
$command->method('getApplication')->willReturn($application);
$invoker = new InvokeSubCommand($output);
self::assertSame(0, ($invoker)($command, ['command' => 'foo'], $input));
self::assertSame('command output here', trim($output->fetch()));
}
public function testInvokeWithPrefixOutputFormatterRunsSubCommand(): void
{
$inputDefinition = new InputDefinition();
$inputDefinition->addOption(new InputOption('verbose', 'v', InputOption::VALUE_NONE, 'Verbose option'));
$input = new ArrayInput(['--verbose' => true], $inputDefinition);
$output = new BufferedOutput();
$application = $this->createMock(Application::class);
$application->expects(self::once())
->method('doRun')
->willReturnCallback(static function (ArrayInput $newInput, OutputInterface $output) {
self::assertSame('foo --verbose=1', (string) $newInput);
$output->writeln('command output here');
return 0;
});
$command = $this->createMock(Command::class);
$command->method('getApplication')->willReturn($application);
$invoker = new InvokeSubCommand($output);
self::assertSame(0, ($invoker)($command, ['command' => 'foo'], $input, new OutputFormatterWithPrefix('prefix> ')));
self::assertSame('prefix> command output here', trim($output->fetch()));
}
}