Skip to content

Commit 165034a

Browse files
authored
Restoring State After Static Changes in Tests (#1571)
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
1 parent b89968d commit 165034a

32 files changed

+110
-435
lines changed

tests/PhpSpreadsheetTests/Calculation/CalculationTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@
99

1010
class CalculationTest extends TestCase
1111
{
12+
private $compatibilityMode;
13+
14+
private $locale;
15+
1216
protected function setUp(): void
1317
{
18+
$this->compatibilityMode = Functions::getCompatibilityMode();
19+
$calculation = Calculation::getInstance();
20+
$this->locale = $calculation->getLocale();
1421
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
1522
}
1623

1724
protected function tearDown(): void
1825
{
26+
Functions::setCompatibilityMode($this->compatibilityMode);
1927
$calculation = Calculation::getInstance();
20-
$calculation->setLocale('en_us');
28+
$calculation->setLocale($this->locale);
2129
}
2230

2331
/**

tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DayTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,29 @@
99

1010
class DayTest extends TestCase
1111
{
12+
private $compatibilityMode;
13+
14+
private $returnDateType;
15+
16+
private $excelCalendar;
17+
1218
protected function setUp(): void
1319
{
20+
$this->compatibilityMode = Functions::getCompatibilityMode();
21+
$this->returnDateType = Functions::getReturnDateType();
22+
$this->excelCalendar = Date::getExcelCalendar();
1423
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
1524
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
1625
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
1726
}
1827

28+
protected function tearDown(): void
29+
{
30+
Functions::setCompatibilityMode($this->compatibilityMode);
31+
Functions::setReturnDateType($this->returnDateType);
32+
Date::setExcelCalendar($this->excelCalendar);
33+
}
34+
1935
/**
2036
* @dataProvider providerDAY
2137
*

tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/CountTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@
88

99
class CountTest extends TestCase
1010
{
11+
private $compatibilityMode;
12+
1113
protected function setUp(): void
1214
{
15+
$this->compatibilityMode = Functions::getCompatibilityMode();
1316
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
1417
}
1518

19+
protected function tearDown(): void
20+
{
21+
Functions::setCompatibilityMode($this->compatibilityMode);
22+
}
23+
1624
/**
1725
* @dataProvider providerBasicCOUNT
1826
*

tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CharTest.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,11 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
65
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
7-
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
86
use PHPUnit\Framework\TestCase;
97

108
class CharTest extends TestCase
119
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
StringHelper::setDecimalSeparator('.');
16-
StringHelper::setThousandsSeparator(',');
17-
StringHelper::setCurrencyCode('$');
18-
}
19-
20-
protected function tearDown(): void
21-
{
22-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
23-
StringHelper::setDecimalSeparator('.');
24-
StringHelper::setThousandsSeparator(',');
25-
StringHelper::setCurrencyCode('$');
26-
}
27-
2810
/**
2911
* @dataProvider providerCHAR
3012
*

tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CleanTest.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,11 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
65
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
7-
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
86
use PHPUnit\Framework\TestCase;
97

108
class CleanTest extends TestCase
119
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
StringHelper::setDecimalSeparator('.');
16-
StringHelper::setThousandsSeparator(',');
17-
StringHelper::setCurrencyCode('$');
18-
}
19-
20-
protected function tearDown(): void
21-
{
22-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
23-
StringHelper::setDecimalSeparator('.');
24-
StringHelper::setThousandsSeparator(',');
25-
StringHelper::setCurrencyCode('$');
26-
}
27-
2810
/**
2911
* @dataProvider providerCLEAN
3012
*

tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CodeTest.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,11 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
65
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
7-
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
86
use PHPUnit\Framework\TestCase;
97

108
class CodeTest extends TestCase
119
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
StringHelper::setDecimalSeparator('.');
16-
StringHelper::setThousandsSeparator(',');
17-
StringHelper::setCurrencyCode('$');
18-
}
19-
20-
protected function tearDown(): void
21-
{
22-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
23-
StringHelper::setDecimalSeparator('.');
24-
StringHelper::setThousandsSeparator(',');
25-
StringHelper::setCurrencyCode('$');
26-
}
27-
2810
/**
2911
* @dataProvider providerCODE
3012
*

tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ConcatenateTest.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,11 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
65
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
7-
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
86
use PHPUnit\Framework\TestCase;
97

108
class ConcatenateTest extends TestCase
119
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
StringHelper::setDecimalSeparator('.');
16-
StringHelper::setThousandsSeparator(',');
17-
StringHelper::setCurrencyCode('$');
18-
}
19-
20-
protected function tearDown(): void
21-
{
22-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
23-
StringHelper::setDecimalSeparator('.');
24-
StringHelper::setThousandsSeparator(',');
25-
StringHelper::setCurrencyCode('$');
26-
}
27-
2810
/**
2911
* @dataProvider providerCONCATENATE
3012
*

tests/PhpSpreadsheetTests/Calculation/Functions/TextData/DollarTest.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,11 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
65
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
7-
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
86
use PHPUnit\Framework\TestCase;
97

108
class DollarTest extends TestCase
119
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
StringHelper::setDecimalSeparator('.');
16-
StringHelper::setThousandsSeparator(',');
17-
StringHelper::setCurrencyCode('$');
18-
}
19-
20-
protected function tearDown(): void
21-
{
22-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
23-
StringHelper::setDecimalSeparator('.');
24-
StringHelper::setThousandsSeparator(',');
25-
StringHelper::setCurrencyCode('$');
26-
}
27-
2810
/**
2911
* @dataProvider providerDOLLAR
3012
*

tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ExactTest.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,11 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
65
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
7-
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
86
use PHPUnit\Framework\TestCase;
97

108
class ExactTest extends TestCase
119
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
StringHelper::setDecimalSeparator('.');
16-
StringHelper::setThousandsSeparator(',');
17-
StringHelper::setCurrencyCode('$');
18-
}
19-
20-
protected function tearDown(): void
21-
{
22-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
23-
StringHelper::setDecimalSeparator('.');
24-
StringHelper::setThousandsSeparator(',');
25-
StringHelper::setCurrencyCode('$');
26-
}
27-
2810
/**
2911
* @dataProvider providerEXACT
3012
*
@@ -33,10 +15,6 @@ protected function tearDown(): void
3315
*/
3416
public function testEXACT($expectedResult, ...$args): void
3517
{
36-
StringHelper::setDecimalSeparator('.');
37-
StringHelper::setThousandsSeparator(' ');
38-
StringHelper::setCurrencyCode('$');
39-
4018
$result = TextData::EXACT(...$args);
4119
self::assertSame($expectedResult, $result);
4220
}

tests/PhpSpreadsheetTests/Calculation/Functions/TextData/FindTest.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,11 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
65
use PhpOffice\PhpSpreadsheet\Calculation\TextData;
7-
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
86
use PHPUnit\Framework\TestCase;
97

108
class FindTest extends TestCase
119
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
StringHelper::setDecimalSeparator('.');
16-
StringHelper::setThousandsSeparator(',');
17-
StringHelper::setCurrencyCode('$');
18-
}
19-
20-
protected function tearDown(): void
21-
{
22-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
23-
StringHelper::setDecimalSeparator('.');
24-
StringHelper::setThousandsSeparator(',');
25-
StringHelper::setCurrencyCode('$');
26-
}
27-
2810
/**
2911
* @dataProvider providerFIND
3012
*

0 commit comments

Comments
 (0)