forked from symfony/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractCommandTestCase.php
46 lines (38 loc) · 1.58 KB
/
AbstractCommandTestCase.php
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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
abstract class AbstractCommandTestCase extends KernelTestCase
{
/**
* This helper method abstracts the boilerplate code needed to test the
* execution of a command.
*
* @param array<string, string|int> $arguments All the arguments passed when executing the command
* @param array<int, mixed> $inputs The (optional) answers given to the command when it asks for the
* value of the missing arguments
*/
protected function executeCommand(array $arguments, array $inputs = []): CommandTester
{
$kernel = self::bootKernel();
// this uses a special testing container that allows you to fetch private services
/** @var Command $command */
$command = static::getContainer()->get($this->getCommandFqcn());
$command->setApplication(new Application($kernel));
$commandTester = new CommandTester($command);
$commandTester->setInputs($inputs);
$commandTester->execute($arguments);
return $commandTester;
}
abstract protected function getCommandFqcn(): string;
}