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.
This commit is contained in:
Grégoire Paris 2019-04-07 15:06:33 +02:00 committed by Grégoire Paris
parent c82e2df9ff
commit 00883fc409
No known key found for this signature in database
GPG Key ID: 24D48B8012B116BF
4 changed files with 41 additions and 1 deletions

View File

@ -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 @@ class ApplicationTest extends TestCase
*/
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 @@ class ApplicationTest extends TestCase
public function testFindAlternativeExceptionMessageMultiple()
{
putenv('COLUMNS=120');
$application = new Application();
$application->add(new \FooCommand());
$application->add(new \Foo1Command());
@ -1692,6 +1701,7 @@ class ApplicationTest extends TestCase
protected function tearDown()
{
putenv($this->colSize ? 'COLUMNS' : 'COLUMNS='.$this->colSize);
putenv('SHELL_VERBOSITY');
unset($_ENV['SHELL_VERBOSITY']);
unset($_SERVER['SHELL_VERBOSITY']);

View File

@ -21,6 +21,19 @@ use Symfony\Component\Console\Output\StreamOutput;
*/
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());

View File

@ -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 @@ class SymfonyStyleTest extends TestCase
protected function tearDown()
{
putenv('COLUMNS');
putenv($this->colSize ? 'COLUMNS' : 'COLUMNS='.$this->colSize);
$this->command = null;
$this->tester = null;
}

View File

@ -16,6 +16,15 @@ use Symfony\Component\Console\Terminal;
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 @@ class TerminalTest extends TestCase
$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');