1

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 Symfony\Component\HttpKernel\EventListener\TestSessionListener 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 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);
    }
}

After that I also tried to extend Symfony\Bundle\FrameworkBundle\Tests\Functional\SessionTest

use Symfony\Bundle\FrameworkBundle\Tests\Functional\SessionTest;

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) Tests\AppBundle\Command\DocumentCommandTest::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
12
  • Why do you need $session ($session = new Session();)? Commented Aug 28, 2019 at 20:41
  • I have to store data in temporarily for further process and didn't find any other easiest way to do that Commented Aug 28, 2019 at 20:47
  • what data do you need to store? I don't see in code Commented Aug 28, 2019 at 20:47
  • It is some string not longer than five character. This is my code that is using session $session = new Session(); $getCD = $session->get($countryCode); $getCD = 1+$getCD; $session->set($countryCode, $getCD); Commented Aug 28, 2019 at 20:51
  • I don't understand why? Please post all code Commented Aug 28, 2019 at 21:00

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Although it passed the test as the output contains valid string, but I need to test full expected output as followed 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 How can I do that? when I insert full expected string in assertContains then it not passed the test
I got failure. FAILURES! Tests: 2, Assertions: 3, Failures: 1.
@AbdulAwal in example there is only one test, are you try to assert something else? Another test? are you leave $this->assertContains('valid',$output); ? i've tried with your code in github.com/devawal/document-evaluation
@AbdulAwal you forgot last character "chr(13)" for $expected variable. try copy/paste from $expected = 'valid to chr(13)';
Not understand which chr?
|
0

Use this instead,

$this->assertStringContainsString('valid',$output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.