From b23601f244aa8e4c3b5b671cb9ef81d414f2b023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sun, 7 Apr 2019 15:06:33 +0200 Subject: [PATCH] Make tests independent from each other Environment variables set in a test need to be restored to their previous values or unset if we want to be able to run tests independently. --- Tests/ApplicationTest.php | 10 ++++++++++ Tests/Helper/ProgressBarTest.php | 13 +++++++++++++ Tests/Style/SymfonyStyleTest.php | 4 +++- Tests/TerminalTest.php | 15 +++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Tests/ApplicationTest.php b/Tests/ApplicationTest.php index ae9d130f5..85484b5c5 100644 --- a/Tests/ApplicationTest.php +++ b/Tests/ApplicationTest.php @@ -41,6 +41,13 @@ class ApplicationTest extends TestCase { protected static $fixturesPath; + private $colSize; + + protected function setUp() + { + $this->colSize = getenv('COLUMNS'); + } + public static function setUpBeforeClass() { self::$fixturesPath = realpath(__DIR__.'/Fixtures/'); @@ -383,6 +390,7 @@ public function testFindWithCommandLoader() */ public function testFindWithAmbiguousAbbreviations($abbreviation, $expectedExceptionMessage) { + putenv('COLUMNS=120'); if (method_exists($this, 'expectException')) { $this->expectException('Symfony\Component\Console\Exception\CommandNotFoundException'); $this->expectExceptionMessage($expectedExceptionMessage); @@ -468,6 +476,7 @@ public function provideInvalidCommandNamesSingle() public function testFindAlternativeExceptionMessageMultiple() { + putenv('COLUMNS=120'); $application = new Application(); $application->add(new \FooCommand()); $application->add(new \Foo1Command()); @@ -1692,6 +1701,7 @@ public function testErrorIsRethrownIfNotHandledByConsoleErrorEventWithCatchingEn protected function tearDown() { + putenv($this->colSize ? 'COLUMNS' : 'COLUMNS='.$this->colSize); putenv('SHELL_VERBOSITY'); unset($_ENV['SHELL_VERBOSITY']); unset($_SERVER['SHELL_VERBOSITY']); diff --git a/Tests/Helper/ProgressBarTest.php b/Tests/Helper/ProgressBarTest.php index e352311cc..214e943d7 100644 --- a/Tests/Helper/ProgressBarTest.php +++ b/Tests/Helper/ProgressBarTest.php @@ -21,6 +21,19 @@ */ class ProgressBarTest extends TestCase { + private $colSize; + + protected function setUp() + { + $this->colSize = getenv('COLUMNS'); + putenv('COLUMNS=120'); + } + + protected function tearDown() + { + putenv($this->colSize ? 'COLUMNS' : 'COLUMNS='.$this->colSize); + } + public function testMultipleStart() { $bar = new ProgressBar($output = $this->getOutputStream()); diff --git a/Tests/Style/SymfonyStyleTest.php b/Tests/Style/SymfonyStyleTest.php index e6e061f44..a6feb122a 100644 --- a/Tests/Style/SymfonyStyleTest.php +++ b/Tests/Style/SymfonyStyleTest.php @@ -26,9 +26,11 @@ class SymfonyStyleTest extends TestCase protected $command; /** @var CommandTester */ protected $tester; + private $colSize; protected function setUp() { + $this->colSize = getenv('COLUMNS'); putenv('COLUMNS=121'); $this->command = new Command('sfstyle'); $this->tester = new CommandTester($this->command); @@ -36,7 +38,7 @@ protected function setUp() protected function tearDown() { - putenv('COLUMNS'); + putenv($this->colSize ? 'COLUMNS' : 'COLUMNS='.$this->colSize); $this->command = null; $this->tester = null; } diff --git a/Tests/TerminalTest.php b/Tests/TerminalTest.php index 91af1d0ab..ca3b87437 100644 --- a/Tests/TerminalTest.php +++ b/Tests/TerminalTest.php @@ -16,6 +16,15 @@ class TerminalTest extends TestCase { + private $colSize; + private $lineSize; + + protected function setUp() + { + $this->colSize = getenv('COLUMNS'); + $this->lineSize = getenv('LINES'); + } + public function test() { putenv('COLUMNS=100'); @@ -31,6 +40,12 @@ public function test() $this->assertSame(60, $terminal->getHeight()); } + protected function tearDown() + { + putenv($this->colSize ? 'COLUMNS' : 'COLUMNS='.$this->colSize); + putenv($this->lineSize ? 'LINES' : 'LINES='.$this->lineSize); + } + public function test_zero_values() { putenv('COLUMNS=0');