diff --git a/tests/PhpSpreadsheetTests/Writer/PreCalcTest.php b/tests/PhpSpreadsheetTests/Writer/PreCalcTest.php
index 9e6a0d8a..2db372c4 100644
--- a/tests/PhpSpreadsheetTests/Writer/PreCalcTest.php
+++ b/tests/PhpSpreadsheetTests/Writer/PreCalcTest.php
@@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class PreCalcTest extends AbstractFunctional
@@ -42,6 +43,136 @@ class PreCalcTest extends AbstractFunctional
];
}
+ private static function autoSize(?ColumnDimension $columnDimension): void
+ {
+ if ($columnDimension === null) {
+ self::fail('Unable to getColumnDimension');
+ } else {
+ $columnDimension->setAutoSize(true);
+ }
+ }
+
+ private static function verifyA2(Calculation $calculation, string $title, ?bool $preCalc): void
+ {
+ $cellValue = 0;
+ // A2 has no cached calculation value if preCalc is false
+ if ($preCalc === false) {
+ self::assertFalse($calculation->getValueFromCache("$title!A2", $cellValue));
+ } else {
+ self::assertTrue($calculation->getValueFromCache("$title!A2", $cellValue));
+ self::assertSame(3, $cellValue);
+ }
+ }
+
+ private const AUTOSIZE_TYPES = ['Xlsx', 'Xls', 'Html'];
+
+ private static function verifyA3B2(Calculation $calculation, string $title, ?bool $preCalc, string $type): void
+ {
+ $cellValue = 0;
+ if (in_array($type, self::AUTOSIZE_TYPES) || $preCalc !== false) {
+ // These 3 types support auto-sizing.
+ // A3 has cached calculation value because it is used in B2 calculation
+ self::assertTrue($calculation->getValueFromCache("$title!A3", $cellValue));
+ self::assertSame(11, $cellValue);
+ // B2 has cached calculation value because its column is auto-sized
+ self::assertTrue($calculation->getValueFromCache("$title!B2", $cellValue));
+ self::assertSame(14, $cellValue);
+ } else {
+ self::assertFalse($calculation->getValueFromCache("$title!A3", $cellValue));
+ self::assertFalse($calculation->getValueFromCache("$title!B2", $cellValue));
+ }
+ }
+
+ private static function readFile(string $file): string
+ {
+ $dataOut = '';
+ $data = file_get_contents($file);
+ // confirm that file contains B2 pre-calculated or not as appropriate
+ if ($data === false) {
+ self::fail("Unable to read $file");
+ } else {
+ $dataOut = $data;
+ }
+
+ return $dataOut;
+ }
+
+ private function verifyXlsx(?bool $preCalc, string $type): void
+ {
+ if ($type === 'Xlsx') {
+ $file = 'zip://';
+ $file .= $this->outfile;
+ $file .= '#xl/worksheets/sheet1.xml';
+ $data = self::readFile($file);
+ // confirm that file contains B2 pre-calculated or not as appropriate
+ if ($preCalc === false) {
+ self::assertStringContainsString('3+A30', $data);
+ } else {
+ self::assertStringContainsString('3+A314', $data);
+ }
+ $file = 'zip://';
+ $file .= $this->outfile;
+ $file .= '#xl/workbook.xml';
+ $data = self::readFile($file);
+ // confirm whether workbook is set to recalculate
+ if ($preCalc === false) {
+ self::assertStringContainsString('', $data);
+ } else {
+ self::assertStringContainsString('', $data);
+ }
+ }
+ }
+
+ private function verifyOds(?bool $preCalc, string $type): void
+ {
+ if ($type === 'Ods') {
+ $file = 'zip://';
+ $file .= $this->outfile;
+ $file .= '#content.xml';
+ $data = self::readFile($file);
+ // confirm that file contains B2 pre-calculated or not as appropriate
+ if ($preCalc === false) {
+ self::assertStringContainsString('table:formula="of:=3+[.A3]" office:value-type="string" office:value="=3+A3"', $data);
+ } else {
+ self::assertStringContainsString(' table:formula="of:=3+[.A3]" office:value-type="float" office:value="14"', $data);
+ }
+ }
+ }
+
+ private function verifyHtml(?bool $preCalc, string $type): void
+ {
+ if ($type === 'Html') {
+ $data = self::readFile($this->outfile);
+ // confirm that file contains B2 pre-calculated or not as appropriate
+ if ($preCalc === false) {
+ self::assertStringContainsString('>=1+2', $data);
+ self::assertStringContainsString('>=3+A3', $data);
+ self::assertStringContainsString('>=5+6', $data);
+ } else {
+ self::assertStringContainsString('>3', $data);
+ self::assertStringContainsString('>14', $data);
+ self::assertStringContainsString('>11', $data);
+ }
+ }
+ }
+
+ private function verifyCsv(?bool $preCalc, string $type): void
+ {
+ if ($type === 'Csv') {
+ $data = self::readFile($this->outfile);
+ // confirm that file contains B2 pre-calculated or not as appropriate
+ if ($preCalc === false) {
+ self::assertStringContainsString('"=1+2"', $data);
+ self::assertStringContainsString('"=3+A3"', $data);
+ self::assertStringContainsString('"=5+6"', $data);
+ } else {
+ self::assertStringContainsString('"3"', $data);
+ self::assertStringContainsString('"14"', $data);
+ self::assertStringContainsString('"11"', $data);
+ }
+ }
+ }
+
/**
* @dataProvider providerPreCalc
*/
@@ -55,11 +186,7 @@ class PreCalcTest extends AbstractFunctional
$sheet->getCell('A3')->setValue('=5+6');
$sheet->getCell('B2')->setValue('=3+A3');
$columnDimension = $sheet->getColumnDimension('B');
- if ($columnDimension === null) {
- self::fail('Unable to getColumnDimension');
- } else {
- $columnDimension->setAutoSize(true);
- }
+ self::autoSize($columnDimension);
$writer = IOFactory::createWriter($spreadsheet, $type);
if ($preCalc !== null) {
@@ -69,100 +196,13 @@ class PreCalcTest extends AbstractFunctional
$writer->save($this->outfile);
$title = $sheet->getTitle();
$calculation = Calculation::getInstance($spreadsheet);
- $cellValue = 0;
- // A2 has no cached calculation value if preCalc is false
- if ($preCalc === false) {
- self::assertFalse($calculation->getValueFromCache("$title!A2", $cellValue));
- } else {
- self::assertTrue($calculation->getValueFromCache("$title!A2", $cellValue));
- self::assertSame(3, $cellValue);
- }
- if ($type === 'Xlsx' || $type === 'Xls' || $type === 'Html' || $preCalc !== false) {
- // These 3 types support auto-sizing.
- // A3 has cached calculation value because it is used in B2 calculation
- self::assertTrue($calculation->getValueFromCache("$title!A3", $cellValue));
- self::assertSame(11, $cellValue);
- // B2 has cached calculation value because its column is auto-sized
- self::assertTrue($calculation->getValueFromCache("$title!B2", $cellValue));
- self::assertSame(14, $cellValue);
- } else {
- self::assertFalse($calculation->getValueFromCache("$title!A3", $cellValue));
- self::assertFalse($calculation->getValueFromCache("$title!B2", $cellValue));
- }
- if ($type === 'Xlsx') {
- $file = 'zip://';
- $file .= $this->outfile;
- $file .= '#xl/worksheets/sheet1.xml';
- $data = file_get_contents($file);
- // confirm that file contains B2 pre-calculated or not as appropriate
- if ($data === false) {
- self::fail('Unable to read worksheet file');
- } else {
- if ($preCalc === false) {
- self::assertStringContainsString('3+A30', $data);
- } else {
- self::assertStringContainsString('3+A314', $data);
- }
- }
- $file = 'zip://';
- $file .= $this->outfile;
- $file .= '#xl/workbook.xml';
- $data = file_get_contents($file);
- // confirm whether workbook is set to recalculate
- if ($data === false) {
- self::fail('Unable to read workbook file');
- } elseif ($preCalc === false) {
- self::assertStringContainsString('', $data);
- } else {
- self::assertStringContainsString('', $data);
- }
- } elseif ($type === 'Ods') {
- $file = 'zip://';
- $file .= $this->outfile;
- $file .= '#content.xml';
- $data = file_get_contents($file);
- // confirm that file contains B2 pre-calculated or not as appropriate
- if ($data === false) {
- self::fail('Unable to read Ods file');
- } else {
- if ($preCalc === false) {
- self::assertStringContainsString('table:formula="of:=3+[.A3]" office:value-type="string" office:value="=3+A3"', $data);
- } else {
- self::assertStringContainsString(' table:formula="of:=3+[.A3]" office:value-type="float" office:value="14"', $data);
- }
- }
- } elseif ($type === 'Html') {
- $data = file_get_contents($this->outfile);
- // confirm that file contains B2 pre-calculated or not as appropriate
- if ($data === false) {
- self::fail('Unable to read Html file');
- } else {
- if ($preCalc === false) {
- self::assertStringContainsString('>=1+2', $data);
- self::assertStringContainsString('>=3+A3', $data);
- self::assertStringContainsString('>=5+6', $data);
- } else {
- self::assertStringContainsString('>3', $data);
- self::assertStringContainsString('>14', $data);
- self::assertStringContainsString('>11', $data);
- }
- }
- } elseif ($type === 'Csv') {
- $data = file_get_contents($this->outfile);
- // confirm that file contains B2 pre-calculated or not as appropriate
- if ($data === false) {
- self::fail('Unable to read Csv file');
- } else {
- if ($preCalc === false) {
- self::assertStringContainsString('"=1+2"', $data);
- self::assertStringContainsString('"=3+A3"', $data);
- self::assertStringContainsString('"=5+6"', $data);
- } else {
- self::assertStringContainsString('"3"', $data);
- self::assertStringContainsString('"14"', $data);
- self::assertStringContainsString('"11"', $data);
- }
- }
- }
+ // verify values in Calculation cache
+ self::verifyA2($calculation, $title, $preCalc);
+ self::verifyA3B2($calculation, $title, $preCalc, $type);
+ // verify values in output file
+ $this->verifyXlsx($preCalc, $type);
+ $this->verifyOds($preCalc, $type);
+ $this->verifyHtml($preCalc, $type);
+ $this->verifyCsv($preCalc, $type);
}
}