启用会话的 Symfony 3 单元测试有问题
Having issue with Symfony 3 Unit testing that has session enabled
我是单元测试的新手。我有 Symfony 3 控制台命令应用程序,它使用 symfony 会话作为临时数据存储。
在我的单元测试中 class 我扩展了 KernelTestCase ,这在我测试它时给了我错误,但是如果我扩展 Symfony\Component\HttpKernel\EventListener\TestSessionListener
class 那么错误就消失了并通过了测试。
一段时间后,我发现无论我在测试用例中写了什么,它总是通过测试。
我的主机class
protected function execute(InputInterface $input, OutputInterface $output)
{
// Retrieve the argument value using getArgument()
$csv_name = $input->getArgument('file');
// Check file name
if ($csv_name == 'input.csv') {
// Get input file from filesystem
$csvData = array_map('str_getcsv', file($this->base_path.'/../web/'.$csv_name));
$formatData = Helpers::formatInputData($csvData);
// Start session
$session = new Session();
$session->start();
foreach ($csvData as $key => $data) {
if (!empty($data[0])) {
$validation = Validator::getInformationData($data, $formatData[$data[1]]);
if (!empty($validation)) {
$output->writeln($validation);
} else {
$output->writeln('valid');
}
}
}
} else {
$output->writeln('Invalid file!');
}
}
我的测试用例
use Symfony\Component\HttpKernel\EventListener\TestSessionListener as BaseTestSessionListener;
class DocumentCommandTest extends BaseTestSessionListener
{
/**
* Test Execute
*
*/
public function testExecute()
{
$kernel = static::createKernel();
$kernel->boot();
$application = new Application($kernel);
$application->add(new DocumentCommand($kernel->getContainer()));
$command = $application->find('identification-requests:process');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
'file' => 'input.csv'
));
$output = $commandTester->getOutput();
$this->assertContains('valid',$output);
}
}
之后我也尝试过扩展Symfony\Bundle\FrameworkBundle\Tests\Functional\SessionTest
use Symfony\Bundle\FrameworkBundle\Tests\Functional\SessionTest;
class DocumentCommandTest extends SessionTest
{
但它给我以下错误
E......... 10 / 10 (100%)
Time: 4.72 seconds, Memory: 20.00MB
There was 1 error:
1) Tests\AppBundle\Command\DocumentCommandTest::testExecute
InvalidArgumentException: The option "test_case" must be set.
我预期的控制台输出如下,需要测试
$ php bin/console identification-requests:process input.csv
valid
valid
valid
document_number_length_invalid
request_limit_exceeded
valid
document_is_expired
valid
document_type_is_invalid
valid
valid
document_number_invalid
valid
document_issue_date_invalid
尝试删除
$output = $commandTester->getOutput();
$this->assertContains('valid',$output);
并添加:
$expected = 'valid
valid
valid
document_number_length_invalid
request_limit_exceeded
valid
document_is_expired
valid
document_type_is_invalid
valid
valid
document_number_invalid
valid
document_issue_date_invalid
';
$testresult = $commandTester->getDisplay();
$this->assertEquals($expected, $testresult);
参见:http://www.inanzzz.com/index.php/post/c7jb/testing-symfony-console-command-with-phpunit
改用这个,
$this->assertStringContainsString('valid',$output);
我是单元测试的新手。我有 Symfony 3 控制台命令应用程序,它使用 symfony 会话作为临时数据存储。
在我的单元测试中 class 我扩展了 KernelTestCase ,这在我测试它时给了我错误,但是如果我扩展 Symfony\Component\HttpKernel\EventListener\TestSessionListener
class 那么错误就消失了并通过了测试。
一段时间后,我发现无论我在测试用例中写了什么,它总是通过测试。
我的主机class
protected function execute(InputInterface $input, OutputInterface $output)
{
// Retrieve the argument value using getArgument()
$csv_name = $input->getArgument('file');
// Check file name
if ($csv_name == 'input.csv') {
// Get input file from filesystem
$csvData = array_map('str_getcsv', file($this->base_path.'/../web/'.$csv_name));
$formatData = Helpers::formatInputData($csvData);
// Start session
$session = new Session();
$session->start();
foreach ($csvData as $key => $data) {
if (!empty($data[0])) {
$validation = Validator::getInformationData($data, $formatData[$data[1]]);
if (!empty($validation)) {
$output->writeln($validation);
} else {
$output->writeln('valid');
}
}
}
} else {
$output->writeln('Invalid file!');
}
}
我的测试用例
use Symfony\Component\HttpKernel\EventListener\TestSessionListener as BaseTestSessionListener;
class DocumentCommandTest extends BaseTestSessionListener
{
/**
* Test Execute
*
*/
public function testExecute()
{
$kernel = static::createKernel();
$kernel->boot();
$application = new Application($kernel);
$application->add(new DocumentCommand($kernel->getContainer()));
$command = $application->find('identification-requests:process');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
'file' => 'input.csv'
));
$output = $commandTester->getOutput();
$this->assertContains('valid',$output);
}
}
之后我也尝试过扩展Symfony\Bundle\FrameworkBundle\Tests\Functional\SessionTest
use Symfony\Bundle\FrameworkBundle\Tests\Functional\SessionTest;
class DocumentCommandTest extends SessionTest
{
但它给我以下错误
E......... 10 / 10 (100%)
Time: 4.72 seconds, Memory: 20.00MB
There was 1 error:
1) Tests\AppBundle\Command\DocumentCommandTest::testExecute
InvalidArgumentException: The option "test_case" must be set.
我预期的控制台输出如下,需要测试
$ php bin/console identification-requests:process input.csv
valid
valid
valid
document_number_length_invalid
request_limit_exceeded
valid
document_is_expired
valid
document_type_is_invalid
valid
valid
document_number_invalid
valid
document_issue_date_invalid
尝试删除
$output = $commandTester->getOutput();
$this->assertContains('valid',$output);
并添加:
$expected = 'valid
valid
valid
document_number_length_invalid
request_limit_exceeded
valid
document_is_expired
valid
document_type_is_invalid
valid
valid
document_number_invalid
valid
document_issue_date_invalid
';
$testresult = $commandTester->getDisplay();
$this->assertEquals($expected, $testresult);
参见:http://www.inanzzz.com/index.php/post/c7jb/testing-symfony-console-command-with-phpunit
改用这个,
$this->assertStringContainsString('valid',$output);