diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/AllSetupTeardown.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/AllSetupTeardown.php index 414670c4..9fb5dfa5 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/AllSetupTeardown.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/AllSetupTeardown.php @@ -27,22 +27,20 @@ class AllSetupTeardown extends TestCase private $returnDateType; /** - * @var Spreadsheet + * @var ?Spreadsheet */ - protected $spreadsheet; + private $spreadsheet; /** - * @var Worksheet + * @var ?Worksheet */ - protected $sheet; + private $sheet; protected function setUp(): void { $this->compatibilityMode = Functions::getCompatibilityMode(); $this->excelCalendar = Date::getExcelCalendar(); $this->returnDateType = Functions::getReturnDateType(); - $this->spreadsheet = new Spreadsheet(); - $this->sheet = $this->spreadsheet->getActiveSheet(); } protected function tearDown(): void @@ -50,7 +48,11 @@ class AllSetupTeardown extends TestCase Date::setExcelCalendar($this->excelCalendar); Functions::setCompatibilityMode($this->compatibilityMode); Functions::setReturnDateType($this->returnDateType); - $this->spreadsheet->disconnectWorksheets(); + $this->sheet = null; + if ($this->spreadsheet !== null) { + $this->spreadsheet->disconnectWorksheets(); + $this->spreadsheet = null; + } } protected static function setMac1904(): void @@ -82,4 +84,24 @@ class AllSetupTeardown extends TestCase $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; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateDifTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateDifTest.php index 3aa9446d..d7d69376 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateDifTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateDifTest.php @@ -12,7 +12,7 @@ class DateDifTest extends AllSetupTeardown public function testDATEDIF($expectedResult, string $formula): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('A1')->setValue("=DATEDIF($formula)"); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateTest.php index 2ba9d41d..5e884707 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateTest.php @@ -14,7 +14,7 @@ class DateTest extends AllSetupTeardown public function testDATE($expectedResult, string $formula): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('A1')->setValue("=DATE($formula)"); self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateValueTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateValueTest.php index cf37162a..521bde1f 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateValueTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateValueTest.php @@ -15,7 +15,7 @@ class DateValueTest extends AllSetupTeardown */ 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 // and second do not take place on same day. $row = 0; @@ -29,8 +29,8 @@ class DateValueTest extends AllSetupTeardown $expectedResult = DateValue::fromString($replYMD); } } - $this->sheet->getCell("A$row")->setValue("=DATEVALUE($dateValue)"); - $result = $this->sheet->getCell("A$row")->getCalculatedValue(); + $this->getSheet()->getCell("A$row")->setValue("=DATEVALUE($dateValue)"); + $result = $this->getSheet()->getCell("A$row")->getCalculatedValue(); $dtEnd = new DateTimeImmutable(); $endDay = $dtEnd->format('d'); } while ($startDay !== $endDay); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DayTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DayTest.php index dc8adf11..af196050 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DayTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DayTest.php @@ -12,7 +12,7 @@ class DayTest extends AllSetupTeardown public function testDAY($expectedResultExcel, string $dateTimeValue): void { $this->mightHaveException($expectedResultExcel); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('A1')->setValue("=DAY($dateTimeValue)"); self::assertSame($expectedResultExcel, $sheet->getCell('A1')->getCalculatedValue()); @@ -32,7 +32,7 @@ class DayTest extends AllSetupTeardown { self::setOpenOffice(); $this->mightHaveException($expectedResultOpenOffice); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('A2')->setValue("=DAY($dateTimeValue)"); self::assertSame($expectedResultOpenOffice, $sheet->getCell('A2')->getCalculatedValue()); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/Days360Test.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/Days360Test.php index bd3a0283..5a3e235d 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/Days360Test.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/Days360Test.php @@ -12,7 +12,7 @@ class Days360Test extends AllSetupTeardown public function testDAYS360($expectedResult, string $formula): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('B1')->setValue('2000-02-29'); $sheet->getCell('C1')->setValue('2000-03-31'); $sheet->getCell('A1')->setValue("=DAYS360($formula)"); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DaysTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DaysTest.php index e4db4f26..a63e45cc 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DaysTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DaysTest.php @@ -17,7 +17,7 @@ class DaysTest extends AllSetupTeardown public function testDAYS($expectedResult, string $formula): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('C1')->setValue('1954-11-30'); $sheet->getCell('A1')->setValue("=DAYS($formula)"); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/EDateTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/EDateTest.php index b3b2bd25..22613fe5 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/EDateTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/EDateTest.php @@ -14,7 +14,7 @@ class EDateTest extends AllSetupTeardown public function testEDATE($expectedResult, string $formula): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue("=EDATE($formula)"); $sheet->getCell('B1')->setValue('1954-11-23'); self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/EoMonthTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/EoMonthTest.php index 45e25e3b..3c39fbbc 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/EoMonthTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/EoMonthTest.php @@ -14,7 +14,7 @@ class EoMonthTest extends AllSetupTeardown public function testEOMONTH($expectedResult, string $formula): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue("=EOMONTH($formula)"); $sheet->getCell('B1')->setValue('1954-11-23'); self::assertEquals($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/HourTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/HourTest.php index 93afbb5d..c1e4f29c 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/HourTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/HourTest.php @@ -12,7 +12,7 @@ class HourTest extends AllSetupTeardown public function testHOUR($expectedResult, string $dateTimeValue): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue("=HOUR($dateTimeValue)"); $sheet->getCell('B1')->setValue('1954-11-23 2:23:46'); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/IsoWeekNumTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/IsoWeekNumTest.php index 6be2e1af..29e35b38 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/IsoWeekNumTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/IsoWeekNumTest.php @@ -13,7 +13,7 @@ class IsoWeekNumTest extends AllSetupTeardown public function testISOWEEKNUM($expectedResult, $dateValue): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue("=ISOWEEKNUM($dateValue)"); $sheet->getCell('B1')->setValue('1954-11-23'); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); @@ -34,7 +34,7 @@ class IsoWeekNumTest extends AllSetupTeardown { $this->mightHaveException($expectedResult); self::setMac1904(); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue("=ISOWEEKNUM($dateValue)"); $sheet->getCell('B1')->setValue('1954-11-23'); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/MinuteTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/MinuteTest.php index 57d7a77e..b5207d7e 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/MinuteTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/MinuteTest.php @@ -12,7 +12,7 @@ class MinuteTest extends AllSetupTeardown public function testMINUTE($expectedResult, string $dateTimeValue): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue("=MINUTE($dateTimeValue)"); $sheet->getCell('B1')->setValue('1954-11-23 2:23:46'); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/MonthTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/MonthTest.php index ed09a993..30465e87 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/MonthTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/MonthTest.php @@ -12,7 +12,7 @@ class MonthTest extends AllSetupTeardown public function testMONTH($expectedResult, string $dateTimeValue): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue("=MONTH($dateTimeValue)"); $sheet->getCell('B1')->setValue('1954-11-23'); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/NetworkDaysTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/NetworkDaysTest.php index b121f7bf..d6b1a89f 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/NetworkDaysTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/NetworkDaysTest.php @@ -14,7 +14,7 @@ class NetworkDaysTest extends AllSetupTeardown public function testNETWORKDAYS($expectedResult, $arg1 = 'omitted', $arg2 = 'omitted', ?array $arg3 = null): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); if ($arg1 !== null) { $sheet->getCell('A1')->setValue($arg1); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/NowTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/NowTest.php index e0f68c24..42362b99 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/NowTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/NowTest.php @@ -8,7 +8,7 @@ class NowTest extends AllSetupTeardown { public function testNow(): void { - $sheet = $this->sheet; + $sheet = $this->getSheet(); // Loop to avoid rare edge case where first calculation // and second do not take place in same second. do { diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/SecondTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/SecondTest.php index 6c264a57..1a411d45 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/SecondTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/SecondTest.php @@ -12,7 +12,7 @@ class SecondTest extends AllSetupTeardown public function testSECOND($expectedResult, string $dateTimeValue): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue("=SECOND($dateTimeValue)"); $sheet->getCell('B1')->setValue('1954-11-23 2:23:46'); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TimeTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TimeTest.php index ae83f05a..3ee1aa55 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TimeTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TimeTest.php @@ -14,7 +14,7 @@ class TimeTest extends AllSetupTeardown public function testTIME($expectedResult, string $formula): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('B1')->setValue('15'); $sheet->getCell('B2')->setValue('32'); $sheet->getCell('B3')->setValue('50'); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TimeValueTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TimeValueTest.php index 23eb4f17..6995c312 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TimeValueTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TimeValueTest.php @@ -15,7 +15,7 @@ class TimeValueTest extends AllSetupTeardown public function testTIMEVALUE($expectedResult, $timeValue): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('B1')->setValue('03:45:52'); $sheet->getCell('A1')->setValue("=TIMEVALUE($timeValue)"); $result = $sheet->getCell('A1')->getCalculatedValue(); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TodayTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TodayTest.php index 6ce82bfd..b7108f75 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TodayTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/TodayTest.php @@ -8,7 +8,7 @@ class TodayTest extends AllSetupTeardown { public function testToday(): void { - $sheet = $this->sheet; + $sheet = $this->getSheet(); // Loop to avoid rare edge case where first calculation // and second do not take place in same second. do { diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WeekDayTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WeekDayTest.php index 625e4414..3c7f682d 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WeekDayTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WeekDayTest.php @@ -14,7 +14,7 @@ class WeekDayTest extends AllSetupTeardown public function testWEEKDAY($expectedResult, string $formula): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('A1')->setValue("=WEEKDAY($formula)"); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WeekNumTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WeekNumTest.php index cf8ac65b..caa71f17 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WeekNumTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WeekNumTest.php @@ -12,7 +12,7 @@ class WeekNumTest extends AllSetupTeardown public function testWEEKNUM($expectedResult, string $formula): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('A1')->setValue("=WEEKNUM($formula)"); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); @@ -32,7 +32,7 @@ class WeekNumTest extends AllSetupTeardown { $this->mightHaveException($expectedResult); self::setMac1904(); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('B1')->setValue('1954-11-23'); $sheet->getCell('A1')->setValue("=WEEKNUM($formula)"); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WorkDayTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WorkDayTest.php index 80829699..3cfa9219 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WorkDayTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WorkDayTest.php @@ -14,7 +14,7 @@ class WorkDayTest extends AllSetupTeardown public function testWORKDAY($expectedResult, $arg1 = 'omitted', $arg2 = 'omitted', ?array $arg3 = null): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); if ($arg1 !== null) { $sheet->getCell('A1')->setValue($arg1); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/YearFracTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/YearFracTest.php index e16ce697..d7427bfd 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/YearFracTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/YearFracTest.php @@ -15,7 +15,7 @@ class YearFracTest extends AllSetupTeardown public function testYEARFRAC($expectedResult, $arg1 = 'omitted', $arg2 = 'omitted', $arg3 = 'omitted'): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); if ($arg1 !== null) { $sheet->getCell('A1')->setValue($arg1); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/YearTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/YearTest.php index 74b3bed0..a31ac7aa 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/YearTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/YearTest.php @@ -12,7 +12,7 @@ class YearTest extends AllSetupTeardown public function testYEAR($expectedResult, string $dateTimeValue): void { $this->mightHaveException($expectedResult); - $sheet = $this->sheet; + $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue("=YEAR($dateTimeValue)"); $sheet->getCell('B1')->setValue('1954-11-23'); self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());