diff --git a/CHANGELOG.md b/CHANGELOG.md index dd66498f19..05d4e19b6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Changed -- Nothing +- `toFormattedString` will now always return a string. This can affect the results of `toArray`, `namedRangeToArray`, and `rangeToArray`. [PR #3304](https://github.com/PHPOffice/PhpSpreadsheet/pull/3304) ### Deprecated diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f40d8b8ed0..6902498f96 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -790,56 +790,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Shared/Trend/Trend.php - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\Formatter\\:\\:splitFormat\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\Formatter\\:\\:splitFormat\\(\\) has parameter \\$sections with no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\Formatter\\:\\:splitFormat\\(\\) has parameter \\$value with no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\Formatter\\:\\:splitFormatCompare\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\Formatter\\:\\:splitFormatCompare\\(\\) has parameter \\$cond with no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\Formatter\\:\\:splitFormatCompare\\(\\) has parameter \\$dfcond with no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\Formatter\\:\\:splitFormatCompare\\(\\) has parameter \\$dfval with no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\Formatter\\:\\:splitFormatCompare\\(\\) has parameter \\$val with no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\Formatter\\:\\:splitFormatCompare\\(\\) has parameter \\$value with no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\Formatter\\:\\:toFormattedString\\(\\) should return string but returns float\\|int\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php - - message: "#^Negated boolean expression is always false\\.$#" count: 1 diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php index be195a8803..03b002e533 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php +++ b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php @@ -2,12 +2,19 @@ namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; +use PhpOffice\PhpSpreadsheet\RichText\RichText; use PhpOffice\PhpSpreadsheet\Style\Color; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; class Formatter { - private static function splitFormatCompare($value, $cond, $val, $dfcond, $dfval) + /** + * @param mixed $value + * @param mixed $val + * @param mixed $dfval + */ + private static function splitFormatCompare($value, ?string $cond, $val, string $dfcond, $dfval): bool { if (!$cond) { $cond = $dfcond; @@ -33,7 +40,8 @@ private static function splitFormatCompare($value, $cond, $val, $dfcond, $dfval) return $value >= $val; } - private static function splitFormat($sections, $value) + /** @param mixed $value */ + private static function splitFormat(array $sections, $value): array { // Extract the relevant section depending on whether number is positive, negative, or zero? // Text not supported yet. @@ -93,7 +101,7 @@ private static function splitFormat($sections, $value) /** * Convert a value in a pre-defined format to a PHP string. * - * @param mixed $value Value to format + * @param null|bool|float|int|RichText|string $value Value to format * @param string $format Format code, see = NumberFormat::FORMAT_* * @param array $callBack Callback function for additional formatting of string * @@ -101,15 +109,18 @@ private static function splitFormat($sections, $value) */ public static function toFormattedString($value, $format, $callBack = null) { + if (is_bool($value)) { + return $value ? Calculation::getTRUE() : Calculation::getFALSE(); + } // For now we do not treat strings although section 4 of a format code affects strings if (!is_numeric($value)) { - return $value; + return (string) $value; } // For 'General' format code, we just pass the value although this is not entirely the way Excel does it, // it seems to round numbers to a total of 10 digits. if (($format === NumberFormat::FORMAT_GENERAL) || ($format === NumberFormat::FORMAT_TEXT)) { - return $value; + return (string) $value; } // Ignore square-$-brackets prefix in format string, like "[$-411]ge.m.d", "[$-010419]0%", etc @@ -127,7 +138,7 @@ function ($matches) { $format = (string) preg_replace('/(\\\(((.)(?!((AM\/PM)|(A\/P))))|([^ ])))(?=(?:[^"]|"[^"]*")*$)/ui', '"${2}"', $format); // Get the sections, there can be up to four sections, separated with a semi-colon (but only if not a quoted literal) - $sections = preg_split('/(;)(?=(?:[^"]|"[^"]*")*$)/u', $format); + $sections = preg_split('/(;)(?=(?:[^"]|"[^"]*")*$)/u', $format) ?: []; [$colors, $format, $value] = self::splitFormat($sections, $value); @@ -145,8 +156,8 @@ function ($matches) { if (substr($format, 0, 1) === '"' && substr($format, -1, 1) === '"' && substr_count($format, '"') === 2) { $value = substr($format, 1, -1); } elseif (preg_match('/[0#, ]%/', $format)) { - // % number format - $value = PercentageFormatter::format($value, $format); + // % number format - avoid weird '-0' problem + $value = PercentageFormatter::format(0 + (float) $value, $format); } else { $value = NumberFormatter::format($value, $format); } diff --git a/tests/PhpSpreadsheetTests/Reader/Ods/HiddenMergeCellsTest.php b/tests/PhpSpreadsheetTests/Reader/Ods/HiddenMergeCellsTest.php index 710b292afd..3f38631c47 100644 --- a/tests/PhpSpreadsheetTests/Reader/Ods/HiddenMergeCellsTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Ods/HiddenMergeCellsTest.php @@ -3,46 +3,43 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods; use PhpOffice\PhpSpreadsheet\Reader\Ods; -use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class HiddenMergeCellsTest extends TestCase { - /** - * @var Spreadsheet - */ - private $spreadsheet; - - protected function setup(): void - { - $filename = 'tests/data/Reader/Ods/HiddenMergeCellsTest.ods'; - $reader = new Ods(); - $this->spreadsheet = $reader->load($filename); - } + private const FILENAME = 'tests/data/Reader/Ods/HiddenMergeCellsTest.ods'; public function testHiddenMergeCells(): void { - $c2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); + $reader = new Ods(); + $spreadsheet = $reader->load(self::FILENAME); + $c2InMergeRange = $spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); self::assertTrue($c2InMergeRange); - $a2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); + $a2InMergeRange = $spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); self::assertTrue($a2InMergeRange); - $a2MergeRangeValue = $this->spreadsheet->getActiveSheet()->getCell('A2')->isMergeRangeValueCell(); + $a2MergeRangeValue = $spreadsheet->getActiveSheet()->getCell('A2')->isMergeRangeValueCell(); self::assertTrue($a2MergeRangeValue); - $cellArray = $this->spreadsheet->getActiveSheet()->rangeToArray('A2:C2'); + $cellArray = $spreadsheet->getActiveSheet()->rangeToArray('A2:C2'); + self::assertSame([['12', '4', '3']], $cellArray); + $cellArray = $spreadsheet->getActiveSheet()->rangeToArray('A2:C2', null, true, false); self::assertSame([[12, 4, 3]], $cellArray); + $spreadsheet->disconnectWorksheets(); } public function testUnmergeHiddenMergeCells(): void { - $this->spreadsheet->getActiveSheet()->unmergeCells('A2:C2'); + $reader = new Ods(); + $spreadsheet = $reader->load(self::FILENAME); + $spreadsheet->getActiveSheet()->unmergeCells('A2:C2'); - $c2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); + $c2InMergeRange = $spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); self::assertFalse($c2InMergeRange); - $a2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); + $a2InMergeRange = $spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); self::assertFalse($a2InMergeRange); - $cellArray = $this->spreadsheet->getActiveSheet()->rangeToArray('A2:C2', null, false, false, false); + $cellArray = $spreadsheet->getActiveSheet()->rangeToArray('A2:C2', null, false, false, false); self::assertSame([[12, '=6-B1', '=A2/B2']], $cellArray); + $spreadsheet->disconnectWorksheets(); } } diff --git a/tests/PhpSpreadsheetTests/Reader/Ods/HiddenWorksheetTest.php b/tests/PhpSpreadsheetTests/Reader/Ods/HiddenWorksheetTest.php index c02c877128..5a0753d181 100644 --- a/tests/PhpSpreadsheetTests/Reader/Ods/HiddenWorksheetTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Ods/HiddenWorksheetTest.php @@ -3,29 +3,19 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods; use PhpOffice\PhpSpreadsheet\Reader\Ods; -use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class HiddenWorksheetTest extends TestCase { - /** - * @var Spreadsheet - */ - private $spreadsheet; - - protected function setup(): void + public function testPageSetup(): void { $filename = 'tests/data/Reader/Ods/HiddenSheet.ods'; $reader = new Ods(); - $this->spreadsheet = $reader->load($filename); - } - - public function testPageSetup(): void - { + $spreadsheet = $reader->load($filename); $assertions = $this->worksheetAssertions(); - foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + foreach ($spreadsheet->getAllSheets() as $worksheet) { if (!array_key_exists($worksheet->getTitle(), $assertions)) { continue; } @@ -40,6 +30,7 @@ public function testPageSetup(): void ); } } + $spreadsheet->disconnectWorksheets(); } private function worksheetAssertions(): array diff --git a/tests/PhpSpreadsheetTests/Reader/Xls/HiddenMergeCellsTest.php b/tests/PhpSpreadsheetTests/Reader/Xls/HiddenMergeCellsTest.php index c70852819e..0a59b7d447 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xls/HiddenMergeCellsTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xls/HiddenMergeCellsTest.php @@ -3,46 +3,43 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls; use PhpOffice\PhpSpreadsheet\Reader\Xls; -use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class HiddenMergeCellsTest extends TestCase { - /** - * @var Spreadsheet - */ - private $spreadsheet; - - protected function setup(): void - { - $filename = 'tests/data/Reader/XLS/HiddenMergeCellsTest.xls'; - $reader = new Xls(); - $this->spreadsheet = $reader->load($filename); - } + private const FILENAME = 'tests/data/Reader/XLS/HiddenMergeCellsTest.xls'; public function testHiddenMergeCells(): void { - $c2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); + $reader = new Xls(); + $spreadsheet = $reader->load(self::FILENAME); + $c2InMergeRange = $spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); self::assertTrue($c2InMergeRange); - $a2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); + $a2InMergeRange = $spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); self::assertTrue($a2InMergeRange); - $a2MergeRangeValue = $this->spreadsheet->getActiveSheet()->getCell('A2')->isMergeRangeValueCell(); + $a2MergeRangeValue = $spreadsheet->getActiveSheet()->getCell('A2')->isMergeRangeValueCell(); self::assertTrue($a2MergeRangeValue); - $cellArray = $this->spreadsheet->getActiveSheet()->rangeToArray('A2:C2'); + $cellArray = $spreadsheet->getActiveSheet()->rangeToArray('A2:C2'); + self::assertSame([['12', '4', '3']], $cellArray); + $cellArray = $spreadsheet->getActiveSheet()->rangeToArray('A2:C2', null, true, false); self::assertSame([[12, 4, 3]], $cellArray); + $spreadsheet->disconnectWorksheets(); } public function testUnmergeHiddenMergeCells(): void { - $this->spreadsheet->getActiveSheet()->unmergeCells('A2:C2'); + $reader = new Xls(); + $spreadsheet = $reader->load(self::FILENAME); + $spreadsheet->getActiveSheet()->unmergeCells('A2:C2'); - $c2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); + $c2InMergeRange = $spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); self::assertFalse($c2InMergeRange); - $a2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); + $a2InMergeRange = $spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); self::assertFalse($a2InMergeRange); - $cellArray = $this->spreadsheet->getActiveSheet()->rangeToArray('A2:C2', null, false, false, false); + $cellArray = $spreadsheet->getActiveSheet()->rangeToArray('A2:C2', null, false, false, false); self::assertSame([[12, '=6-B1', '=A2/B2']], $cellArray); + $spreadsheet->disconnectWorksheets(); } } diff --git a/tests/PhpSpreadsheetTests/Reader/Xls/HiddenWorksheetTest.php b/tests/PhpSpreadsheetTests/Reader/Xls/HiddenWorksheetTest.php index 82fd6e128e..e31b388049 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xls/HiddenWorksheetTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xls/HiddenWorksheetTest.php @@ -3,29 +3,19 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls; use PhpOffice\PhpSpreadsheet\Reader\Xls; -use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class HiddenWorksheetTest extends TestCase { - /** - * @var Spreadsheet - */ - private $spreadsheet; - - protected function setup(): void + public function testPageSetup(): void { $filename = 'tests/data/Reader/XLS/HiddenSheet.xls'; $reader = new Xls(); - $this->spreadsheet = $reader->load($filename); - } - - public function testPageSetup(): void - { + $spreadsheet = $reader->load($filename); $assertions = $this->worksheetAssertions(); - foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + foreach ($spreadsheet->getAllSheets() as $worksheet) { if (!array_key_exists($worksheet->getTitle(), $assertions)) { continue; } @@ -40,6 +30,7 @@ public function testPageSetup(): void ); } } + $spreadsheet->disconnectWorksheets(); } private function worksheetAssertions(): array diff --git a/tests/PhpSpreadsheetTests/Reader/Xls/NumberFormatGeneralTest.php b/tests/PhpSpreadsheetTests/Reader/Xls/NumberFormatGeneralTest.php index 8086789232..3bfea8bf22 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xls/NumberFormatGeneralTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xls/NumberFormatGeneralTest.php @@ -21,7 +21,7 @@ public function testGeneral(): void self::assertSame(2.95, $sheet->getCell('D2')->getValue()); self::assertSame(2.95, $sheet->getCell('D2')->getCalculatedValue()); self::assertSame('€ 2.95', $sheet->getCell('D2')->getFormattedValue()); - self::assertSame(21, $array[1][4]); + self::assertSame('21', $array[1][4]); self::assertSame(21, $sheet->getCell('E2')->getValue()); self::assertSame(21, $sheet->getCell('E2')->getCalculatedValue()); self::assertSame('21', $sheet->getCell('E2')->getFormattedValue()); diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/HiddenMergeCellsTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/HiddenMergeCellsTest.php index 4919cd272c..9d4fc81808 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xlsx/HiddenMergeCellsTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/HiddenMergeCellsTest.php @@ -3,46 +3,43 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; -use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class HiddenMergeCellsTest extends TestCase { - /** - * @var Spreadsheet - */ - private $spreadsheet; - - protected function setup(): void - { - $filename = 'tests/data/Reader/XLSX/HiddenMergeCellsTest.xlsx'; - $reader = new Xlsx(); - $this->spreadsheet = $reader->load($filename); - } + private const FILENAME = 'tests/data/Reader/XLSX/HiddenMergeCellsTest.xlsx'; public function testHiddenMergeCells(): void { - $c2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); + $reader = new Xlsx(); + $spreadsheet = $reader->load(self::FILENAME); + $c2InMergeRange = $spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); self::assertTrue($c2InMergeRange); - $a2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); + $a2InMergeRange = $spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); self::assertTrue($a2InMergeRange); - $a2MergeRangeValue = $this->spreadsheet->getActiveSheet()->getCell('A2')->isMergeRangeValueCell(); + $a2MergeRangeValue = $spreadsheet->getActiveSheet()->getCell('A2')->isMergeRangeValueCell(); self::assertTrue($a2MergeRangeValue); - $cellArray = $this->spreadsheet->getActiveSheet()->rangeToArray('A2:C2'); + $cellArray = $spreadsheet->getActiveSheet()->rangeToArray('A2:C2'); + self::assertSame([['12', '4', '3']], $cellArray); + $cellArray = $spreadsheet->getActiveSheet()->rangeToArray('A2:C2', null, true, false); self::assertSame([[12, 4, 3]], $cellArray); + $spreadsheet->disconnectWorksheets(); } public function testUnmergeHiddenMergeCells(): void { - $this->spreadsheet->getActiveSheet()->unmergeCells('A2:C2'); + $reader = new Xlsx(); + $spreadsheet = $reader->load(self::FILENAME); + $spreadsheet->getActiveSheet()->unmergeCells('A2:C2'); - $c2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); + $c2InMergeRange = $spreadsheet->getActiveSheet()->getCell('C2')->isInMergeRange(); self::assertFalse($c2InMergeRange); - $a2InMergeRange = $this->spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); + $a2InMergeRange = $spreadsheet->getActiveSheet()->getCell('A2')->isInMergeRange(); self::assertFalse($a2InMergeRange); - $cellArray = $this->spreadsheet->getActiveSheet()->rangeToArray('A2:C2', null, false, false, false); + $cellArray = $spreadsheet->getActiveSheet()->rangeToArray('A2:C2', null, false, false, false); self::assertSame([[12, '=6-B1', '=A2/B2']], $cellArray); + $spreadsheet->disconnectWorksheets(); } } diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/HiddenWorksheetTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/HiddenWorksheetTest.php index 130d76a642..e11954eb51 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xlsx/HiddenWorksheetTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/HiddenWorksheetTest.php @@ -3,29 +3,19 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; -use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class HiddenWorksheetTest extends TestCase { - /** - * @var Spreadsheet - */ - private $spreadsheet; - - protected function setup(): void + public function testPageSetup(): void { $filename = 'tests/data/Reader/XLSX/HiddenSheet.xlsx'; $reader = new Xlsx(); - $this->spreadsheet = $reader->load($filename); - } - - public function testPageSetup(): void - { + $spreadsheet = $reader->load($filename); $assertions = $this->worksheetAssertions(); - foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + foreach ($spreadsheet->getAllSheets() as $worksheet) { if (!array_key_exists($worksheet->getTitle(), $assertions)) { continue; } @@ -40,6 +30,7 @@ public function testPageSetup(): void ); } } + $spreadsheet->disconnectWorksheets(); } private function worksheetAssertions(): array diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2488Test.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2488Test.php index 805b6ff591..9b77df4efd 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2488Test.php +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2488Test.php @@ -28,7 +28,7 @@ public function testPreliminaries(): void } } - public function testIssue2450(): void + public function testIssue2488(): void { // Cell explicitly typed as numeric but without value. $filename = self::$testbook; @@ -38,12 +38,24 @@ public function testIssue2450(): void // E1 and D3 are numeric/boolean without value. // So is E2, but I don't see a practical difference // between null string and null in that case. + $expected = [ + ['1', '2', '3', '0', null, '-1', '-2', '-3'], + ['a', 'b', 'c', 'xxx', '', 'd', 'e', 'f'], + ['FALSE', 'FALSE', 'FALSE', null, 'TRUE', 'TRUE', 'TRUE', 'TRUE'], + ]; + self::assertSame($expected, $sheet->toArray()); + $expected = [ + ['1', '2', '3', '0', '', '-1', '-2', '-3'], + ['a', 'b', 'c', 'xxx', '', 'd', 'e', 'f'], + ['FALSE', 'FALSE', 'FALSE', '', 'TRUE', 'TRUE', 'TRUE', 'TRUE'], + ]; + self::assertSame($expected, $sheet->toArray('')); $expected = [ [1, 2, 3, 0, null, -1, -2, -3], ['a', 'b', 'c', 'xxx', '', 'd', 'e', 'f'], [false, false, false, null, true, true, true, true], ]; - self::assertSame($expected, $sheet->toArray()); + self::assertSame($expected, $sheet->toArray(null, true, false)); $spreadsheet->disconnectWorksheets(); } diff --git a/tests/PhpSpreadsheetTests/RichTextTest.php b/tests/PhpSpreadsheetTests/RichTextTest.php index e6f5551478..04a1d72a9e 100644 --- a/tests/PhpSpreadsheetTests/RichTextTest.php +++ b/tests/PhpSpreadsheetTests/RichTextTest.php @@ -36,5 +36,14 @@ public function testTextElements(): void $cloneText = clone $richText; self::assertEquals($richText, $cloneText); self::assertNotSame($richText, $cloneText); + + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->getCell('A1')->setValue($richText); + self::assertInstanceOf(RichText::class, $sheet->getCell('A1')->getValue()); + self::assertSame('ABC', $sheet->getCell('A1')->getFormattedValue()); + $sheet->getCell('B1')->setValue(-3.5); + self::assertSame([['ABC', '-3.5']], $sheet->toArray()); + $spreadsheet->disconnectWorksheets(); } } diff --git a/tests/PhpSpreadsheetTests/Worksheet/MergeBehaviourTest.php b/tests/PhpSpreadsheetTests/Worksheet/MergeBehaviourTest.php index 68c9d87a8e..7d7a2c8366 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/MergeBehaviourTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/MergeBehaviourTest.php @@ -38,8 +38,9 @@ public function testMergeCellsDefaultBehaviour(): void $worksheet->fromArray($this->testDataRaw, null, 'A1', true); $worksheet->mergeCells($mergeRange); - $mergeResult = $worksheet->toArray(null, true, true, false); + $mergeResult = $worksheet->toArray(null, true, false, false); self::assertSame($expectedResult, $mergeResult); + $spreadsheet->disconnectWorksheets(); } public function testMergeCellsDefaultBehaviourFormatted(): void @@ -57,6 +58,7 @@ public function testMergeCellsDefaultBehaviourFormatted(): void $mergeResult = $worksheet->toArray(null, true, true, false); self::assertSame($expectedResult, $mergeResult); + $spreadsheet->disconnectWorksheets(); } public function testMergeCellsHideBehaviour(): void @@ -73,8 +75,9 @@ public function testMergeCellsHideBehaviour(): void $worksheet->fromArray($this->testDataRaw, null, 'A1', true); $worksheet->mergeCells($mergeRange, Worksheet::MERGE_CELL_CONTENT_HIDE); - $mergeResult = $worksheet->toArray(null, true, true, false); + $mergeResult = $worksheet->toArray(null, true, false, false); self::assertSame($expectedResult, $mergeResult); + $spreadsheet->disconnectWorksheets(); } public function testMergeCellsHideBehaviourFormatted(): void @@ -92,6 +95,7 @@ public function testMergeCellsHideBehaviourFormatted(): void $mergeResult = $worksheet->toArray(null, true, true, false); self::assertSame($expectedResult, $mergeResult); + $spreadsheet->disconnectWorksheets(); } /** @@ -108,6 +112,7 @@ public function testMergeCellsMergeBehaviour(array $testData, string $mergeRange $mergeResult = $worksheet->toArray(null, true, true, false); self::assertSame($expectedResult, $mergeResult); + $spreadsheet->disconnectWorksheets(); } public function mergeCellsMergeBehaviourProvider(): array @@ -162,5 +167,6 @@ public function testMergeCellsMergeBehaviourFormatted(): void $mergeResult = $worksheet->toArray(null, true, true, false); self::assertSame($expectedResult, $mergeResult); + $spreadsheet->disconnectWorksheets(); } } diff --git a/tests/PhpSpreadsheetTests/Worksheet/WorksheetNamedRangesTest.php b/tests/PhpSpreadsheetTests/Worksheet/WorksheetNamedRangesTest.php index 506e753c4e..3b7f64c577 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/WorksheetNamedRangesTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/WorksheetNamedRangesTest.php @@ -9,154 +9,180 @@ class WorksheetNamedRangesTest extends TestCase { - /** - * @var Spreadsheet - */ - private $spreadsheet; - - protected function setUp(): void + protected function getSpreadsheet(): Spreadsheet { $reader = new Xlsx(); - $this->spreadsheet = $reader->load('tests/data/Worksheet/namedRangeTest.xlsx'); + + return $reader->load('tests/data/Worksheet/namedRangeTest.xlsx'); } public function testCellExists(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'GREETING'; - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $cellExists = $worksheet->cellExists($namedCell); self::assertTrue($cellExists); + $spreadsheet->disconnectWorksheets(); } public function testCellExistsUtf8(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Χαιρετισμός'; - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $cellExists = $worksheet->cellExists($namedCell); self::assertTrue($cellExists); + $spreadsheet->disconnectWorksheets(); } public function testCellNotExists(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'GOODBYE'; - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $cellExists = $worksheet->cellExists($namedCell); self::assertFalse($cellExists); + $spreadsheet->disconnectWorksheets(); } public function testCellExistsInvalidScope(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Result'; - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $cellExists = $worksheet->cellExists($namedCell); self::assertFalse($cellExists); + $spreadsheet->disconnectWorksheets(); } public function testCellExistsRange(): void { + $spreadsheet = $this->getSpreadsheet(); $namedRange = 'Range1'; $this->expectException(Exception::class); $this->expectExceptionMessage('Cell coordinate string can not be a range of cells'); - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $worksheet->cellExists($namedRange); + $spreadsheet->disconnectWorksheets(); } public function testGetCell(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'GREETING'; - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $cell = $worksheet->getCell($namedCell); self::assertSame('Hello', $cell->getValue()); + $spreadsheet->disconnectWorksheets(); } public function testGetCellUtf8(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Χαιρετισμός'; - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $cell = $worksheet->getCell($namedCell); self::assertSame('नमस्ते', $cell->getValue()); + $spreadsheet->disconnectWorksheets(); } public function testGetCellNotExists(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'GOODBYE'; $this->expectException(Exception::class); $this->expectExceptionMessage("Invalid cell coordinate {$namedCell}"); - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $worksheet->getCell($namedCell); + $spreadsheet->disconnectWorksheets(); } public function testGetCellInvalidScope(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Result'; $ucNamedCell = strtoupper($namedCell); $this->expectException(Exception::class); $this->expectExceptionMessage("Invalid cell coordinate {$ucNamedCell}"); - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $worksheet->getCell($namedCell); + $spreadsheet->disconnectWorksheets(); } public function testGetCellLocalScoped(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Result'; - $this->spreadsheet->setActiveSheetIndexByName('Sheet2'); - $worksheet = $this->spreadsheet->getActiveSheet(); + $spreadsheet->setActiveSheetIndexByName('Sheet2'); + $worksheet = $spreadsheet->getActiveSheet(); $cell = $worksheet->getCell($namedCell); self::assertSame(8, $cell->getCalculatedValue()); + $spreadsheet->disconnectWorksheets(); } public function testGetCellNamedFormula(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Result'; - $this->spreadsheet->setActiveSheetIndexByName('Sheet2'); - $worksheet = $this->spreadsheet->getActiveSheet(); + $spreadsheet->setActiveSheetIndexByName('Sheet2'); + $worksheet = $spreadsheet->getActiveSheet(); $cell = $worksheet->getCell($namedCell); self::assertSame(8, $cell->getCalculatedValue()); + $spreadsheet->disconnectWorksheets(); } public function testGetCellWithNamedRange(): void { + $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Range1'; $this->expectException(Exception::class); $this->expectExceptionMessage('Cell coordinate string can not be a range of cells'); - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $worksheet->getCell($namedCell); + $spreadsheet->disconnectWorksheets(); } public function testNamedRangeToArray(): void { + $spreadsheet = $this->getSpreadsheet(); $namedRange = 'Range1'; - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $rangeData = $worksheet->namedRangeToArray($namedRange); + self::assertSame([['1', '2', '3']], $rangeData); + $rangeData = $worksheet->namedRangeToArray($namedRange, null, true, false); self::assertSame([[1, 2, 3]], $rangeData); + $spreadsheet->disconnectWorksheets(); } public function testInvalidNamedRangeToArray(): void { + $spreadsheet = $this->getSpreadsheet(); $namedRange = 'Range2'; $this->expectException(Exception::class); $this->expectExceptionMessage("Named Range {$namedRange} does not exist"); - $worksheet = $this->spreadsheet->getActiveSheet(); + $worksheet = $spreadsheet->getActiveSheet(); $rangeData = $worksheet->namedRangeToArray($namedRange); self::assertSame([[1, 2, 3]], $rangeData); + $spreadsheet->disconnectWorksheets(); } }