交响乐6
教义 3
phpunit
我的 origin 命令正在正常运行。没什么特别的。现在我想写一个测试用例。但我得到的只是:
跑步:
php bin/phpunit tests/Command/IsbnCleanupCommandTest.php
有 1 个错误:
- App\Tests\Command\IsbnCleanupCommandTest::testExecute ArgumentCountError:函数参数太少 App\Command\IsbnCleanupCommand::__construct(), 0 传入 /var/www/company/tests/Command/IsbnCleanupCommandTest.php 第 18 行 正好 3 个预期
为什么测试时必须向构造函数传递参数?如果我运行该命令,则不必这样做。
我的命令
<?php
namespace App\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\TitelmetaRepository;
use App\Repository\IsbnRepository;
#[AsCommand(
name: 'IsbnCleanup',
description: 'Add a short description for your command',
)]
class IsbnCleanupCommand extends Command
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager, TitelMetaRepository $titelMetaRepository, IsbnRepository $isbnRepository)
{
parent::__construct();
$this->entityManager = $entityManager;
$this->titelMetaRepository = $titelMetaRepository;
$this->isbnRepository = $isbnRepository;
}
protected function configure(): void
{
$this
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$arg1 = $input->getArgument('arg1');
// set state value to NULL (=reset)
$this->isbnRepository->resetStateValues();
/* … flawless code … */
$io->success('You have a new command! Now make it your own! Pass --help to see your options.');
return Command::SUCCESS;
}
}
我的测试
<?php
namespace App\Tests\Command;
use App\Command\IsbnCleanupCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
class IsbnCleanupCommandTest extends KernelTestCase
{
public function testExecute()
{
$kernel = static::createKernel();
$kernel->boot();
$application = new Application($kernel);
$application->add(new IsbnCleanupCommand());
$command = $application->find('app:IsbnCleanup');
$commandTester = new CommandTester($command);
$commandTester->execute([
// pass arguments to the helper
// 'username' => 'John',
// ...
]);
// the output of the command in the console
$output = $commandTester->getDisplay();
$this->assertStringContainsString('...', $output);
// or use the tester to assert on the exit code
// $this->assertSame(0, $commandTester->getStatusCode());
}
}
如此处所述,我可以在更改代码后运行测试,不再需要构造函数了。
<?php
// tests/Command/CreateUserCommandTest.php
namespace App\Tests\Command;
use App\Command\IsbnCleanupCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
class IsbnCleanupCommandTest extends KernelTestCase
{
public function testExecute()
{
$kernel = self::bootKernel();
$application = new Application($kernel);
$command = $application->find('IsbnCleanup');
$commandTester = new CommandTester($command);
$commandTester->execute([
// pass arguments to the helper
//'username' => 'Wouter',
// prefix the key with two dashes when passing options,
// e.g: '--some-option' => 'option_value',
// use brackets for testing array value,
// e.g: '--some-option' => ['option_value'],
]);
$commandTester->assertCommandIsSuccessful();
// the output of the command in the console
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Username: Wouter', $output);
// ...
}
}