I am new in unit testing. I have Symfony 3 console command application which is using symfony session for temporary data store.
In my unit test class I extend KernelTestCase which give me error while I tested it but if I extend SymfonyComponentHttpKernelEventListenerTestSessionListener
class then the error gone and passed the test.
After some time I realize that it always passed test no mater what I write inside the test case.
My Console 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!'); } }
My test case
use SymfonyComponentHttpKernelEventListenerTestSessionListener 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); } }
After that I also tried to extend SymfonyBundleFrameworkBundleTestsFunctionalSessionTest
use SymfonyBundleFrameworkBundleTestsFunctionalSessionTest; class DocumentCommandTest extends SessionTest {
but it give me the following error
E......... 10 / 10 (100%) Time: 4.72 seconds, Memory: 20.00MB There was 1 error: 1) TestsAppBundleCommandDocumentCommandTest::testExecute InvalidArgumentException: The option "test_case" must be set.
My expected output from console is the following that needs to be tested
$ 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
Advertisement
Answer
Try removing
$output = $commandTester->getOutput(); $this->assertContains('valid',$output);
and add:
$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);
see: http://www.inanzzz.com/index.php/post/c7jb/testing-symfony-console-command-with-phpunit