Skip to content

Commit

Permalink
Make tests independent from each other
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
greg0ire committed Apr 7, 2019
1 parent 7d3072a commit b23601f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -468,6 +476,7 @@ public function provideInvalidCommandNamesSingle()

public function testFindAlternativeExceptionMessageMultiple()
{
putenv('COLUMNS=120');
$application = new Application();
$application->add(new \FooCommand());
$application->add(new \Foo1Command());
Expand Down Expand Up @@ -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']);
Expand Down
13 changes: 13 additions & 0 deletions Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 3 additions & 1 deletion Tests/Style/SymfonyStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ 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);
}

protected function tearDown()
{
putenv('COLUMNS');
putenv($this->colSize ? 'COLUMNS' : 'COLUMNS='.$this->colSize);
$this->command = null;
$this->tester = null;
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/TerminalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit b23601f

Please sign in to comment.