Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.2.0
-----

* Added `SingleCommandApplication::setAutoExit()` to allow testing via `CommandTester`

5.1.0
-----

Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Console/SingleCommandApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class SingleCommandApplication extends Command
{
private $version = 'UNKNOWN';
private $autoExit = true;
private $running = false;

public function setVersion(string $version): self
Expand All @@ -30,6 +31,16 @@ public function setVersion(string $version): self
return $this;
}

/**
* @final
*/
public function setAutoExit(bool $autoExit): self
{
$this->autoExit = $autoExit;

return $this;
}

public function run(InputInterface $input = null, OutputInterface $output = null): int
{
if ($this->running) {
Expand All @@ -38,6 +49,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null

// We use the command name as the application name
$application = new Application($this->getName() ?: 'UNKNOWN', $this->version);
$application->setAutoExit($this->autoExit);
// Fix the usage of the command displayed with "--help"
$this->setName($_SERVER['argv'][0]);
$application->add($this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Tester\CommandTester;

class SingleCommandApplicationTest extends TestCase
{
public function testRun()
{
$command = new class extends SingleCommandApplication {
protected function execute(InputInterface $input, OutputInterface $output): int
{
return 0;
}
};

$command->setAutoExit(false);
$this->assertSame(0, (new CommandTester($command))->execute([]));
}
}