Skip to content

Commit

Permalink
Restoring State After Static Changes in Tests (#1571)
Browse files Browse the repository at this point in the history
This request does not change any source code, only tests.

For a change on which I was working, a test passed when run on its own,
but failed when run as part of the full test suite. It turned out that
an existing test had changed a static value,
thousands separator in this case, and failed to restore it.
The test turned out to be AdvancedBinderTest.

The search for the offending test was more difficult than it should have
been because 26 test scripts which had nothing to do with thousands
separator nevertheless changed that value. They all changed
decimal separator, currency code, and compatibility mode as well,
again for no reason. I changed all of those to eliminate those operations.

I changed the following tests, which actually do change the static
properties identified above for a reason, to restore them as part of teardown.
- CalculationTest sets compatibilityMode and locale
- DayTest sets compatibilityMode, returnDateType, and excelCalendar
- CountTest sets compatibilityMode
- FunctionsTest sets compatibilityMode and returnDateType
- AdvancedValueBinderTest sets currencyCode, decimalSeparator, thousandsSeparator
- StringHelperTest sets currencyCode, decimalSeparator, thousandsSeparator
- NumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
- HtmlNumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
  • Loading branch information
oleibman authored Jul 15, 2020
1 parent b89968d commit 165034a
Show file tree
Hide file tree
Showing 32 changed files with 110 additions and 435 deletions.
10 changes: 9 additions & 1 deletion tests/PhpSpreadsheetTests/Calculation/CalculationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@

class CalculationTest extends TestCase
{
private $compatibilityMode;

private $locale;

protected function setUp(): void
{
$this->compatibilityMode = Functions::getCompatibilityMode();
$calculation = Calculation::getInstance();
$this->locale = $calculation->getLocale();
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

protected function tearDown(): void
{
Functions::setCompatibilityMode($this->compatibilityMode);
$calculation = Calculation::getInstance();
$calculation->setLocale('en_us');
$calculation->setLocale($this->locale);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,29 @@

class DayTest extends TestCase
{
private $compatibilityMode;

private $returnDateType;

private $excelCalendar;

protected function setUp(): void
{
$this->compatibilityMode = Functions::getCompatibilityMode();
$this->returnDateType = Functions::getReturnDateType();
$this->excelCalendar = Date::getExcelCalendar();
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
}

protected function tearDown(): void
{
Functions::setCompatibilityMode($this->compatibilityMode);
Functions::setReturnDateType($this->returnDateType);
Date::setExcelCalendar($this->excelCalendar);
}

/**
* @dataProvider providerDAY
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@

class CountTest extends TestCase
{
private $compatibilityMode;

protected function setUp(): void
{
$this->compatibilityMode = Functions::getCompatibilityMode();
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

protected function tearDown(): void
{
Functions::setCompatibilityMode($this->compatibilityMode);
}

/**
* @dataProvider providerBasicCOUNT
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\TestCase;

class CharTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

/**
* @dataProvider providerCHAR
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\TestCase;

class CleanTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

/**
* @dataProvider providerCLEAN
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\TestCase;

class CodeTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

/**
* @dataProvider providerCODE
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\TestCase;

class ConcatenateTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

/**
* @dataProvider providerCONCATENATE
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\TestCase;

class DollarTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

/**
* @dataProvider providerDOLLAR
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\TestCase;

class ExactTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

/**
* @dataProvider providerEXACT
*
Expand All @@ -33,10 +15,6 @@ protected function tearDown(): void
*/
public function testEXACT($expectedResult, ...$args): void
{
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(' ');
StringHelper::setCurrencyCode('$');

$result = TextData::EXACT(...$args);
self::assertSame($expectedResult, $result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\TestCase;

class FindTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

/**
* @dataProvider providerFIND
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\TestCase;

class FixedTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

/**
* @dataProvider providerFIXED
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\TestCase;

class LeftTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

/**
* @dataProvider providerLEFT
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\TestCase;

class LenTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator(',');
StringHelper::setCurrencyCode('$');
}

/**
* @dataProvider providerLEN
*
Expand Down
Loading

0 comments on commit 165034a

Please sign in to comment.