Skip to content

SingleCommandApplication can't use in CommandTester #37723

@ochi51

Description

@ochi51

Symfony version(s) affected: v5.1.2

Description
I'm testing a SingleCommandApplication with CommandTester(PHPUnit).
However, the execution result cannot be received because it is exit.
I think this is because autoExit is true.
If possible, I want to run setAutoExit(false) from the test code.

How to reproduce

<?php

require __DIR__.'/vendor/autoload.php';

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;

class MainCommand extends SingleCommandApplication
{
    public function __construct()
    {
        parent::__construct('Test');
        $this->setUp();
    }

    private function setUp()
    {
        $this
            ->setCode(
                function (InputInterface $input, OutputInterface $output) {
                    return Command::SUCCESS;
                }
            );
    }
}
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;

class MainCommandTest extends TestCase
{
    public function test()
    {
        $command = new \MainCommand();
        $commandTester = new CommandTester($command);
        $commandTester->execute([]);
        self::assertEquals(Command::SUCCESS, $commandTester->getStatusCode());
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions