Minor Improvement to Test Cleanup DateTime

Permit spreadsheet allocated as private member in test class to be garbage-collected after test completion.
This commit is contained in:
Owen Leibman 2021-05-13 18:14:26 -07:00 committed by Mark Baker
parent efe8f49123
commit 4bd506b414
24 changed files with 57 additions and 35 deletions

View File

@ -27,22 +27,20 @@ class AllSetupTeardown extends TestCase
private $returnDateType; private $returnDateType;
/** /**
* @var Spreadsheet * @var ?Spreadsheet
*/ */
protected $spreadsheet; private $spreadsheet;
/** /**
* @var Worksheet * @var ?Worksheet
*/ */
protected $sheet; private $sheet;
protected function setUp(): void protected function setUp(): void
{ {
$this->compatibilityMode = Functions::getCompatibilityMode(); $this->compatibilityMode = Functions::getCompatibilityMode();
$this->excelCalendar = Date::getExcelCalendar(); $this->excelCalendar = Date::getExcelCalendar();
$this->returnDateType = Functions::getReturnDateType(); $this->returnDateType = Functions::getReturnDateType();
$this->spreadsheet = new Spreadsheet();
$this->sheet = $this->spreadsheet->getActiveSheet();
} }
protected function tearDown(): void protected function tearDown(): void
@ -50,7 +48,11 @@ class AllSetupTeardown extends TestCase
Date::setExcelCalendar($this->excelCalendar); Date::setExcelCalendar($this->excelCalendar);
Functions::setCompatibilityMode($this->compatibilityMode); Functions::setCompatibilityMode($this->compatibilityMode);
Functions::setReturnDateType($this->returnDateType); Functions::setReturnDateType($this->returnDateType);
$this->sheet = null;
if ($this->spreadsheet !== null) {
$this->spreadsheet->disconnectWorksheets(); $this->spreadsheet->disconnectWorksheets();
$this->spreadsheet = null;
}
} }
protected static function setMac1904(): void protected static function setMac1904(): void
@ -82,4 +84,24 @@ class AllSetupTeardown extends TestCase
$this->expectException(CalcException::class); $this->expectException(CalcException::class);
} }
} }
protected function getSpreadsheet(): Spreadsheet
{
if ($this->spreadsheet !== null) {
return $this->spreadsheet;
}
$this->spreadsheet = new Spreadsheet();
return $this->spreadsheet;
}
protected function getSheet(): Worksheet
{
if ($this->sheet !== null) {
return $this->sheet;
}
$this->sheet = $this->getSpreadsheet()->getActiveSheet();
return $this->sheet;
}
} }

View File

@ -12,7 +12,7 @@ class DateDifTest extends AllSetupTeardown
public function testDATEDIF($expectedResult, string $formula): void public function testDATEDIF($expectedResult, string $formula): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
$sheet->getCell('A1')->setValue("=DATEDIF($formula)"); $sheet->getCell('A1')->setValue("=DATEDIF($formula)");
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -14,7 +14,7 @@ class DateTest extends AllSetupTeardown
public function testDATE($expectedResult, string $formula): void public function testDATE($expectedResult, string $formula): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
$sheet->getCell('A1')->setValue("=DATE($formula)"); $sheet->getCell('A1')->setValue("=DATE($formula)");
self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -15,7 +15,7 @@ class DateValueTest extends AllSetupTeardown
*/ */
public function testDATEVALUE($expectedResult, string $dateValue): void public function testDATEVALUE($expectedResult, string $dateValue): void
{ {
$this->sheet->getCell('B1')->setValue('1954-07-20'); $this->getSheet()->getCell('B1')->setValue('1954-07-20');
// Loop to avoid extraordinarily rare edge case where first calculation // Loop to avoid extraordinarily rare edge case where first calculation
// and second do not take place on same day. // and second do not take place on same day.
$row = 0; $row = 0;
@ -29,8 +29,8 @@ class DateValueTest extends AllSetupTeardown
$expectedResult = DateValue::fromString($replYMD); $expectedResult = DateValue::fromString($replYMD);
} }
} }
$this->sheet->getCell("A$row")->setValue("=DATEVALUE($dateValue)"); $this->getSheet()->getCell("A$row")->setValue("=DATEVALUE($dateValue)");
$result = $this->sheet->getCell("A$row")->getCalculatedValue(); $result = $this->getSheet()->getCell("A$row")->getCalculatedValue();
$dtEnd = new DateTimeImmutable(); $dtEnd = new DateTimeImmutable();
$endDay = $dtEnd->format('d'); $endDay = $dtEnd->format('d');
} while ($startDay !== $endDay); } while ($startDay !== $endDay);

View File

@ -12,7 +12,7 @@ class DayTest extends AllSetupTeardown
public function testDAY($expectedResultExcel, string $dateTimeValue): void public function testDAY($expectedResultExcel, string $dateTimeValue): void
{ {
$this->mightHaveException($expectedResultExcel); $this->mightHaveException($expectedResultExcel);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
$sheet->getCell('A1')->setValue("=DAY($dateTimeValue)"); $sheet->getCell('A1')->setValue("=DAY($dateTimeValue)");
self::assertSame($expectedResultExcel, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResultExcel, $sheet->getCell('A1')->getCalculatedValue());
@ -32,7 +32,7 @@ class DayTest extends AllSetupTeardown
{ {
self::setOpenOffice(); self::setOpenOffice();
$this->mightHaveException($expectedResultOpenOffice); $this->mightHaveException($expectedResultOpenOffice);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('A2')->setValue("=DAY($dateTimeValue)"); $sheet->getCell('A2')->setValue("=DAY($dateTimeValue)");
self::assertSame($expectedResultOpenOffice, $sheet->getCell('A2')->getCalculatedValue()); self::assertSame($expectedResultOpenOffice, $sheet->getCell('A2')->getCalculatedValue());
} }

View File

@ -12,7 +12,7 @@ class Days360Test extends AllSetupTeardown
public function testDAYS360($expectedResult, string $formula): void public function testDAYS360($expectedResult, string $formula): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('B1')->setValue('2000-02-29'); $sheet->getCell('B1')->setValue('2000-02-29');
$sheet->getCell('C1')->setValue('2000-03-31'); $sheet->getCell('C1')->setValue('2000-03-31');
$sheet->getCell('A1')->setValue("=DAYS360($formula)"); $sheet->getCell('A1')->setValue("=DAYS360($formula)");

View File

@ -17,7 +17,7 @@ class DaysTest extends AllSetupTeardown
public function testDAYS($expectedResult, string $formula): void public function testDAYS($expectedResult, string $formula): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
$sheet->getCell('C1')->setValue('1954-11-30'); $sheet->getCell('C1')->setValue('1954-11-30');
$sheet->getCell('A1')->setValue("=DAYS($formula)"); $sheet->getCell('A1')->setValue("=DAYS($formula)");

View File

@ -14,7 +14,7 @@ class EDateTest extends AllSetupTeardown
public function testEDATE($expectedResult, string $formula): void public function testEDATE($expectedResult, string $formula): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('A1')->setValue("=EDATE($formula)"); $sheet->getCell('A1')->setValue("=EDATE($formula)");
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -14,7 +14,7 @@ class EoMonthTest extends AllSetupTeardown
public function testEOMONTH($expectedResult, string $formula): void public function testEOMONTH($expectedResult, string $formula): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('A1')->setValue("=EOMONTH($formula)"); $sheet->getCell('A1')->setValue("=EOMONTH($formula)");
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -12,7 +12,7 @@ class HourTest extends AllSetupTeardown
public function testHOUR($expectedResult, string $dateTimeValue): void public function testHOUR($expectedResult, string $dateTimeValue): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('A1')->setValue("=HOUR($dateTimeValue)"); $sheet->getCell('A1')->setValue("=HOUR($dateTimeValue)");
$sheet->getCell('B1')->setValue('1954-11-23 2:23:46'); $sheet->getCell('B1')->setValue('1954-11-23 2:23:46');
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -13,7 +13,7 @@ class IsoWeekNumTest extends AllSetupTeardown
public function testISOWEEKNUM($expectedResult, $dateValue): void public function testISOWEEKNUM($expectedResult, $dateValue): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('A1')->setValue("=ISOWEEKNUM($dateValue)"); $sheet->getCell('A1')->setValue("=ISOWEEKNUM($dateValue)");
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());
@ -34,7 +34,7 @@ class IsoWeekNumTest extends AllSetupTeardown
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
self::setMac1904(); self::setMac1904();
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('A1')->setValue("=ISOWEEKNUM($dateValue)"); $sheet->getCell('A1')->setValue("=ISOWEEKNUM($dateValue)");
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -12,7 +12,7 @@ class MinuteTest extends AllSetupTeardown
public function testMINUTE($expectedResult, string $dateTimeValue): void public function testMINUTE($expectedResult, string $dateTimeValue): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('A1')->setValue("=MINUTE($dateTimeValue)"); $sheet->getCell('A1')->setValue("=MINUTE($dateTimeValue)");
$sheet->getCell('B1')->setValue('1954-11-23 2:23:46'); $sheet->getCell('B1')->setValue('1954-11-23 2:23:46');
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -12,7 +12,7 @@ class MonthTest extends AllSetupTeardown
public function testMONTH($expectedResult, string $dateTimeValue): void public function testMONTH($expectedResult, string $dateTimeValue): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('A1')->setValue("=MONTH($dateTimeValue)"); $sheet->getCell('A1')->setValue("=MONTH($dateTimeValue)");
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -14,7 +14,7 @@ class NetworkDaysTest extends AllSetupTeardown
public function testNETWORKDAYS($expectedResult, $arg1 = 'omitted', $arg2 = 'omitted', ?array $arg3 = null): void public function testNETWORKDAYS($expectedResult, $arg1 = 'omitted', $arg2 = 'omitted', ?array $arg3 = null): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
if ($arg1 !== null) { if ($arg1 !== null) {
$sheet->getCell('A1')->setValue($arg1); $sheet->getCell('A1')->setValue($arg1);
} }

View File

@ -8,7 +8,7 @@ class NowTest extends AllSetupTeardown
{ {
public function testNow(): void public function testNow(): void
{ {
$sheet = $this->sheet; $sheet = $this->getSheet();
// Loop to avoid rare edge case where first calculation // Loop to avoid rare edge case where first calculation
// and second do not take place in same second. // and second do not take place in same second.
do { do {

View File

@ -12,7 +12,7 @@ class SecondTest extends AllSetupTeardown
public function testSECOND($expectedResult, string $dateTimeValue): void public function testSECOND($expectedResult, string $dateTimeValue): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('A1')->setValue("=SECOND($dateTimeValue)"); $sheet->getCell('A1')->setValue("=SECOND($dateTimeValue)");
$sheet->getCell('B1')->setValue('1954-11-23 2:23:46'); $sheet->getCell('B1')->setValue('1954-11-23 2:23:46');
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -14,7 +14,7 @@ class TimeTest extends AllSetupTeardown
public function testTIME($expectedResult, string $formula): void public function testTIME($expectedResult, string $formula): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('B1')->setValue('15'); $sheet->getCell('B1')->setValue('15');
$sheet->getCell('B2')->setValue('32'); $sheet->getCell('B2')->setValue('32');
$sheet->getCell('B3')->setValue('50'); $sheet->getCell('B3')->setValue('50');

View File

@ -15,7 +15,7 @@ class TimeValueTest extends AllSetupTeardown
public function testTIMEVALUE($expectedResult, $timeValue): void public function testTIMEVALUE($expectedResult, $timeValue): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('B1')->setValue('03:45:52'); $sheet->getCell('B1')->setValue('03:45:52');
$sheet->getCell('A1')->setValue("=TIMEVALUE($timeValue)"); $sheet->getCell('A1')->setValue("=TIMEVALUE($timeValue)");
$result = $sheet->getCell('A1')->getCalculatedValue(); $result = $sheet->getCell('A1')->getCalculatedValue();

View File

@ -8,7 +8,7 @@ class TodayTest extends AllSetupTeardown
{ {
public function testToday(): void public function testToday(): void
{ {
$sheet = $this->sheet; $sheet = $this->getSheet();
// Loop to avoid rare edge case where first calculation // Loop to avoid rare edge case where first calculation
// and second do not take place in same second. // and second do not take place in same second.
do { do {

View File

@ -14,7 +14,7 @@ class WeekDayTest extends AllSetupTeardown
public function testWEEKDAY($expectedResult, string $formula): void public function testWEEKDAY($expectedResult, string $formula): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
$sheet->getCell('A1')->setValue("=WEEKDAY($formula)"); $sheet->getCell('A1')->setValue("=WEEKDAY($formula)");
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -12,7 +12,7 @@ class WeekNumTest extends AllSetupTeardown
public function testWEEKNUM($expectedResult, string $formula): void public function testWEEKNUM($expectedResult, string $formula): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
$sheet->getCell('A1')->setValue("=WEEKNUM($formula)"); $sheet->getCell('A1')->setValue("=WEEKNUM($formula)");
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());
@ -32,7 +32,7 @@ class WeekNumTest extends AllSetupTeardown
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
self::setMac1904(); self::setMac1904();
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
$sheet->getCell('A1')->setValue("=WEEKNUM($formula)"); $sheet->getCell('A1')->setValue("=WEEKNUM($formula)");
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());

View File

@ -14,7 +14,7 @@ class WorkDayTest extends AllSetupTeardown
public function testWORKDAY($expectedResult, $arg1 = 'omitted', $arg2 = 'omitted', ?array $arg3 = null): void public function testWORKDAY($expectedResult, $arg1 = 'omitted', $arg2 = 'omitted', ?array $arg3 = null): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
if ($arg1 !== null) { if ($arg1 !== null) {
$sheet->getCell('A1')->setValue($arg1); $sheet->getCell('A1')->setValue($arg1);
} }

View File

@ -15,7 +15,7 @@ class YearFracTest extends AllSetupTeardown
public function testYEARFRAC($expectedResult, $arg1 = 'omitted', $arg2 = 'omitted', $arg3 = 'omitted'): void public function testYEARFRAC($expectedResult, $arg1 = 'omitted', $arg2 = 'omitted', $arg3 = 'omitted'): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
if ($arg1 !== null) { if ($arg1 !== null) {
$sheet->getCell('A1')->setValue($arg1); $sheet->getCell('A1')->setValue($arg1);
} }

View File

@ -12,7 +12,7 @@ class YearTest extends AllSetupTeardown
public function testYEAR($expectedResult, string $dateTimeValue): void public function testYEAR($expectedResult, string $dateTimeValue): void
{ {
$this->mightHaveException($expectedResult); $this->mightHaveException($expectedResult);
$sheet = $this->sheet; $sheet = $this->getSheet();
$sheet->getCell('A1')->setValue("=YEAR($dateTimeValue)"); $sheet->getCell('A1')->setValue("=YEAR($dateTimeValue)");
$sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('B1')->setValue('1954-11-23');
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());