repo
stringlengths
7
63
file_url
stringlengths
81
284
file_path
stringlengths
5
200
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 15:02:33
2026-01-05 05:24:06
truncated
bool
2 classes
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/SheetsXlsxChartTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/SheetsXlsxChartTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Chart\DataSeries; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\IReader; use PHPUnit\Framework\TestCase; class SheetsXlsxChartTest extends TestCase { public function testLoadSheetsXlsxChart(): void { $filename = 'tests/data/Reader/XLSX/sheetsChartsTest.xlsx'; $reader = IOFactory::createReader('Xlsx'); $spreadsheet = $reader->load($filename, IReader::LOAD_WITH_CHARTS); $worksheet = $spreadsheet->getActiveSheet(); $charts = $worksheet->getChartCollection(); self::assertEquals(2, $worksheet->getChartCount()); self::assertCount(2, $charts); $chart1 = $charts[0]; self::assertNotNull($chart1); $pa1 = $chart1->getPlotArea(); self::assertNotNull($pa1); self::assertEquals(2, $pa1->getPlotSeriesCount()); $pg1 = $pa1->getPlotGroup()[0]; self::assertEquals(DataSeries::TYPE_LINECHART, $pg1->getPlotType()); self::assertCount(2, $pg1->getPlotLabels()); self::assertCount(2, $pg1->getPlotValues()); self::assertCount(2, $pg1->getPlotCategories()); $chart2 = $charts[1]; self::assertNotNull($chart2); $pa1 = $chart2->getPlotArea(); self::assertNotNull($pa1); self::assertEquals(2, $pa1->getPlotSeriesCount()); $pg1 = $pa1->getPlotGroupByIndex(0); //Before a refresh, data values are empty foreach ($pg1->getPlotValues() as $dv) { self::assertEmpty($dv->getPointCount()); } $pg1->refresh($worksheet); foreach ($pg1->getPlotValues() as $dv) { self::assertEquals(9, $dv->getPointCount()); } self::assertEquals(DataSeries::TYPE_SCATTERCHART, $pg1->getPlotType()); self::assertCount(2, $pg1->getPlotLabels()); self::assertCount(2, $pg1->getPlotValues()); self::assertCount(2, $pg1->getPlotCategories()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Style\Conditional; use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; class ConditionalTest extends AbstractFunctional { public function testLoadingConditionalDoesntLoseSelectedCell(): void { $filename = 'tests/data/Reader/XLSX/ConditionalFormat_Ranges.xlsx'; $reader = IOFactory::createReader('Xlsx'); $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); self::assertSame('A1', $worksheet->getActiveCell()); self::assertSame('A1', $worksheet->getSelectedCells()); } /** * Test check if conditional style with type 'notContainsText' works on xlsx. */ public function testConditionalNotContainsText(): void { $filename = 'tests/data/Reader/XLSX/conditionalFormatting3Test.xlsx'; $reader = IOFactory::createReader('Xlsx'); $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); $styles = $worksheet->getConditionalStyles('A1:A5'); self::assertCount(1, $styles); /** @var Conditional $notContainsTextStyle */ $notContainsTextStyle = $styles[0]; self::assertEquals('A', $notContainsTextStyle->getText()); self::assertEquals(Conditional::CONDITION_NOTCONTAINSTEXT, $notContainsTextStyle->getConditionType()); self::assertEquals(Conditional::OPERATOR_NOTCONTAINS, $notContainsTextStyle->getOperatorType()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/XlsxTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/XlsxTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\IReader; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Shared\File; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Style\Conditional; use PhpOffice\PhpSpreadsheet\Style\Style; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class XlsxTest extends TestCase { const XLSX_PRECISION = 1.0E-8; public function testLoadXlsxRowColumnAttributes(): void { $filename = 'tests/data/Reader/XLSX/rowColumnAttributeTest.xlsx'; $reader = new Xlsx(); $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); for ($row = 1; $row <= 4; ++$row) { self::assertEquals($row * 5 + 10, floor($worksheet->getRowDimension($row)->getRowHeight())); } self::assertFalse($worksheet->getRowDimension(5)->getVisible()); for ($column = 1; $column <= 4; ++$column) { $columnAddress = Coordinate::stringFromColumnIndex($column); self::assertEquals( $column * 2 + 2, floor($worksheet->getColumnDimension($columnAddress)->getWidth()) ); } self::assertFalse($worksheet->getColumnDimension('E')->getVisible()); $spreadsheet->disconnectWorksheets(); } public function testLoadXlsxWithStyles(): void { $expectedColours = [ 1 => ['A' => 'C00000', 'C' => 'FF0000', 'E' => 'FFC000'], 3 => ['A' => '7030A0', 'C' => 'FFFFFF', 'E' => 'FFFF00'], 5 => ['A' => '002060', 'C' => 'FFFFFF', 'E' => '92D050'], 7 => ['A' => '0070C0', 'C' => '00B0F0', 'E' => '00B050'], ]; $filename = 'tests/data/Reader/XLSX/stylesTest.xlsx'; $reader = new Xlsx(); $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); for ($row = 1; $row <= 8; $row += 2) { for ($column = 'A'; $column !== 'G'; StringHelper::stringIncrement($column), StringHelper::stringIncrement($column)) { self::assertEquals( $expectedColours[$row][$column], $worksheet->getStyle($column . $row)->getFill()->getStartColor()->getRGB() ); } } $spreadsheet->disconnectWorksheets(); } /** * Test load Xlsx file without styles.xml. */ public function testLoadXlsxWithoutStyles(): void { $filename = 'tests/data/Reader/XLSX/issue.2246a.xlsx'; $reader = new Xlsx(); $spreadsheet = $reader->load($filename); $tempFilename = File::temporaryFilename(); $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save($tempFilename); $reader = new Xlsx(); $reloadedSpreadsheet = $reader->load($tempFilename); unlink($tempFilename); $reloadedWorksheet = $reloadedSpreadsheet->getActiveSheet(); self::assertEquals('TipoDato', $reloadedWorksheet->getCell('A1')->getValue()); $spreadsheet->disconnectWorksheets(); } /** * Test load Xlsx file with empty styles.xml. */ public function testLoadXlsxWithEmptyStyles(): void { $filename = 'tests/data/Reader/XLSX/issue.2246b.xlsx'; $reader = new Xlsx(); $spreadsheet = $reader->load($filename); $tempFilename = File::temporaryFilename(); $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save($tempFilename); $reader = new Xlsx(); $reloadedSpreadsheet = $reader->load($tempFilename); unlink($tempFilename); $reloadedWorksheet = $reloadedSpreadsheet->getActiveSheet(); self::assertEquals('TipoDato', $reloadedWorksheet->getCell('A1')->getValue()); $spreadsheet->disconnectWorksheets(); } public function testLoadXlsxAutofilter(): void { $filename = 'tests/data/Reader/XLSX/autofilterTest.xlsx'; $reader = new Xlsx(); $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); $autofilter = $worksheet->getAutoFilter(); self::assertEquals('A1:D57', $autofilter->getRange()); self::assertEquals( AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER, $autofilter->getColumn('A')->getFilterType() ); $spreadsheet->disconnectWorksheets(); } public function testLoadXlsxPageSetup(): void { $filename = 'tests/data/Reader/XLSX/pageSetupTest.xlsx'; $reader = new Xlsx(); $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); $pageMargins = $worksheet->getPageMargins(); // Convert from inches to cm for testing self::assertEqualsWithDelta(2.5, $pageMargins->getTop() * 2.54, self::XLSX_PRECISION); self::assertEqualsWithDelta(3.3, $pageMargins->getLeft() * 2.54, self::XLSX_PRECISION); self::assertEqualsWithDelta(3.3, $pageMargins->getRight() * 2.54, self::XLSX_PRECISION); self::assertEqualsWithDelta(1.3, $pageMargins->getHeader() * 2.54, self::XLSX_PRECISION); self::assertEquals(PageSetup::PAPERSIZE_A4, $worksheet->getPageSetup()->getPaperSize()); self::assertEquals(['A10', 'A20', 'A30', 'A40', 'A50'], array_keys($worksheet->getBreaks())); } public function testLoadXlsxConditionalFormatting(): void { $filename = 'tests/data/Reader/XLSX/conditionalFormattingTest.xlsx'; $reader = new Xlsx(); $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); $conditionalStyle = $worksheet->getCell('B2')->getStyle()->getConditionalStyles(); self::assertNotEmpty($conditionalStyle); $conditionalRule = $conditionalStyle[0]; self::assertNotEmpty($conditionalRule->getConditions()); self::assertEquals(Conditional::CONDITION_CELLIS, $conditionalRule->getConditionType()); self::assertEquals(Conditional::OPERATOR_BETWEEN, $conditionalRule->getOperatorType()); self::assertEquals(['200', '400'], $conditionalRule->getConditions()); /** @var mixed[][] */ $temp = $conditionalRule->getStyle()->exportArray(); self::assertSame('#,##0.00_-"€"', $temp['numberFormat']['formatCode']); $spreadsheet->disconnectWorksheets(); } /** * Test load Xlsx file without cell reference. */ public function testLoadXlsxWithoutCellReference(): void { $filename = 'tests/data/Reader/XLSX/without_cell_reference.xlsx'; $reader = new Xlsx(); $spreadsheet = $reader->load($filename); self::assertSame(1, $spreadsheet->getActiveSheet()->getCell('A1')->getValue()); $spreadsheet->disconnectWorksheets(); } /** * Test load Xlsx file and use a read filter. */ public function testLoadWithReadFilter(): void { $filename = 'tests/data/Reader/XLSX/without_cell_reference.xlsx'; $reader = new Xlsx(); $reader->setReadFilter(new OddColumnReadFilter()); $spreadsheet = $reader->load($filename); $data = $spreadsheet->getActiveSheet()->toArray(formatData: false); $ref = [1, null, 3, null, 5, null, 7, null, 9, null]; for ($i = 0; $i < 10; ++$i) { self::assertSame($ref, \array_slice($data[$i], 0, 10, true)); } $spreadsheet->disconnectWorksheets(); } /** * Test load Xlsx file with drawing having double attributes. */ public function testLoadXlsxWithDoubleAttrDrawing(): void { $filename = 'tests/data/Reader/XLSX/double_attr_drawing.xlsx'; $reader = new Xlsx(); $spreadsheet = $reader->load($filename); self::assertSame('TOSHIBA_HITACHI_SKYWORTH', $spreadsheet->getActiveSheet()->getTitle()); $spreadsheet->disconnectWorksheets(); } /** * Test correct save and load xlsx files with empty drawings. * Such files can be generated by Google Sheets. */ public function testLoadSaveWithEmptyDrawings(): void { $filename = 'tests/data/Reader/XLSX/empty_drawing.xlsx'; $reader = new Xlsx(); $excel = $reader->load($filename); $resultFilename = File::temporaryFilename(); $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($excel); $writer->save($resultFilename); $excel = $reader->load($resultFilename); unlink($resultFilename); self::assertSame(1.0, $excel->getActiveSheet()->getCell('A1')->getValue()); $excel->disconnectWorksheets(); } /** * Test if all whitespace is removed from a style definition string. * This is needed to parse it into properties with the correct keys. */ #[DataProvider('providerStripsWhiteSpaceFromStyleString')] public function testStripsWhiteSpaceFromStyleString(string $string): void { $string = Xlsx::stripWhiteSpaceFromStyleString($string); self::assertEquals(preg_match('/\s/', $string), 0); } public static function providerStripsWhiteSpaceFromStyleString(): array { return [ ['position:absolute;margin-left:424.5pt;margin-top:169.5pt;width:67.5pt; height:13.5pt;z-index:5;mso-wrap-style:tight'], ['position:absolute;margin-left:424.5pt;margin-top:169.5pt;width:67.5pt; height:13.5pt;z-index:5;mso-wrap-style:tight'], ['position:absolute; margin-left:424.5pt; margin-top:169.5pt; width:67.5pt; height:13.5pt;z-index:5;mso-wrap-style:tight'], ]; } public function testLoadDataOnlyLoadsAlsoTables(): void { $filename = 'tests/data/Reader/XLSX/data_with_tables.xlsx'; $reader = new Xlsx(); $excel = $reader->load($filename, IReader::READ_DATA_ONLY); self::assertEquals(['First', 'Second'], $excel->getSheetNames()); $table = $excel->getTableByName('Tableau1'); $firstSheet = $excel->getSheetByName('First'); $secondSheet = $excel->getSheetByName('Second'); if (!$table || !$firstSheet || !$secondSheet) { self::fail('Table or Sheet not found.'); } self::assertEquals('A1:B5', $table->getRange()); self::assertEquals([['1', '2', '3']], $firstSheet->toArray()); self::assertEquals([ ['Colonne1', 'Colonne2'], ['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ], $secondSheet->toArray()); $excel->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3277Test.php
tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3277Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\IOFactory; use PHPUnit\Framework\TestCase; class Issue3277Test extends TestCase { public function testIssue3227(): void { $filename = 'tests/data/Reader/XLSX/issue.3277.xlsx'; $reader = IOFactory::createReader('Xlsx'); $spreadsheet = $reader->load($filename); $array = $spreadsheet->getActiveSheet()->toArray(); self::assertSame('data', $array[0][0]); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2885Test.php
tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2885Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PHPUnit\Framework\TestCase; class Issue2885Test extends TestCase { private static string $testbook = 'tests/data/Reader/XLSX/issue.2885.xlsx'; public function testIssue2885(): void { $filename = self::$testbook; $reader = new Xlsx(); $spreadsheet = $reader->load($filename); $sheet = $spreadsheet->getActiveSheet(); self::assertSame('[$-809]0%', $sheet->getStyle('A1')->getNumberFormat()->getFormatCode()); $finishColumns = $sheet->getHighestColumn(); $rowsCount = $sheet->getHighestRow(); $rows = $sheet->rangeToArray("A1:{$finishColumns}{$rowsCount}"); self::assertSame('8%', $rows[0][0]); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/SharedFormulaTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/SharedFormulaTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\IReader; use PHPUnit\Framework\TestCase; class SharedFormulaTest extends TestCase { private static string $testbook = 'samples/templates/32readwriteAreaChart1.xlsx'; public function testPreliminaries(): void { $file = 'zip://'; $file .= self::$testbook; $file .= '#xl/worksheets/sheet1.xml'; $data = file_get_contents($file); // confirm that file contains shared formula if ($data === false) { self::fail('Unable to read file'); } else { self::assertStringContainsString('<c r="D6"><f t="shared" ca="1" si="0"/>', $data); self::assertStringContainsString('<c r="E6"><f t="shared" ca="1" si="0"/>', $data); } } public function testLoadSheetsXlsxChart(): void { $filename = self::$testbook; $reader = IOFactory::createReader('Xlsx'); $spreadsheet = $reader->load($filename, IReader::LOAD_WITH_CHARTS); $sheet = $spreadsheet->getActiveSheet(); self::assertSame('=(RANDBETWEEN(-50,250)+100)*10', $sheet->getCell('D6')->getValue()); self::assertSame('=(RANDBETWEEN(-50,250)+100)*10', $sheet->getCell('E6')->getValue()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3495Test.php
tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3495Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; class Issue3495Test extends \PHPUnit\Framework\TestCase { private static string $testbook = 'tests/data/Reader/XLSX/issue.3495d.xlsx'; public function testPreliminaries(): void { $file = 'zip://'; $file .= self::$testbook; $file2 = $file; $file .= '#xl/styles.xml'; $file2 .= '#xl/worksheets/sheet1.xml'; $data = file_get_contents($file); if ($data === false) { self::fail('Unable to read file'); } else { // default style plus one other style self::assertStringContainsString( '<cellXfs count="2">' . '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>' . '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" quotePrefix="1"/>', $data ); } $data = file_get_contents($file2); if ($data === false) { self::fail('Unable to read file'); } else { // cells B1, C1, D1 all nominally use quotePrefix s="1" self::assertStringContainsString('<c r="B1" s="1">', $data); self::assertStringContainsString('<c r="C1" s="1" t="s">', $data); self::assertStringContainsString('<c r="D1" s="1" t="s">', $data); } } public function testFormulaDespiteQuotePrefix(): void { $reader = new Xlsx(); $spreadsheet = $reader->load(self::$testbook); $sheet = $spreadsheet->getActiveSheet(); self::assertSame('=2+3', $sheet->getCell('B1')->getValue()); self::assertSame('=1+2', $sheet->getCell('C1')->getValue()); self::assertSame('3', $sheet->getCell('D1')->getValue()); self::assertSame(5, $sheet->getCell('B1')->getCalculatedValue()); self::assertSame('=1+2', $sheet->getCell('C1')->getCalculatedValue()); self::assertSame('3', $sheet->getCell('D1')->getCalculatedValue()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/InvalidFileTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/InvalidFileTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PHPUnit\Framework\TestCase; class InvalidFileTest extends TestCase { public function testInvalidFileLoad(): void { $this->expectException(ReaderException::class); $this->expectExceptionMessage('Could not find zip member'); $temp = __FILE__; $reader = new Xlsx(); $reader->load($temp); } public function testInvalidFileNames(): void { $this->expectException(ReaderException::class); $this->expectExceptionMessage('Could not find zip member'); $temp = __FILE__; $reader = new Xlsx(); $reader->listWorksheetNames($temp); } public function testInvalidInfo(): void { $this->expectException(ReaderException::class); $this->expectExceptionMessage('Could not find zip member'); $temp = __FILE__; $reader = new Xlsx(); $reader->listWorksheetInfo($temp); } public function testOdsFileLoad(): void { $this->expectException(ReaderException::class); $this->expectExceptionMessage('Could not find zip member'); $temp = 'samples/templates/OOCalcTest.ods'; $reader = new Xlsx(); $reader->load($temp); } public function testOdsFileNames(): void { $this->expectException(ReaderException::class); $this->expectExceptionMessage('Could not find zip member'); $temp = 'samples/templates/OOCalcTest.ods'; $reader = new Xlsx(); $reader->listWorksheetNames($temp); } public function testOdsInfo(): void { $this->expectException(ReaderException::class); $this->expectExceptionMessage('Could not find zip member'); $temp = 'samples/templates/OOCalcTest.ods'; $reader = new Xlsx(); $reader->listWorksheetInfo($temp); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/NamespaceStdTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/NamespaceStdTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\Font; class NamespaceStdTest extends \PHPUnit\Framework\TestCase { private static string $testbook = 'tests/data/Reader/XLSX/namespacestd.xlsx'; public function testPreliminaries(): void { $file = 'zip://'; $file .= self::$testbook; $file .= '#xl/workbook.xml'; $data = file_get_contents($file); // confirm that file contains expected namespaced xml tag if ($data === false) { self::fail('Unable to read file'); } else { if (!str_contains(__FILE__, 'NonStd')) { self::assertStringNotContainsString('nonstd', self::$testbook); self::assertStringContainsString('<workbook ', $data); } else { self::assertStringContainsString('nonstd', self::$testbook); self::assertStringContainsString('<x:workbook ', $data); } } } public function testInfo(): void { $reader = new Xlsx(); $workSheetInfo = $reader->listWorkSheetInfo(self::$testbook); $info0 = $workSheetInfo[0]; self::assertEquals('SylkTest', $info0['worksheetName']); self::assertEquals('J', $info0['lastColumnLetter']); self::assertEquals(9, $info0['lastColumnIndex']); self::assertEquals(18, $info0['totalRows']); self::assertEquals(10, $info0['totalColumns']); } public function testSheetNames(): void { $reader = new Xlsx(); $worksheetNames = $reader->listWorksheetNames(self::$testbook); self::assertEquals(['SylkTest', 'Second'], $worksheetNames); } public function testActive(): void { $reader = new Xlsx(); $spreadsheet = $reader->load(self::$testbook); $sheet = $spreadsheet->getActiveSheet(); self::assertSame('Second', $sheet->getTitle()); self::assertSame('A2', $sheet->getFreezePane()); self::assertSame('A2', $sheet->getTopLeftCell()); self::assertSame('B3', $sheet->getSelectedCells()); $sheet = $spreadsheet->getSheetByNameOrThrow('SylkTest'); self::assertNull($sheet->getFreezePane()); self::assertNull($sheet->getTopLeftCell()); } public function testLoadXlsx(): void { $reader = new Xlsx(); $spreadsheet = $reader->load(self::$testbook); $sheet = $spreadsheet->getSheet(0); self::assertEquals('SylkTest', $sheet->getTitle()); if (str_contains(__FILE__, 'NonStd')) { self::markTestIncomplete('Not yet ready'); } self::assertEquals('FFFF0000', $sheet->getCell('A1')->getStyle()->getFont()->getColor()->getARGB()); self::assertEquals(Fill::FILL_PATTERN_GRAY125, $sheet->getCell('A2')->getStyle()->getFill()->getFillType()); self::assertEquals(Font::UNDERLINE_SINGLE, $sheet->getCell('A4')->getStyle()->getFont()->getUnderline()); self::assertEquals('Test with (;) in string', $sheet->getCell('A4')->getValue()); self::assertEquals(22269, $sheet->getCell('A10')->getValue()); self::assertEquals('dd/mm/yyyy', $sheet->getCell('A10')->getStyle()->getNumberFormat()->getFormatCode()); self::assertEquals('19/12/1960', $sheet->getCell('A10')->getFormattedValue()); self::assertEquals(1.5, $sheet->getCell('A11')->getValue()); self::assertEquals('# ?/?', $sheet->getCell('A11')->getStyle()->getNumberFormat()->getFormatCode()); self::assertEquals('1 1/2', $sheet->getCell('A11')->getFormattedValue()); self::assertEquals('=B1+C1', $sheet->getCell('H1')->getValue()); self::assertEquals('=E2&F2', $sheet->getCell('J2')->getValue()); self::assertEquals('=SUM(C1:C4)', $sheet->getCell('I5')->getValue()); self::assertEquals('=MEDIAN(B6:B8)', $sheet->getCell('B9')->getValue()); self::assertEquals(11, $sheet->getCell('E1')->getStyle()->getFont()->getSize()); self::assertTrue($sheet->getCell('E1')->getStyle()->getFont()->getBold()); self::assertTrue($sheet->getCell('E1')->getStyle()->getFont()->getItalic()); self::assertEquals(Font::UNDERLINE_SINGLE, $sheet->getCell('E1')->getStyle()->getFont()->getUnderline()); self::assertFalse($sheet->getCell('E2')->getStyle()->getFont()->getBold()); self::assertFalse($sheet->getCell('E2')->getStyle()->getFont()->getItalic()); self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('E2')->getStyle()->getFont()->getUnderline()); self::assertTrue($sheet->getCell('E3')->getStyle()->getFont()->getBold()); self::assertFalse($sheet->getCell('E3')->getStyle()->getFont()->getItalic()); self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('E3')->getStyle()->getFont()->getUnderline()); self::assertFalse($sheet->getCell('E4')->getStyle()->getFont()->getBold()); self::assertTrue($sheet->getCell('E4')->getStyle()->getFont()->getItalic()); self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('E4')->getStyle()->getFont()->getUnderline()); self::assertTrue($sheet->getCell('F1')->getStyle()->getFont()->getBold()); self::assertFalse($sheet->getCell('F1')->getStyle()->getFont()->getItalic()); self::assertEquals(Font::UNDERLINE_SINGLE, $sheet->getCell('F1')->getStyle()->getFont()->getUnderline()); self::assertFalse($sheet->getCell('F2')->getStyle()->getFont()->getBold()); self::assertFalse($sheet->getCell('F2')->getStyle()->getFont()->getItalic()); self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('F2')->getStyle()->getFont()->getUnderline()); self::assertTrue($sheet->getCell('F3')->getStyle()->getFont()->getBold()); self::assertTrue($sheet->getCell('F3')->getStyle()->getFont()->getItalic()); self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('F3')->getStyle()->getFont()->getUnderline()); self::assertFalse($sheet->getCell('F4')->getStyle()->getFont()->getBold()); self::assertFalse($sheet->getCell('F4')->getStyle()->getFont()->getItalic()); self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('F4')->getStyle()->getFont()->getUnderline()); self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C10')->getStyle()->getBorders()->getTop()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C10')->getStyle()->getBorders()->getRight()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C10')->getStyle()->getBorders()->getBottom()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C10')->getStyle()->getBorders()->getLeft()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C12')->getStyle()->getBorders()->getTop()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C12')->getStyle()->getBorders()->getRight()->getBorderStyle()); self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C12')->getStyle()->getBorders()->getBottom()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C12')->getStyle()->getBorders()->getLeft()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C14')->getStyle()->getBorders()->getTop()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C14')->getStyle()->getBorders()->getRight()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C14')->getStyle()->getBorders()->getBottom()->getBorderStyle()); self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C14')->getStyle()->getBorders()->getLeft()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C16')->getStyle()->getBorders()->getTop()->getBorderStyle()); self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C16')->getStyle()->getBorders()->getRight()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C16')->getStyle()->getBorders()->getBottom()->getBorderStyle()); self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C16')->getStyle()->getBorders()->getLeft()->getBorderStyle()); self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getTop()->getBorderStyle()); self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getRight()->getBorderStyle()); self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getBottom()->getBorderStyle()); self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getLeft()->getBorderStyle()); } public function testLoadXlsxSheet2Contents(): void { $reader = new Xlsx(); $spreadsheet = $reader->load(self::$testbook); $sheet = $spreadsheet->getSheet(1); self::assertEquals('Second', $sheet->getTitle()); self::assertSame('Hyperlink', $sheet->getCell('B2')->getValue()); $hyper = $sheet->getCell('B2')->getHyperlink(); self::assertSame('http://www.example.com/', $hyper->getUrl()); self::assertSame('Comment', $sheet->getCell('B3')->getValue()); $comment = $sheet->getComment('B3'); // Created as "threaded comment" with Excel 365, not quite as expected. self::assertStringContainsString('This is a comment', (string) $comment); } public function testLoadXlsxSheet2Styles(): void { $reader = new Xlsx(); $spreadsheet = $reader->load(self::$testbook); $sheet = $spreadsheet->getSheet(1); self::assertEquals('Second', $sheet->getTitle()); if (str_contains(__FILE__, 'NonStd')) { self::markTestIncomplete('Not yet ready'); } self::assertEquals('center', $sheet->getCell('A2')->getStyle()->getAlignment()->getHorizontal()); self::assertSame('inherit', $sheet->getCell('A2')->getStyle()->getProtection()->getLocked()); self::assertEquals('top', $sheet->getCell('A3')->getStyle()->getAlignment()->getVertical()); self::assertSame('unprotected', $sheet->getCell('A3')->getStyle()->getProtection()->getLocked()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalIconSetTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalIconSetTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\IconSetValues; use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; class ConditionalIconSetTest extends AbstractFunctional { public function testIconSet(): void { $filename = 'tests/data/Reader/XLSX/conditionalFormattingIconSet.xlsx'; $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); $spreadsheet = $reader->load($filename); $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx'); $spreadsheet->disconnectWorksheets(); $worksheet = $reloadedSpreadsheet->getActiveSheet(); $columnIndex = 'A'; foreach (IconSetValues::cases() as $iconSetValue) { // styles $styles = $worksheet->getConditionalStyles("{$columnIndex}2:{$columnIndex}11"); self::assertCount(1, $styles); // icon set $iconSet = $styles[0]->getIconSet(); self::assertNotNull($iconSet); self::assertSame($iconSetValue, $iconSet->getIconSetType() ?? IconSetValues::ThreeTrafficLights1); StringHelper::stringIncrement($columnIndex); } // icon set attributes $columnIndex = 'A'; foreach ( [ ['reverse' => false, 'showValue' => false], ['reverse' => true, 'showValue' => false], ['reverse' => false, 'showValue' => true], ] as $expected ) { $styles = $worksheet->getConditionalStyles("{$columnIndex}2:{$columnIndex}11"); $iconSet = $styles[0]->getIconSet(); self::assertNotNull($iconSet); self::assertSame($expected['reverse'], $iconSet->getReverse() ?? false); self::assertSame($expected['showValue'], $iconSet->getShowValue() ?? true); self::assertFalse($iconSet->getCustom() ?? false); StringHelper::stringIncrement($columnIndex); } // cfvos $columnIndex = 'A'; foreach ( [ [['percent', '0', true], ['percent', '33', false], ['percent', '67', true]], [['percent', '0', true], ['num', '3', false], ['num', '7', true]], [['percent', '0', true], ['formula', '10/3', false], ['formula', '10/2', true]], [['percent', '0', true], ['percentile', '33', false], ['percentile', '67', true]], ] as $expected ) { $styles = $worksheet->getConditionalStyles("{$columnIndex}2:{$columnIndex}11"); $iconSet = $styles[0]->getIconSet(); self::assertNotNull($iconSet); $cfvos = $iconSet->getCfvos(); self::assertCount(count($expected), $cfvos); foreach ($expected as $i => [$type, $value, $gte]) { $cfvo = $cfvos[$i]; self::assertSame($type, $cfvo->getType()); self::assertSame($value, $cfvo->getValue()); self::assertSame($gte, $cfvo->getGreaterThanOrEqual() ?? true); self::assertNull($cfvo->getCellFormula()); } StringHelper::stringIncrement($columnIndex); } // unsupported icon sets for ($columnIndex = 'R'; $columnIndex <= 'U'; StringHelper::stringIncrement($columnIndex)) { $styles = $worksheet->getConditionalStyles("{$columnIndex}2:{$columnIndex}11"); $iconSet = $styles[0]->getIconSet(); self::assertNull($iconSet); } } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2450Test.php
tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2450Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\IOFactory; use PHPUnit\Framework\TestCase; class Issue2450Test extends TestCase { public function testIssue2450(): void { // Style specified as GENERAL rather than General. $filename = 'tests/data/Reader/XLSX/issue.2450.xlsx'; $reader = IOFactory::createReader('Xlsx'); $spreadsheet = $reader->load($filename); $sheet = $spreadsheet->getActiveSheet(); $birthYears = [ 'C2' => $sheet->getCell('C2')->getFormattedValue(), 'C3' => $sheet->getCell('C3')->getFormattedValue(), 'C4' => $sheet->getCell('C4')->getFormattedValue(), ]; self::assertSame( [ 'C2' => '1932', 'C3' => '1964', 'C4' => '1988', ], $birthYears ); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3613Test.php
tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3613Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; class Issue3613Test extends AbstractFunctional { private static string $testbook = 'tests/data/Reader/XLSX/issue.3613.xlsx'; // Partial fix only. We will no longer throw exception on save. // But calculation for cell value is 0, which is incorrect. public function testIssue3613(): void { $reader = new Xlsx(); $spreadsheet = $reader->load(self::$testbook); $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx'); $spreadsheet->disconnectWorksheets(); $sheet = $reloadedSpreadsheet->getActiveSheet(); self::assertSame('=ROUND(MAX((O4-P4)*{0.03;0.1;0.2;0.25;0.3;0.35;0.45}-{0;2520;16920;31920;52920;85920;181920},0)-Q4,2)', $sheet->getCell('N4')->getValue()); $reloadedSpreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3863Test.php
tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3863Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Cell\DataValidator; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; class Issue3863Test extends \PHPUnit\Framework\TestCase { private static string $testbook = 'tests/data/Reader/XLSX/issue.3863.xlsx'; public function testPreliminaries(): void { $file = 'zip://'; $file .= self::$testbook; $file .= '#xl/worksheets/sheet1.xml'; $data = file_get_contents($file); if ($data === false) { self::fail('Unable to read file'); } else { // Only 1 Data Validation and it does not specify operator self::assertStringContainsString('<dataValidations count="1"><dataValidation type="whole" allowBlank="1" showInputMessage="1" showErrorMessage="1" sqref="A1" xr:uid="{D0F98CC5-7234-4ADF-BD42-F33321DCD3CA}"><formula1>5</formula1><formula2>10</formula2></dataValidation></dataValidations>', $data); } } public function testValidData(): void { $reader = new Xlsx(); $spreadsheet = $reader->load(self::$testbook); $sheet = $spreadsheet->getActiveSheet(); self::assertSame('between', $sheet->getCell('A1')->getDataValidation()->getOperator()); $validator = new DataValidator(); self::assertTrue($validator->isValid($sheet->getCell('A1'))); $sheet->getCell('A1')->setValue(3); self::assertFalse($validator->isValid($sheet->getCell('A1'))); $sheet->getCell('A1')->setValue(7); self::assertTrue($validator->isValid($sheet->getCell('A1'))); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/MyXlsxTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/MyXlsxTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PHPUnit\Framework\TestCase; class MyXlsxTest extends TestCase { public function testCustomSpreadsheetCustomLoader(): void { $reader = new MyXlsxReader(); $infile = 'tests/data/Reader/XLSX/colorscale.xlsx'; /** @var MySpreadsheet */ $mySpreadsheet = $reader->load($infile); self::assertSame(64, $mySpreadsheet->calcSquare('A3')); $mySpreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3807Test.php
tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3807Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; class Issue3807Test extends \PHPUnit\Framework\TestCase { private static string $testbook = 'tests/data/Reader/XLSX/issue.3807.xlsx'; public function testPreliminaries(): void { $file = 'zip://'; $file .= self::$testbook; $file .= '#xl/_rels/workbook.xml.rels'; $data = file_get_contents($file); // rels file points to non-existent theme file if ($data === false) { self::fail('Unable to read file'); } else { self::assertStringContainsString('Target="theme/theme1.xml/../../../../../../../../1.txt"', $data); } } public function testLoadable(): void { $reader = new Xlsx(); $spreadsheet = $reader->load(self::$testbook); $sheet = $spreadsheet->getActiveSheet(); // Assert anything to confirm read succeeded self::assertSame(1, $sheet->getCell('B1')->getValue()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/AutoFilterTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/AutoFilterTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Xlsx\AutoFilter; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter as WorksheetAutoFilter; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; use SimpleXMLElement; class AutoFilterTest extends TestCase { private function getXMLInstance(string $ref): SimpleXMLElement { return new SimpleXMLElement( '<?xml version="1.0" encoding="UTF-8"?>' . '<root>' . '<autoFilter ref="' . $ref . '"></autoFilter>' . '</root>' ); } public static function loadDataProvider(): array { return [ ['$B3$E8', 0, 'B3E8'], ['$B3:$E8', 1, 'B3:E8'], ]; } #[\PHPUnit\Framework\Attributes\DataProvider('loadDataProvider')] public function testLoad(string $ref, int $expectedReadAutoFilterCalled, string $expectedRef): void { $worksheetAutoFilter = $this->getMockBuilder(WorksheetAutoFilter::class) ->disableOriginalConstructor() ->getMock(); $worksheetAutoFilter->expects(self::exactly($expectedReadAutoFilterCalled ? 1 : 0)) ->method('setRange') ->with($expectedRef); $worksheet = $this->getMockBuilder(Worksheet::class) ->disableOriginalConstructor() ->getMock(); $worksheet->expects(self::exactly($expectedReadAutoFilterCalled ? 1 : 0)) ->method('getAutoFilter') ->willReturn($worksheetAutoFilter); $autoFilter = new AutoFilter($worksheet, $this->getXMLInstance($ref)); $autoFilter->load(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/VerticalAlignTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/VerticalAlignTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\IOFactory; use PHPUnit\Framework\TestCase; class VerticalAlignTest extends TestCase { public function testVerticalAlignStyle(): void { $filename = 'tests/data/Reader/XLSX/verticalAlignTest.xlsx'; $reader = IOFactory::createReader('Xlsx'); $sheet = $reader->load($filename)->getActiveSheet(); self::assertTrue($sheet->getCell('A1')->getStyle()->getFont()->getSuperscript()); self::assertFalse($sheet->getCell('A1')->getStyle()->getFont()->getSubscript()); self::assertTrue($sheet->getCell('B1')->getStyle()->getFont()->getSubscript()); self::assertFalse($sheet->getCell('B1')->getStyle()->getFont()->getSuperscript()); self::assertFalse($sheet->getCell('C1')->getStyle()->getFont()->getSubscript()); self::assertFalse($sheet->getCell('C1')->getStyle()->getFont()->getSuperscript()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2501Test.php
tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2501Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\IOFactory; use PHPUnit\Framework\TestCase; class Issue2501Test extends TestCase { private static string $testbook = 'tests/data/Reader/XLSX/issue.2501.b.xlsx'; public function testPreliminaries(): void { $file = 'zip://'; $file .= self::$testbook; $file .= '#xl/worksheets/sheet1.xml'; $data = file_get_contents($file); // confirm that file contains expected merge ranges if ($data === false) { self::fail('Unable to read file'); } else { self::assertStringContainsString('<mergeCells count="3"><mergeCell ref="A:A"/><mergeCell ref="B:D"/><mergeCell ref="E2:E4"/></mergeCells>', $data); } $file = 'zip://'; $file .= self::$testbook; $file .= '#xl/worksheets/sheet2.xml'; $data = file_get_contents($file); // confirm that file contains expected merged ranges if ($data === false) { self::fail('Unable to read file'); } else { self::assertStringContainsString('<mergeCells count="3"><mergeCell ref="1:1"/><mergeCell ref="2:4"/><mergeCell ref="B5:D5"/></mergeCells>', $data); } } public function testIssue2501(): void { // Merged cell range specified as 1:1" $filename = self::$testbook; $reader = IOFactory::createReader('Xlsx'); $spreadsheet = $reader->load($filename); $sheet = $spreadsheet->getSheetByNameOrThrow('Columns'); $expected = [ 'A1:A1048576', 'B1:D1048576', 'E2:E4', ]; self::assertSame($expected, array_values($sheet->getMergeCells())); $sheet = $spreadsheet->getSheetByNameOrThrow('Rows'); $expected = [ 'A1:XFD1', 'A2:XFD4', 'B5:D5', ]; self::assertSame($expected, array_values($sheet->getMergeCells())); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalBorderTest.php
tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalBorderTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader; use PhpOffice\PhpSpreadsheet\Shared\File; use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter; use PHPUnit\Framework\TestCase; class ConditionalBorderTest extends TestCase { public function testReadAndWriteConditionalBorder(): void { $testfile = 'tests/data/Reader/XLSX/issue.3370.xlsx'; $file = 'zip://'; $file .= $testfile; $file .= '#xl/styles.xml'; $data = file_get_contents($file); $expected = '<dxfs count="1"><dxf><border><left/><right/><vertical/><horizontal/></border></dxf></dxfs>'; self::assertStringContainsString($expected, $data); $reader = new XlsxReader(); $spreadsheet = $reader->load($testfile); $outfile = File::temporaryFilename(); $writer = new XlsxWriter($spreadsheet); $writer->save($outfile); $spreadsheet->disconnectWorksheets(); $file = 'zip://'; $file .= $outfile; $file .= '#xl/styles.xml'; $data = file_get_contents($file); unlink($outfile); $expected = '<dxfs count="1"><dxf><border><left/><right/></border></dxf></dxfs>'; self::assertStringContainsString($expected, $data); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Utility/File.php
tests/PhpSpreadsheetTests/Reader/Utility/File.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Utility; class File { // https://stackoverflow.com/questions/16511021/convert-mime-type-to-file-extension-php public static function mime2ext(string $mime): string { $mime_map = [ 'video/3gpp2' => '3g2', 'video/3gp' => '3gp', 'video/3gpp' => '3gp', 'application/x-compressed' => '7zip', 'audio/x-acc' => 'aac', 'audio/ac3' => 'ac3', 'application/postscript' => 'ai', 'audio/x-aiff' => 'aif', 'audio/aiff' => 'aif', 'audio/x-au' => 'au', 'video/x-msvideo' => 'avi', 'video/msvideo' => 'avi', 'video/avi' => 'avi', 'application/x-troff-msvideo' => 'avi', 'application/macbinary' => 'bin', 'application/mac-binary' => 'bin', 'application/x-binary' => 'bin', 'application/x-macbinary' => 'bin', 'image/bmp' => 'bmp', 'image/x-bmp' => 'bmp', 'image/x-bitmap' => 'bmp', 'image/x-xbitmap' => 'bmp', 'image/x-win-bitmap' => 'bmp', 'image/x-windows-bmp' => 'bmp', 'image/ms-bmp' => 'bmp', 'image/x-ms-bmp' => 'bmp', 'application/bmp' => 'bmp', 'application/x-bmp' => 'bmp', 'application/x-win-bitmap' => 'bmp', 'application/cdr' => 'cdr', 'application/coreldraw' => 'cdr', 'application/x-cdr' => 'cdr', 'application/x-coreldraw' => 'cdr', 'image/cdr' => 'cdr', 'image/x-cdr' => 'cdr', 'zz-application/zz-winassoc-cdr' => 'cdr', 'application/mac-compactpro' => 'cpt', 'application/pkix-crl' => 'crl', 'application/pkcs-crl' => 'crl', 'application/x-x509-ca-cert' => 'crt', 'application/pkix-cert' => 'crt', 'text/css' => 'css', 'text/x-comma-separated-values' => 'csv', 'text/comma-separated-values' => 'csv', 'application/vnd.msexcel' => 'csv', 'application/x-director' => 'dcr', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx', 'application/x-dvi' => 'dvi', 'message/rfc822' => 'eml', 'application/x-msdownload' => 'exe', 'video/x-f4v' => 'f4v', 'audio/x-flac' => 'flac', 'video/x-flv' => 'flv', 'image/gif' => 'gif', 'application/gpg-keys' => 'gpg', 'application/x-gtar' => 'gtar', 'application/x-gzip' => 'gzip', 'application/mac-binhex40' => 'hqx', 'application/mac-binhex' => 'hqx', 'application/x-binhex40' => 'hqx', 'application/x-mac-binhex40' => 'hqx', 'text/html' => 'html', 'image/x-icon' => 'ico', 'image/x-ico' => 'ico', 'image/vnd.microsoft.icon' => 'ico', 'text/calendar' => 'ics', 'application/java-archive' => 'jar', 'application/x-java-application' => 'jar', 'application/x-jar' => 'jar', 'image/jp2' => 'jp2', 'video/mj2' => 'jp2', 'image/jpx' => 'jp2', 'image/jpm' => 'jp2', 'image/jpeg' => 'jpeg', 'image/pjpeg' => 'jpeg', 'application/x-javascript' => 'js', 'application/json' => 'json', 'text/json' => 'json', 'application/vnd.google-earth.kml+xml' => 'kml', 'application/vnd.google-earth.kmz' => 'kmz', 'text/x-log' => 'log', 'audio/x-m4a' => 'm4a', 'audio/mp4' => 'm4a', 'application/vnd.mpegurl' => 'm4u', 'audio/midi' => 'mid', 'application/vnd.mif' => 'mif', 'video/quicktime' => 'mov', 'video/x-sgi-movie' => 'movie', 'audio/mpeg' => 'mp3', 'audio/mpg' => 'mp3', 'audio/mpeg3' => 'mp3', 'audio/mp3' => 'mp3', 'video/mp4' => 'mp4', 'video/mpeg' => 'mpeg', 'application/oda' => 'oda', 'audio/ogg' => 'ogg', 'video/ogg' => 'ogg', 'application/ogg' => 'ogg', 'font/otf' => 'otf', 'application/x-pkcs10' => 'p10', 'application/pkcs10' => 'p10', 'application/x-pkcs12' => 'p12', 'application/x-pkcs7-signature' => 'p7a', 'application/pkcs7-mime' => 'p7c', 'application/x-pkcs7-mime' => 'p7c', 'application/x-pkcs7-certreqresp' => 'p7r', 'application/pkcs7-signature' => 'p7s', 'application/pdf' => 'pdf', 'application/octet-stream' => 'pdf', 'application/x-x509-user-cert' => 'pem', 'application/x-pem-file' => 'pem', 'application/pgp' => 'pgp', 'application/x-httpd-php' => 'php', 'application/php' => 'php', 'application/x-php' => 'php', 'text/php' => 'php', 'text/x-php' => 'php', 'application/x-httpd-php-source' => 'php', 'image/png' => 'png', 'image/x-png' => 'png', 'application/powerpoint' => 'ppt', 'application/vnd.ms-powerpoint' => 'ppt', 'application/vnd.ms-office' => 'ppt', 'application/msword' => 'doc', 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx', 'application/x-photoshop' => 'psd', 'image/vnd.adobe.photoshop' => 'psd', 'audio/x-realaudio' => 'ra', 'audio/x-pn-realaudio' => 'ram', 'application/x-rar' => 'rar', 'application/rar' => 'rar', 'application/x-rar-compressed' => 'rar', 'audio/x-pn-realaudio-plugin' => 'rpm', 'application/x-pkcs7' => 'rsa', 'text/rtf' => 'rtf', 'text/richtext' => 'rtx', 'video/vnd.rn-realvideo' => 'rv', 'application/x-stuffit' => 'sit', 'application/smil' => 'smil', 'text/srt' => 'srt', 'image/svg+xml' => 'svg', 'application/x-shockwave-flash' => 'swf', 'application/x-tar' => 'tar', 'application/x-gzip-compressed' => 'tgz', 'image/tiff' => 'tiff', 'font/ttf' => 'ttf', 'text/plain' => 'txt', 'text/x-vcard' => 'vcf', 'application/videolan' => 'vlc', 'text/vtt' => 'vtt', 'audio/x-wav' => 'wav', 'audio/wave' => 'wav', 'audio/wav' => 'wav', 'application/wbxml' => 'wbxml', 'video/webm' => 'webm', 'image/webp' => 'webp', 'audio/x-ms-wma' => 'wma', 'application/wmlc' => 'wmlc', 'video/x-ms-wmv' => 'wmv', 'video/x-ms-asf' => 'wmv', 'font/woff' => 'woff', 'font/woff2' => 'woff2', 'application/xhtml+xml' => 'xhtml', 'application/excel' => 'xl', 'application/msexcel' => 'xls', 'application/x-msexcel' => 'xls', 'application/x-ms-excel' => 'xls', 'application/x-excel' => 'xls', 'application/x-dos_ms_excel' => 'xls', 'application/xls' => 'xls', 'application/x-xls' => 'xls', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx', 'application/vnd.ms-excel' => 'xlsx', 'application/xml' => 'xml', 'text/xml' => 'xml', 'text/xsl' => 'xsl', 'application/xspf+xml' => 'xspf', 'application/x-compress' => 'z', 'application/x-zip' => 'zip', 'application/zip' => 'zip', 'application/x-zip-compressed' => 'zip', 'application/s-compressed' => 'zip', 'multipart/x-zip' => 'zip', 'text/x-scriptzsh' => 'zsh', ]; return $mime_map[$mime] ?? ''; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Reader/Security/XmlScannerTest.php
tests/PhpSpreadsheetTests/Reader/Security/XmlScannerTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Reader\Security; use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; use PhpOffice\PhpSpreadsheet\Reader\Xls; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PHPUnit\Framework\TestCase; use XMLReader; class XmlScannerTest extends TestCase { #[\PHPUnit\Framework\Attributes\DataProvider('providerValidXML')] public function testValidXML(string $filename, string $expectedResult): void { $reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml()); $result = $reader->scanFile($filename); self::assertEquals($expectedResult, $result); } public static function providerValidXML(): array { $tests = []; $glob = glob('tests/data/Reader/Xml/XEETestValid*.xml'); self::assertNotFalse($glob); foreach ($glob as $file) { $filename = realpath($file); $expectedResult = (string) file_get_contents($file); if (preg_match('/UTF-16(LE|BE)?/', $file, $matches) == 1) { $expectedResult = (string) mb_convert_encoding($expectedResult, 'UTF-8', $matches[0]); $expectedResult = preg_replace('/encoding\s*=\s*[\'"]UTF-\d+(LE|BE)?[\'"]/', '', $expectedResult) ?? $expectedResult; } $tests[basename($file)] = [(string) $filename, $expectedResult]; } return $tests; } #[\PHPUnit\Framework\Attributes\DataProvider('providerInvalidXML')] public function testInvalidXML(string $filename): void { $this->expectException(ReaderException::class); $reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml()); $expectedResult = 'FAILURE: Should throw an Exception rather than return a value'; $result = $reader->scanFile($filename); self::assertEquals($expectedResult, $result); } public static function providerInvalidXML(): array { $tests = []; $glob = glob('tests/data/Reader/Xml/XEETestInvalidUTF*.xml'); self::assertNotFalse($glob); foreach ($glob as $file) { $filename = realpath($file); $tests[basename($file)] = [(string) $filename]; } return $tests; } public function testGetSecurityScannerForXmlBasedReader(): void { $fileReader = new Xlsx(); $scanner = $fileReader->getSecurityScanner(); // Must return an object... // ... of the correct type self::assertInstanceOf(XmlScanner::class, $scanner); } public function testGetSecurityScannerForNonXmlBasedReader(): void { $fileReader = new Xls(); $scanner = $fileReader->getSecurityScanner(); // Must return a null... self::assertNull($scanner); } public function testGetSecurityScannerForNonXmlBasedReader2(): void { $this->expectException(ReaderException::class); $this->expectExceptionMessage('Security scanner is unexpectedly null'); $fileReader = new Xls(); $fileReader->getSecurityScannerOrThrow(); } #[\PHPUnit\Framework\Attributes\DataProvider('providerValidXMLForCallback')] public function testSecurityScanWithCallback(string $filename, string $expectedResult): void { $fileReader = new Xlsx(); $scanner = $fileReader->getSecurityScannerOrThrow(); $scanner->setAdditionalCallback('strrev'); $xml = $scanner->scanFile($filename); self::assertEquals(strrev($expectedResult), $xml); } public static function providerValidXMLForCallback(): array { $tests = []; $glob = glob('tests/data/Reader/Xml/SecurityScannerWithCallback*.xml'); self::assertNotFalse($glob); foreach ($glob as $file) { $tests[basename($file)] = [(string) realpath($file), (string) file_get_contents($file)]; } return $tests; } public function testLibxmlDisableEntityLoaderIsRestoredWithoutShutdown(): void { $reader = new Xlsx(); unset($reader); $reader = new XMLReader(); $opened = $reader->open('tests/data/Reader/Xml/SecurityScannerWithCallbackExample.xml'); self::assertTrue($opened); } public function testEncodingAllowsMixedCase(): void { $scanner = new XmlScanner(); $output = $scanner->scan($input = '<?xml version="1.0" encoding="utf-8"?><foo>bar</foo>'); self::assertSame($input, $output); } #[\PHPUnit\Framework\Attributes\DataProvider('providerInvalidXlsx')] public function testInvalidXlsx(string $filename, string $message): void { $this->expectException(ReaderException::class); $this->expectExceptionMessage($message); $reader = new Xlsx(); $reader->load("tests/data/Reader/XLSX/$filename"); } public static function providerInvalidXlsx(): array { return [ ['utf7white.dontuse', 'UTF-7 encoding not permitted'], ['utf7quoteorder.dontuse', 'UTF-7 encoding not permitted'], ['utf8and16.dontuse', 'Double encoding not permitted'], ['utf8and16.entity.dontuse', 'Detected use of ENTITY'], ['utf8entity.dontuse', 'Detected use of ENTITY'], ['utf16entity.dontuse', 'Detected use of ENTITY'], ['ebcdic.dontuse', 'EBCDIC encoding not permitted'], ]; } #[\PHPUnit\Framework\Attributes\DataProvider('providerValidUtf16')] public function testValidUtf16(string $filename): void { $reader = new Xlsx(); $spreadsheet = $reader->load("tests/data/Reader/XLSX/$filename"); $sheet = $spreadsheet->getActiveSheet(); self::assertSame(1, $sheet->getCell('A1')->getValue()); $spreadsheet->disconnectWorksheets(); } public static function providerValidUtf16(): array { return [ ['utf16be.xlsx'], ['utf16be.bom.xlsx'], ]; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ColumnTest.php
tests/PhpSpreadsheetTests/Worksheet/ColumnTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Column; use PHPUnit\Framework\TestCase; class ColumnTest extends TestCase { public function testInstantiateColumnDefault(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $column = new Column($sheet); $columnIndex = $column->getColumnIndex(); self::assertEquals('A', $columnIndex); $spreadsheet->disconnectWorksheets(); } public function testInstantiateColumnSpecified(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $column = new Column($sheet, 'E'); $columnIndex = $column->getColumnIndex(); self::assertEquals('E', $columnIndex); $spreadsheet->disconnectWorksheets(); } public function testGetCellIterator(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $column = new Column($sheet); $cellIterator = $column->getCellIterator(); self::assertSame(1, $cellIterator->key()); $spreadsheet->disconnectWorksheets(); } public function testGetRowIterator(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $column = new Column($sheet); $cellIterator = $column->getRowIterator(); self::assertSame(1, $cellIterator->key()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ProtectionTest.php
tests/PhpSpreadsheetTests/Worksheet/ProtectionTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Worksheet\Protection; use PHPUnit\Framework\TestCase; class ProtectionTest extends TestCase { public function testVerifyPassword(): void { $protection = new Protection(); self::assertTrue($protection->verify('foo'), 'non-protected always pass'); $protection->setSheet(true); self::assertTrue($protection->verify('foo'), 'no password will always pass 1'); self::assertTrue($protection->verify('xyz'), 'no password will always pass 2'); $protection->setPassword('foo', true); $hashx1 = $protection->getPassword(); self::assertSame('foo', $hashx1, 'was not stored as-is, without hashing'); self::assertFalse($protection->verify('foo'), 'setting already hashed password will not match'); $protection->setPassword('foo'); $hashx2 = $protection->getPassword(); self::assertSame('CC40', $hashx2, 'was hashed'); self::assertTrue($protection->verify('foo'), 'setting non-hashed password will hash it and not match'); $protection->setAlgorithm(Protection::ALGORITHM_MD5); self::assertFalse($protection->verify('foo'), 'changing algorithm will not match anymore'); $protection->setPassword('foo'); $hash1 = $protection->getPassword(); $protection->setPassword('foo'); $hash2 = $protection->getPassword(); self::assertSame(24, mb_strlen($hash1)); self::assertSame(24, mb_strlen($hash2)); self::assertNotSame($hash1, $hash2, 'was hashed with automatic salt'); self::assertTrue($protection->verify('foo'), 'setting password again, will hash with proper algorithm and will match'); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/CloneTest.php
tests/PhpSpreadsheetTests/Worksheet/CloneTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class CloneTest extends TestCase { public function testUnattachedIndex(): void { $spreadsheet = new Spreadsheet(); $sheet1 = $spreadsheet->getActiveSheet(); $sheet1->getCell('A1')->setValue(10); $sheet2 = clone $sheet1; $sheet2->getCell('A1')->setValue(20); self::assertSame(0, $spreadsheet->getIndex($sheet1)); $idx = $spreadsheet->getIndex($sheet2, true); self::assertSame(-1, $idx); $sheet2->setTitle('clone'); $spreadsheet->addSheet($sheet2); $idx = $spreadsheet->getIndex($sheet2, true); self::assertSame(1, $idx); self::assertSame(10, $spreadsheet->getSheet(0)->getCell('A1')->getValue()); self::assertSame(20, $spreadsheet->getSheet(1)->getCell('A1')->getValue()); $spreadsheet->disconnectWorksheets(); } public function testGetCloneIndex(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Sheet does not exist'); $spreadsheet = new Spreadsheet(); $sheet1 = $spreadsheet->getActiveSheet(); $sheet1->getCell('A1')->setValue(10); $sheet2 = clone $sheet1; $spreadsheet->getSheet($spreadsheet->getIndex($sheet2)) ->setCellValue('A1', 100); } public function testSerialize1(): void { $spreadsheet = new Spreadsheet(); $sheet1 = $spreadsheet->getActiveSheet(); $sheet1->getCell('A1')->setValue(10); $serialized = serialize($sheet1); $newSheet = unserialize($serialized); self::assertInstanceOf(Worksheet::class, $newSheet); self::assertSame(10, $newSheet->getCell('A1')->getValue()); self::assertNotSame($newSheet, $sheet1); self::assertNotNull($newSheet->getParent()); self::assertNotSame($newSheet->getParent(), $sheet1->getParent()); $newSheet->getParent()->disconnectWorksheets(); $spreadsheet->disconnectWorksheets(); } public function testSerialize2(): void { $sheet1 = new Worksheet(); $sheet1->getCell('A1')->setValue(10); $serialized = serialize($sheet1); $newSheet = unserialize($serialized); self::assertInstanceOf(Worksheet::class, $newSheet); self::assertSame(10, $newSheet->getCell('A1')->getValue()); self::assertNotSame($newSheet, $sheet1); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/RowCellIterator2Test.php
tests/PhpSpreadsheetTests/Worksheet/RowCellIterator2Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator; use PhpOffice\PhpSpreadsheet\Worksheet\RowCellIterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class RowCellIterator2Test extends TestCase { // Phpstan does not think RowCellIterator can return null private static function isCellNull(?Cell $item): bool { return $item === null; } #[DataProvider('providerExistingCell')] public function testEndRangeTrue(?bool $existing, string $expectedResultFirst, string $expectedResultLast): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('C2')->setValue('cellb2'); $sheet->getCell('F2')->setValue('cellf2'); $iterator = new RowCellIterator($sheet, 2, 'B', 'H'); if (isset($existing)) { $iterator->setIterateOnlyExistingCells($existing); } $lastCoordinate = ''; $firstCoordinate = ''; foreach ($iterator as $cell) { if (!self::isCellNull($cell)) { $lastCoordinate = $cell->getCoordinate(); if (!$firstCoordinate) { $firstCoordinate = $lastCoordinate; } } } self::assertSame($expectedResultFirst, $firstCoordinate); self::assertSame($expectedResultLast, $lastCoordinate); } public static function providerExistingCell(): array { return [ [null, 'B2', 'H2'], [false, 'B2', 'H2'], [true, 'C2', 'F2'], ]; } #[DataProvider('providerEmptyRow')] public function testEmptyRow(?bool $existing, int $expectedResult): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('B2')->setValue('cellb2'); $sheet->getCell('F2')->setValue('cellf2'); $iterator = new RowCellIterator($sheet, 3); if (isset($existing)) { $iterator->setIterateOnlyExistingCells($existing); } $numCells = 0; foreach ($iterator as $cell) { ++$numCells; } self::assertSame($expectedResult, $numCells); } public static function providerEmptyRow(): array { return [ [null, 6], // Default behaviour [false, 6], [true, 0], ]; } #[DataProvider('providerNullOrCreate')] public function testNullOrCreateOption(?bool $existingBehaviour, int $expectedCreatedResult, mixed $expectedNullResult): void { if (!is_int($expectedNullResult)) { self::fail('unexpected unused arg'); } $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $iterator = new RowCellIterator($sheet, 2); if (isset($existingBehaviour)) { $iterator->setIfNotExists($existingBehaviour); } $notExistsBehaviour = $iterator->getIfNotExists(); self::assertSame($expectedCreatedResult > 0, $notExistsBehaviour); } #[DataProvider('providerNullOrCreate')] public function testNullOrCreate(?bool $existing, int $expectedCreatedResult, int $expectedNullResult): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('B2')->setValue('cellb2'); $sheet->getCell('F2')->setValue('cellf2'); $iterator = new RowCellIterator($sheet, 2); if (isset($existing)) { $iterator->setIfNotExists($existing); } $numCreatedCells = $numEmptyCells = 0; foreach ($iterator as $cell) { $numCreatedCells += (int) (!self::isCellNull($cell) && $cell->getValue() === null); $numEmptyCells += (int) self::isCellNull($cell); } self::assertSame($expectedCreatedResult, $numCreatedCells); self::assertSame($expectedNullResult, $numEmptyCells); } public static function providerNullOrCreate(): array { return [ [null, 4, 0], // Default behaviour [CellIterator::IF_NOT_EXISTS_CREATE_NEW, 4, 0], [CellIterator::IF_NOT_EXISTS_RETURN_NULL, 0, 4], ]; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Issue4128Test.php
tests/PhpSpreadsheetTests/Worksheet/Issue4128Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class Issue4128Test extends TestCase { public function testIssue4128(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); self::assertSame('A1', $sheet->getActiveCell()); self::assertSame('A1', $sheet->getSelectedCells()); $sheet->setCellValue('D1', 'MyDate'); self::assertSame('A1', $sheet->getActiveCell()); self::assertSame('A1', $sheet->getSelectedCells()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Worksheet3Test.php
tests/PhpSpreadsheetTests/Worksheet/Worksheet3Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class Worksheet3Test extends TestCase { // All these tests involve setting one property to a // non-default value. They should be reviewed if defaults change. public function testPageSetup(): void { $worksheet1 = new Worksheet(); $worksheet1->getPageSetup()->setOrientation('landscape'); $pageSetup = clone $worksheet1->getPageSetup(); $worksheet2 = new Worksheet(); $worksheet2->setPageSetup($pageSetup); self::assertSame('landscape', $worksheet2->getPageSetup()->getOrientation()); } public function testPageMargins(): void { $worksheet1 = new Worksheet(); $worksheet1->getPageMargins()->setLeft(0.75); $pageMargins = clone $worksheet1->getPageMargins(); $worksheet2 = new Worksheet(); $worksheet2->setPageMargins($pageMargins); self::assertSame(0.75, $worksheet2->getPageMargins()->getLeft()); } public function testHeaderFooter(): void { $worksheet1 = new Worksheet(); $worksheet1->getHeaderFooter()->setDifferentOddEven(true); $headerFooter = clone $worksheet1->getHeaderFooter(); $worksheet2 = new Worksheet(); $worksheet2->setHeaderFooter($headerFooter); self::assertTrue($worksheet2->getHeaderFooter()->getDifferentOddEven()); } public function testSheetView(): void { $worksheet1 = new Worksheet(); $worksheet1->getSheetView()->setView('pageLayout'); $sheetView = clone $worksheet1->getSheetView(); $worksheet2 = new Worksheet(); $worksheet2->setSheetView($sheetView); self::assertSame('pageLayout', $worksheet2->getSheetView()->getView()); } public function testProtection(): void { $worksheet1 = new Worksheet(); $worksheet1->getProtection()->setSpinCount(4321); $protection = clone $worksheet1->getProtection(); $worksheet2 = new Worksheet(); $worksheet2->setProtection($protection); self::assertSame(4321, $worksheet2->getProtection()->getSpinCount()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorEmptyTest.php
tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorEmptyTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator; use PhpOffice\PhpSpreadsheet\Worksheet\ColumnIterator; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class ColumnIteratorEmptyTest extends TestCase { private static function getPopulatedSheet(Spreadsheet $spreadsheet): Worksheet { $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValueExplicit('A1', 'Hello World', DataType::TYPE_STRING); $sheet->setCellValueExplicit('C2', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('D2', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('E2', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('E3', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('F2', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('F3', 'PHP', DataType::TYPE_STRING); $sheet->setCellValueExplicit('G2', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('G3', 'PHP', DataType::TYPE_STRING); $sheet->setCellValueExplicit('H2', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('H3', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('H4', 'PHP', DataType::TYPE_STRING); return $sheet; } #[DataProvider('emptyColumnBasicProvider')] public function testIteratorEmptyColumn(string $columnId, bool $expectedEmpty): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnIterator($sheet, 'A', 'I'); $iterator->seek($columnId); $column = $iterator->current(); $isEmpty = $column->isEmpty(); self::assertSame($expectedEmpty, $isEmpty); $spreadsheet->disconnectWorksheets(); } public static function emptyColumnBasicProvider(): array { return [ ['A', false], ['B', true], ['C', false], ['D', false], ['E', false], ['F', false], ['G', false], ['H', false], ['I', true], ]; } #[DataProvider('emptyColumnNullAsEmptyProvider')] public function testIteratorEmptyColumnWithNull(string $columnId, bool $expectedEmpty): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnIterator($sheet, 'A', 'I'); $iterator->seek($columnId); $column = $iterator->current(); $isEmpty = $column->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL); self::assertSame($expectedEmpty, $isEmpty); $spreadsheet->disconnectWorksheets(); } public static function emptyColumnNullAsEmptyProvider(): array { return [ ['A', false], ['B', true], ['C', true], ['D', false], ['E', false], ['F', false], ['G', false], ['H', false], ['I', true], ]; } #[DataProvider('emptyColumnEmptyStringAsEmptyProvider')] public function testIteratorEmptyColumnWithEmptyString(string $columnId, bool $expectedEmpty): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnIterator($sheet, 'A', 'I'); $iterator->seek($columnId); $column = $iterator->current(); $isEmpty = $column->isEmpty(CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL); self::assertSame($expectedEmpty, $isEmpty); $spreadsheet->disconnectWorksheets(); } public static function emptyColumnEmptyStringAsEmptyProvider(): array { return [ ['A', false], ['B', true], ['C', false], ['D', true], ['E', false], ['F', false], ['G', false], ['H', false], ['I', true], ]; } #[DataProvider('emptyColumnNullAndEmptyStringAsEmptyProvider')] public function testIteratorEmptyColumnWithNullAndEmptyString(string $columnId, bool $expectedEmpty): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnIterator($sheet, 'A', 'I'); $iterator->seek($columnId); $column = $iterator->current(); $isEmpty = $column->isEmpty( CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL | CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL ); self::assertSame($expectedEmpty, $isEmpty); $spreadsheet->disconnectWorksheets(); } public static function emptyColumnNullAndEmptyStringAsEmptyProvider(): array { return [ ['A', false], ['B', true], ['C', true], ['D', true], ['E', true], ['F', false], ['G', false], ['H', false], ['I', true], ]; } public function testIteratorEmptyColumnWithRowLimit(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $sheet->setCellValue('A8', 'NO LONGER EMPTY'); $iterator = new ColumnIterator($sheet, 'A', 'A'); $column = $iterator->current(); $isEmpty = $column->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL); self::assertFalse($isEmpty); $isEmpty = $column->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL, 2, 7); self::assertTrue($isEmpty); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Issue641Test.php
tests/PhpSpreadsheetTests/Worksheet/Issue641Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class Issue641Test extends TestCase { /** * Problem cloning sheet referred to in formulas. */ public function testIssue641(): void { $xlsx = new Spreadsheet(); $xlsx->removeSheetByIndex(0); $availableWs = []; $worksheet = $xlsx->createSheet(); $worksheet->setTitle('Condensed A'); $worksheet->getCell('A1')->setValue("=SUM('Detailed A'!A1:A10)"); $worksheet->getCell('A2')->setValue(mt_rand(1, 30)); $availableWs[] = 'Condensed A'; $worksheet = $xlsx->createSheet(); $worksheet->setTitle('Condensed B'); $worksheet->getCell('A1')->setValue("=SUM('Detailed B'!A1:A10)"); $worksheet->getCell('A2')->setValue(mt_rand(1, 30)); $availableWs[] = 'Condensed B'; // at this point the value in worksheet 'Condensed B' cell A1 is // =SUM('Detailed B'!A1:A10) // worksheet in question is cloned and totals are attached $totalWs1 = clone $xlsx->getSheet($xlsx->getSheetCount() - 1); $totalWs1->setTitle('Condensed Total'); $xlsx->addSheet($totalWs1); $formula = '='; foreach ($availableWs as $ws) { $formula .= sprintf("+'%s'!A2", $ws); } $totalWs1->getCell('A1')->setValue("=SUM('Detailed Total'!A1:A10)"); $totalWs1->getCell('A2')->setValue($formula); $availableWs = []; $worksheet = $xlsx->createSheet(); $worksheet->setTitle('Detailed A'); for ($step = 1; $step <= 10; ++$step) { $worksheet->getCell("A{$step}")->setValue(mt_rand(1, 30)); } $availableWs[] = 'Detailed A'; $worksheet = $xlsx->createSheet(); $worksheet->setTitle('Detailed B'); for ($step = 1; $step <= 10; ++$step) { $worksheet->getCell("A{$step}")->setValue(mt_rand(1, 30)); } $availableWs[] = 'Detailed B'; $totalWs2 = clone $xlsx->getSheet($xlsx->getSheetCount() - 1); $totalWs2->setTitle('Detailed Total'); $xlsx->addSheet($totalWs2); for ($step = 1; $step <= 10; ++$step) { $formula = '='; foreach ($availableWs as $ws) { $formula .= sprintf("+'%s'!A%s", $ws, $step); } $totalWs2->getCell("A{$step}")->setValue($formula); } self::assertSame("=SUM('Detailed A'!A1:A10)", $xlsx->getSheetByName('Condensed A')?->getCell('A1')?->getValue()); self::assertSame("=SUM('Detailed B'!A1:A10)", $xlsx->getSheetByName('Condensed B')?->getCell('A1')?->getValue()); self::assertSame("=SUM('Detailed Total'!A1:A10)", $xlsx->getSheetByName('Condensed Total')?->getCell('A1')?->getValue()); self::assertSame("=+'Detailed A'!A1+'Detailed B'!A1", $xlsx->getSheetByName('Detailed Total')?->getCell('A1')?->getValue()); $xlsx->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/MemoryDrawing2.php
tests/PhpSpreadsheetTests/Worksheet/MemoryDrawing2.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; class MemoryDrawing2 extends MemoryDrawing { protected static string $getImageSize = 'getImageSizeXXXXXXX'; }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/CopyCellsTest.php
tests/PhpSpreadsheetTests/Worksheet/CopyCellsTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class CopyCellsTest extends TestCase { public function testCopyCells(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->fromArray( [ ['hello1', 'goodbye1', 'neither1', 'constant'], ['hello2', 'goodbye2', 'neither2'], ['hello3', 'goodbye3', 'neither3'], ['hello4', 'goodbye4', 'neither4'], ['hello5', 'goodbye5', 'neither5'], ], ); $sheet->getCell('E3')->setValue('=A1&B1'); $sheet->getStyle('E3')->getFont()->setBold(true); $sheet->copyCells('E3', 'E3:F7'); $result1 = $sheet->rangeToArray('E3:F7', null, false, false); $expected1 = [ ['=A1&B1', '=B1&C1'], ['=A2&B2', '=B2&C2'], ['=A3&B3', '=B3&C3'], ['=A4&B4', '=B4&C4'], ['=A5&B5', '=B5&C5'], ]; self::assertSame($expected1, $result1); self::assertSame('goodbye3neither3', $sheet->getCell('F5')->getCalculatedValue()); self::assertTrue($sheet->getCell('F5')->getStyle()->getFont()->getBold()); $sheet->getCell('E14')->setValue('=A5&$D$1'); $sheet->copyCells('E14', 'E10:E14'); $result2 = $sheet->rangeToArray('E10:E14', null, false, false); $expected2 = [ ['=A1&$D$1'], ['=A2&$D$1'], ['=A3&$D$1'], ['=A4&$D$1'], ['=A5&$D$1'], ]; self::assertSame($expected2, $result2); self::assertSame('hello4constant', $sheet->getCell('E13')->getCalculatedValue()); $sheet->getCell('I3')->setValue('=A1&$B1'); $sheet->getStyle('I3')->getFont()->setItalic(true); $sheet->copyCells('I3', 'I3:J7', false); $result3 = $sheet->rangeToArray('I3:J7', null, false, false); $expected3 = [ ['=A1&$B1', '=B1&$B1'], ['=A2&$B2', '=B2&$B2'], ['=A3&$B3', '=B3&$B3'], ['=A4&$B4', '=B4&$B4'], ['=A5&$B5', '=B5&$B5'], ]; self::assertSame($expected3, $result3); self::assertSame('hello2goodbye2', $sheet->getCell('I4')->getCalculatedValue()); self::assertFalse($sheet->getCell('I5')->getStyle()->getFont()->getItalic()); try { $sheet->copyCells('invalid', 'Z1:Z10'); self::fail('Did not receive expected exception'); } catch (SpreadsheetException $e) { self::assertStringContainsString('Invalid cell coordinate', $e->getMessage()); } try { $sheet->copyCells('A1', 'invalid'); self::fail('Did not receive expected exception'); } catch (SpreadsheetException $e) { self::assertStringContainsString('Column string index', $e->getMessage()); } $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/PageBreakTest.php
tests/PhpSpreadsheetTests/Worksheet/PageBreakTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\CellAddress; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class PageBreakTest extends TestCase { public function testBreaksString(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setBreak('A20', Worksheet::BREAK_ROW); $sheet->setBreak('A40', Worksheet::BREAK_ROW); $sheet->setBreak('H1', Worksheet::BREAK_COLUMN); $sheet->setBreak('X1', Worksheet::BREAK_COLUMN); $breaks1 = $sheet->getBreaks(); self::assertSame( [ 'A20' => Worksheet::BREAK_ROW, 'A40' => Worksheet::BREAK_ROW, 'H1' => Worksheet::BREAK_COLUMN, 'X1' => Worksheet::BREAK_COLUMN, ], $breaks1 ); $sheet->setBreak('A40', Worksheet::BREAK_NONE); $sheet->setBreak('H1', Worksheet::BREAK_NONE); $sheet->setBreak('XX1', Worksheet::BREAK_NONE); $breaks2 = $sheet->getBreaks(); self::assertSame( [ 'A20' => Worksheet::BREAK_ROW, 'X1' => Worksheet::BREAK_COLUMN, ], $breaks2 ); $spreadsheet->disconnectWorksheets(); } public function testBreaksArray(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setBreak([1, 20], Worksheet::BREAK_ROW); $sheet->setBreak([1, 40], Worksheet::BREAK_ROW); $sheet->setBreak([8, 1], Worksheet::BREAK_COLUMN); $sheet->setBreak([24, 1], Worksheet::BREAK_COLUMN); $breaks1 = $sheet->getBreaks(); self::assertSame( [ 'A20' => Worksheet::BREAK_ROW, 'A40' => Worksheet::BREAK_ROW, 'H1' => Worksheet::BREAK_COLUMN, 'X1' => Worksheet::BREAK_COLUMN, ], $breaks1 ); $sheet->setBreak([1, 40], Worksheet::BREAK_NONE); $sheet->setBreak([8, 1], Worksheet::BREAK_NONE); $sheet->setBreak([50, 1], Worksheet::BREAK_NONE); $breaks2 = $sheet->getBreaks(); self::assertSame( [ 'A20' => Worksheet::BREAK_ROW, 'X1' => Worksheet::BREAK_COLUMN, ], $breaks2 ); $spreadsheet->disconnectWorksheets(); } public function testBreaksCellAddress(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setBreak(new CellAddress('A20'), Worksheet::BREAK_ROW, 16383); $sheet->setBreak(new CellAddress('A40', $sheet), Worksheet::BREAK_ROW); $sheet->setBreak(new CellAddress('H1'), Worksheet::BREAK_COLUMN); $sheet->setBreak(new CellAddress('X1', $sheet), Worksheet::BREAK_COLUMN); $breaks1 = $sheet->getBreaks(); self::assertSame( [ 'A20' => Worksheet::BREAK_ROW, 'A40' => Worksheet::BREAK_ROW, 'H1' => Worksheet::BREAK_COLUMN, 'X1' => Worksheet::BREAK_COLUMN, ], $breaks1 ); $sheet->setBreak(new CellAddress('A40'), Worksheet::BREAK_NONE); $sheet->setBreak(new CellAddress('H1', $sheet), Worksheet::BREAK_NONE); $sheet->setBreak(new CellAddress('XX1'), Worksheet::BREAK_NONE); $breaks2 = $sheet->getBreaks(); self::assertSame( [ 'A20' => Worksheet::BREAK_ROW, 'X1' => Worksheet::BREAK_COLUMN, ], $breaks2 ); $spreadsheet->disconnectWorksheets(); } public function testBreaksOtherMethods(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setBreak('A20', Worksheet::BREAK_ROW, 16383); $sheet->setBreak('A40', Worksheet::BREAK_ROW); $sheet->setBreak('H1', Worksheet::BREAK_COLUMN); $sheet->setBreak('X1', Worksheet::BREAK_COLUMN); $rowBreaks = $sheet->getRowBreaks(); self::assertCount(2, $rowBreaks); self::assertSame(Worksheet::BREAK_ROW, $rowBreaks['A20']->getBreakType()); self::assertSame('A20', $rowBreaks['A20']->getCoordinate()); self::assertSame(16383, $rowBreaks['A20']->getMaxColOrRow()); self::assertSame(1, $rowBreaks['A20']->getColumnInt()); self::assertSame('A', $rowBreaks['A20']->getColumnString()); self::assertSame(20, $rowBreaks['A20']->getRow()); self::assertSame(Worksheet::BREAK_ROW, $rowBreaks['A20']->getBreakType()); self::assertSame('A40', $rowBreaks['A40']->getCoordinate()); self::assertSame(-1, $rowBreaks['A40']->getMaxColOrRow()); self::assertSame(1, $rowBreaks['A40']->getColumnInt()); self::assertSame('A', $rowBreaks['A40']->getColumnString()); self::assertSame(40, $rowBreaks['A40']->getRow()); self::assertSame(Worksheet::BREAK_ROW, $rowBreaks['A40']->getBreakType()); $columnBreaks = $sheet->getColumnBreaks(); self::assertCount(2, $columnBreaks); self::assertSame(Worksheet::BREAK_COLUMN, $columnBreaks['H1']->getBreakType()); self::assertSame('H1', $columnBreaks['H1']->getCoordinate()); self::assertSame(8, $columnBreaks['H1']->getColumnInt()); self::assertSame('H', $columnBreaks['H1']->getColumnString()); self::assertSame(1, $columnBreaks['H1']->getRow()); self::assertSame(Worksheet::BREAK_COLUMN, $columnBreaks['H1']->getBreakType()); self::assertSame('X1', $columnBreaks['X1']->getCoordinate()); self::assertSame(24, $columnBreaks['X1']->getColumnInt()); self::assertSame('X', $columnBreaks['X1']->getColumnString()); self::assertSame(1, $columnBreaks['X1']->getRow()); self::assertSame(Worksheet::BREAK_COLUMN, $columnBreaks['X1']->getBreakType()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/WorksheetNamedRangesTest.php
tests/PhpSpreadsheetTests/Worksheet/WorksheetNamedRangesTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class WorksheetNamedRangesTest extends TestCase { protected function getSpreadsheet(): Spreadsheet { $reader = new Xlsx(); return $reader->load('tests/data/Worksheet/namedRangeTest.xlsx'); } public function testCellExists(): void { $spreadsheet = $this->getSpreadsheet(); $namedCell = 'GREETING'; $worksheet = $spreadsheet->getActiveSheet(); $cellExists = $worksheet->cellExists($namedCell); self::assertTrue($cellExists); $spreadsheet->disconnectWorksheets(); } public function testCellExistsUtf8(): void { $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Χαιρετισμός'; $worksheet = $spreadsheet->getActiveSheet(); $cellExists = $worksheet->cellExists($namedCell); self::assertTrue($cellExists); $spreadsheet->disconnectWorksheets(); } public function testCellNotExists(): void { $spreadsheet = $this->getSpreadsheet(); $namedCell = 'GOODBYE'; $worksheet = $spreadsheet->getActiveSheet(); $cellExists = $worksheet->cellExists($namedCell); self::assertFalse($cellExists); $spreadsheet->disconnectWorksheets(); } public function testCellExistsInvalidScope(): void { $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Result'; $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 = $spreadsheet->getActiveSheet(); $worksheet->cellExists($namedRange); $spreadsheet->disconnectWorksheets(); } public function testGetCell(): void { $spreadsheet = $this->getSpreadsheet(); $namedCell = 'GREETING'; $worksheet = $spreadsheet->getActiveSheet(); $cell = $worksheet->getCell($namedCell); self::assertSame('Hello', $cell->getValue()); $spreadsheet->disconnectWorksheets(); } public function testGetCellUtf8(): void { $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Χαιρετισμός'; $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 = $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 = $spreadsheet->getActiveSheet(); $worksheet->getCell($namedCell); $spreadsheet->disconnectWorksheets(); } public function testGetCellLocalScoped(): void { $spreadsheet = $this->getSpreadsheet(); $namedCell = 'Result'; $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'; $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 = $spreadsheet->getActiveSheet(); $worksheet->getCell($namedCell); $spreadsheet->disconnectWorksheets(); } public function testNamedRangeToArray(): void { $spreadsheet = $this->getSpreadsheet(); $namedRange = 'Range1'; $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 = $spreadsheet->getActiveSheet(); $rangeData = $worksheet->namedRangeToArray($namedRange); self::assertSame([[1, 2, 3]], $rangeData); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ApplyStylesTest.php
tests/PhpSpreadsheetTests/Worksheet/ApplyStylesTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class ApplyStylesTest extends TestCase { public function testApplyFromArray(): void { $spreadsheet = new Spreadsheet(); $sheet1 = $spreadsheet->getActiveSheet(); $sheet2 = $spreadsheet->createSheet(); $sheet3 = $spreadsheet->createSheet(); $cell = 'B4'; $sheet1->getCell($cell)->setValue('first'); $sheet1->getStyle($cell)->getFont()->setName('Arial'); $cell = 'C9'; $sheet2->getCell($cell)->setValue('second'); $sheet2->getStyle($cell)->getFont()->setName('Arial'); $cell = 'A6'; $sheet3->getCell($cell)->setValue('third'); $sheet3->getStyle($cell)->getFont()->setName('Arial'); self::assertSame(2, $spreadsheet->getActiveSheetIndex()); self::assertSame('B4', $sheet1->getSelectedCells()); self::assertSame('C9', $sheet2->getSelectedCells()); self::assertSame('A6', $sheet3->getSelectedCells()); $cell = 'D12'; $styleArray = ['font' => ['name' => 'Courier New']]; $sheet2->getStyle($cell)->applyFromArray($styleArray); self::assertSame(1, $spreadsheet->getActiveSheetIndex()); self::assertSame('B4', $sheet1->getSelectedCells()); self::assertSame('D12', $sheet2->getSelectedCells()); self::assertSame('A6', $sheet3->getSelectedCells()); $spreadsheet->disconnectWorksheets(); } public function testApplyStylesFromArray(): void { $spreadsheet = new Spreadsheet(); $sheet1 = $spreadsheet->getActiveSheet(); $sheet2 = $spreadsheet->createSheet(); $sheet3 = $spreadsheet->createSheet(); $cell = 'B4'; $sheet1->getCell($cell)->setValue('first'); $sheet1->getStyle($cell)->getFont()->setName('Arial'); $cell = 'C9'; $sheet2->getCell($cell)->setValue('second'); $sheet2->getStyle($cell)->getFont()->setName('Arial'); $cell = 'A6'; $sheet3->getCell($cell)->setValue('third'); $sheet3->getStyle($cell)->getFont()->setName('Arial'); self::assertSame(2, $spreadsheet->getActiveSheetIndex()); self::assertSame('B4', $sheet1->getSelectedCells()); self::assertSame('C9', $sheet2->getSelectedCells()); self::assertSame('A6', $sheet3->getSelectedCells()); $cell = 'D12'; $styleArray = ['font' => ['name' => 'Courier New']]; $sheet2->applyStylesFromArray($cell, $styleArray); self::assertSame(2, $spreadsheet->getActiveSheetIndex(), 'should be unchanged'); self::assertSame('B4', $sheet1->getSelectedCells(), 'should be unchanged'); self::assertSame('C9', $sheet2->getSelectedCells(), 'should be unchanged'); self::assertSame('A6', $sheet3->getSelectedCells(), 'should be unchanged'); $spreadsheet->disconnectWorksheets(); } public function testNoSpreadsheet(): void { $sheet2 = new Worksheet(); $cell = 'D12'; self::assertFalse($sheet2->applyStylesFromArray($cell, ['font' => ['name' => 'Courier New']])); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/MemoryDrawingTest.php
tests/PhpSpreadsheetTests/Worksheet/MemoryDrawingTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception; use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; use PHPUnit\Framework\TestCase; class MemoryDrawingTest extends TestCase { public function testMemoryDrawing(): void { $name = 'In-Memory image'; $gdImage = @imagecreatetruecolor(120, 20); self::assertNotFalse($gdImage); $textColor = (int) imagecolorallocate($gdImage, 255, 255, 255); imagestring($gdImage, 1, 5, 5, 'Created with PhpSpreadsheet', $textColor); $drawing = new MemoryDrawing(); $drawing->setName($name); $drawing->setDescription('In-Memory image 1'); $drawing->setCoordinates('A1'); $drawing->setImageResource($gdImage); $drawing->setRenderingFunction(MemoryDrawing::RENDERING_PNG); $drawing->setMimeType(MemoryDrawing::MIMETYPE_PNG); self::assertIsObject($drawing->getImageResource()); self::assertSame(MemoryDrawing::MIMETYPE_DEFAULT, $drawing->getMimeType()); self::assertSame(MemoryDrawing::RENDERING_DEFAULT, $drawing->getRenderingFunction()); } public function testMemoryDrawingFromString(): void { $imageFile = __DIR__ . '/../../data/Worksheet/officelogo.jpg'; $imageString = file_get_contents($imageFile); self::assertNotFalse($imageString); $drawing = MemoryDrawing::fromString($imageString); self::assertIsObject($drawing->getImageResource()); self::assertSame(MemoryDrawing::MIMETYPE_JPEG, $drawing->getMimeType()); self::assertSame(MemoryDrawing::RENDERING_JPEG, $drawing->getRenderingFunction()); } public function testMemoryDrawingFromInvalidString(): void { $this->expectException(Exception::class); $this->expectExceptionMessage('Value cannot be converted to an image'); $imageString = 'I am not an image'; MemoryDrawing::fromString($imageString); } public function testMemoryDrawingFromStream(): void { $imageFile = __DIR__ . '/../../data/Worksheet/officelogo.jpg'; $imageStream = fopen($imageFile, 'rb'); self::assertNotFalse($imageStream); $drawing = MemoryDrawing::fromStream($imageStream); fclose($imageStream); self::assertIsObject($drawing->getImageResource()); self::assertSame(MemoryDrawing::MIMETYPE_JPEG, $drawing->getMimeType()); self::assertSame(MemoryDrawing::RENDERING_JPEG, $drawing->getRenderingFunction()); } public function testMemoryDrawingFromStreamNoGetImageSize(): void { $imageFile = __DIR__ . '/../../data/Worksheet/officelogo.jpg'; $imageStream = fopen($imageFile, 'rb'); self::assertNotFalse($imageStream); $drawing = MemoryDrawing2::fromStream($imageStream); fclose($imageStream); self::assertIsObject($drawing->getImageResource()); self::assertSame(MemoryDrawing::MIMETYPE_DEFAULT, $drawing->getMimeType()); self::assertSame(MemoryDrawing::RENDERING_DEFAULT, $drawing->getRenderingFunction()); } public function testMemoryDrawingGif(): void { $imageFile = __DIR__ . '/../../data/Writer/XLSX/green_square.gif'; $imageStream = fopen($imageFile, 'rb'); self::assertNotFalse($imageStream); $drawing = MemoryDrawing::fromStream($imageStream); fclose($imageStream); self::assertIsObject($drawing->getImageResource()); self::assertSame(MemoryDrawing::MIMETYPE_GIF, $drawing->getMimeType()); self::assertSame(MemoryDrawing::RENDERING_GIF, $drawing->getRenderingFunction()); } public function testMemoryDrawingBmp(): void { $imageFile = __DIR__ . '/../../data/Writer/XLSX/brown_square_256.bmp'; $imageStream = fopen($imageFile, 'rb'); self::assertNotFalse($imageStream); $drawing = MemoryDrawing::fromStream($imageStream); fclose($imageStream); self::assertIsObject($drawing->getImageResource()); self::assertSame(MemoryDrawing::MIMETYPE_DEFAULT, $drawing->getMimeType(), 'bmp not supporteed - use default'); self::assertSame(MemoryDrawing::RENDERING_DEFAULT, $drawing->getRenderingFunction(), 'bmp not supported - use default'); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php
tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception as Except; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\ColumnIterator; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class ColumnIteratorTest extends TestCase { private const CELL_VALUES = [ [110, 210, 310, 410, 510, 610], [120, 220, 320, 420, 520, 620], [130, null, 330, null, 530], ]; private static function getPopulatedSheet(Spreadsheet $spreadsheet): Worksheet { $sheet = $spreadsheet->getActiveSheet(); $sheet->fromArray(self::CELL_VALUES); return $sheet; } public function testIteratorFullRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnIterator($sheet); $columnIndexResult = 'A'; self::assertEquals($columnIndexResult, $iterator->key()); $counter = 0; foreach ($iterator as $key => $column) { ++$counter; self::assertEquals($columnIndexResult, $key); StringHelper::stringIncrement($columnIndexResult); } self::assertCount($counter, self::CELL_VALUES[0]); $spreadsheet->disconnectWorksheets(); } public function testIteratorStartEndRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnIterator($sheet, 'B', 'D'); $columnIndexResult = 'B'; self::assertEquals($columnIndexResult, $iterator->key()); $counter = 0; foreach ($iterator as $key => $column) { ++$counter; self::assertEquals($columnIndexResult, $key); StringHelper::stringIncrement($columnIndexResult); } self::assertSame(3, $counter); $spreadsheet->disconnectWorksheets(); } public function testIteratorSeekAndPrev(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnIterator($sheet, 'B', 'D'); $columnIndexResult = 'D'; $iterator->seek('D'); self::assertEquals($columnIndexResult, $iterator->key()); $counter = 0; while ($iterator->valid() !== false) { ++$counter; self::assertEquals($columnIndexResult, $iterator->key(), "counter $counter"); // https://stackoverflow.com/questions/37027277/decrement-character-with-php $columnIndexResult = chr(ord($columnIndexResult) - 1); $iterator->prev(); } self::assertSame(3, $counter); $spreadsheet->disconnectWorksheets(); } public function testIteratorResetStart(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnIterator($sheet, 'B', 'D'); $iterator->resetStart('E'); $key = $iterator->key(); self::assertSame('E', $key); $lastColumn = $iterator->key(); while ($iterator->valid() !== false) { $iterator->next(); $lastColumn = $iterator->key(); } self::assertSame('F', $lastColumn); $spreadsheet->disconnectWorksheets(); } public function testSeekOutOfRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $this->expectException(Except::class); $iterator = new ColumnIterator($sheet, 'B', 'D'); $iterator->seek('A'); $spreadsheet->disconnectWorksheets(); } public function testPrevOutOfRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnIterator($sheet, 'B', 'D'); $iterator->prev(); self::assertFalse($iterator->valid()); $spreadsheet->disconnectWorksheets(); } public function testResetStartOutOfRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $this->expectException(Except::class); $iterator = new ColumnIterator($sheet, 'B', 'D'); $iterator->resetStart('H'); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Issue4112Test.php
tests/PhpSpreadsheetTests/Worksheet/Issue4112Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; class Issue4112Test extends AbstractFunctional { /** * Problem deleting all sheets then adding one. */ #[\PHPUnit\Framework\Attributes\DataProvider('providerSheetNumber')] public function testIssue4112(?int $sheetNumber): void { $mySpreadsheet = new Spreadsheet(); $mySpreadsheet->removeSheetByIndex(0); $worksheet = new Worksheet($mySpreadsheet, 'addedsheet'); self::assertSame(-1, $mySpreadsheet->getActiveSheetIndex()); $mySpreadsheet->addSheet($worksheet, $sheetNumber); self::assertSame('addedsheet', $mySpreadsheet->getActiveSheet()->getTitle()); $row = 1; $col = 1; $worksheet->getCell([$col, $row])->setValue('id_uti'); self::assertSame('id_uti', $worksheet->getCell([$col, $row])->getValue()); $mySpreadsheet->disconnectWorksheets(); } public static function providerSheetNumber(): array { return [ 'problem case' => [0], 'normal case' => [null], 'negative 1 (as if there were no sheets)' => [-1], 'diffeent negative number' => [-4], 'positive number' => [4], ]; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ColumnCellIteratorTest.php
tests/PhpSpreadsheetTests/Worksheet/ColumnCellIteratorTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Exception as Except; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\ColumnCellIterator; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class ColumnCellIteratorTest extends TestCase { private const CELL_VALUES = [ [110, 210, 310, 410, 510, 610, 710], [120, 220, 320, 420, 520, 620], [130, 230, 330, 430, 530, 630], [140, 240, 340, 440, 540, 640], [150, 250, 350, 450, 550, 650], [160, null, 360, null, 560], ]; // Phpstan does not think RowCellIterator can return null private static function isCellNull(?Cell $item): bool { return $item === null; } private static function getPopulatedSheet(Spreadsheet $spreadsheet): Worksheet { $sheet = $spreadsheet->getActiveSheet(); $sheet->fromArray(self::CELL_VALUES); return $sheet; } public function testIteratorFullRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnCellIterator($sheet, 'A'); $ColumnCellIndexResult = 1; self::assertEquals($ColumnCellIndexResult, $iterator->key()); $values = []; foreach ($iterator as $key => $ColumnCell) { self::assertFalse(self::isCellNull($ColumnCell)); $values[] = $ColumnCell->getValue(); self::assertEquals($ColumnCellIndexResult++, $key); } $transposed = array_map(null, ...self::CELL_VALUES); self::assertSame($transposed[0], $values); $spreadsheet->disconnectWorksheets(); } public function testIteratorStartEndRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnCellIterator($sheet, 'A', 2, 4); $ColumnCellIndexResult = 2; self::assertEquals($ColumnCellIndexResult, $iterator->key()); $values = []; foreach ($iterator as $key => $ColumnCell) { self::assertFalse(self::isCellNull($ColumnCell)); $values[] = $ColumnCell->getValue(); self::assertEquals($ColumnCellIndexResult++, $key); } self::assertSame([120, 130, 140], $values); $spreadsheet->disconnectWorksheets(); } public function testIteratorSeekAndPrev(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnCellIterator($sheet, 'A', 2, 4); $columnIndexResult = 4; $iterator->seek(4); self::assertEquals($columnIndexResult, $iterator->key()); $values = []; while ($iterator->valid()) { $current = $iterator->current(); self::assertNotNull($current); $cell = $current->getCoordinate(); $values[] = $sheet->getCell($cell)->getValue(); $iterator->prev(); } self::assertSame([140, 130, 120], $values); $spreadsheet->disconnectWorksheets(); } public function testSeekOutOfRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $this->expectException(Except::class); $this->expectExceptionMessage('Row 1 is out of range'); $iterator = new ColumnCellIterator($sheet, 'A', 2, 4); $iterator->seek(1); $spreadsheet->disconnectWorksheets(); } public function testSeekNotExisting(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $this->expectException(Except::class); $this->expectExceptionMessage('Cell does not exist'); $iterator = new ColumnCellIterator($sheet, 'A', 2, 4); $iterator->setIterateOnlyExistingCells(true); $iterator->seek(2); } public function testPrevOutOfRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new ColumnCellIterator($sheet, 'A', 2, 4); $iterator->prev(); self::assertFalse($iterator->valid()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/DefaultPaperSizeTest.php
tests/PhpSpreadsheetTests/Worksheet/DefaultPaperSizeTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; use PHPUnit\Framework\TestCase; class DefaultPaperSizeTest extends TestCase { private int $paperSize; private string $orientation; protected function setUp(): void { $this->paperSize = PageSetup::getPaperSizeDefault(); $this->orientation = PageSetup::getOrientationDefault(); } protected function tearDown(): void { PageSetup::setPaperSizeDefault($this->paperSize); PageSetup::setOrientationDefault($this->orientation); } public function testChangeDefault(): void { PageSetup::setPaperSizeDefault(PageSetup::PAPERSIZE_A4); PageSetup::setOrientationDefault(PageSetup::ORIENTATION_LANDSCAPE); $spreadsheet = new Spreadsheet(); $sheet1 = $spreadsheet->getActiveSheet(); $sheet2 = $spreadsheet->createSheet(); $sheet3 = $spreadsheet->createSheet(); self::assertSame(PageSetup::PAPERSIZE_A4, $sheet1->getPageSetup()->getPaperSize()); self::assertSame(PageSetup::PAPERSIZE_A4, $sheet2->getPageSetup()->getPaperSize()); self::assertSame(PageSetup::PAPERSIZE_A4, $sheet3->getPageSetup()->getPaperSize()); self::assertSame(PageSetup::ORIENTATION_LANDSCAPE, $sheet1->getPageSetup()->getOrientation()); self::assertSame(PageSetup::ORIENTATION_LANDSCAPE, $sheet2->getPageSetup()->getOrientation()); self::assertSame(PageSetup::ORIENTATION_LANDSCAPE, $sheet3->getPageSetup()->getOrientation()); $spreadsheet->disconnectWorksheets(); } public function testUnchangedDefault(): void { //PageSetup::setPaperSizeDefault(PageSetup::PAPERSIZE_A4); //PageSetup::setOrientationDefault(PageSetup::ORIENTATION_LANDSCAPE); $spreadsheet = new Spreadsheet(); $sheet1 = $spreadsheet->getActiveSheet(); $sheet2 = $spreadsheet->createSheet(); $sheet3 = $spreadsheet->createSheet(); self::assertSame(PageSetup::PAPERSIZE_LETTER, $sheet1->getPageSetup()->getPaperSize()); self::assertSame(PageSetup::PAPERSIZE_LETTER, $sheet2->getPageSetup()->getPaperSize()); self::assertSame(PageSetup::PAPERSIZE_LETTER, $sheet3->getPageSetup()->getPaperSize()); self::assertSame(PageSetup::ORIENTATION_DEFAULT, $sheet1->getPageSetup()->getOrientation()); self::assertSame(PageSetup::ORIENTATION_DEFAULT, $sheet2->getPageSetup()->getOrientation()); self::assertSame(PageSetup::ORIENTATION_DEFAULT, $sheet3->getPageSetup()->getOrientation()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/RowIteratorEmptyTest.php
tests/PhpSpreadsheetTests/Worksheet/RowIteratorEmptyTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator; use PhpOffice\PhpSpreadsheet\Worksheet\RowIterator; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class RowIteratorEmptyTest extends TestCase { private static function getPopulatedSheet(Spreadsheet $spreadsheet): Worksheet { $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValueExplicit('A1', 'Hello World', DataType::TYPE_STRING); $sheet->setCellValueExplicit('B3', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('B4', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('B5', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('C5', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('B6', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('C6', 'PHP', DataType::TYPE_STRING); $sheet->setCellValueExplicit('B7', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('C7', 'PHP', DataType::TYPE_STRING); $sheet->setCellValueExplicit('B8', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('C8', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('D8', 'PHP', DataType::TYPE_STRING); return $sheet; } #[DataProvider('emptyRowBasicProvider')] public function testIteratorEmptyRow(int $rowId, bool $expectedEmpty): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowIterator($sheet, 1, 9); $iterator->seek($rowId); $row = $iterator->current(); $isEmpty = $row->isEmpty(); self::assertSame($expectedEmpty, $isEmpty); $spreadsheet->disconnectWorksheets(); } public static function emptyRowBasicProvider(): array { return [ [1, false], [2, true], [3, false], [4, false], [5, false], [6, false], [7, false], [8, false], [9, true], ]; } #[DataProvider('emptyRowNullAsEmptyProvider')] public function testIteratorEmptyRowWithNull(int $rowId, bool $expectedEmpty): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowIterator($sheet, 1, 9); $iterator->seek($rowId); $row = $iterator->current(); $isEmpty = $row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL); self::assertSame($expectedEmpty, $isEmpty); $spreadsheet->disconnectWorksheets(); } public static function emptyRowNullAsEmptyProvider(): array { return [ [1, false], [2, true], [3, true], [4, false], [5, false], [6, false], [7, false], [8, false], [9, true], ]; } #[DataProvider('emptyRowEmptyStringAsEmptyProvider')] public function testIteratorEmptyRowWithEmptyString(int $rowId, bool $expectedEmpty): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowIterator($sheet, 1, 9); $iterator->seek($rowId); $row = $iterator->current(); $isEmpty = $row->isEmpty(CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL); self::assertSame($expectedEmpty, $isEmpty); $spreadsheet->disconnectWorksheets(); } public static function emptyRowEmptyStringAsEmptyProvider(): array { return [ [1, false], [2, true], [3, false], [4, true], [5, false], [6, false], [7, false], [8, false], [9, true], ]; } #[DataProvider('emptyRowNullAndEmptyStringAsEmptyProvider')] public function testIteratorEmptyRowWithNullAndEmptyString(int $rowId, bool $expectedEmpty): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowIterator($sheet, 1, 9); $iterator->seek($rowId); $row = $iterator->current(); $isEmpty = $row->isEmpty( CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL | CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL ); self::assertSame($expectedEmpty, $isEmpty); $spreadsheet->disconnectWorksheets(); } public static function emptyRowNullAndEmptyStringAsEmptyProvider(): array { return [ [1, false], [2, true], [3, true], [4, true], [5, true], [6, false], [7, false], [8, false], [9, true], ]; } public function testIteratorEmptyRowWithColumnLimit(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $sheet->setCellValue('E3', 'NO LONGER EMPTY'); $iterator = new RowIterator($sheet, 3, 3); $row = $iterator->current(); $isEmpty = $row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL); self::assertFalse($isEmpty); $isEmpty = $row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL, 'A', 'D'); self::assertTrue($isEmpty); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Issue1425Test.php
tests/PhpSpreadsheetTests/Worksheet/Issue1425Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class Issue1425Test extends TestCase { private static function setBackground(Worksheet $sheet, string $cells, string $color = 'ffffff00'): void { $sheet->getStyle($cells) ->getFill() ->setFillType(Fill::FILL_SOLID); $sheet->getStyle($cells) ->getFill() ->getStartColor() ->setArgb($color); } /** * Not extending styles to all cells after insertNewColumnBefore. */ public function testIssue4125(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Before'); $sheet->getCell('A1')->setValue(1); $sheet->getCell('A2')->setValue(2); $sheet->getCell('B1')->setValue(3); $sheet->getCell('B2')->setValue(4); self::setBackground($sheet, 'A1:A2'); self::setBackground($sheet, 'B1:B2', 'FF00FFFF'); $sheet->insertNewColumnBefore('B', 1); $expected = [ 'A1' => 'ffffff00', 'A2' => 'ffffff00', 'B1' => 'ffffff00', 'B2' => 'ffffff00', 'C1' => 'FF00FFFF', 'C2' => 'FF00FFFF', ]; foreach ($expected as $key => $value) { $color = $sheet->getStyle($key) ->getFill()->getStartColor()->getArgb(); self::assertSame($value, $color, "error in cell $key"); } $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Issue1203Test.php
tests/PhpSpreadsheetTests/Worksheet/Issue1203Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class Issue1203Test extends TestCase { public static function testCopyFormula(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 1); $sheet->setCellValue('A5', 5); $sheet->setCellValue('E5', '=A5+$A$1'); $sheet->insertNewRowBefore(5, 1); $e5 = $sheet->getCell('E5')->getValue(); self::assertNull($e5); self::assertSame('=A6+$A$1', $sheet->getCell('E6')->getValue()); $sheet->copyFormula('E6', 'E5'); self::assertSame('=A5+$A$1', $sheet->getCell('E5')->getValue()); $sheet->copyFormula('E6', 'H9'); self::assertSame('=D9+$A$1', $sheet->getCell('H9')->getValue()); $sheet->copyFormula('A6', 'Z9'); self::assertSame(5, $sheet->getCell('Z9')->getValue()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ProtectedRangeTest.php
tests/PhpSpreadsheetTests/Worksheet/ProtectedRangeTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class ProtectedRangeTest extends TestCase { public function testInsertRow1(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->protectCells('B2:D4 J2:L4 F2:H4', name: 'ProtectedBlock1'); $sheet->protectCells('M2:O4 Q7:R9 T1:T3'); $sheet->protectCells('B8 C9 D1', name: 'ProtectedBlock3'); $sheet->insertNewRowBefore(2); $ranges = $sheet->getProtectedCellRanges(); $rangeKeys = array_keys($ranges); self::assertSame( [ 'B3:D5 J3:L5 F3:H5', 'M3:O5 Q8:R10 T1:T4', 'B9 C10 D1', ], $rangeKeys ); self::assertSame('ProtectedBlock1', $ranges[$rangeKeys[0]]->getName()); self::assertSame('ProtectedBlock3', $ranges[$rangeKeys[2]]->getName()); $spreadsheet->disconnectWorksheets(); } public function testRemoveRow1(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->protectCells('B2:D4 J2:L4 F2:H4'); $sheet->protectCells('M2:O4 Q7:R9 T1:T3', name: 'ProtectedBlock2'); $sheet->protectCells('B8 C9 D1 E3'); $sheet->removeRow(3); $ranges = $sheet->getProtectedCellRanges(); $rangeKeys = array_keys($ranges); // PhpSpreadsheet has methods to merge cell addresses in a row, // but not in a column. So the results here are not as concise as // they might be, but they are nevertheless accurate. self::assertSame( [ 'B2:B3 C2:C3 D2:D3 F2:F3 G2:G3 H2:H3 J2:J3 K2:K3 L2:L3', 'M2:M3 N2:N3 O2:O3 Q6:Q8 R6:R8 T1:T2', 'B7 C8 D1', ], $rangeKeys ); self::assertSame('ProtectedBlock2', $ranges[$rangeKeys[1]]->getName()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ColumnRowStyleTest.php
tests/PhpSpreadsheetTests/Worksheet/ColumnRowStyleTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class ColumnRowStyleTest extends TestCase { public function testColumnStyle(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $columnStyle = $sheet->getStyle('B:C'); $columnStyle->applyFromArray([ 'font' => ['name' => 'Who knows'], ]); self::assertSame( 'Who knows', $sheet->getColumnStyle('B')?->getFont()->getName() ); self::assertSame( 'Who knows', $sheet->getColumnStyle('C')?->getFont()->getName() ); self::assertNull( $sheet->getColumnStyle('A')?->getFont()->getName() ); self::assertNull( $sheet->getColumnStyle('D')?->getFont()->getName() ); $spreadsheet->disconnectWorksheets(); } public function testRowStyle(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $rowStyle = $sheet->getStyle('2:3'); $rowStyle->applyFromArray([ 'font' => ['name' => 'Who knows'], ]); self::assertSame( 'Who knows', $sheet->getRowStyle(2)?->getFont()->getName() ); self::assertSame( 'Who knows', $sheet->getRowStyle(3)?->getFont()->getName() ); self::assertNull( $sheet->getRowStyle(1)?->getFont()->getName() ); self::assertNull( $sheet->getRowStyle(4)?->getFont()->getName() ); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ColumnCellIterator2Test.php
tests/PhpSpreadsheetTests/Worksheet/ColumnCellIterator2Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator; use PhpOffice\PhpSpreadsheet\Worksheet\ColumnCellIterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class ColumnCellIterator2Test extends TestCase { // Phpstan does not think RowCellIterator can return null private static function isCellNull(?Cell $item): bool { return $item === null; } #[DataProvider('providerExistingCell')] public function testEndRange(?bool $existing, string $expectedResultFirst, string $expectedResultLast): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('B2')->setValue('cellb2'); $sheet->getCell('B6')->setValue('cellb6'); $iterator = new ColumnCellIterator($sheet, 'B', 1, 8); if (isset($existing)) { $iterator->setIterateOnlyExistingCells($existing); } $lastCoordinate = ''; $firstCoordinate = ''; foreach ($iterator as $cell) { if (!self::isCellNull($cell)) { $lastCoordinate = $cell->getCoordinate(); if (!$firstCoordinate) { $firstCoordinate = $lastCoordinate; } } } self::assertSame($expectedResultFirst, $firstCoordinate); self::assertSame($expectedResultLast, $lastCoordinate); } public static function providerExistingCell(): array { return [ [null, 'B1', 'B8'], [false, 'B1', 'B8'], [true, 'B2', 'B6'], ]; } #[DataProvider('providerEmptyColumn')] public function testEmptyColumn(?bool $existing, int $expectedResult): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('B2')->setValue('cellb2'); $sheet->getCell('B6')->setValue('cellb6'); $iterator = new ColumnCellIterator($sheet, 'C'); if (isset($existing)) { $iterator->setIterateOnlyExistingCells($existing); } $numCells = 0; foreach ($iterator as $cell) { ++$numCells; } self::assertSame($expectedResult, $numCells); } public static function providerEmptyColumn(): array { return [ [null, 6], // Default behaviour [false, 6], [true, 0], ]; } #[DataProvider('providerNullOrCreate')] public function testNullOrCreateOption(?bool $existingBehaviour, int $expectedCreatedResult, mixed $expectedNullResult): void { if (!is_int($expectedNullResult)) { self::fail('unexpected unused arg'); } $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $iterator = new ColumnCellIterator($sheet, 'F'); if (isset($existingBehaviour)) { $iterator->setIfNotExists($existingBehaviour); } $notExistsBehaviour = $iterator->getIfNotExists(); self::assertSame($expectedCreatedResult > 0, $notExistsBehaviour); } #[DataProvider('providerNullOrCreate')] public function testNullOrCreate(?bool $existing, int $expectedCreatedResult, int $expectedNullResult): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('B2')->setValue('cellb2'); $sheet->getCell('F4')->setValue('cellf2'); $iterator = new ColumnCellIterator($sheet, 'F'); if (isset($existing)) { $iterator->setIfNotExists($existing); } $numCreatedCells = $numEmptyCells = 0; foreach ($iterator as $cell) { $numCreatedCells += (int) (!self::isCellNull($cell) && $cell->getValue() === null); $numEmptyCells += (int) (self::isCellNull($cell)); } self::assertSame($expectedCreatedResult, $numCreatedCells); self::assertSame($expectedNullResult, $numEmptyCells); } public static function providerNullOrCreate(): array { return [ [null, 3, 0], // Default behaviour [CellIterator::IF_NOT_EXISTS_CREATE_NEW, 3, 0], [CellIterator::IF_NOT_EXISTS_RETURN_NULL, 0, 3], ]; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ByColumnAndRowTest.php
tests/PhpSpreadsheetTests/Worksheet/ByColumnAndRowTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class ByColumnAndRowTest extends TestCase { public function testSetCellValueByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue([2, 2], 2); self::assertSame(2, $sheet->getCell('B2')->getValue()); $spreadsheet->disconnectWorksheets(); } public function testSetCellValueExplicitByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValueExplicit([2, 2], '="PHP Rules"', DataType::TYPE_STRING); self::assertSame('="PHP Rules"', $sheet->getCell('B2')->getValue()); self::assertSame(DataType::TYPE_STRING, $sheet->getCell('B2')->getDataType()); $spreadsheet->disconnectWorksheets(); } public function testCellExistsByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $cellExists = $sheet->cellExists([2, 2]); self::assertFalse($cellExists); $sheet->setCellValue('B2', 2); $cellExists = $sheet->cellExists([2, 2]); self::assertTrue($cellExists); $spreadsheet->disconnectWorksheets(); } public function testGetCellByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('B2', 2); $cell = $sheet->getCell([2, 2]); self::assertSame('B2', $cell->getCoordinate()); self::assertSame(2, $cell->getValue()); $spreadsheet->disconnectWorksheets(); } public function testGetStyleByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->getStyle('B2:C3')->getFont()->setBold(true); $rangeStyle = $sheet->getStyle([2, 2, 3, 3]); self::assertTrue($rangeStyle->getFont()->getBold()); $cellStyle = $sheet->getStyle([2, 2]); self::assertTrue($cellStyle->getFont()->getBold()); $spreadsheet->disconnectWorksheets(); } public function testSetBreakByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('B2', 2); $sheet->setBreak([2, 2], Worksheet::BREAK_COLUMN); $breaks = $sheet->getBreaks(); self::assertArrayHasKey('B2', $breaks); self::assertSame(Worksheet::BREAK_COLUMN, $breaks['B2']); $spreadsheet->disconnectWorksheets(); } public function testMergeCellsByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->mergeCells([2, 2, 3, 3]); $mergeRanges = $sheet->getMergeCells(); self::assertArrayHasKey('B2:C3', $mergeRanges); $spreadsheet->disconnectWorksheets(); } public function testUnergeCellsByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->mergeCells('B2:C3'); $mergeRanges = $sheet->getMergeCells(); self::assertArrayHasKey('B2:C3', $mergeRanges); $sheet->unmergeCells([2, 2, 3, 3]); $mergeRanges = $sheet->getMergeCells(); self::assertEmpty($mergeRanges); $spreadsheet->disconnectWorksheets(); } public function testProtectCellsByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->protectCells([2, 2, 3, 3], 'secret', false); $protectedRanges2 = $sheet->getProtectedCellRanges(); self::assertArrayHasKey('B2:C3', $protectedRanges2); $spreadsheet->disconnectWorksheets(); } public function testUnprotectCellsByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->protectCells('B2:C3', 'secret', false); $protectedRanges = $sheet->getProtectedCellRanges(); self::assertArrayHasKey('B2:C3', $protectedRanges); $sheet->unprotectCells([2, 2, 3, 3]); $protectedRanges = $sheet->getProtectedCellRanges(); self::assertEmpty($protectedRanges); $spreadsheet->disconnectWorksheets(); } public function testSetAutoFilterByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->setAutoFilter([2, 2, 3, 3]); $autoFilter = $sheet->getAutoFilter(); self::assertSame('B2:C3', $autoFilter->getRange()); $spreadsheet->disconnectWorksheets(); } public function testFreezePaneByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->freezePane([2, 2]); $freezePane = $sheet->getFreezePane(); self::assertSame('B2', $freezePane); $spreadsheet->disconnectWorksheets(); } public function testGetCommentByColumnAndRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('B2', 2); $spreadsheet->getActiveSheet() ->getComment('B2') ->getText()->createTextRun('My Test Comment'); $comment = $sheet->getComment([2, 2]); self::assertSame('My Test Comment', $comment->getText()->getPlainText()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Issue1457Test.php
tests/PhpSpreadsheetTests/Worksheet/Issue1457Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class Issue1457Test extends TestCase { public function testIssue1457(): void { $sheet = new Worksheet(); $sqref = 'C14:O15 C161:O1081 C16:H160 J16:O160 Q5'; $sheet->protectCells($sqref); $protectedRanges = $sheet->getProtectedCellRanges(); self::assertCount(1, $protectedRanges); $range0 = reset($protectedRanges); self::assertSame($sqref, $range0->getSqref()); $expected = [ ['C14', 'O15'], ['C161', 'O1081'], ['C16', 'H160'], ['J16', 'O160'], ['Q5'], ]; self::assertSame($expected, $range0->allRanges()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ColumnDimension2Test.php
tests/PhpSpreadsheetTests/Worksheet/ColumnDimension2Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Helper\Dimension; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Html; use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; class ColumnDimension2Test extends AbstractFunctional { #[\PHPUnit\Framework\Attributes\DataProvider('providerType')] public function testSetsAndDefaults(string $type): void { $columns = ['J', 'A', 'F', 'M', 'N', 'T', 'S']; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $expectedCm = 0.45; foreach ($columns as $column) { $sheet->getColumnDimension($column)->setWidth($expectedCm, 'cm'); } if ($type === 'Html') { $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $type, null, [self::class, 'inlineCss']); } else { $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $type); } $spreadsheet->disconnectWorksheets(); $sheet = $reloadedSpreadsheet->getActiveSheet(); for ($column = 'A'; $column !== 'Z'; StringHelper::stringIncrement($column)) { if (in_array($column, $columns, true)) { self::assertEqualsWithDelta($expectedCm, $sheet->getColumnDimension($column)->getWidth(Dimension::UOM_CENTIMETERS), 1E-3, "column $column"); } elseif ($type === 'Xls' && $column <= 'T') { // Xls is a bit weird. Columns through max used // actually set a width in the spreadsheet file. // Columns above max used obviously do not. self::assertEqualsWithDelta(9.140625, $sheet->getColumnDimension($column)->getWidth(), 1E-3, "column $column"); } elseif ($type === 'Html' && $column <= 'T') { // Html is a lot like Xls. Columns through max used // actually set a width in the spreadsheet file. // Columns above max used obviously do not. self::assertEqualsWithDelta(7.998, $sheet->getColumnDimension($column)->getWidth(), 1E-3, "column $column"); } else { self::assertSame(-1.0, $sheet->getColumnDimension($column)->getWidth(), "column $column"); } } $reloadedSpreadsheet->disconnectWorksheets(); } public static function providerType(): array { return [ ['Xlsx'], ['Xls'], ['Ods'], ['Html'], ]; } public static function inlineCss(Html $writer): void { $writer->setUseInlineCss(true); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/MergeBehaviourTest.php
tests/PhpSpreadsheetTests/Worksheet/MergeBehaviourTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; use PHPUnit\Framework\Attributes\DataProvider; class MergeBehaviourTest extends AbstractFunctional { private const TEST_DATA_RAW = [ [1.1, 2.2, '=ROUND(A1+B1, 1)'], [4.4, 5.5, '=ROUND(A2+B2, 1)'], ['=ROUND(A1+A2, 1)', '=ROUND(B1+B2, 1)', '=ROUND(A3+B3, 1)'], ]; private const TEST_DATE_FORMATTED = [ ['=DATE(1960, 12, 19)', '=DATE(2022, 09, 15)'], ]; public function testMergeCellsDefaultBehaviour(): void { $expectedResult = [ [1.1, null, null], [null, null, null], [null, null, null], ]; $mergeRange = 'A1:C3'; $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->fromArray(self::TEST_DATA_RAW, null, 'A1', true); $worksheet->mergeCells($mergeRange); $mergeResult = $worksheet->toArray(null, true, false, false); self::assertSame($expectedResult, $mergeResult); $spreadsheet->disconnectWorksheets(); } public function testMergeCellsDefaultBehaviourFormatted(): void { $expectedResult = [ ['1960-12-19', null], ]; $mergeRange = 'A1:B1'; $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->fromArray(self::TEST_DATE_FORMATTED, null, 'A1', true); $worksheet->getStyle($mergeRange)->getNumberFormat()->setFormatCode('yyyy-mm-dd'); $worksheet->mergeCells($mergeRange); $mergeResult = $worksheet->toArray(null, true, true, false); self::assertSame($expectedResult, $mergeResult); $spreadsheet->disconnectWorksheets(); } public function testMergeCellsHideBehaviour(): void { $expectedResult = [ [1.1, 2.2, 3.3], [4.4, 5.5, 9.9], [5.5, 7.7, 13.2], ]; $mergeRange = 'A1:C3'; $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->fromArray(self::TEST_DATA_RAW, null, 'A1', true); $worksheet->mergeCells($mergeRange, Worksheet::MERGE_CELL_CONTENT_HIDE); $mergeResult = $worksheet->toArray(null, true, false, false); self::assertSame($expectedResult, $mergeResult); $spreadsheet->disconnectWorksheets(); } public function testMergeCellsHideBehaviourFormatted(): void { $expectedResult = [ ['1960-12-19', '2022-09-15'], ]; $mergeRange = 'A1:B1'; $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->fromArray(self::TEST_DATE_FORMATTED, null, 'A1', true); $worksheet->getStyle($mergeRange)->getNumberFormat()->setFormatCode('yyyy-mm-dd'); $worksheet->mergeCells($mergeRange, Worksheet::MERGE_CELL_CONTENT_HIDE); $mergeResult = $worksheet->toArray(null, true, true, false); self::assertSame($expectedResult, $mergeResult); $spreadsheet->disconnectWorksheets(); } /** * @param mixed[] $testData * @param mixed[] $expectedResult */ #[DataProvider('mergeCellsMergeBehaviourProvider')] public function testMergeCellsMergeBehaviour(array $testData, string $mergeRange, array $expectedResult): void { $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->fromArray($testData, null, 'A1', true); // Force a precalculation to populate the calculation cache, so that we can verify that it is being cleared $worksheet->toArray(); $worksheet->mergeCells($mergeRange, Worksheet::MERGE_CELL_CONTENT_MERGE); $mergeResult = $worksheet->toArray(null, true, true, false); self::assertSame($expectedResult, $mergeResult); $spreadsheet->disconnectWorksheets(); } public static function mergeCellsMergeBehaviourProvider(): array { return [ 'With Calculated Values' => [ self::TEST_DATA_RAW, 'A1:C3', [ ['1.1 2.2 1.1 4.4 5.5 0 1.1 0 0', null, null], [null, null, null], [null, null, null], ], ], 'With Empty Cells' => [ [ [1, '', 2], [null, 3, null], [4, null, 5], ], 'A1:C3', [ ['1 2 3 4 5', null, null], [null, null, null], [null, null, null], ], ], 'A2 referenced and therefore created' => [ [ [12, '=5+1', '=A1/A2'], ], 'A1:C1', [ ['12 6 #DIV/0!', null, null], [null, null, null], ], ], ]; } public function testMergeCellsMergeBehaviourFormatted(): void { $expectedResult = [ ['1960-12-19 2022-09-15', null], ]; $mergeRange = 'A1:B1'; $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->fromArray(self::TEST_DATE_FORMATTED, null, 'A1', true); $worksheet->getStyle($mergeRange)->getNumberFormat()->setFormatCode('yyyy-mm-dd'); $worksheet->mergeCells($mergeRange, Worksheet::MERGE_CELL_CONTENT_MERGE); $mergeResult = $worksheet->toArray(null, true, true, false); self::assertSame($expectedResult, $mergeResult); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ColumnDimensionTest.php
tests/PhpSpreadsheetTests/Worksheet/ColumnDimensionTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Helper\Dimension; use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension; use PHPUnit\Framework\TestCase; class ColumnDimensionTest extends TestCase { public function testInstantiateColumnDimensionDefault(): void { $expected = 'A'; $columnDimension = new ColumnDimension(); $result = $columnDimension->getColumnIndex(); self::assertEquals($expected, $result); } public function testGetAndSetColumnIndex(): void { $expected = 'B'; $columnDimension = new ColumnDimension(); $columnDimension->setColumnIndex($expected); $result = $columnDimension->getColumnIndex(); self::assertSame($expected, $result); } public function testGetAndSetWidth(): void { $expected = 1.2; $columnDimension = new ColumnDimension(); $columnDimension->setWidth($expected); $result = $columnDimension->getWidth(); self::assertSame($expected, $result); $expectedPx = 32.0; $expectedPt = 24.0; $columnDimension->setWidth($expectedPx, Dimension::UOM_PIXELS); $resultPx = $columnDimension->getWidth(Dimension::UOM_PIXELS); self::assertSame($expectedPx, $resultPx); $resultPt = $columnDimension->getWidth(Dimension::UOM_POINTS); self::assertSame($expectedPt, $resultPt); } public function testGetAndSetAutoSize(): void { $expected = true; $columnDimension = new ColumnDimension(); $columnDimension->setAutoSize($expected); $result = $columnDimension->getAutoSize(); self::assertTrue($result); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/RowDimensionTest.php
tests/PhpSpreadsheetTests/Worksheet/RowDimensionTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Helper\Dimension; use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension; use PHPUnit\Framework\TestCase; class RowDimensionTest extends TestCase { public function testInstantiateColumnDimensionDefault(): void { $expected = 0; $rowDimension = new RowDimension(); $result = $rowDimension->getRowIndex(); self::assertEquals($expected, $result); } public function testGetAndSetColumnIndex(): void { $expected = 2; $rowDimension = new RowDimension(); $rowDimension->setRowIndex($expected); $result = $rowDimension->getRowIndex(); self::assertSame($expected, $result); } public function testGetAndSetHeight(): void { $expected = 1.2; $columnDimension = new RowDimension(); $columnDimension->setRowHeight($expected); $result = $columnDimension->getRowHeight(); self::assertSame($expected, $result); $expectedPx = 32.0; $expectedPt = 24.0; $columnDimension->setRowHeight($expectedPx, Dimension::UOM_PIXELS); $resultPx = $columnDimension->getRowHeight(Dimension::UOM_PIXELS); self::assertSame($expectedPx, $resultPx); $resultPt = $columnDimension->getRowHeight(Dimension::UOM_POINTS); self::assertSame($expectedPt, $resultPt); } public function testRowZeroHeight(): void { $expected = true; $rowDimension = new RowDimension(); $rowDimension->setZeroHeight($expected); $result = $rowDimension->getZeroHeight(); self::assertTrue($result); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ColumnDimension3Test.php
tests/PhpSpreadsheetTests/Worksheet/ColumnDimension3Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter; use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; class ColumnDimension3Test extends AbstractFunctional { public function testXlsxDefault(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('B1')->setValue('hello'); $sheet->getCell('C1')->setValue(2); $sheet->getCell('A1')->setValue('=REPT("ABCDEFGHIJKLMNOPQRS*",13)'); $sheet->getStyle('A1')->getFont() ->setSize(11) ->setName('Courier New'); $sheet->getColumnDimension('A')->setAutoSize(true); $sheet->getColumnDimension('B')->setAutoSize(true); $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx'); $spreadsheet->disconnectWorksheets(); $sheet = $reloadedSpreadsheet->getActiveSheet(); self::assertGreaterThan(255.0, $sheet->getColumnDimension('A')->getWidth()); self::assertLessThan(255.0, $sheet->getColumnDimension('B')->getWidth()); self::assertSame(-1.0, $sheet->getColumnDimension('C')->getWidth()); $reloadedSpreadsheet->disconnectWorksheets(); } public function testXlsxRestrict(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('B1')->setValue('hello'); $sheet->getCell('C1')->setValue(2); $sheet->getCell('A1')->setValue('=REPT("ABCDEFGHIJKLMNOPQRS*",13)'); $sheet->getStyle('A1')->getFont() ->setSize(11) ->setName('Courier New'); $sheet->getColumnDimension('A')->setAutoSize(true); $sheet->getColumnDimension('B')->setAutoSize(true); $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', null, [self::class, 'restrictWidth']); $spreadsheet->disconnectWorksheets(); $sheet = $reloadedSpreadsheet->getActiveSheet(); self::assertSame(255.0, $sheet->getColumnDimension('A')->getWidth()); self::assertLessThan(255.0, $sheet->getColumnDimension('B')->getWidth()); self::assertSame(-1.0, $sheet->getColumnDimension('C')->getWidth()); $reloadedSpreadsheet->disconnectWorksheets(); } public function testXls(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('B1')->setValue('hello'); $sheet->getCell('C1')->setValue(2); $sheet->getCell('A1')->setValue('=REPT("ABCDEFGHIJKLMNOPQRS*",13)'); $sheet->getStyle('A1')->getFont() ->setSize(11) ->setName('Courier New'); $sheet->getColumnDimension('A')->setAutoSize(true); $sheet->getColumnDimension('B')->setAutoSize(true); $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xls'); $spreadsheet->disconnectWorksheets(); $sheet = $reloadedSpreadsheet->getActiveSheet(); self::assertSame(255.0, $sheet->getColumnDimension('A')->getWidth()); self::assertLessThan(255.0, $sheet->getColumnDimension('B')->getWidth()); self::assertLessThan(255.0, $sheet->getColumnDimension('C')->getWidth()); $reloadedSpreadsheet->disconnectWorksheets(); } public static function restrictWidth(XlsxWriter $writer): void { $writer->setRestrictMaxColumnWidth(true); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/PrintAreaTest.php
tests/PhpSpreadsheetTests/Worksheet/PrintAreaTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class PrintAreaTest extends TestCase { #[DataProvider('removeRowsProvider')] public function testRemoveRows(string $expected, int $rowNumber, int $numberOfRows): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getPageSetup()->setPrintArea('B5:M41'); $sheet->removeRow($rowNumber, $numberOfRows); self::assertSame($expected, $sheet->getPageSetup()->getPrintArea()); $spreadsheet->disconnectWorksheets(); } public static function removeRowsProvider(): array { return [ 'before beginning of printArea' => ['B3:M39', 3, 2], 'creep into printArea' => ['B3:M37', 3, 4], 'entirely within printArea' => ['B5:M36', 6, 5], 'creep past end of printArea' => ['B5:M34', 35, 8], 'after end of printArea' => ['B5:M41', 55, 8], 'entire printArea' => ['', 5, 37], 'entire printArea+' => ['', 4, 47], ]; } #[DataProvider('removeColumnsProvider')] public function testRemoveColumns(string $expected, string $column, int $numberOfColumns): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getPageSetup()->setPrintArea('D5:M41'); $sheet->removeColumn($column, $numberOfColumns); self::assertSame($expected, $sheet->getPageSetup()->getPrintArea()); $spreadsheet->disconnectWorksheets(); } public static function removeColumnsProvider(): array { return [ 'before beginning of printArea' => ['B5:K41', 'B', 2], 'creep into printArea' => ['B5:I41', 'B', 4], 'entirely within printArea' => ['D5:I41', 'E', 4], 'creep past end of printArea' => ['D5:K41', 'L', 3], 'after end of printArea' => ['D5:M41', 'P', 8], 'entire printArea' => ['', 'D', 10], 'entire printArea+' => ['', 'C', 15], ]; } #[DataProvider('addRowsProvider')] public function testAddRows(string $expected, int $rowNumber, int $numberOfRows): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getPageSetup()->setPrintArea('D5:M41'); $sheet->insertNewRowBefore($rowNumber, $numberOfRows); self::assertSame($expected, $sheet->getPageSetup()->getPrintArea()); $spreadsheet->disconnectWorksheets(); } public static function addRowsProvider(): array { return [ 'entirely within printArea' => ['D5:M44', 15, 3], 'above printArea' => ['D9:M45', 3, 4], 'below printArea' => ['D5:M41', 48, 4], ]; } #[DataProvider('addColumnsProvider')] public function testAddColumns(string $expected, string $column, int $numberOfColumns): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getPageSetup()->setPrintArea('D5:M41'); $sheet->insertNewColumnBefore($column, $numberOfColumns); self::assertSame($expected, $sheet->getPageSetup()->getPrintArea()); $spreadsheet->disconnectWorksheets(); } public static function addColumnsProvider(): array { return [ 'entirely within printArea' => ['D5:P41', 'J', 3], 'left of printArea' => ['H5:Q41', 'C', 4], 'right of printArea' => ['D5:M41', 'Q', 4], ]; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Issue4241Test.php
tests/PhpSpreadsheetTests/Worksheet/Issue4241Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; use PHPUnit\Framework\TestCase; class Issue4241Test extends TestCase { public function testIssue4241(): void { // setWorksheet needed to come after setPath $badPath = 'tests/data/Writer/XLSX/xgreen_square.gif'; $goodPath = 'tests/data/Writer/XLSX/green_square.gif'; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $drawing = new Drawing(); $drawing->setName('Green Square'); $drawing->setWorksheet($sheet); $drawings = $sheet->getDrawingCollection(); self::assertCount(1, $drawings); $drawing0 = $drawings[0]; self::assertInstanceOf(Drawing::class, $drawing0); self::assertSame('', $drawing0->getPath()); self::assertSame('A1', $drawing0->getCoordinates()); $maxRow = $sheet->getHighestDataRow(); $maxCol = $sheet->getHighestDataColumn(); self::assertSame(1, $maxRow); self::assertSame('A', $maxCol); $drawing->setCoordinates('E5'); $drawings = $sheet->getDrawingCollection(); self::assertCount(1, $drawings); $drawing0 = $drawings[0]; self::assertInstanceOf(Drawing::class, $drawing0); self::assertSame('', $drawing0->getPath()); self::assertSame('E5', $drawing0->getCoordinates()); $maxRow = $sheet->getHighestDataRow(); $maxCol = $sheet->getHighestDataColumn(); self::assertSame(1, $maxRow); self::assertSame('A', $maxCol); $drawing->setPath($badPath, false); $drawings = $sheet->getDrawingCollection(); self::assertCount(1, $drawings); $drawing0 = $drawings[0]; self::assertInstanceOf(Drawing::class, $drawing0); self::assertSame('', $drawing0->getPath()); self::assertSame('E5', $drawing0->getCoordinates()); $maxRow = $sheet->getHighestDataRow(); $maxCol = $sheet->getHighestDataColumn(); self::assertSame(1, $maxRow); self::assertSame('A', $maxCol); $drawing->setPath($goodPath); $drawings = $sheet->getDrawingCollection(); self::assertCount(1, $drawings); $drawing0 = $drawings[0]; self::assertInstanceOf(Drawing::class, $drawing0); self::assertSame($goodPath, $drawing0->getPath()); self::assertSame('E5', $drawing0->getCoordinates()); $maxRow = $sheet->getHighestDataRow(); $maxCol = $sheet->getHighestDataColumn(); self::assertSame(5, $maxRow); self::assertSame('E', $maxCol); $drawing->setCoordinates('G3'); $drawings = $sheet->getDrawingCollection(); self::assertCount(1, $drawings); $drawing0 = $drawings[0]; self::assertInstanceOf(Drawing::class, $drawing0); self::assertSame($goodPath, $drawing0->getPath()); self::assertSame('G3', $drawing0->getCoordinates()); $maxRow = $sheet->getHighestDataRow(); $maxCol = $sheet->getHighestDataColumn(); self::assertSame(5, $maxRow); self::assertSame('G', $maxCol); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/RowDimensionSaveTest.php
tests/PhpSpreadsheetTests/Worksheet/RowDimensionSaveTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf; use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; class RowDimensionSaveTest extends AbstractFunctional { #[\PHPUnit\Framework\Attributes\DataProvider('typeProvider')] public function testSaveNoAllocateRowDimension(string $type): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('A1')->setValue(1); $sheet->getCell('A2')->setValue(2); $sheet->getCell('A3')->setValue(3); $sheet->getRowDimension(2)->setVisible(false); $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $type); self::assertFalse($sheet->rowDimensionExists(1)); self::assertTrue($sheet->rowDimensionExists(2)); self::assertFalse($sheet->rowDimensionExists(3)); $spreadsheet->disconnectWorksheets(); $reloadedSpreadsheet->disconnectWorksheets(); } public static function typeProvider(): array { return [ ['Xlsx'], ['Xls'], ['Ods'], ['Html'], ]; } public function testSaveNoAllocateRowDimensionMpdf(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('A1')->setValue(1); $sheet->getCell('A2')->setValue(2); $sheet->getCell('A3')->setValue(3); $sheet->getRowDimension(2)->setVisible(false); $writer = new Mpdf($spreadsheet); $writer->generateHtmlAll(); self::assertFalse($sheet->rowDimensionExists(1)); self::assertTrue($sheet->rowDimensionExists(2)); self::assertFalse($sheet->rowDimensionExists(3)); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/PageMarginsTest.php
tests/PhpSpreadsheetTests/Worksheet/PageMarginsTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Worksheet\PageMargins; use PHPUnit\Framework\TestCase; class PageMarginsTest extends TestCase { #[\PHPUnit\Framework\Attributes\DataProvider('providerPointsAndInches')] public function testPointsToInches(float $value, float $expectedResult): void { $actualResult = PageMargins::fromPoints($value); self::assertSame($expectedResult, $actualResult); } #[\PHPUnit\Framework\Attributes\DataProvider('providerPointsAndInches')] public function testInchesToPoints(float $expectedResult, float $value): void { $actualResult = PageMargins::toPoints($value); self::assertSame($expectedResult, $actualResult); } #[\PHPUnit\Framework\Attributes\DataProvider('providerCentimetersAndInches')] public function testCentimetersToInches(float $value, float $expectedResult): void { $actualResult = PageMargins::fromCentimeters($value); self::assertSame($expectedResult, $actualResult); } #[\PHPUnit\Framework\Attributes\DataProvider('providerCentimetersAndInches')] public function testPointsToCentimeters(float $expectedResult, float $value): void { $actualResult = PageMargins::toCentimeters($value); self::assertSame($expectedResult, $actualResult); } #[\PHPUnit\Framework\Attributes\DataProvider('providerMillimetersAndInches')] public function testMillimetersToInches(float $value, float $expectedResult): void { $actualResult = PageMargins::fromMillimeters($value); self::assertSame($expectedResult, $actualResult); } #[\PHPUnit\Framework\Attributes\DataProvider('providerMillimetersAndInches')] public function testPointsToMillimeters(float $expectedResult, float $value): void { $actualResult = PageMargins::toMillimeters($value); self::assertSame($expectedResult, $actualResult); } public static function providerPointsAndInches(): array { return [ [36, 0.5], [72, 1.0], [90, 1.25], [144, 2.0], ]; } public static function providerCentimetersAndInches(): array { return [ [1.27, 0.5], [2.54, 1.0], ]; } public static function providerMillimetersAndInches(): array { return [ [12.7, 0.5], [25.4, 1.0], ]; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/MergeCellsDeletedTest.php
tests/PhpSpreadsheetTests/Worksheet/MergeCellsDeletedTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class MergeCellsDeletedTest extends TestCase { public function testDeletedColumns(): void { $infile = 'tests/data/Reader/XLSX/issue.282.xlsx'; $reader = new XlsxReader(); $spreadsheet = $reader->load($infile); $sheet = $spreadsheet->getSheetByNameOrThrow('Sheet1'); $mergeCells = $sheet->getMergeCells(); self::assertSame(['B1:F1', 'G1:I1'], array_values($mergeCells)); // Want to delete column B,C,D,E,F $sheet->removeColumnByIndex(2, 5); $mergeCells2 = $sheet->getMergeCells(); self::assertSame(['B1:D1'], array_values($mergeCells2)); $spreadsheet->disconnectWorksheets(); } public function testDeletedRows(): void { $infile = 'tests/data/Reader/XLSX/issue.282.xlsx'; $reader = new XlsxReader(); $spreadsheet = $reader->load($infile); $sheet = $spreadsheet->getSheetByNameOrThrow('Sheet2'); $mergeCells = $sheet->getMergeCells(); self::assertSame(['A2:A6', 'A7:A9'], array_values($mergeCells)); // Want to delete rows 2 to 4 $sheet->removeRow(2, 3); $mergeCells2 = $sheet->getMergeCells(); self::assertSame(['A4:A6'], array_values($mergeCells2)); $spreadsheet->disconnectWorksheets(); } private static function yellowBackground(Worksheet $sheet, string $cells, string $color = 'ffffff00'): void { $sheet->getStyle($cells) ->getFill() ->setFillType(Fill::FILL_SOLID); $sheet->getStyle($cells) ->getFill() ->getStartColor() ->setArgb($color); $sheet->getStyle($cells) ->getAlignment() ->setHorizontal(Alignment::HORIZONTAL_CENTER); } public static function testDeletedColumns2(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Before'); $sheet->getCell('A1')->setValue('a1'); $sheet->getCell('J1')->setValue('j1'); $sheet->getCell('K1')->setValue('will delete d-f'); $sheet->getCell('C1')->setValue('c1-g1'); $sheet->mergeCells('C1:G1'); self::yellowBackground($sheet, 'C1'); $sheet->getCell('A2')->setValue('a2'); $sheet->getCell('J2')->setValue('j2'); $sheet->getCell('B2')->setValue('b2-c2'); $sheet->mergeCells('B2:C2'); self::yellowBackground($sheet, 'B2'); $sheet->getCell('G2')->setValue('g2-h2'); $sheet->mergeCells('G2:H2'); self::yellowBackground($sheet, 'G2', 'FF00FFFF'); $sheet->getCell('A3')->setValue('a3'); $sheet->getCell('J3')->setValue('j3'); $sheet->getCell('D3')->setValue('d3-g3'); $sheet->mergeCells('D3:G3'); self::yellowBackground($sheet, 'D3'); $sheet->getCell('A4')->setValue('a4'); $sheet->getCell('J4')->setValue('j4'); $sheet->getCell('B4')->setValue('b4-d4'); $sheet->mergeCells('B4:D4'); self::yellowBackground($sheet, 'B4'); $sheet->getCell('A5')->setValue('a5'); $sheet->getCell('J5')->setValue('j5'); $sheet->getCell('D5')->setValue('d5-e5'); $sheet->mergeCells('D5:E5'); self::yellowBackground($sheet, 'D5'); $sheet->removeColumn('D', 3); $expected = [ 'C1:D1', // was C1:G1, drop 3 inside cells 'B2:C2', // was B2:C2, unaffected 'D2:E2', // was G2:H2, move 3 columns left //'D2:E2', // was D3:G3, start in delete range 'B4:C4', // was B4:D4, truncated at start of delete range //'D5:E5', // was D5:E5, start in delete range ]; self::assertSame($expected, array_keys($sheet->getMergeCells())); $spreadsheet->disconnectWorksheets(); } public static function testDeletedRows2(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Before'); $sheet->getCell('A1')->setValue('a1'); $sheet->getCell('A10')->setValue('a10'); $sheet->getCell('A11')->setValue('will delete 4-6'); $sheet->getCell('A3')->setValue('a3-a7'); $sheet->mergeCells('A3:A7'); self::yellowBackground($sheet, 'A3'); $sheet->getCell('B1')->setValue('b1'); $sheet->getCell('B10')->setValue('b10'); $sheet->getCell('B2')->setValue('b2-b3'); $sheet->mergeCells('B2:B3'); self::yellowBackground($sheet, 'B2'); $sheet->getCell('B7')->setValue('b7-b8'); $sheet->mergeCells('B7:B8'); self::yellowBackground($sheet, 'B7', 'FF00FFFF'); $sheet->getCell('C1')->setValue('c1'); $sheet->getCell('C10')->setValue('c10'); $sheet->getCell('C4')->setValue('c4-c7'); $sheet->mergeCells('C4:C7'); self::yellowBackground($sheet, 'C4'); $sheet->getCell('D1')->setValue('d1'); $sheet->getCell('D10')->setValue('d10'); $sheet->getCell('D2')->setValue('d2-d4'); $sheet->mergeCells('D2:D4'); self::yellowBackground($sheet, 'd2'); $sheet->getCell('E1')->setValue('e1'); $sheet->getCell('E10')->setValue('e10'); $sheet->getCell('E4')->setValue('e4-e5'); $sheet->mergeCells('E4:E5'); self::yellowBackground($sheet, 'E4'); $sheet->removeRow(4, 3); $expected = [ 'A3:A4', // was A3:A7, drop 3 inside cells 'B2:B3', // was B2:B3, unaffected 'B4:B5', // was B7:B8, move 3 columns up //'C4:C7', // was C4:C7, start in delete range 'D2:D3', // was D2:D4, truncated at start of delete range //'E4:E5', // was E4:E5, start in delete range ]; self::assertSame($expected, array_keys($sheet->getMergeCells())); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/IteratorTest.php
tests/PhpSpreadsheetTests/Worksheet/IteratorTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Iterator; use PHPUnit\Framework\TestCase; class IteratorTest extends TestCase { public function testIteratorFullRange(): void { $spreadsheet = new Spreadsheet(); $spreadsheet->createSheet(); $spreadsheet->createSheet(); $iterator = new Iterator($spreadsheet); $columnIndexResult = 0; self::assertEquals($columnIndexResult, $iterator->key()); foreach ($iterator as $key => $column) { self::assertEquals($columnIndexResult++, $key); } self::assertSame(3, $columnIndexResult); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/SetValueExplicitWorksheetTest.php
tests/PhpSpreadsheetTests/Worksheet/SetValueExplicitWorksheetTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use ArgumentCountError; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use ReflectionMethod; class SetValueExplicitWorksheetTest extends TestCase { protected string $method = 'setCellValueExplicit'; protected int $requiredParameters = 3; public function testRequired(): void { $reflectionMethod = new ReflectionMethod(Worksheet::class, $this->method); $requiredParameters = $reflectionMethod->getNumberOfRequiredParameters(); self::assertSame($this->requiredParameters, $requiredParameters); } public static function setCellValueExplicitTypeArgumentProvider(): array { return require 'tests/data/Cell/SetValueExplicitTypeArguments.php'; } #[DataProvider('setCellValueExplicitTypeArgumentProvider')] public function testSetCellValueExplicitTypeArgumentHandling( mixed $value, ?string $dataType, mixed $expectedValue, string $expectedDataType ): void { $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $coordinate = 'A1'; $cell = $worksheet->getCell($coordinate); try { if ($dataType) { $worksheet->{$this->method}($coordinate, $value, $dataType); } else { $worksheet->{$this->method}($coordinate, $value); self::assertSame(2, $this->requiredParameters); } self::assertSame($expectedValue, $cell->getValue()); self::assertSame($expectedDataType, $cell->getDataType()); } catch (ArgumentCountError) { self::assertSame(3, $this->requiredParameters); self::assertNull($dataType); } $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ConditionalIntersectionTest.php
tests/PhpSpreadsheetTests/Worksheet/ConditionalIntersectionTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Conditional; use PHPUnit\Framework\TestCase; class ConditionalIntersectionTest extends TestCase { public function testGetConditionalStyles(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->fromArray([ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], ]); $condition1 = new Conditional(); $condition1->setConditionType(Conditional::CONDITION_CELLIS); $condition1->setOperatorType(Conditional::OPERATOR_BETWEEN); $condition1->setConditions([2, 3]); $condition1->getStyle()->getFont() ->setBold(true); $conditionalStyles = [$condition1]; $sheet->setConditionalStyles('A1:C3 B1:B3', $conditionalStyles); self::assertEmpty($sheet->getConditionalStyles('A2')); $cond = $sheet->getConditionalStyles('B2'); self::assertCount(1, $cond); self::assertSame(Conditional::CONDITION_CELLIS, $cond[0]->getConditionType()); self::assertSame(Conditional::OPERATOR_BETWEEN, $cond[0]->getOperatorType()); self::assertSame([2, 3], $cond[0]->getConditions()); self::assertTrue($cond[0]->getStyle()->getFont()->getBold()); $spreadsheet->disconnectWorksheets(); } public function testGetConditionalRange(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->fromArray([ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], ]); $condition1 = new Conditional(); $condition1->setConditionType(Conditional::CONDITION_CELLIS); $condition1->setOperatorType(Conditional::OPERATOR_BETWEEN); $condition1->setConditions([2, 3]); $condition1->getStyle()->getFont() ->setBold(true); $conditionalStyles = [$condition1]; $sheet->setConditionalStyles('A1:C3 B1:B3', $conditionalStyles); self::assertNull($sheet->getConditionalRange('A2')); self::assertSame('A1:C3 B1:B3', $sheet->getConditionalRange('B2')); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Worksheet2Test.php
tests/PhpSpreadsheetTests/Worksheet/Worksheet2Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class Worksheet2Test extends TestCase { public function testMiscellaneous(): void { $invalid = Worksheet::getInvalidCharacters(); self::assertSame(['*', ':', '/', '\\', '?', '[', ']'], $invalid); $worksheet = new Worksheet(); $coord1 = $worksheet->getCoordinates(); self::assertSame([], $coord1); $worksheet->getCell('B3')->setValue(1); $worksheet->getCell('G2')->setValue(2); $coord2 = $worksheet->getCoordinates(false); // in order added self::assertSame(['B3', 'G2'], $coord2); $coord3 = $worksheet->getCoordinates(); // sorted by row then column self::assertSame(['G2', 'B3'], $coord3); $worksheet = new Worksheet(); $worksheet->disconnectCells(); $coord4 = $worksheet->getCoordinates(); self::assertSame([], $coord4); } public function testHighestColumn(): void { $worksheet = new Worksheet(); $worksheet->getCell('A1')->setValue(1); $worksheet->getCell('B1')->setValue(2); $worksheet->getCell('A2')->setValue(3); self::assertSame('B', $worksheet->getHighestColumn(1)); self::assertSame('A', $worksheet->getHighestColumn(2)); } public function testHighestRow(): void { $worksheet = new Worksheet(); $worksheet->getCell('A1')->setValue(1); $worksheet->getCell('B1')->setValue(2); $worksheet->getCell('B2')->setValue(3); self::assertSame(1, $worksheet->getHighestRow('A')); self::assertSame(2, $worksheet->getHighestRow('B')); self::assertSame(['row' => 2, 'column' => 'B'], $worksheet->getHighestRowAndColumn()); } public function testUnmergeNonRange(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Merge can only be removed from a range'); $worksheet = new Worksheet(); $worksheet->unmergeCells('A1'); } public function testUnprotectNotProtected(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Cell range A1:B2 not known as protected'); $worksheet = new Worksheet(); $worksheet->unprotectCells('A1:B2'); } public function testFreezeRange(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Freeze pane can not be set on a range'); $worksheet = new Worksheet(); $worksheet->freezePane('A1:B2'); } private function getPane(Worksheet $sheet): ?string { return $sheet->getFreezePane(); } public function testFreeze(): void { $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $pane = $worksheet->getActivePane(); self::assertEmpty($pane); $worksheet->setSelectedCells('D3'); $worksheet->freezePane('A2'); $freeze = $this->getPane($worksheet); $pane = $worksheet->getActivePane(); $selected = $worksheet->getSelectedCells(); self::assertSame('A2', $freeze); self::assertSame('D3', $selected); self::assertSame('bottomLeft', $pane); $worksheet->unfreezePane(); $freeze = $this->getPane($worksheet); self::assertNull($freeze); $pane = $worksheet->getActivePane(); $selected = $worksheet->getSelectedCells(); self::assertEquals('', $pane); self::assertSame('D3', $selected); $spreadsheet->disconnectWorksheets(); } public function testFreezeA1(): void { $worksheet = new Worksheet(); $worksheet->freezePane('A1'); $freeze = $this->getPane($worksheet); self::assertNull($freeze); } public function testInsertBeforeRowOne(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Rows can only be inserted before at least row 1'); $worksheet = new Worksheet(); $worksheet->insertNewRowBefore(0); } public function testRemoveBeforeRowOne(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Rows to be deleted should at least start from row 1'); $worksheet = new Worksheet(); $worksheet->removeRow(0); } public function testInsertNumericColumn(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Column references should not be numeric'); $worksheet = new Worksheet(); $worksheet->insertNewColumnBefore('0'); } public function testRemoveNumericColumn(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Column references should not be numeric'); $worksheet = new Worksheet(); $worksheet->removeColumn('0'); } public function testInsertColumnByIndexBeforeOne(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Columns can only be inserted before at least column A (1)'); $worksheet = new Worksheet(); $worksheet->insertNewColumnBeforeByIndex(0); } public function testRemoveColumnByIndexBeforeOne(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Columns to be deleted should at least start from column A (1)'); $worksheet = new Worksheet(); $worksheet->removeColumnByIndex(0); } public function testInsertColumnByIndex(): void { $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->getCell('A1')->setValue(10); $worksheet->insertNewColumnBeforeByIndex(1); self::assertSame(10, $worksheet->getCell('B1')->getValue()); self::assertNull($worksheet->getCell('A1')->getValue()); $spreadsheet->disconnectWorksheets(); } public function testRemoveColumnByIndex(): void { $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->getCell('B1')->setValue(10); $worksheet->removeColumnByIndex(1); self::assertSame(10, $worksheet->getCell('A1')->getValue()); self::assertNull($worksheet->getCell('B1')->getValue()); $spreadsheet->disconnectWorksheets(); } public function testRemoveCommentInvalid1(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Cell coordinate string can not be a range'); $worksheet = new Worksheet(); $worksheet->removeComment('A1:B2'); } public function testRemoveCommentInvalid2(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Cell coordinate string must not be absolute'); $worksheet = new Worksheet(); $worksheet->removeComment('$A$1'); } public function testRemoveCommentInvalid3(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Cell coordinate can not be zero-length string'); $worksheet = new Worksheet(); $worksheet->removeComment(''); } public function testGetCommentInvalid1(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Cell coordinate string can not be a range'); $worksheet = new Worksheet(); $worksheet->getComment('A1:B2'); } public function testGetCommentInvalid2(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Cell coordinate string must not be absolute'); $worksheet = new Worksheet(); $worksheet->getComment('$A$1'); } public function testGetCommentInvalid3(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('Cell coordinate can not be zero-length string'); $worksheet = new Worksheet(); $worksheet->getComment(''); } public function testResetTabColor(): void { $worksheet = new Worksheet(); self::assertSame('FF000000', $worksheet->getTabColor()->getArgb()); $worksheet->getTabColor()->setArgb('FF800000'); self::assertSame('FF800000', $worksheet->getTabColor()->getArgb()); $worksheet->resetTabColor(); self::assertSame('FF000000', $worksheet->getTabColor()->getArgb()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ByColumnAndRowUndeprecatedTest.php
tests/PhpSpreadsheetTests/Worksheet/ByColumnAndRowUndeprecatedTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class ByColumnAndRowUndeprecatedTest extends TestCase { private ?Spreadsheet $spreadsheet = null; protected function tearDown(): void { if ($this->spreadsheet !== null) { $this->spreadsheet->disconnectWorksheets(); $this->spreadsheet = null; } } private function getSpreadsheet(): Spreadsheet { $this->spreadsheet = new Spreadsheet(); return $this->spreadsheet; } public function testSetCellValueByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue([2, 2], 2); self::assertSame(2, $sheet->getCell('B2')->getValue()); } public function testSetCellValueExplicitByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValueExplicit([2, 2], '="PHP Rules"', DataType::TYPE_STRING); self::assertSame('="PHP Rules"', $sheet->getCell('B2')->getValue()); self::assertSame(DataType::TYPE_STRING, $sheet->getCell('B2')->getDataType()); } public function testCellExistsByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $cellExists = $sheet->cellExists([2, 2]); self::assertFalse($cellExists); $sheet->setCellValue('B2', 2); $cellExists = $sheet->cellExists([2, 2]); self::assertTrue($cellExists); } public function testGetCellByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('B2', 2); $cell = $sheet->getCell([2, 2]); self::assertSame('B2', $cell->getCoordinate()); self::assertSame(2, $cell->getValue()); } public function testGetStyleByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->getStyle('B2:C3')->getFont()->setBold(true); $rangeStyle = $sheet->getStyle([2, 2, 3, 3]); self::assertTrue($rangeStyle->getFont()->getBold()); $cellStyle = $sheet->getStyle([2, 2]); self::assertTrue($cellStyle->getFont()->getBold()); } public function testSetBreakByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('B2', 2); $sheet->setBreak([2, 2], Worksheet::BREAK_COLUMN); $breaks = $sheet->getBreaks(); self::assertArrayHasKey('B2', $breaks); self::assertSame(Worksheet::BREAK_COLUMN, $breaks['B2']); } public function testMergeCellsByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->mergeCells([2, 2, 3, 3]); $mergeRanges = $sheet->getMergeCells(); self::assertArrayHasKey('B2:C3', $mergeRanges); } public function testUnmergeCellsByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->mergeCells('B2:C3'); $mergeRanges = $sheet->getMergeCells(); self::assertArrayHasKey('B2:C3', $mergeRanges); $sheet->unmergeCells([2, 2, 3, 3]); $mergeRanges = $sheet->getMergeCells(); self::assertEmpty($mergeRanges); } public function testProtectCellsByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->protectCells([2, 2, 3, 3], 'secret', false); $protectedRanges = $sheet->getProtectedCellRanges(); self::assertArrayHasKey('B2:C3', $protectedRanges); } public function testUnprotectCellsByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->protectCells('B2:C3', 'secret', false); $protectedRanges = $sheet->getProtectedCellRanges(); self::assertArrayHasKey('B2:C3', $protectedRanges); $sheet->unprotectCells([2, 2, 3, 3]); $protectedRanges = $sheet->getProtectedCellRanges(); self::assertEmpty($protectedRanges); } public function testSetAutoFilterByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->setAutoFilter([2, 2, 3, 3]); $autoFilter = $sheet->getAutoFilter(); self::assertSame('B2:C3', $autoFilter->getRange()); } public function testFreezePaneByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $data = [['A', 'B'], ['C', 'D']]; $sheet->fromArray($data, null, 'B2', true); $sheet->freezePane([2, 2]); $freezePane = $sheet->getFreezePane(); self::assertSame('B2', $freezePane); } public function testGetCommentByColumnAndRow(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('B2', 2); $spreadsheet->getActiveSheet() ->getComment('B2') ->getText()->createTextRun('My Test Comment'); $comment = $sheet->getComment([2, 2]); self::assertSame('My Test Comment', $comment->getText()->getPlainText()); } public function testMergeCellsBadArray(): void { $this->expectException(SpreadsheetException::class); $this->expectExceptionMessage('CellRange array length must be 2 or 4'); $spreadsheet = $this->getSpreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->mergeCells([2, 2, 3]); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/RowTest.php
tests/PhpSpreadsheetTests/Worksheet/RowTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Row; use PHPUnit\Framework\TestCase; class RowTest extends TestCase { public function testInstantiateRowDefault(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $row = new Row($sheet); $rowIndex = $row->getRowIndex(); self::assertEquals(1, $rowIndex); $spreadsheet->disconnectWorksheets(); } public function testInstantiateRowSpecified(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $row = new Row($sheet, 5); $rowIndex = $row->getRowIndex(); self::assertEquals(5, $rowIndex); $spreadsheet->disconnectWorksheets(); } public function testGetCellIterator(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $row = new Row($sheet); $cellIterator = $row->getCellIterator(); self::assertSame(1, $cellIterator->getCurrentColumnIndex()); $spreadsheet->disconnectWorksheets(); } public function testGetColumnIterator(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $row = new Row($sheet); $cellIterator = $row->getColumnIterator(); self::assertSame(1, $cellIterator->getCurrentColumnIndex()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/RowCellIteratorTest.php
tests/PhpSpreadsheetTests/Worksheet/RowCellIteratorTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception as Except; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\RowCellIterator; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class RowCellIteratorTest extends TestCase { private const CELL_VALUES = [ [110, 120, 130, 140, 150, 160, 170], [210, 220, 230, 240, 250], [310, 320, 330, 340, 350, 360], [410, 420, 430, 440, 450], [510, 520, 530, 540, 550, 560], [610, 620, 630, 640, 650], ]; private static function getPopulatedSheet(Spreadsheet $spreadsheet): Worksheet { $sheet = $spreadsheet->getActiveSheet(); $sheet->fromArray(self::CELL_VALUES); return $sheet; } public function testIteratorFullRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowCellIterator($sheet); $RowCellIndexResult = 'A'; self::assertEquals($RowCellIndexResult, $iterator->key()); $values = []; foreach ($iterator as $key => $RowCell) { $values[] = $RowCell->getValue(); self::assertEquals($RowCellIndexResult, $key); StringHelper::stringIncrement($RowCellIndexResult); } self::assertSame(self::CELL_VALUES[0], $values); $spreadsheet->disconnectWorksheets(); } public function testIteratorStartEndRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowCellIterator($sheet, 2, 'B', 'D'); $RowCellIndexResult = 'B'; self::assertEquals($RowCellIndexResult, $iterator->key()); $values = []; foreach ($iterator as $key => $RowCell) { $values[] = $RowCell->getValue(); self::assertEquals($RowCellIndexResult, $key); StringHelper::stringIncrement($RowCellIndexResult); } self::assertSame([220, 230, 240], $values); $spreadsheet->disconnectWorksheets(); } public function testIteratorSeekAndPrev(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowCellIterator($sheet, 2, 'B', 'D'); $RowCellIndexResult = 'D'; $iterator->seek('D'); self::assertEquals($RowCellIndexResult, $iterator->key()); $values = []; while ($iterator->valid()) { $current = $iterator->current(); self::assertNotNull($current); $cell = $current->getCoordinate(); $values[] = $sheet->getCell($cell)->getValue(); $iterator->prev(); } self::assertSame([240, 230, 220], $values); $spreadsheet->disconnectWorksheets(); } public function testSeekOutOfRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $this->expectException(Except::class); $this->expectExceptionMessage('Column A is out of range'); $iterator = new RowCellIterator($sheet, 2, 'B', 'D'); self::assertFalse($iterator->getIterateOnlyExistingCells()); self::assertEquals(2, $iterator->getCurrentColumnIndex()); $iterator->seek('A'); $spreadsheet->disconnectWorksheets(); } public function testSeekNotExisting(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $this->expectException(Except::class); $this->expectExceptionMessage('Cell does not exist'); $iterator = new RowCellIterator($sheet, 2, 'B', 'D'); $iterator->setIterateOnlyExistingCells(true); $iterator->seek('B'); $spreadsheet->disconnectWorksheets(); } public function testPrevOutOfRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowCellIterator($sheet, 2, 'B', 'D'); $iterator->prev(); self::assertFalse($iterator->valid()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ConditionalStyleTest.php
tests/PhpSpreadsheetTests/Worksheet/ConditionalStyleTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Color; use PhpOffice\PhpSpreadsheet\Style\Conditional; use PHPUnit\Framework\TestCase; class ConditionalStyleTest extends TestCase { protected Spreadsheet $spreadsheet; protected function setUp(): void { parent::setUp(); $this->spreadsheet = new Spreadsheet(); $conditional1 = new Conditional(); $conditional1->setConditionType(Conditional::CONDITION_CELLIS); $conditional1->setOperatorType(Conditional::OPERATOR_LESSTHAN); $conditional1->addCondition('0'); $conditional1->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_RED); $conditional1->getStyle()->getFont()->setBold(true); $conditional2 = new Conditional(); $conditional2->setConditionType(Conditional::CONDITION_CELLIS); $conditional2->setOperatorType(Conditional::OPERATOR_EQUAL); $conditional2->addCondition('0'); $conditional2->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_YELLOW); $conditional2->getStyle()->getFont()->setBold(true); $conditional3 = new Conditional(); $conditional3->setConditionType(Conditional::CONDITION_CELLIS); $conditional3->setOperatorType(Conditional::OPERATOR_GREATERTHAN); $conditional3->addCondition('0'); $conditional3->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_GREEN); $conditional3->getStyle()->getFont()->setBold(true); $conditionalStyles = $this->spreadsheet->getActiveSheet()->getStyle('A1:C3')->getConditionalStyles(); $conditionalStyles[] = $conditional1; $conditionalStyles[] = $conditional2; $conditionalStyles[] = $conditional3; $this->spreadsheet->getActiveSheet()->getStyle('A1:C3')->setConditionalStyles($conditionalStyles); $this->spreadsheet->getActiveSheet() ->duplicateConditionalStyle( $this->spreadsheet->getActiveSheet()->getConditionalStyles('A1:C3'), 'F1' ); } #[\PHPUnit\Framework\Attributes\DataProvider('cellConditionalStylesProvider')] public function testCellHasConditionalStyles(string $cellReference, bool $expectedHasConditionalStyles): void { $cellHasConditionalStyles = $this->spreadsheet->getActiveSheet()->conditionalStylesExists($cellReference); self::assertSame($expectedHasConditionalStyles, $cellHasConditionalStyles); } #[\PHPUnit\Framework\Attributes\DataProvider('cellConditionalStylesProvider')] public function testCellGetConditionalStyles(string $cellReference, bool $expectedGetConditionalStyles): void { $cellHasConditionalStyles = $this->spreadsheet->getActiveSheet()->getConditionalStyles($cellReference); self::assertSame($expectedGetConditionalStyles, !empty($cellHasConditionalStyles)); } public static function cellConditionalStylesProvider(): array { return [ ['A1', true], ['B2', true], ['B4', false], ['A1:C3', true], ['A1:B2', false], ['F1', true], ['F2', false], ['A1:F1', false], ]; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/ToArrayTest.php
tests/PhpSpreadsheetTests/Worksheet/ToArrayTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class ToArrayTest extends TestCase { public static function testToArray(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $inputArray = [ ['a', 'b', null, 'd', 'e'], [], [null, null, null, null, 'f'], [], [1, 0, null, 4], ]; $sheet->fromArray($inputArray, null, 'A1', true); $expected1 = [ ['a', 'b', null, 'd', 'e'], [null, null, null, null, null], [null, null, null, null, 'f'], [null, null, null, null, null], [1, 0, null, 4, null], ]; $result1 = $sheet->toArray(null, false, false, false, false); self::assertSame($expected1, $result1); $expected2 = [ 1 => ['A' => 'a', 'B' => 'b', 'C' => null, 'D' => 'd', 'E' => 'e'], 2 => ['A' => null, 'B' => null, 'C' => null, 'D' => null, 'E' => null], 3 => ['A' => null, 'B' => null, 'C' => null, 'D' => null, 'E' => 'f'], 4 => ['A' => null, 'B' => null, 'C' => null, 'D' => null, 'E' => null], 5 => ['A' => 1, 'B' => 0, 'C' => null, 'D' => 4, 'E' => null], ]; $result2 = $sheet->toArray(null, false, false, true, false); self::assertSame($expected2, $result2); $expected3 = [ [null, null, null, null], [null, null, null, 'f'], [null, null, null, null], [0, null, 4, null], ]; $result3 = $sheet->rangeToArray('B2:E5', null, false, false, false, false); self::assertSame($expected3, $result3); $expected4 = [ 2 => ['B' => null, 'C' => null, 'D' => null, 'E' => null], 3 => ['B' => null, 'C' => null, 'D' => null, 'E' => 'f'], 4 => ['B' => null, 'C' => null, 'D' => null, 'E' => null], 5 => ['B' => 0, 'C' => null, 'D' => 4, 'E' => null], ]; $result4 = $sheet->rangeToArray('B2:E5', null, false, false, true, false); self::assertSame($expected4, $result4); $spreadsheet->disconnectWorksheets(); } public static function testMaxCol(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('A1')->setValue('start'); $sheet->getCell('XFD1')->setValue('end'); $array = $sheet->toArray(null, false, false, false, false); self::assertCount(1, $array); self::assertCount(16384, $array[0]); self::assertSame('start', $array[0][0]); self::assertSame('end', $array[0][16383]); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/InsertTest.php
tests/PhpSpreadsheetTests/Worksheet/InsertTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class InsertTest extends TestCase { public function testInsertRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->fromArray([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], ]); $sheet->getRowDimension(1000)->setVisible(false); $sheet->getStyle('C3')->getFont()->setBold(true); self::assertSame(1000, $sheet->getHighestRow()); self::assertSame(5, $sheet->getHighestDataRow()); $currentRow = 4; $sheet->insertNewRowBefore($currentRow, 1); self::assertSame(1001, $sheet->getHighestRow()); self::assertSame(6, $sheet->getHighestDataRow()); self::assertTrue($sheet->getStyle('C3')->getFont()->getBold()); self::assertSame(11, $sheet->getCell('C3')->getValue()); self::assertTrue($sheet->getStyle('C4')->getFont()->getBold()); self::assertNull($sheet->getCell('C4')->getValue()); self::assertFalse($sheet->getRowDimension(1001)->getVisible()); self::assertTrue($sheet->getRowDimension(1000)->getVisible()); $spreadsheet->disconnectWorksheets(); } public function testInsertColumn(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->fromArray([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], ]); $sheet->getColumnDimension('ZY')->setVisible(false); $sheet->getStyle('C3')->getFont()->setBold(true); self::assertSame('ZY', $sheet->getHighestColumn()); self::assertSame('D', $sheet->getHighestDataColumn()); $currentColumn = 'D'; $sheet->insertNewColumnBefore($currentColumn, 1); self::assertSame('ZZ', $sheet->getHighestColumn()); self::assertSame('E', $sheet->getHighestDataColumn()); self::assertTrue($sheet->getStyle('C3')->getFont()->getBold()); self::assertSame(11, $sheet->getCell('C3')->getValue()); self::assertTrue($sheet->getStyle('D3')->getFont()->getBold()); self::assertNull($sheet->getCell('D3')->getValue()); self::assertFalse($sheet->getColumnDimension('ZZ')->getVisible()); self::assertTrue($sheet->getColumnDimension('ZY')->getVisible()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/RemoveTest.php
tests/PhpSpreadsheetTests/Worksheet/RemoveTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Color; use PhpOffice\PhpSpreadsheet\Style\Fill; use PHPUnit\Framework\TestCase; class RemoveTest extends TestCase { public function testRemoveRow(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $fillColors = [ 'FFFF0000', 'FF00FF00', 'FF0000FF', ]; $rowHeights = [-1.0, -1.0, 1.2, 1.3, 1.4, 1.5, -1.0, -1.0, -1.0]; for ($row = 1; $row < 10; ++$row) { $sheet->getCell("B$row") ->getStyle() ->getFill() ->setFillType(Fill::FILL_SOLID) ->setStartColor(new Color($fillColors[$row % 3])); $sheet->getCell("B$row")->setValue("X$row"); $height = $rowHeights[$row - 1]; if ($height > 0) { $sheet->getRowDimension($row)->setRowHeight($height); } } //$mapRow = [1, 2, 3, 4, 5, 6, 7, 8, 9]; $sheet->removeRow(4, 2); $mapRow = [1, 2, 3, 6, 7, 8, 9]; $rowCount = count($mapRow); for ($row = 1; $row <= $rowCount; ++$row) { $mappedRow = $mapRow[$row - 1]; self::assertSame("X$mappedRow", $sheet->getCell("B$row")->getValue(), "Row value $row mapped to $mappedRow"); self::assertSame($fillColors[$mappedRow % 3], $sheet->getCell("B$row")->getStyle()->getFill()->getStartColor()->getArgb(), "Row fill color $row mapped to $mappedRow"); self::assertSame($rowHeights[$mappedRow - 1], $sheet->getRowDimension($row)->getRowHeight(), "Row height $row mapped to $mappedRow"); } $spreadsheet->disconnectWorksheets(); } public function testRemoveColumn(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $fillColors = [ 'FFFF0000', 'FF00FF00', 'FF0000FF', ]; $colWidths = [-1, -1, 1.2, 1.3, 1.4, 1.5, -1, -1, -1]; for ($colNumber = 1; $colNumber < 10; ++$colNumber) { $col = Coordinate::stringFromColumnIndex($colNumber); $sheet->getCell("{$col}1") ->getStyle() ->getFill() ->setFillType(Fill::FILL_SOLID) ->setStartColor(new Color($fillColors[$colNumber % 3])); $sheet->getCell("{$col}1")->setValue("100$col"); $width = $colWidths[$colNumber - 1]; if ($width > 0) { $sheet->getColumnDimension($col)->setWidth($width); } } //$mapCol = [1, 2, 3, 4, 5, 6, 7, 8, 9]; $sheet->removeColumn('D', 2); $mapCol = [1, 2, 3, 6, 7, 8, 9]; $colCount = count($mapCol); for ($colNumber = 1; $colNumber < $colCount; ++$colNumber) { $col = Coordinate::stringFromColumnIndex($colNumber); $mappedCol = $mapCol[$colNumber - 1]; $mappedColString = Coordinate::stringFromColumnIndex($mappedCol); self::assertSame("100$mappedColString", $sheet->getCell("{$col}1")->getValue(), "Column value $colNumber mapped to $mappedCol"); self::assertSame($fillColors[$mappedCol % 3], $sheet->getCell("{$col}1")->getStyle()->getFill()->getStartColor()->getArgb(), "Col fill color $col mapped to $mappedColString"); self::assertEquals($colWidths[$mappedCol - 1], $sheet->getColumnDimension($col)->getWidth(), "Col width $col mapped to $mappedColString"); } $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php
tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use Exception; use PhpOffice\PhpSpreadsheet\Cell\CellAddress; use PhpOffice\PhpSpreadsheet\Cell\CellRange; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator; use PhpOffice\PhpSpreadsheet\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class WorksheetTest extends TestCase { public function testSetTitle(): void { $testTitle = str_repeat('a', 31); $worksheet = new Worksheet(); $worksheet->setTitle($testTitle); self::assertSame($testTitle, $worksheet->getTitle()); } public static function setTitleInvalidProvider(): array { return [ [str_repeat('a', 32), 'Maximum 31 characters allowed in sheet title.'], ['invalid*title', 'Invalid character found in sheet title'], ]; } #[DataProvider('setTitleInvalidProvider')] public function testSetTitleInvalid(string $title, string $expectMessage): void { // First, test setting title with validation disabled -- should be successful $worksheet = new Worksheet(); $worksheet->setTitle($title, true, false); // Next, test again with validation enabled -- this time we should fail $worksheet = new Worksheet(); $this->expectException(Exception::class); $this->expectExceptionMessage($expectMessage); $worksheet->setTitle($title); } public function testSetTitleDuplicate(): void { // Create a Spreadsheet with three Worksheets (the first is created automatically) $spreadsheet = new Spreadsheet(); $spreadsheet->createSheet(); $spreadsheet->createSheet(); // Set unique title -- should be unchanged $sheet = $spreadsheet->getSheet(0); $sheet->setTitle('Test Title'); self::assertSame('Test Title', $sheet->getTitle()); // Set duplicate title -- should have numeric suffix appended $sheet = $spreadsheet->getSheet(1); $sheet->setTitle('Test Title'); self::assertSame('Test Title 1', $sheet->getTitle()); // Set duplicate title with validation disabled -- should be unchanged $sheet = $spreadsheet->getSheet(2); $sheet->setTitle('Test Title', true, false); self::assertSame('Test Title', $sheet->getTitle()); } public function testSetCodeName(): void { $testCodeName = str_repeat('a', 31); $worksheet = new Worksheet(); $worksheet->setCodeName($testCodeName); self::assertSame($testCodeName, $worksheet->getCodeName()); } public static function setCodeNameInvalidProvider(): array { return [ [str_repeat('a', 32), 'Maximum 31 characters allowed in sheet code name.'], ['invalid*code*name', 'Invalid character found in sheet code name'], ['', 'Sheet code name cannot be empty'], ]; } #[DataProvider('setCodeNameInvalidProvider')] public function testSetCodeNameInvalid(string $codeName, string $expectMessage): void { // First, test setting code name with validation disabled -- should be successful $worksheet = new Worksheet(); $worksheet->setCodeName($codeName, false); // Next, test again with validation enabled -- this time we should fail $worksheet = new Worksheet(); $this->expectException(Exception::class); $this->expectExceptionMessage($expectMessage); $worksheet->setCodeName($codeName); } public function testSetCodeNameDuplicate(): void { // Create a Spreadsheet with three Worksheets (the first is created automatically) $spreadsheet = new Spreadsheet(); $spreadsheet->createSheet(); $spreadsheet->createSheet(); // Set unique code name -- should be massaged to Snake_Case $sheet = $spreadsheet->getSheet(0); $sheet->setCodeName('Test Code Name'); self::assertSame('Test_Code_Name', $sheet->getCodeName()); // Set duplicate code name -- should be massaged and have numeric suffix appended $sheet = $spreadsheet->getSheet(1); $sheet->setCodeName('Test Code Name'); self::assertSame('Test_Code_Name_1', $sheet->getCodeName()); // Set duplicate code name with validation disabled -- should be unchanged, and unmassaged $sheet = $spreadsheet->getSheet(2); $sheet->setCodeName('Test Code Name', false); self::assertSame('Test Code Name', $sheet->getCodeName()); } public function testFreezePaneSelectedCell(): void { $worksheet = new Worksheet(); $worksheet->freezePane('B2'); self::assertSame('B2', $worksheet->getTopLeftCell()); } public static function extractSheetTitleProvider(): array { return [ ['B2', '', '', 'B2'], ['testTitle!B2', 'testTitle', 'B2', 'B2'], ['test!Title!B2', 'test!Title', 'B2', 'B2'], ['test Title!B2', 'test Title', 'B2', 'B2'], ['test!Title!B2', 'test!Title', 'B2', 'B2'], ["'testSheet 1'!A3", "'testSheet 1'", 'A3', 'A3'], ["'testSheet1'!A2", "'testSheet1'", 'A2', 'A2'], ["'testSheet 2'!A1", "'testSheet 2'", 'A1', 'A1'], ]; } #[DataProvider('extractSheetTitleProvider')] public function testExtractSheetTitle(string $range, string $expectTitle, string $expectCell, string $expectCell2): void { // only cell reference self::assertSame($expectCell, Worksheet::extractSheetTitle($range)); // with title in array $arRange = Worksheet::extractSheetTitle($range, true); self::assertSame($expectTitle, $arRange[0]); self::assertSame($expectCell2, $arRange[1]); } /** * Fix https://github.com/PHPOffice/PhpSpreadsheet/issues/868 when cells are not removed correctly * on row deletion. */ public function testRemoveCellsCorrectlyWhenRemovingRow(): void { $workbook = new Spreadsheet(); $worksheet = $workbook->getActiveSheet(); $worksheet->getCell('A2')->setValue('A2'); $worksheet->getCell('C1')->setValue('C1'); $worksheet->removeRow(1); self::assertEquals( 'A2', $worksheet->getCell('A1')->getValue() ); self::assertNull( $worksheet->getCell('C1')->getValue() ); } public static function removeColumnProvider(): array { return [ 'Remove first column' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ], 'A', 1, [ ['B1', 'C1'], ['B2', 'C2'], ], 'B', ], 'Remove middle column' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ], 'B', 1, [ ['A1', 'C1'], ['A2', 'C2'], ], 'B', ], 'Remove last column' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ], 'C', 1, [ ['A1', 'B1'], ['A2', 'B2'], ], 'B', ], 'Remove a column out of range' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ], 'D', 1, [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ], 'C', ], 'Remove multiple columns' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ], 'B', 5, [ ['A1'], ['A2'], ], 'A', ], 'Data includes nulls' => [ [ ['A1', 'B1', 'C1', 'D1', 'E1'], [null, 'B2', 'C2', 'D2', 'E2'], ['A3', null, 'C3', 'D3', 'E3'], ['A4', 'B4', null, 'D4', 'E4'], ['A5', 'B5', 'C5', null, 'E5'], ['A6', 'B6', 'C6', 'D6', null], ], 'B', 2, [ ['A1', 'D1', 'E1'], [null, 'D2', 'E2'], ['A3', 'D3', 'E3'], ['A4', 'D4', 'E4'], ['A5', null, 'E5'], ['A6', 'D6', null], ], 'C', ], ]; } /** * @param mixed[] $initialData * @param mixed[] $expectedData */ #[DataProvider('removeColumnProvider')] public function testRemoveColumn( array $initialData, string $columnToBeRemoved, int $columnsToBeRemoved, array $expectedData, string $expectedHighestColumn ): void { $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->fromArray($initialData); $worksheet->removeColumn($columnToBeRemoved, $columnsToBeRemoved); self::assertSame($expectedHighestColumn, $worksheet->getHighestColumn()); self::assertSame($expectedData, $worksheet->toArray()); } public static function removeRowsProvider(): array { return [ 'Remove all rows except first one' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'], ['A4', 'B4', 'C4'], ], 2, 3, [ ['A1', 'B1', 'C1'], ], 1, ], 'Remove all rows except last one' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'], ['A4', 'B4', 'C4'], ], 1, 3, [ ['A4', 'B4', 'C4'], ], 1, ], 'Remove last row' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'], ['A4', 'B4', 'C4'], ], 4, 1, [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'], ], 3, ], 'Remove first row' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'], ['A4', 'B4', 'C4'], ], 1, 1, [ ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'], ['A4', 'B4', 'C4'], ], 3, ], 'Remove all rows except first and last' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'], ['A4', 'B4', 'C4'], ], 2, 2, [ ['A1', 'B1', 'C1'], ['A4', 'B4', 'C4'], ], 2, ], 'Remove non existing rows' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'], ['A4', 'B4', 'C4'], ], 2, 10, [ ['A1', 'B1', 'C1'], ], 1, ], 'Remove only non existing rows' => [ [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'], ['A4', 'B4', 'C4'], ], 5, 10, [ ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3'], ['A4', 'B4', 'C4'], ], 4, ], 'Data includes nulls' => [ [ ['A1', 'B1', 'C1', 'D1', 'E1'], [null, 'B2', 'C2', 'D2', 'E2'], ['A3', null, 'C3', 'D3', 'E3'], ['A4', 'B4', null, 'D4', 'E4'], ['A5', 'B5', 'C5', null, 'E5'], ['A6', 'B6', 'C6', 'D6', null], ], 1, 2, [ ['A3', null, 'C3', 'D3', 'E3'], ['A4', 'B4', null, 'D4', 'E4'], ['A5', 'B5', 'C5', null, 'E5'], ['A6', 'B6', 'C6', 'D6', null], ], 4, ], ]; } /** * @param mixed[] $initialData * @param mixed[] $expectedData */ #[DataProvider('removeRowsProvider')] public function testRemoveRows( array $initialData, int $rowToRemove, int $rowsQtyToRemove, array $expectedData, int $expectedHighestRow ): void { $workbook = new Spreadsheet(); $worksheet = $workbook->getActiveSheet(); $worksheet->fromArray($initialData); $worksheet->removeRow($rowToRemove, $rowsQtyToRemove); self::assertSame($expectedData, $worksheet->toArray()); self::assertSame($expectedHighestRow, $worksheet->getHighestRow()); } private static function getPopulatedSheetForEmptyRowTest(Spreadsheet $spreadsheet): Worksheet { $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValueExplicit('A1', 'Hello World', DataType::TYPE_STRING); $sheet->setCellValueExplicit('B3', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('B4', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('B5', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('C5', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('B6', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('C6', 'PHP', DataType::TYPE_STRING); $sheet->setCellValueExplicit('B7', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('C7', 'PHP', DataType::TYPE_STRING); $sheet->setCellValueExplicit('B8', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('C8', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('D8', 'PHP', DataType::TYPE_STRING); return $sheet; } private static function getPopulatedSheetForEmptyColumnTest(Spreadsheet $spreadsheet): Worksheet { $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValueExplicit('A1', 'Hello World', DataType::TYPE_STRING); $sheet->setCellValueExplicit('C2', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('D2', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('E2', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('E3', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('F2', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('F3', 'PHP', DataType::TYPE_STRING); $sheet->setCellValueExplicit('G2', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('G3', 'PHP', DataType::TYPE_STRING); $sheet->setCellValueExplicit('H2', null, DataType::TYPE_NULL); $sheet->setCellValueExplicit('H3', '', DataType::TYPE_STRING); $sheet->setCellValueExplicit('H4', 'PHP', DataType::TYPE_STRING); return $sheet; } #[DataProvider('emptyRowProvider')] public function testIsEmptyRow(int $rowId, bool $expectedEmpty): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheetForEmptyRowTest($spreadsheet); $isEmpty = $sheet->isEmptyRow($rowId, CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL | CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL); self::assertSame($expectedEmpty, $isEmpty); $spreadsheet->disconnectWorksheets(); } public static function emptyRowProvider(): array { return [ [1, false], [2, true], [3, true], [4, true], [5, true], [6, false], [7, false], [8, false], [9, true], ]; } #[DataProvider('emptyColumnProvider')] public function testIsEmptyColumn(string $columnId, bool $expectedEmpty): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheetForEmptyColumnTest($spreadsheet); $isEmpty = $sheet->isEmptyColumn($columnId, CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL | CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL); self::assertSame($expectedEmpty, $isEmpty); $spreadsheet->disconnectWorksheets(); } public static function emptyColumnProvider(): array { return [ ['A', false], ['B', true], ['C', true], ['D', true], ['E', true], ['F', false], ['G', false], ['H', false], ['I', true], ]; } public function testGetTableNames(): void { $reader = new Xlsx(); $spreadsheet = $reader->load('tests/data/Worksheet/Table/TableFormulae.xlsx'); $worksheet = $spreadsheet->getActiveSheet(); $tables = $worksheet->getTableNames(); self::assertSame(['DeptSales'], $tables); } public function testGetTableByName(): void { $reader = new Xlsx(); $spreadsheet = $reader->load('tests/data/Worksheet/Table/TableFormulae.xlsx'); $worksheet = $spreadsheet->getActiveSheet(); $table = $worksheet->getTableByName('Non-existent Table'); self::assertNull($table); $table = $worksheet->getTableByName('DeptSales'); self::assertInstanceOf(Table::class, $table); } /** * @param mixed[] $initialData * @param int[] $hiddenRows * @param mixed[] $expectedData */ #[DataProvider('toArrayHiddenRowsProvider')] public function testHiddenRows( array $initialData, array $hiddenRows, array $expectedData ): void { $workbook = new Spreadsheet(); $worksheet = $workbook->getActiveSheet(); $worksheet->fromArray($initialData); foreach ($hiddenRows as $hiddenRow) { $worksheet ->getRowDimension($hiddenRow) ->setVisible(false); } self::assertSame($expectedData, $worksheet->toArray(null, false, false, true, true)); } public static function toArrayHiddenRowsProvider(): array { return [ [ [[1], [2], [3], [4], [5], [6]], [2, 3, 5], [1 => ['A' => 1], 4 => ['A' => 4], 6 => ['A' => 6]], ], [ [[1], [2], [3], [4], [5], [6]], [1, 3, 6], [2 => ['A' => 2], 4 => ['A' => 4], 5 => ['A' => 5]], ], ]; } /** * @param mixed[] $initialData * @param string[] $hiddenColumns * @param mixed[] $expectedData */ #[DataProvider('toArrayHiddenColumnsProvider')] public function testHiddenColumns( array $initialData, array $hiddenColumns, array $expectedData ): void { $workbook = new Spreadsheet(); $worksheet = $workbook->getActiveSheet(); $worksheet->fromArray($initialData); foreach ($hiddenColumns as $hiddenColumn) { $worksheet ->getColumnDimension($hiddenColumn) ->setVisible(false); } self::assertSame($expectedData, $worksheet->toArray(null, false, false, true, true)); } public static function toArrayHiddenColumnsProvider(): array { return [ [ ['A', 'B', 'C', 'D', 'E', 'F'], ['B', 'C', 'E'], [1 => ['A' => 'A', 'D' => 'D', 'F' => 'F']], ], [ ['A', 'B', 'C', 'D', 'E', 'F'], ['A', 'C', 'F'], [1 => ['B' => 'B', 'D' => 'D', 'E' => 'E']], ], ]; } /** @param mixed[] $expected */ #[DataProvider('rangeToArrayProvider')] public function testRangeToArrayWithCellRangeObject(array $expected, string $fromCell, string $toCell): void { $initialData = array_chunk(range('A', 'Y'), 5); $workbook = new Spreadsheet(); $worksheet = $workbook->getActiveSheet(); $worksheet->fromArray($initialData); $cellRange = new CellRange(new CellAddress($fromCell), new CellAddress($toCell)); self::assertSame($expected, $worksheet->rangeToArray((string) $cellRange)); } public static function rangeToArrayProvider(): array { return [ [ [['A', 'B'], ['F', 'G']], 'A1', 'B2', ], [ [['G', 'H', 'I'], ['L', 'M', 'N'], ['Q', 'R', 'S']], 'B2', 'D4', ], ]; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoSizeTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoSizeTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table\TableStyle; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class AutoSizeTest extends TestCase { private Spreadsheet $spreadsheet; private Worksheet $worksheet; protected function setUp(): void { $this->spreadsheet = new Spreadsheet(); $this->worksheet = $this->spreadsheet->getActiveSheet(); $this->worksheet->setCellValue('A1', 'YEAR') ->setCellValue('B1', 'QUARTER') ->setCellValue('C1', 'COUNTRY') ->setCellValue('D1', 'SALES'); $dataArray = [ ['10', 'Q1', 'United States', 790], ['10', 'Q2', 'United States', 730], ['10', 'Q3', 'United States', 860], ['10', 'Q4', 'United States', 850], ]; $this->worksheet->fromArray($dataArray, null, 'A2'); $toColumn = $this->worksheet->getHighestColumn(); StringHelper::stringIncrement($toColumn); for ($i = 'A'; $i !== $toColumn; StringHelper::stringIncrement($i)) { $this->worksheet->getColumnDimension($i)->setAutoSize(true); } } protected function tearDown(): void { $this->spreadsheet->disconnectWorksheets(); unset($this->spreadsheet, $this->worksheet); } private function setTable(): Table { $table = new Table('A1:D5', 'Sales_Data'); $tableStyle = new TableStyle(); $tableStyle->setTheme(TableStyle::TABLE_STYLE_MEDIUM2); $table->setStyle($tableStyle); $this->worksheet->addTable($table); return $table; } /** @return float[] */ private function readColumnSizes(): array { $columnSizes = []; $toColumn = $this->worksheet->getHighestColumn(); StringHelper::stringIncrement($toColumn); for ($column = 'A'; $column !== $toColumn; StringHelper::stringIncrement($column)) { $columnSizes[$column] = $this->worksheet->getColumnDimension($column)->getWidth(); } return $columnSizes; } public function testStandardAutoSize(): void { $this->worksheet->calculateColumnWidths(); $columnSizes = $this->readColumnSizes(); self::assertSame(['A' => 5.856, 'B' => 9.283, 'C' => 16.425, 'D' => 6.998], $columnSizes); } public function testAutoFilterAutoSize(): void { $this->worksheet->setAutoFilter('A1:D5'); $this->worksheet->calculateColumnWidths(); $columnSizes = $this->readColumnSizes(); self::assertSame(['A' => 8.141, 'B' => 11.569, 'C' => 16.425, 'D' => 9.283], $columnSizes); } public function testTableWithAutoFilterAutoSize(): void { $this->setTable(); $this->worksheet->calculateColumnWidths(); $columnSizes = $this->readColumnSizes(); self::assertSame(['A' => 8.141, 'B' => 11.569, 'C' => 16.425, 'D' => 9.283], $columnSizes); } public function testTableWithoutHiddenHeadersAutoSize(): void { $table = $this->setTable(); $table->setShowHeaderRow(false); $this->worksheet->calculateColumnWidths(); $columnSizes = $this->readColumnSizes(); self::assertSame(['A' => 5.856, 'B' => 9.283, 'C' => 16.425, 'D' => 6.998], $columnSizes); } public function testTableWithAutoFilterCenterHeaders(): void { $this->setTable(); $this->worksheet->getStyle('A1:D1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); $this->worksheet->calculateColumnWidths(); $columnSizes = $this->readColumnSizes(); self::assertSame(['A' => 10.569, 'B' => 13.997, 'C' => 16.425, 'D' => 11.711], $columnSizes); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/DrawingTest.php
tests/PhpSpreadsheetTests/Worksheet/DrawingTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Shared\XMLWriter; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing; use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException; use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter; use PHPUnit\Framework\TestCase; use ReflectionMethod; class DrawingTest extends TestCase { public function testCloningWorksheetWithImages(): void { $gdImage = @imagecreatetruecolor(120, 20); $textColor = ($gdImage === false) ? false : imagecolorallocate($gdImage, 255, 255, 255); if ($gdImage === false || $textColor === false) { self::fail('imagecreatetruecolor or imagecolorallocate failed'); } else { $spreadsheet = new Spreadsheet(); $aSheet = $spreadsheet->getActiveSheet(); imagestring($gdImage, 1, 5, 5, 'Created with PhpSpreadsheet', $textColor); $drawing = new MemoryDrawing(); $drawing->setName('In-Memory image 1'); $drawing->setDescription('In-Memory image 1'); $drawing->setCoordinates('A1'); $drawing->setImageResource($gdImage); $drawing->setRenderingFunction( MemoryDrawing::RENDERING_JPEG ); $drawing->setMimeType(MemoryDrawing::MIMETYPE_DEFAULT); $drawing->setHeight(36); $drawing->setWorksheet($aSheet); $originDrawingCount = count($aSheet->getDrawingCollection()); $clonedWorksheet = clone $aSheet; $clonedDrawingCount = count($clonedWorksheet->getDrawingCollection()); self::assertEquals($originDrawingCount, $clonedDrawingCount); self::assertNotSame($aSheet, $clonedWorksheet); self::assertNotSame($aSheet->getDrawingCollection(), $clonedWorksheet->getDrawingCollection()); $spreadsheet->disconnectWorksheets(); } } public function testChangeWorksheet(): void { $spreadsheet = new Spreadsheet(); $sheet1 = $spreadsheet->getActiveSheet(); $sheet2 = $spreadsheet->createSheet(); $drawing = new Drawing(); $drawing->setName('Green Square'); $drawing->setPath('tests/data/Writer/XLSX/green_square.gif'); self::assertEquals($drawing->getWidth(), 150); self::assertEquals($drawing->getHeight(), 150); $drawing->setCoordinates('A1'); $drawing->setOffsetX(30); $drawing->setOffsetY(10); $drawing->setWorksheet($sheet1); try { $drawing->setWorksheet($sheet2); self::fail('Should throw exception when attempting set worksheet without specifying override'); } catch (PhpSpreadsheetException $e) { self::assertStringContainsString('A Worksheet has already been assigned.', $e->getMessage()); } self::assertSame($sheet1, $drawing->getWorksheet()); self::assertCount(1, $sheet1->getDrawingCollection()); self::assertCount(0, $sheet2->getDrawingCollection()); $drawing->setWorksheet($sheet2, true); self::assertSame($sheet2, $drawing->getWorksheet()); self::assertCount(0, $sheet1->getDrawingCollection()); self::assertCount(1, $sheet2->getDrawingCollection()); } public function testHeaderFooter(): void { $drawing1 = new HeaderFooterDrawing(); $drawing1->setName('Blue Square'); $drawing1->setPath('tests/data/Writer/XLSX/blue_square.png'); self::assertEquals($drawing1->getWidth(), 100); self::assertEquals($drawing1->getHeight(), 100); $drawing2 = new HeaderFooterDrawing(); $drawing2->setName('Blue Square'); $drawing2->setPath('tests/data/Writer/XLSX/blue_square.png'); self::assertSame($drawing1->getHashCode(), $drawing2->getHashCode()); $drawing2->setOffsetX(100); self::assertNotEquals($drawing1->getHashCode(), $drawing2->getHashCode()); } public function testSetWidthAndHeight(): void { $drawing = new Drawing(); $drawing->setName('Blue Square'); $drawing->setPath('tests/data/Writer/XLSX/blue_square.png'); self::assertSame(100, $drawing->getWidth()); self::assertSame(100, $drawing->getHeight()); self::assertTrue($drawing->getResizeProportional()); $drawing->setResizeProportional(false); $drawing->setWidthAndHeight(150, 200); self::assertSame(150, $drawing->getWidth()); self::assertSame(200, $drawing->getHeight()); $drawing->setResizeProportional(true); $drawing->setWidthAndHeight(300, 250); // width increase% more than height, so scale width self::assertSame(188, $drawing->getWidth()); self::assertSame(250, $drawing->getHeight()); $drawing->setResizeProportional(false); $drawing->setWidthAndHeight(150, 200); $drawing->setResizeProportional(true); // height increase% more than width, so scale height $drawing->setWidthAndHeight(175, 350); self::assertSame(175, $drawing->getWidth()); self::assertSame(234, $drawing->getHeight()); } public function testBadAddToXml(): void { $this->expectException(WriterException::class); $this->expectExceptionMessage('Invalid parameters passed.'); $drawing = new Drawing(); $drawing->setName('Blue Square'); $drawing->setPath('tests/data/Writer/XLSX/blue_square.png'); $objWriter = new XMLWriter(); $spreadsheet = new Spreadsheet(); $writerXlsx = new XlsxWriter($spreadsheet); $writerXlsxDrawing = new XlsxWriter\Drawing($writerXlsx); $reflectionMethod = new ReflectionMethod($writerXlsxDrawing, 'writeDrawing'); $reflectionMethod->invokeArgs($writerXlsxDrawing, [$objWriter, $drawing]); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/SheetViewTest.php
tests/PhpSpreadsheetTests/Worksheet/SheetViewTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Worksheet\SheetView; use PHPUnit\Framework\TestCase; class SheetViewTest extends TestCase { public function testView(): void { $sheetView = new SheetView(); self::assertSame(SheetView::SHEETVIEW_NORMAL, $sheetView->getView()); $sheetView->setView(SheetView::SHEETVIEW_PAGE_LAYOUT); self::assertSame(SheetView::SHEETVIEW_PAGE_LAYOUT, $sheetView->getView()); $sheetView->setView(null); self::assertSame(SheetView::SHEETVIEW_NORMAL, $sheetView->getView()); } public function testBadView(): void { $this->expectException(PhpSpreadsheetException::class); $this->expectExceptionMessage('Invalid sheetview layout type.'); $sheetView = new SheetView(); $sheetView->setView('unknown'); } public function testBadZoomScaleNormal(): void { $this->expectException(PhpSpreadsheetException::class); $this->expectExceptionMessage('Scale must be greater than or equal to 1.'); $sheetView = new SheetView(); $sheetView->setZoomScaleNormal(0); } public function testBadZoomScale(): void { $this->expectException(PhpSpreadsheetException::class); $this->expectExceptionMessage('Scale must be greater than or equal to 1.'); $sheetView = new SheetView(); $sheetView->setZoomScale(0); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Protection2Test.php
tests/PhpSpreadsheetTests/Worksheet/Protection2Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Protection; use PHPUnit\Framework\TestCase; class Protection2Test extends TestCase { public function testisHiddenOnFormulaBar(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('A1')->setValue('X') ->getStyle()->getProtection() ->setHidden(Protection::PROTECTION_UNPROTECTED); $sheet->getCell('A2')->setValue('=SUM(1,2)') ->getStyle()->getProtection() ->setHidden(Protection::PROTECTION_UNPROTECTED); $sheet->getCell('B1')->setValue('X') ->getStyle()->getProtection() ->setHidden(Protection::PROTECTION_PROTECTED); $sheet->getCell('B2')->setValue('=SUM(1,2)') ->getStyle()->getProtection() ->setHidden(Protection::PROTECTION_PROTECTED); $sheet->getCell('C1')->setValue('X'); $sheet->getCell('C2')->setValue('=SUM(1,2)'); self::assertFalse($sheet->getCell('A1')->isHiddenOnFormulaBar()); self::assertFalse($sheet->getCell('A2')->isHiddenOnFormulaBar()); self::assertFalse($sheet->getCell('B1')->isHiddenOnFormulaBar()); self::assertFalse($sheet->getCell('B2')->isHiddenOnFormulaBar()); self::assertFalse($sheet->getCell('C1')->isHiddenOnFormulaBar()); self::assertFalse($sheet->getCell('C2')->isHiddenOnFormulaBar()); $sheetProtection = $sheet->getProtection(); $sheetProtection->setSheet(true); self::assertFalse($sheet->getCell('A1')->isHiddenOnFormulaBar()); self::assertFalse($sheet->getCell('A2')->isHiddenOnFormulaBar()); self::assertFalse($sheet->getCell('B1')->isHiddenOnFormulaBar(), 'not a formula1'); self::assertTrue($sheet->getCell('B2')->isHiddenOnFormulaBar()); self::assertFalse($sheet->getCell('C1')->isHiddenOnFormulaBar(), 'not a formula2'); self::assertTrue($sheet->getCell('C2')->isHiddenOnFormulaBar()); self::assertFalse($sheet->getCell('D1')->isHiddenOnFormulaBar(), 'uninitialized cell is not formula'); $spreadsheet->disconnectWorksheets(); } /** * Same as above, but uses $sheet->isCellHiddenOnFormulaBar * rather than $sheet->getCell()->isHiddenOnFormulaBar(). */ public function testisHiddenOnFormulaBar2(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('A1')->setValue('X') ->getStyle()->getProtection() ->setHidden(Protection::PROTECTION_UNPROTECTED); $sheet->getCell('A2')->setValue('=SUM(1,2)') ->getStyle()->getProtection() ->setHidden(Protection::PROTECTION_UNPROTECTED); $sheet->getCell('B1')->setValue('X') ->getStyle()->getProtection() ->setHidden(Protection::PROTECTION_PROTECTED); $sheet->getCell('B2')->setValue('=SUM(1,2)') ->getStyle()->getProtection() ->setHidden(Protection::PROTECTION_PROTECTED); $sheet->getCell('C1')->setValue('X'); $sheet->getCell('C2')->setValue('=SUM(1,2)'); self::assertFalse($sheet->isCellHiddenOnFormulaBar('A1')); self::assertFalse($sheet->isCellHiddenOnFormulaBar('A2')); self::assertFalse($sheet->isCellHiddenOnFormulaBar('B1')); self::assertFalse($sheet->isCellHiddenOnFormulaBar('B2')); self::assertFalse($sheet->isCellHiddenOnFormulaBar('C1')); self::assertFalse($sheet->isCellHiddenOnFormulaBar('C2')); $sheetProtection = $sheet->getProtection(); $sheetProtection->setSheet(true); self::assertFalse($sheet->isCellHiddenOnFormulaBar('A1')); self::assertFalse($sheet->isCellHiddenOnFormulaBar('A2')); self::assertFalse($sheet->isCellHiddenOnFormulaBar('B1'), 'not a formula1'); self::assertTrue($sheet->isCellHiddenOnFormulaBar('B2')); self::assertFalse($sheet->isCellHiddenOnFormulaBar('C1'), 'not a formula2'); self::assertTrue($sheet->isCellHiddenOnFormulaBar('C2')); self::assertFalse($sheet->isCellHiddenOnFormulaBar('D1'), 'uninitialized cell is not formula'); self::assertFalse($sheet->cellExists('D1')); $spreadsheet->disconnectWorksheets(); } public function testisLocked(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('A1')->setValue('X') ->getStyle()->getProtection() ->setLocked(Protection::PROTECTION_UNPROTECTED); $sheet->getCell('A2')->setValue('=SUM(1,2)') ->getStyle()->getProtection() ->setLocked(Protection::PROTECTION_UNPROTECTED); $sheet->getCell('B1')->setValue('X') ->getStyle()->getProtection() ->setLocked(Protection::PROTECTION_PROTECTED); $sheet->getCell('B2')->setValue('=SUM(1,2)') ->getStyle()->getProtection() ->setLocked(Protection::PROTECTION_PROTECTED); $sheet->getCell('C1')->setValue('X'); $sheet->getCell('C2')->setValue('=SUM(1,2)'); self::assertFalse($sheet->getCell('A1')->isLocked()); self::assertFalse($sheet->getCell('A2')->isLocked()); self::assertFalse($sheet->getCell('B1')->isLocked()); self::assertFalse($sheet->getCell('B2')->isLocked()); self::assertFalse($sheet->getCell('C1')->isLocked()); self::assertFalse($sheet->getCell('C2')->isLocked()); $sheetProtection = $sheet->getProtection(); $sheetProtection->setSheet(true); self::assertFalse($sheet->getCell('A1')->isLocked()); self::assertFalse($sheet->getCell('A2')->isLocked()); self::assertTrue($sheet->getCell('B1')->isLocked()); self::assertTrue($sheet->getCell('B2')->isLocked()); self::assertTrue($sheet->getCell('C1')->isLocked()); self::assertTrue($sheet->getCell('C2')->isLocked()); self::assertTrue($sheet->getCell('D1')->isLocked(), 'uninitialized cell'); $spreadsheet->disconnectWorksheets(); } /** * Same as above, but uses $sheet->isCellLocked * rather than $sheet->getCell()->isLocked(). */ public function testisLocked2(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getCell('A1')->setValue('X') ->getStyle()->getProtection() ->setLocked(Protection::PROTECTION_UNPROTECTED); $sheet->getCell('A2')->setValue('=SUM(1,2)') ->getStyle()->getProtection() ->setLocked(Protection::PROTECTION_UNPROTECTED); $sheet->getCell('B1')->setValue('X') ->getStyle()->getProtection() ->setLocked(Protection::PROTECTION_PROTECTED); $sheet->getCell('B2')->setValue('=SUM(1,2)') ->getStyle()->getProtection() ->setLocked(Protection::PROTECTION_PROTECTED); $sheet->getCell('C1')->setValue('X'); $sheet->getCell('C2')->setValue('=SUM(1,2)'); self::assertFalse($sheet->isCellLocked('A1')); self::assertFalse($sheet->isCellLocked('A2')); self::assertFalse($sheet->isCellLocked('B1')); self::assertFalse($sheet->isCellLocked('B2')); self::assertFalse($sheet->isCellLocked('C1')); self::assertFalse($sheet->isCellLocked('C2')); $sheetProtection = $sheet->getProtection(); $sheetProtection->setSheet(true); self::assertFalse($sheet->isCellLocked('A1')); self::assertFalse($sheet->isCellLocked('A2')); self::assertTrue($sheet->isCellLocked('B1')); self::assertTrue($sheet->isCellLocked('B2')); self::assertTrue($sheet->isCellLocked('C1')); self::assertTrue($sheet->isCellLocked('C2')); self::assertTrue($sheet->isCellLocked('D1'), 'uninitialized cell'); self::assertFalse($sheet->cellExists('D1')); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/WorksheetParentTest.php
tests/PhpSpreadsheetTests/Worksheet/WorksheetParentTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception as SpException; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class WorksheetParentTest extends TestCase { public function testNormal(): void { $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); self::assertSame($spreadsheet, $worksheet->getParent()); self::assertSame($spreadsheet, $worksheet->getParentOrThrow()); } public function testGetParent(): void { $worksheet = new Worksheet(); self::assertNull($worksheet->getParent()); } public function testGetParentOrThrow(): void { $this->expectException(SpException::class); $this->expectExceptionMessage('Sheet does not have a parent'); $worksheet = new Worksheet(); $worksheet->getParentOrThrow(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php
tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet; use PhpOffice\PhpSpreadsheet\Exception as Except; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\RowIterator; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class RowIteratorTest extends TestCase { private const CELL_VALUES = [ [110, 120, 130], [210, 220], [310, 320, 330], [410, 420], [510, 520, 530], [610, 620], ]; private static function getPopulatedSheet(Spreadsheet $spreadsheet): Worksheet { $sheet = $spreadsheet->getActiveSheet(); $sheet->fromArray(self::CELL_VALUES); return $sheet; } public function testIteratorFullRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowIterator($sheet); $rowIndexResult = 1; self::assertEquals($rowIndexResult, $iterator->key()); $counter = 0; foreach ($iterator as $key => $row) { ++$counter; self::assertEquals($rowIndexResult++, $key); } self::assertCount($counter, self::CELL_VALUES); $spreadsheet->disconnectWorksheets(); } public function testIteratorStartEndRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowIterator($sheet, 2, 4); $rowIndexResult = 2; self::assertEquals($rowIndexResult, $iterator->key()); $counter = 0; foreach ($iterator as $key => $row) { ++$counter; self::assertEquals($rowIndexResult++, $key); } self::assertSame(3, $counter); $spreadsheet->disconnectWorksheets(); } public function testIteratorSeekAndPrev(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowIterator($sheet, 2, 4); $columnIndexResult = 4; $iterator->seek(4); self::assertEquals($columnIndexResult, $iterator->key()); $counter = 0; while ($iterator->valid() !== false) { ++$counter; self::assertEquals($columnIndexResult, $iterator->key()); --$columnIndexResult; $iterator->prev(); } self::assertSame(3, $counter); $spreadsheet->disconnectWorksheets(); } public function testIteratorResetStart(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowIterator($sheet, 2, 4); $iterator->resetStart(5); $key = $iterator->key(); self::assertSame(5, $key); $lastRow = $iterator->key(); while ($iterator->valid() !== false) { $iterator->next(); $lastRow = $iterator->key(); } self::assertSame(6, $lastRow); $spreadsheet->disconnectWorksheets(); } public function testSeekOutOfRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $this->expectException(Except::class); $iterator = new RowIterator($sheet, 2, 4); $iterator->seek(1); } public function testPrevOutOfRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $iterator = new RowIterator($sheet, 2, 4); $iterator->prev(); self::assertFalse($iterator->valid()); } public function testResetStartOutOfRange(): void { $spreadsheet = new Spreadsheet(); $sheet = self::getPopulatedSheet($spreadsheet); $this->expectException(Except::class); $iterator = new RowIterator($sheet, 2, 4); $iterator->resetStart(10); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Table/ColumnTest.php
tests/PhpSpreadsheetTests/Worksheet/Table/ColumnTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table\Column; class ColumnTest extends SetupTeardown { protected function initTable(): Table { $sheet = $this->getSheet(); $sheet->getCell('G1')->setValue('Heading'); $sheet->getCell('G2')->setValue(2); $sheet->getCell('G3')->setValue(3); $sheet->getCell('G4')->setValue(4); $sheet->getCell('H1')->setValue('Heading2'); $sheet->getCell('H2')->setValue(1); $sheet->getCell('H3')->setValue(2); $sheet->getCell('H4')->setValue(3); $this->maxRow = $maxRow = 4; $table = new Table(); $table->setRange("G1:H$maxRow"); return $table; } public function testVariousGets(): void { $table = $this->initTable(); $column = $table->getColumn('H'); $result = $column->getColumnIndex(); self::assertEquals('H', $result); } public function testGetBadColumnIndex(): void { $this->expectException(PhpSpreadsheetException::class); $this->expectExceptionMessage('Column is outside of current table range.'); $table = $this->initTable(); $table->getColumn('B'); } public function testSetColumnIndex(): void { $table = $this->initTable(); $column = $table->getColumn('H'); $column->setShowFilterButton(false); $expectedResult = 'G'; $result = $column->setColumnIndex($expectedResult); $result = $result->getColumnIndex(); self::assertEquals($expectedResult, $result); } public function testVariousSets(): void { $table = $this->initTable(); $column = $table->getColumn('H'); $result = $column->setShowFilterButton(false); self::assertFalse($result->getShowFilterButton()); $label = 'Total'; $result = $column->setTotalsRowLabel($label); self::assertEquals($label, $result->getTotalsRowLabel()); $function = 'sum'; $result = $column->setTotalsRowFunction($function); self::assertEquals($function, $result->getTotalsRowFunction()); $formula = '=SUM(Sales_Data[[#This Row],[Q1]:[Q4]])'; $column->setColumnFormula($formula); self::assertEquals($formula, $column->getColumnFormula()); } public function testTable(): void { $table = $this->initTable(); $column = new Column('H'); $column->setTable($table); self::assertEquals($table, $column->getTable()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Table/FormulaTest.php
tests/PhpSpreadsheetTests/Worksheet/Table/FormulaTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PHPUnit\Framework\TestCase; class FormulaTest extends TestCase { public function testCellFormulaUpdateOnTableNameChange(): void { $reader = new Xlsx(); $filename = 'tests/data/Worksheet/Table/TableFormulae.xlsx'; $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); // Verify original formulae // Row Formula self::assertSame("=DeptSales[[#This Row],[Sales\u{a0}Amount]]*DeptSales[[#This Row],[% Commission]]", $worksheet->getCell('E2')->getValue()); // Totals Formula self::assertSame('=SUBTOTAL(109,DeptSales[Commission Amount])', $worksheet->getCell('E8')->getValue()); $table = $worksheet->getTableCollection()[0]; if ($table === null) { self::markTestSkipped('Unable to read table for testing.'); } $table->setName('tblSalesByDepartment'); // Verify modified formulae // Row Formula self::assertSame("=tblSalesByDepartment[[#This Row],[Sales\u{a0}Amount]]*tblSalesByDepartment[[#This Row],[% Commission]]", $worksheet->getCell('E2')->getValue()); // Totals Formula self::assertSame('=SUBTOTAL(109,tblSalesByDepartment[Commission Amount])', $worksheet->getCell('E8')->getValue()); $spreadsheet->disconnectWorksheets(); } public function testNamedFormulaUpdateOnTableNameChange(): void { $reader = new Xlsx(); $filename = 'tests/data/Worksheet/Table/TableFormulae.xlsx'; $spreadsheet = $reader->load($filename); $table = $spreadsheet->getActiveSheet()->getTableCollection()[0]; if ($table === null) { self::markTestSkipped('Unable to read table for testing.'); } $namedFormula = $spreadsheet->getNamedFormula('CommissionTotal'); if ($namedFormula === null) { self::markTestSkipped('Unable to read named formula for testing.'); } // Verify original formula self::assertSame('SUBTOTAL(109,DeptSales[Commission Amount])', $namedFormula->getFormula()); $table->setName('tblSalesByDepartment'); // Verify modified formula self::assertSame('SUBTOTAL(109,tblSalesByDepartment[Commission Amount])', $namedFormula->getFormula()); $spreadsheet->disconnectWorksheets(); } public function testCellFormulaUpdateOnHeadingColumnChange(): void { $reader = new Xlsx(); $filename = 'tests/data/Worksheet/Table/TableFormulae.xlsx'; $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); // Verify original formulae // Row Formula self::assertSame("=DeptSales[[#This Row],[Sales\u{a0}Amount]]*DeptSales[[#This Row],[% Commission]]", $worksheet->getCell('E2')->getValue()); // Totals Formula self::assertSame('=SUBTOTAL(109,DeptSales[Commission Amount])', $worksheet->getCell('E8')->getValue()); $worksheet->getCell('D1')->setValue('Commission %age'); $worksheet->getCell('E1')->setValue('Commission'); // Verify modified formulae // Row Formula self::assertSame("=DeptSales[[#This Row],[Sales\u{a0}Amount]]*DeptSales[[#This Row],[Commission %age]]", $worksheet->getCell('E2')->getValue()); // Totals Formula self::assertSame('=SUBTOTAL(109,DeptSales[Commission])', $worksheet->getCell('E8')->getValue()); $spreadsheet->disconnectWorksheets(); } public function testCellFormulaUpdateOnHeadingColumnChangeSlash(): void { $reader = new Xlsx(); $filename = 'tests/data/Worksheet/Table/TableFormulae.xlsx'; $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); // Verify original formulae // Row Formula self::assertSame("=DeptSales[[#This Row],[Sales\u{a0}Amount]]*DeptSales[[#This Row],[% Commission]]", $worksheet->getCell('E2')->getValue()); // Totals Formula self::assertSame('=SUBTOTAL(109,DeptSales[Commission Amount])', $worksheet->getCell('E8')->getValue()); $worksheet->getCell('D1')->setValue('Commission %age'); $worksheet->getCell('E1')->setValue('Commission/Amount'); $worksheet->getCell('E1')->setValue('Commission/Amount2'); // Verify modified formulae // Row Formula self::assertSame("=DeptSales[[#This Row],[Sales\u{a0}Amount]]*DeptSales[[#This Row],[Commission %age]]", $worksheet->getCell('E2')->getValue()); // Totals Formula self::assertSame('=SUBTOTAL(109,DeptSales[Commission/Amount2])', $worksheet->getCell('E8')->getValue()); $spreadsheet->disconnectWorksheets(); } public function testNamedFormulaUpdateOnHeadingColumnChange(): void { $reader = new Xlsx(); $filename = 'tests/data/Worksheet/Table/TableFormulae.xlsx'; $spreadsheet = $reader->load($filename); $worksheet = $spreadsheet->getActiveSheet(); $table = $spreadsheet->getActiveSheet()->getTableCollection()[0]; if ($table === null) { self::markTestSkipped('Unable to read table for testing.'); } $namedFormula = $spreadsheet->getNamedFormula('CommissionTotal'); if ($namedFormula === null) { self::markTestSkipped('Unable to read named formula for testing.'); } // Verify original formula self::assertSame('SUBTOTAL(109,DeptSales[Commission Amount])', $namedFormula->getFormula()); $worksheet->getCell('E1')->setValue('Commission'); // Verify modified formula self::assertSame('SUBTOTAL(109,DeptSales[Commission])', $namedFormula->getFormula()); $spreadsheet->disconnectWorksheets(); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Table/SetupTeardown.php
tests/PhpSpreadsheetTests/Worksheet/Table/SetupTeardown.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class SetupTeardown extends TestCase { private ?Spreadsheet $spreadsheet = null; private ?Worksheet $sheet = null; protected int $maxRow = 4; protected function tearDown(): void { $this->sheet = null; if ($this->spreadsheet !== null) { $this->spreadsheet->disconnectWorksheets(); $this->spreadsheet = null; } } 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; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php
tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Cell\AddressRange; use PhpOffice\PhpSpreadsheet\Cell\CellAddress; use PhpOffice\PhpSpreadsheet\Cell\CellRange; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table\Column; use PHPUnit\Framework\Attributes\DataProvider; class TableTest extends SetupTeardown { private const INITIAL_RANGE = 'H2:O256'; public function testToString(): void { $expectedResult = self::INITIAL_RANGE; $table = new Table(self::INITIAL_RANGE); // magic __toString should return the active table range $result = (string) $table; self::assertEquals($expectedResult, $result); } #[DataProvider('validTableNamesProvider')] public function testValidTableNames(string $name, string $expected): void { $table = new Table(self::INITIAL_RANGE); $result = $table->setName($name); self::assertSame($expected, $result->getName()); } public static function validTableNamesProvider(): array { return [ ['', ''], ['Table_1', 'Table_1'], ['_table_2', '_table_2'], ['\table_3', '\table_3'], [" Table_4 \n", 'Table_4'], ['table.5', 'table.5'], 'issue 4739' => ['x', 'x'], ['தமிழ்', 'தமிழ்'], // UTF-8 letters with combined character ]; } #[DataProvider('invalidTableNamesProvider')] public function testInvalidTableNames(string $name): void { $table = new Table(self::INITIAL_RANGE); $this->expectException(PhpSpreadsheetException::class); $table->setName($name); } public static function invalidTableNamesProvider(): array { return [ ['C'], ['c'], ['R'], ['r'], ['Z100'], ['Z$100'], ['R1C1'], ['R1C'], ['R11C11'], ['123'], ['=Table'], ['Name/Slash'], ['ிக'], // starting with UTF-8 combined character [bin2hex(random_bytes(255))], // random string with length greater than 255 ]; } public function testUniqueTableNameOnBindToWorksheet(): void { $this->expectException(PhpSpreadsheetException::class); $sheet = $this->getSheet(); $table1 = new Table(); $table1->setName('Table_1'); $sheet->addTable($table1); $table2 = new Table(); $table2->setName('tABlE_1'); // case-insensitive $sheet->addTable($table2); } public function testUniqueTableNameOnNameChange(): void { $this->expectException(PhpSpreadsheetException::class); $sheet = $this->getSheet(); $table1 = new Table(); $table1->setName('Table_1'); $sheet->addTable($table1); $table2 = new Table(); $table2->setName('table_2'); // case-insensitive $sheet->addTable($table2); $table2->setName('tAbLe_1'); } public function testVariousSets(): void { $table = new Table(self::INITIAL_RANGE); $result = $table->setShowHeaderRow(false); self::assertFalse($result->getShowHeaderRow()); $result = $table->setShowTotalsRow(true); self::assertTrue($result->getShowTotalsRow()); } public function testGetWorksheet(): void { $sheet = $this->getSheet(); $table = new Table(self::INITIAL_RANGE); $sheet->addTable($table); $result = $table->getWorksheet(); self::assertSame($sheet, $result); } public function testSetWorksheet(): void { $table = new Table(self::INITIAL_RANGE); $spreadsheet = $this->getSpreadsheet(); $sheet2 = $spreadsheet->createSheet(); // Setters return the instance to implement the fluent interface $result = $table->setWorksheet($sheet2); self::assertSame(self::INITIAL_RANGE, $result->getRange()); } public function testGetRange(): void { $expectedResult = self::INITIAL_RANGE; $table = new Table(self::INITIAL_RANGE); // Result should be the active table range $result = $table->getRange(); self::assertEquals($expectedResult, $result); } /** * @param AddressRange<CellAddress>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $fullRange */ public function xtestSetRangeValidRange(string|array|AddressRange $fullRange, string $actualRange): void { $table = new Table(self::INITIAL_RANGE); $result = $table->setRange($fullRange); self::assertEquals($actualRange, $result->getRange()); } public function testSetRangeValidRange(): void { foreach ($this->validTableRanges() as $arrayEntry) { $this->xtestSetRangeValidRange($arrayEntry[0], $arrayEntry[1]); } } /** @return array{array{array{int, int, int, int}|CellRange|string, string}} */ public function validTableRanges(): array { $sheet = $this->getSheet(); $title = $sheet->getTitle(); return [ ["$title!G1:J512", 'G1:J512'], ['K1:N20', 'K1:N20'], [[3, 5, 6, 8], 'C5:F8'], [new CellRange(new CellAddress('C5', $sheet), new CellAddress('F8', $sheet)), 'C5:F8'], ]; } public function testClearRange(): void { $expectedResult = ''; $table = new Table(self::INITIAL_RANGE); // Setters return the instance to implement the fluent interface $result = $table->setRange(''); // Result should be a clear range $result = $result->getRange(); self::assertEquals($expectedResult, $result); } #[DataProvider('invalidTableRangeProvider')] public function testSetRangeInvalidRange(string $range): void { $this->expectException(PhpSpreadsheetException::class); new Table($range); } public static function invalidTableRangeProvider(): array { return [ ['A1'], ['B1:A4'], ['B12:B4'], ['D1:A1'], ]; } public function testGetColumnsEmpty(): void { // There should be no columns yet defined $table = new Table(self::INITIAL_RANGE); $result = $table->getColumns(); self::assertCount(0, $result); } public function testGetColumnOffset(): void { $columnIndexes = [ 'H' => 0, 'K' => 3, 'M' => 5, ]; $table = new Table(self::INITIAL_RANGE); // If we request a specific column by its column ID, we should get an // integer returned representing the column offset within the range foreach ($columnIndexes as $columnIndex => $columnOffset) { $result = $table->getColumnOffset($columnIndex); self::assertEquals($columnOffset, $result); } } public function testRemoveColumns(): void { $sheet = $this->getSheet(); $sheet->fromArray(range('H', 'O'), null, 'H2'); $table = new Table(self::INITIAL_RANGE); $table->getColumn('L')->setShowFilterButton(false); $sheet->addTable($table); $sheet->removeColumn('K', 2); $result = $table->getRange(); self::assertEquals('H2:M256', $result); // Check that the prop that was set for column L is no longer set self::assertTrue($table->getColumn('L')->getShowFilterButton()); } public function testRemoveRows(): void { $sheet = $this->getSheet(); $sheet->fromArray(range('H', 'O'), null, 'H2'); $table = new Table(self::INITIAL_RANGE); $sheet->addTable($table); $sheet->removeRow(42, 128); $result = $table->getRange(); self::assertEquals('H2:O128', $result); } public function testInsertColumns(): void { $sheet = $this->getSheet(); $sheet->fromArray(range('H', 'O'), null, 'H2'); $table = new Table(self::INITIAL_RANGE); $table->getColumn('N')->setShowFilterButton(false); $sheet->addTable($table); $sheet->insertNewColumnBefore('N', 3); $result = $table->getRange(); self::assertEquals('H2:R256', $result); // Check that column N no longer has a prop self::assertTrue($table->getColumn('N')->getShowFilterButton()); // Check that the prop originally set in column N has been moved to column Q self::assertFalse($table->getColumn('Q')->getShowFilterButton()); } public function testInsertRows(): void { $sheet = $this->getSheet(); $sheet->fromArray(range('H', 'O'), null, 'H2'); $table = new Table(self::INITIAL_RANGE); $sheet->addTable($table); $sheet->insertNewRowBefore(3, 4); $result = $table->getRange(); self::assertEquals('H2:O260', $result); } public function testGetInvalidColumnOffset(): void { $this->expectException(PhpSpreadsheetException::class); $invalidColumn = 'G'; $sheet = $this->getSheet(); $table = new Table(); $table->setWorksheet($sheet); $table->getColumnOffset($invalidColumn); } public function testSetColumnWithString(): void { $expectedResult = 'L'; $table = new Table(self::INITIAL_RANGE); // Setters return the instance to implement the fluent interface $result = $table->setColumn($expectedResult); $result = $result->getColumns(); // Result should be an array of \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\Table\Column // objects for each column we set indexed by the column ID self::assertCount(1, $result); self::assertArrayHasKey($expectedResult, $result); } public function testSetInvalidColumnWithString(): void { $this->expectException(PhpSpreadsheetException::class); $table = new Table(self::INITIAL_RANGE); $invalidColumn = 'A'; $table->setColumn($invalidColumn); } public function testSetColumnWithColumnObject(): void { $expectedResult = 'M'; $columnObject = new Column($expectedResult); $table = new Table(self::INITIAL_RANGE); // Setters return the instance to implement the fluent interface $result = $table->setColumn($columnObject); $result = $result->getColumns(); // Result should be an array of \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\Table\Column // objects for each column we set indexed by the column ID self::assertCount(1, $result); self::assertArrayHasKey($expectedResult, $result); } public function testSetInvalidColumnWithObject(): void { $this->expectException(PhpSpreadsheetException::class); $invalidColumn = 'E'; $table = new Table(self::INITIAL_RANGE); $table->setColumn($invalidColumn); } public function testGetColumns(): void { $table = new Table(self::INITIAL_RANGE); $columnIndexes = ['L', 'M']; foreach ($columnIndexes as $columnIndex) { $table->setColumn($columnIndex); } $result = $table->getColumns(); // Result should be an array of \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\Table\Column // objects for each column we set indexed by the column ID self::assertCount(count($columnIndexes), $result); foreach ($columnIndexes as $columnIndex) { self::assertArrayHasKey($columnIndex, $result); } $table->setRange(''); self::assertCount(0, $table->getColumns()); self::assertSame('', $table->getRange()); } public function testGetColumn(): void { $table = new Table(self::INITIAL_RANGE); $columnIndexes = ['L', 'M']; foreach ($columnIndexes as $columnIndex) { $table->setColumn($columnIndex); } // If we request a specific column by its column ID, we should // get a \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\Table\Column object returned foreach ($columnIndexes as $columnIndex) { $result = $table->getColumn($columnIndex); self::assertSame($columnIndex, $result->getColumnIndex()); } } public function testGetColumnByOffset(): void { $table = new Table(self::INITIAL_RANGE); $columnIndexes = [ 0 => 'H', 3 => 'K', 5 => 'M', ]; // If we request a specific column by its offset, we should // get a \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\Table\Column object returned foreach ($columnIndexes as $columnIndex => $columnID) { $result = $table->getColumnByOffset($columnIndex); self::assertEquals($result->getColumnIndex(), $columnID); } } public function testGetColumnIfNotSet(): void { $table = new Table(self::INITIAL_RANGE); // If we request a specific column by its column ID, we should // get a \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\Table\Column object returned $result = $table->getColumn('K'); self::assertSame('K', $result->getColumnIndex()); } public function testGetColumnWithoutRangeSet(): void { $this->expectException(PhpSpreadsheetException::class); $table = new Table(self::INITIAL_RANGE); // Clear the range $table->setRange(''); $table->getColumn('A'); } public function testClearRangeWithExistingColumns(): void { $table = new Table(self::INITIAL_RANGE); $expectedResult = ''; $columnIndexes = ['L', 'M', 'N']; foreach ($columnIndexes as $columnIndex) { $table->setColumn($columnIndex); } // Setters return the instance to implement the fluent interface $result = $table->setRange(''); // Range should be cleared $result = $result->getRange(); self::assertEquals($expectedResult, $result); // Column array should be cleared $result = $table->getColumns(); self::assertCount(0, $result); } public function testSetRangeWithExistingColumns(): void { $table = new Table(self::INITIAL_RANGE); $expectedResult = 'G1:J512'; // These columns should be retained $columnIndexes1 = ['I', 'J']; foreach ($columnIndexes1 as $columnIndex) { $table->setColumn($columnIndex); } // These columns should be discarded $columnIndexes2 = ['K', 'L', 'M']; foreach ($columnIndexes2 as $columnIndex) { $table->setColumn($columnIndex); } // Setters return the instance to implement the fluent interface $result = $table->setRange($expectedResult); // Range should be correctly set $result = $result->getRange(); self::assertEquals($expectedResult, $result); // Only columns that existed in the original range and that // still fall within the new range should be retained $result = $table->getColumns(); self::assertCount(count($columnIndexes1), $result); } public function testClone(): void { $sheet = $this->getSheet(); $table = new Table(self::INITIAL_RANGE); $sheet->addTable($table); $columnIndexes = ['L', 'M']; foreach ($columnIndexes as $columnIndex) { $table->setColumn($columnIndex); } $result = clone $table; self::assertSame($table->getRange(), $result->getRange()); self::assertNull($result->getWorksheet()); self::assertNotNull($table->getWorksheet()); $tableColumns = $table->getColumns(); $resultColumns = $result->getColumns(); self::assertCount(2, $tableColumns); self::assertCount(2, $resultColumns); self::assertArrayHasKey('L', $tableColumns); self::assertArrayHasKey('L', $resultColumns); self::assertArrayHasKey('M', $tableColumns); self::assertArrayHasKey('M', $resultColumns); } public function testNoWorksheet(): void { $table = new Table(); self::assertNull($table->getWorksheet()); } public function testClearColumn(): void { $table = new Table(self::INITIAL_RANGE); $columnIndexes = ['J', 'K', 'L', 'M']; foreach ($columnIndexes as $columnIndex) { $table->setColumn($columnIndex); } $columns = $table->getColumns(); self::assertCount(4, $columns); self::assertArrayHasKey('J', $columns); self::assertArrayHasKey('K', $columns); self::assertArrayHasKey('L', $columns); self::assertArrayHasKey('M', $columns); $table->clearColumn('K'); $columns = $table->getColumns(); self::assertCount(3, $columns); self::assertArrayHasKey('J', $columns); self::assertArrayHasKey('L', $columns); self::assertArrayHasKey('M', $columns); } public function testAutoFilterRule(): void { $table = new Table(self::INITIAL_RANGE); $columnFilter = $table->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $autoFilterRuleObject = new AutoFilter\Column\Rule($columnFilter); self::assertEquals(AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_FILTER, $autoFilterRuleObject->getRuleType()); $ruleParent = $autoFilterRuleObject->getParent(); if ($ruleParent === null) { self::fail('Unexpected null parent'); } else { self::assertEquals('H', $ruleParent->getColumnIndex()); self::assertSame($columnFilter, $ruleParent); } } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Table/Issue3820Test.php
tests/PhpSpreadsheetTests/Worksheet/Table/Issue3820Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table; class Issue3820Test extends SetupTeardown { public function testTableOnOtherSheet(): void { // Clone worksheet failed when table was on sheet $spreadsheet = $this->getSpreadsheet(); $sheet = $this->getSheet(); $sheet->setTitle('Original'); $sheet->fromArray( [ ['MyCol', 'Colonne2', 'Colonne3'], [10, 20], [2], [3], [4], ], null, 'B1', true ); $table = new Table('B1:D5', 'Tableau1'); $sheet->addTable($table); $clonedSheet = clone $sheet; $clonedSheet->setTitle('Cloned'); $spreadsheet->addsheet($clonedSheet); $originalTable = $spreadsheet->getTableByName('Tableau1'); $newTable = $spreadsheet->getTableByName('Tableau1clone'); self::assertNotNull($newTable); self::assertSame($table, $originalTable); self::assertSame('Cloned', $newTable->getWorksheet()?->getTitle()); self::assertSame('B1:D5', $newTable->getRange()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Table/TableStyleTest.php
tests/PhpSpreadsheetTests/Worksheet/Table/TableStyleTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table\TableStyle; class TableStyleTest extends SetupTeardown { private const INITIAL_RANGE = 'H2:O256'; public function testVariousSets(): void { $table = new Table(self::INITIAL_RANGE); $style = $table->getStyle(); $result = $style->setTheme(TableStyle::TABLE_STYLE_DARK1); self::assertEquals(TableStyle::TABLE_STYLE_DARK1, $result->getTheme()); $style->setShowFirstColumn(true); self::assertTrue($style->getShowFirstColumn()); $style->setShowLastColumn(true); self::assertTrue($style->getShowLastColumn()); $style->setShowRowStripes(true); self::assertTrue($style->getShowRowStripes()); $style->setShowColumnStripes(true); self::assertTrue($style->getShowColumnStripes()); } public function testTable(): void { $table = new Table(self::INITIAL_RANGE); $style = new TableStyle(); $style->setTable($table); self::assertEquals($table, $style->getTable()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Table/Issue3659Test.php
tests/PhpSpreadsheetTests/Worksheet/Table/Issue3659Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table; class Issue3659Test extends SetupTeardown { public function testTableOnOtherSheet(): void { $spreadsheet = $this->getSpreadsheet(); $sheet = $this->getSheet(); $sheet->setTitle('Feuil1'); $tableSheet = $spreadsheet->createSheet(); $tableSheet->setTitle('sheet_with_table'); $tableSheet->fromArray( [ ['MyCol', 'Colonne2', 'Colonne3'], [10, 20], [2], [3], [4], ], null, 'B1', true ); $table = new Table('B1:D5', 'Tableau1'); $tableSheet->addTable($table); $sheet->setSelectedCells('F7'); $tableSheet->setSelectedCells('F8'); self::assertSame($sheet, $spreadsheet->getActiveSheet()); $sheet->getCell('A1')->setValue('=SUM(Tableau1[MyCol])'); $sheet->getCell('A2')->setValue('=SUM(Tableau1[])'); $sheet->getCell('A3')->setValue('=SUM(Tableau1)'); $sheet->getCell('A4')->setValue('=CONCAT(Tableau1)'); self::assertSame(19, $sheet->getCell('A1')->getCalculatedValue()); self::assertSame(39, $sheet->getCell('A2')->getCalculatedValue()); self::assertSame(39, $sheet->getCell('A3')->getCalculatedValue()); self::assertSame('1020234', $sheet->getCell('A4')->getCalculatedValue(), 'Header row not included'); self::assertSame('F7', $sheet->getSelectedCells()); self::assertSame('F8', $tableSheet->getSelectedCells()); self::assertSame($sheet, $spreadsheet->getActiveSheet()); } public function testTableAsArray(): void { $spreadsheet = $this->getSpreadsheet(); $spreadsheet->returnArrayAsArray(); $sheet = $this->getSheet(); $sheet->setTitle('Feuil1'); $tableSheet = $spreadsheet->createSheet(); $tableSheet->setTitle('sheet_with_table'); $tableSheet->fromArray( [ ['MyCol', 'Colonne2', 'Colonne3'], [10, 20], [2], [3], [4], ], null, 'B1', true ); $table = new Table('B1:D5', 'Tableau1'); $tableSheet->addTable($table); $sheet->setSelectedCells('F7'); $tableSheet->setSelectedCells('F8'); self::assertSame($sheet, $spreadsheet->getActiveSheet()); $sheet->getCell('F1')->setValue('=Tableau1[MyCol]'); $sheet->getCell('H1')->setValue('=Tableau1[]'); $sheet->getCell('F9')->setValue('=Tableau1'); $sheet->getCell('J9')->setValue('=CONCAT(Tableau1)'); $sheet->getCell('J11')->setValue('=SUM(Tableau1[])'); $expectedResult = [[10], [2], [3], [4]]; self::assertSame($expectedResult, $sheet->getCell('F1')->getCalculatedValue()); $expectedResult = [ [10, 20, null], [2, null, null], [3, null, null], [4, null, null], ]; self::assertSame($expectedResult, $sheet->getCell('H1')->getCalculatedValue()); self::assertSame($expectedResult, $sheet->getCell('F9')->getCalculatedValue()); self::assertSame('1020234', $sheet->getCell('J9')->getCalculatedValue(), 'Header row not included'); self::assertSame(39, $sheet->getCell('J11')->getCalculatedValue(), 'Header row not included'); self::assertSame('F7', $sheet->getSelectedCells()); self::assertSame('F8', $tableSheet->getSelectedCells()); self::assertSame($sheet, $spreadsheet->getActiveSheet()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Table/RemoveTableTest.php
tests/PhpSpreadsheetTests/Worksheet/Table/RemoveTableTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table; class RemoveTableTest extends SetupTeardown { private const INITIAL_RANGE = 'H2:O256'; public function testRemoveTable(): void { $sheet = $this->getSheet(); $table = new Table(self::INITIAL_RANGE); $table->setName('Table1'); $sheet->addTable($table); self::assertEquals(1, $sheet->getTableCollection()->count()); $sheet->removeTableByName('table1'); // case-insensitive self::assertEquals(0, $sheet->getTableCollection()->count()); } public function testRemoveCollection(): void { $sheet = $this->getSheet(); $table = new Table(self::INITIAL_RANGE); $table->setName('Table1'); $sheet->addTable($table); self::assertEquals(1, $sheet->getTableCollection()->count()); $sheet->removeTableCollection(); self::assertEquals(0, $sheet->getTableCollection()->count()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/Table/Issue3635Test.php
tests/PhpSpreadsheetTests/Worksheet/Table/Issue3635Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table; class Issue3635Test extends SetupTeardown { public function testNewExample(): void { $sheet = $this->getSheet(); $sheet->setTitle('Feuil1'); $sheet->fromArray( [ ['Numbers'], [1], [2], [3], [4], [5], ], null, 'B2', true ); $table = new Table('B2:B7', 'Tableau2'); $sheet->addTable($table); $sheet->getCell('E2')->setValue('=IF(E3>0,SUM(SUM(Tableau2),SUM(Tableau2)),"NOPE")'); $sheet->getCell('G2')->setValue('=IF(G3>0,SUM(SUM(Tableau2),SUM(Tableau2)),"NOPE")'); $sheet->getCell('G3')->setValue(1); self::assertSame('NOPE', $sheet->getCell('E2')->getCalculatedValue()); self::assertSame(30, $sheet->getCell('G2')->getCalculatedValue()); } public function testOriginalExample(): void { $sheet = $this->getSheet(); $formula = '=IF([@BAUTEIL]="EK","Entrauchungsklappe",(IF([@BAUTEIL]="VE","Entrauchungsventilator",(IF([@BAUTEIL]="JK","Jalousieklappe",(IF([@BAUTEIL]="LK","Lichtkuppel","ok")))))))'; $sheet->fromArray( [ ['BAUTEIL', 'Result'], ['EK', $formula], ['VE', $formula], ['EK', $formula], ['JK', $formula], ['LK', $formula], ['None', $formula], [null, $formula], [null, $formula], [null, $formula], ], null, 'A1', true ); $table = new Table('A1:B10', 'Tableau3'); $sheet->addTable($table); self::assertSame('Entrauchungsklappe', $sheet->getCell('B2')->getCalculatedValue()); self::assertSame('Entrauchungsventilator', $sheet->getCell('B3')->getCalculatedValue()); self::assertSame('Entrauchungsklappe', $sheet->getCell('B4')->getCalculatedValue()); self::assertSame('Jalousieklappe', $sheet->getCell('B5')->getCalculatedValue()); self::assertSame('Lichtkuppel', $sheet->getCell('B6')->getCalculatedValue()); self::assertSame('ok', $sheet->getCell('B7')->getCalculatedValue()); self::assertSame('ok', $sheet->getCell('B8')->getCalculatedValue()); self::assertSame('ok', $sheet->getCell('B9')->getCalculatedValue()); self::assertSame('ok', $sheet->getCell('B10')->getCalculatedValue()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/ColumnTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/ColumnTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class ColumnTest extends SetupTeardown { protected function initSheet(): Worksheet { $sheet = $this->getSheet(); $sheet->getCell('G1')->setValue('Heading'); $sheet->getCell('G2')->setValue(2); $sheet->getCell('G3')->setValue(3); $sheet->getCell('G4')->setValue(4); $sheet->getCell('H1')->setValue('Heading2'); $sheet->getCell('H2')->setValue(1); $sheet->getCell('H3')->setValue(2); $sheet->getCell('H4')->setValue(3); $this->maxRow = $maxRow = 4; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("G1:H$maxRow"); return $sheet; } public function testVariousGets(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $result = $columnFilter->getColumnIndex(); self::assertEquals('H', $result); } public function testGetBadColumnIndex(): void { $this->expectException(PhpSpreadsheetException::class); $this->expectExceptionMessage('Column is outside of current autofilter range.'); $sheet = $this->initSheet(); $sheet->getAutoFilter()->getColumn('B'); } public function testSetColumnIndex(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $expectedResult = 'G'; $result = $columnFilter->setColumnIndex($expectedResult); $result = $result->getColumnIndex(); self::assertEquals($expectedResult, $result); } public function testSetParent(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); // Setters return the instance to implement the fluent interface $result = $columnFilter->setParent(null); self::assertSame('filters', $result->getFilterType()); } public function testVariousSets(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $result = $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); $result = $result->getFilterType(); self::assertEquals(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER, $result); $result = $columnFilter->setJoin(Column::AUTOFILTER_COLUMN_JOIN_AND); $result = $result->getJoin(); self::assertEquals(Column::AUTOFILTER_COLUMN_JOIN_AND, $result); } public function testSetInvalidFilterTypeThrowsException(): void { $this->expectException(PhpSpreadsheetException::class); $this->expectExceptionMessage('Invalid filter type for column AutoFilter.'); $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $expectedResult = 'Unfiltered'; $columnFilter->setFilterType($expectedResult); } public function testSetInvalidJoinThrowsException(): void { $this->expectException(PhpSpreadsheetException::class); $this->expectExceptionMessage('Invalid rule connection for column AutoFilter.'); $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $expectedResult = 'Neither'; $columnFilter->setJoin($expectedResult); } public function testGetAttributes(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $attributeSet = [ 'val' => 100, 'maxVal' => 200, ]; $result = $columnFilter->setAttributes($attributeSet); $result = $result->getAttributes(); self::assertSame($attributeSet, $result); } public function testSetAttribute(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $attributeSet = [ 'val' => 100, 'maxVal' => 200, ]; foreach ($attributeSet as $attributeName => $attributeValue) { $columnFilter->setAttribute($attributeName, $attributeValue); } self::assertSame($attributeSet, $columnFilter->getAttributes()); } public function testGetAttribute(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $attributeSet = [ 'val' => 100, 'maxVal' => 200, ]; $columnFilter->setAttributes($attributeSet); foreach ($attributeSet as $attributeName => $attributeValue) { $result = $columnFilter->getAttribute($attributeName); self::assertSame($attributeValue, $result); } $result = $columnFilter->getAttribute('nonExistentAttribute'); self::assertNull($result); } public function testClone(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $originalRule = $columnFilter->getRules(); $result = clone $columnFilter; self::assertSame($columnFilter->getColumnIndex(), $result->getColumnIndex()); self::assertSame($columnFilter->getFilterType(), $result->getFilterType()); self::assertSame($columnFilter->getJoin(), $result->getJoin()); self::assertNull($result->getParent()); self::assertNotNull($columnFilter->getParent()); self::assertContainsOnlyInstancesOf(Rule::class, $result->getRules()); $clonedRule = $result->getRules(); self::assertCount(1, $clonedRule); self::assertCount(1, $originalRule); self::assertNotSame($originalRule[0], $clonedRule[0]); self::assertSame($originalRule[0]->getRuleType(), $clonedRule[0]->getRuleType()); self::assertSame($originalRule[0]->getValue(), $clonedRule[0]->getValue()); self::assertSame($originalRule[0]->getOperator(), $clonedRule[0]->getOperator()); self::assertSame($originalRule[0]->getGrouping(), $clonedRule[0]->getGrouping()); self::assertSame($result, $clonedRule[0]->getParent()); } public function testRuleManipulation(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('H'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $originalRules = $columnFilter->getRules(); self::assertCount(1, $originalRules); $rule0 = $columnFilter->getRule(0); self::assertSame($originalRules[0], $rule0); $rule1 = $columnFilter->getRule(1); self::assertNotEquals($originalRules[0], $rule1); self::assertCount(2, $columnFilter->getRules()); self::assertSame(Column::AUTOFILTER_COLUMN_JOIN_OR, $columnFilter->getJoin()); $columnFilter->setJoin(Column::AUTOFILTER_COLUMN_JOIN_AND); $rule2 = new Rule(); $columnFilter->addRule($rule2); self::assertCount(3, $columnFilter->getRules()); self::assertSame(Column::AUTOFILTER_COLUMN_JOIN_AND, $columnFilter->getJoin()); $columnFilter->deleteRule(2); self::assertCount(2, $columnFilter->getRules()); self::assertSame(Column::AUTOFILTER_COLUMN_JOIN_AND, $columnFilter->getJoin()); $columnFilter->deleteRule(1); self::assertCount(1, $columnFilter->getRules()); self::assertSame(Column::AUTOFILTER_COLUMN_JOIN_OR, $columnFilter->getJoin()); $columnFilter->addRule($rule1); $columnFilter->addRule($rule2); $columnFilter->setJoin(Column::AUTOFILTER_COLUMN_JOIN_AND); self::assertCount(3, $columnFilter->getRules()); self::assertSame(Column::AUTOFILTER_COLUMN_JOIN_AND, $columnFilter->getJoin()); $columnFilter->clearRules(); self::assertCount(0, $columnFilter->getRules()); self::assertSame(Column::AUTOFILTER_COLUMN_JOIN_OR, $columnFilter->getJoin()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/RuleCustomTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/RuleCustomTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; class RuleCustomTest extends SetupTeardown { protected function initSheet(): Worksheet { $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue('Heading'); $sheet->getCell('A2')->setValue(2); $sheet->getCell('A3')->setValue(3); $sheet->getCell('A4')->setValue(4); $sheet->getCell('B1')->setValue('Heading2'); $sheet->getCell('B2')->setValue(1); $sheet->getCell('B3')->setValue(2); $sheet->getCell('B4')->setValue(3); $this->maxRow = $maxRow = 4; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:B$maxRow"); return $sheet; } /** @param mixed[] $expectedResult */ #[DataProvider('providerCondition')] public function testRuleCondition(array $expectedResult, string $condition): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( $condition, 3 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals($expectedResult, $this->getVisible()); } public static function providerCondition(): array { return [ [[3], Rule::AUTOFILTER_COLUMN_RULE_EQUAL], [[2, 4], Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL], [[4], Rule::AUTOFILTER_COLUMN_RULE_GREATERTHAN], [[3, 4], Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL], [[2], Rule::AUTOFILTER_COLUMN_RULE_LESSTHAN], [[2, 3], Rule::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL], ]; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/SetupTeardown.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/SetupTeardown.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class SetupTeardown extends TestCase { private ?Spreadsheet $spreadsheet = null; private ?Worksheet $sheet = null; protected int $maxRow = 4; protected function tearDown(): void { $this->sheet = null; if ($this->spreadsheet !== null) { $this->spreadsheet->disconnectWorksheets(); $this->spreadsheet = null; } } 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; } /** @return int[] */ public function getVisible(): array { return $this->getVisibleSheet($this->getSheet()); } /** @return int[] */ public function getVisibleSheet(Worksheet $sheet): array { $sheet->getAutoFilter()->showHideRows(); $actualVisible = []; for ($row = 2; $row <= $this->maxRow; ++$row) { if ($sheet->isRowVisible($row)) { $actualVisible[] = $row; } } return $actualVisible; } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/RuleTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/RuleTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Exception as SpException; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class RuleTest extends SetupTeardown { protected function initSheet(): Worksheet { $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue('Heading'); $sheet->getCell('A2')->setValue(2); $sheet->getCell('A3')->setValue(3); $sheet->getCell('A4')->setValue(4); $sheet->getCell('B1')->setValue('Heading2'); $sheet->getCell('B2')->setValue(1); $sheet->getCell('B3')->setValue(2); $sheet->getCell('B4')->setValue(3); $this->maxRow = $maxRow = 4; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:B$maxRow"); return $sheet; } public function testRule(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $autoFilterRuleObject = new Rule($columnFilter); self::assertEquals(Rule::AUTOFILTER_RULETYPE_FILTER, $autoFilterRuleObject->getRuleType()); self::assertEquals([3], $this->getVisible()); $ruleParent = $autoFilterRuleObject->getParent(); if ($ruleParent === null) { self::fail('Unexpected null parent'); } else { self::assertEquals('A', $ruleParent->getColumnIndex()); self::assertSame($columnFilter, $ruleParent); } } public function testSetParent(): void { $sheet = $this->initSheet(); $autoFilterRuleObject = new Rule(); $autoFilterRuleObject->setParent($sheet->getAutoFilter()->getColumn('B')); $columnFilter = $autoFilterRuleObject->getParent(); if ($columnFilter === null) { self::fail('Unexpected null parent'); } else { $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); self::assertEquals(Rule::AUTOFILTER_RULETYPE_FILTER, $autoFilterRuleObject->getRuleType()); self::assertEquals([4], $this->getVisible()); } } public function testBadSetRule(): void { $this->expectException(SpException::class); $this->expectExceptionMessage('Invalid operator for column AutoFilter Rule.'); $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( 'xyz', 3 ); } public function testBadSetGrouping(): void { $this->expectException(SpException::class); $this->expectExceptionMessage('Invalid grouping for column AutoFilter Rule.'); $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( '', 3 ); $autoFilterRuleObject = new Rule($columnFilter); $autoFilterRuleObject->setGrouping('xyz'); } public function testClone(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ); $autoFilterRuleObject = new Rule($columnFilter); $result = clone $autoFilterRuleObject; self::assertSame($autoFilterRuleObject->getRuleType(), $result->getRuleType()); self::assertSame($autoFilterRuleObject->getValue(), $result->getValue()); self::assertSame($autoFilterRuleObject->getRuleType(), $result->getRuleType()); self::assertSame($autoFilterRuleObject->getOperator(), $result->getOperator()); self::assertSame($autoFilterRuleObject->getGrouping(), $result->getGrouping()); self::assertNotNull($autoFilterRuleObject->getParent()); self::assertNull($result->getParent()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterTodayTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterTodayTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use DateTimeImmutable; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PHPUnit\Framework\Attributes\DataProvider; class AutoFilterTodayTest extends SetupTeardown { public static function providerYesterdayTodayTomorrow(): array { return [ [[2, 5], Rule::AUTOFILTER_RULETYPE_DYNAMIC_TODAY], [[3, 6], Rule::AUTOFILTER_RULETYPE_DYNAMIC_TOMORROW], [[4, 7], Rule::AUTOFILTER_RULETYPE_DYNAMIC_YESTERDAY], ]; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerYesterdayTodayTomorrow')] public function testYesterdayTodayTomorrow(array $expectedVisible, string $rule): void { // Loop to avoid rare edge case where first calculation // and second do not take place in same day. do { $sheet = $this->getSpreadsheet()->createSheet(); $dtStart = new DateTimeImmutable(); $startDay = $dtStart->format('d'); $sheet->getCell('A1')->setValue('Date'); $sheet->getCell('A2')->setValue('=NOW()'); // cache result for consistency in later calculations $sheet->getCell('A2')->getCalculatedValue(); $sheet->getCell('A3')->setValue('=A2+1'); $sheet->getCell('A4')->setValue('=A2-1'); $sheet->getCell('A5')->setValue('=TODAY()'); // cache result for consistency in later calculations $sheet->getCell('A5')->getCalculatedValue(); $sheet->getCell('A6')->setValue('=A5+1'); $sheet->getCell('A7')->setValue('=A5-1'); $this->maxRow = $maxRow = 7; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, '', $rule ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); $autoFilter->showHideRows(); $dtEnd = new DateTimeImmutable(); $endDay = $dtEnd->format('d'); } while ($startDay !== $endDay); self::assertEquals($expectedVisible, $this->getVisibleSheet($sheet)); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterMonthTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterMonthTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use DateTimeImmutable; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; class AutoFilterMonthTest extends SetupTeardown { public static function providerMonth(): array { return [ [[2, 3], Rule::AUTOFILTER_RULETYPE_DYNAMIC_THISMONTH], [[4], Rule::AUTOFILTER_RULETYPE_DYNAMIC_NEXTMONTH], [[6], Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTMONTH], ]; } private function setCells(Worksheet $sheet, int $startMonth): void { $sheet->getCell('A1')->setValue('Date'); $sheet->getCell('A2')->setValue('=TODAY()'); // cache result for consistency in later calculations $sheet->getCell('A2')->getCalculatedValue(); $sheet->getCell('A3')->setValue('=DATE(YEAR(A2), MONTH(A2), 1)'); if ($startMonth === 12) { $sheet->getCell('A4')->setValue('=DATE(YEAR(A2) + 1, 1, 1)'); $sheet->getCell('A5')->setValue('=DATE(YEAR(A2) + 1, 2, 1)'); } elseif ($startMonth === 11) { $sheet->getCell('A4')->setValue('=DATE(YEAR(A2), MONTH(A2) + 1, 1)'); $sheet->getCell('A5')->setValue('=DATE(YEAR(A2) + 1, 1, 1)'); } else { $sheet->getCell('A4')->setValue('=DATE(YEAR(A2), MONTH(A2) + 1, 1)'); $sheet->getCell('A5')->setValue('=DATE(YEAR(A2), MONTH(A2) + 2, 1)'); } if ($startMonth === 1) { $sheet->getCell('A6')->setValue('=DATE(YEAR(A2) - 1, 12, 1)'); $sheet->getCell('A7')->setValue('=DATE(YEAR(A2) - 1, 10, 1)'); } elseif ($startMonth === 2) { $sheet->getCell('A6')->setValue('=DATE(YEAR(A2), 1, 1)'); $sheet->getCell('A7')->setValue('=DATE(YEAR(A2) - 1, 12, 1)'); } else { $sheet->getCell('A6')->setValue('=DATE(YEAR(A2), MONTH(A2) - 1, 1)'); $sheet->getCell('A7')->setValue('=DATE(YEAR(A2), MONTH(A2) - 2, 1)'); } $sheet->getCell('A8')->setValue('=DATE(YEAR(A2) + 1, MONTH(A2), 1)'); $sheet->getCell('A9')->setValue('=DATE(YEAR(A2) - 1, MONTH(A2), 1)'); $this->maxRow = 9; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerMonth')] public function testMonths(array $expectedVisible, string $rule): void { // Loop to avoid rare edge case where first calculation // and second do not take place in same day. do { $sheet = $this->getSpreadsheet()->createSheet(); $dtStart = new DateTimeImmutable(); $startDay = (int) $dtStart->format('d'); $startMonth = (int) $dtStart->format('m'); $this->setCells($sheet, $startMonth); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, '', $rule ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); $autoFilter->showHideRows(); $dtEnd = new DateTimeImmutable(); $endDay = (int) $dtEnd->format('d'); } while ($startDay !== $endDay); self::assertEquals($expectedVisible, $this->getVisibleSheet($sheet)); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterYearTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterYearTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use DateTimeImmutable; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PHPUnit\Framework\Attributes\DataProvider; class AutoFilterYearTest extends SetupTeardown { public static function providerYear(): array { return [ [[5, 6, 7], Rule::AUTOFILTER_RULETYPE_DYNAMIC_THISYEAR], [[2, 3, 4], Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTYEAR], [[8, 9, 10], Rule::AUTOFILTER_RULETYPE_DYNAMIC_NEXTYEAR], [[], Rule::AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_2], [[2, 5, 8, 11], Rule::AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_1], [[4, 7, 10], Rule::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_11], ]; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerYear')] public function testYears(array $expectedVisible, string $rule): void { // Loop to avoid rare edge case where first calculation // and second do not take place in same day. do { $sheet = $this->getSpreadsheet()->createSheet(); $dtStart = new DateTimeImmutable(); $startDay = (int) $dtStart->format('d'); $sheet->getCell('A1')->setValue('Date'); $year = (int) $dtStart->format('Y') - 1; $row = 1; $iteration = 0; while ($iteration < 3) { for ($month = 3; $month < 13; $month += 4) { ++$row; $sheet->getCell("A$row")->setValue("=DATE($year, $month, 1)"); } ++$year; ++$iteration; } ++$row; $sheet->getCell("A$row")->setValue('=DATE(2041, 1, 1)'); // beyond epoch ++$row; $sheet->getCell("A$row")->setValue(7000989091802000122); // issue 4696 ++$row; // empty row at end $this->maxRow = $maxRow = $row; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, '', $rule ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); $autoFilter->showHideRows(); $dtEnd = new DateTimeImmutable(); $endDay = (int) $dtEnd->format('d'); } while ($startDay !== $endDay); self::assertEquals($expectedVisible, $this->getVisibleSheet($sheet)); } public function testYearToDate(): void { // Loop to avoid rare edge case where first calculation // and second do not take place in same day. do { $sheet = $this->getSpreadsheet()->createSheet(); $dtStart = new DateTimeImmutable(); $startDay = (int) $dtStart->format('d'); $startMonth = (int) $dtStart->format('m'); $sheet->getCell('A1')->setValue('Date'); $sheet->getCell('A2')->setValue('=TODAY()'); // cache result for consistency in later calculations $sheet->getCell('A2')->getCalculatedValue(); $sheet->getCell('A3')->setValue('=DATE(YEAR(A2), 12, 31)'); $sheet->getCell('A4')->setValue('=A3 + 1'); $sheet->getCell('A5')->setValue('=DATE(YEAR(A2), 1, 1)'); $sheet->getCell('A6')->setValue('=A5 - 1'); $this->maxRow = $maxRow = 6; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, '', Rule::AUTOFILTER_RULETYPE_DYNAMIC_YEARTODATE ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); $autoFilter->showHideRows(); $dtEnd = new DateTimeImmutable(); $endDay = (int) $dtEnd->format('d'); } while ($startDay !== $endDay); $expected = ($startMonth === 12 && $startDay === 31) ? [2, 3, 5] : [2, 5]; self::assertEquals($expected, $this->getVisibleSheet($sheet)); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterCustomTextTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterCustomTextTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; class AutoFilterCustomTextTest extends SetupTeardown { public function initsheet(): Worksheet { $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue('Header'); $sheet->getCell('A2')->setValue('abc'); $sheet->getCell('A3')->setValue('cBa'); $sheet->getCell('A4')->setValue('cab'); // nothing in cell A5 $sheet->getCell('A6')->setValue('c*b'); $sheet->getCell('A7')->setValue('c?b'); $sheet->getCell('A8')->setValue('a'); $sheet->getCell('A9')->setValue('zzbc'); $sheet->getCell('A10')->setValue('zzbcd'); $sheet->getCell('A11')->setValue('~pqr'); $sheet->getCell('A12')->setValue('pqr~'); $this->maxRow = 12; return $sheet; } public static function providerCustomText(): array { return [ 'begins with a' => [[2, 8], 'a*'], 'ends with b' => [[4, 6, 7], '*b'], 'contains c' => [[2, 3, 4, 6, 7, 9, 10], '*c*'], 'empty' => [[5], ''], 'contains asterisk' => [[6], '*~**'], 'contains question mark' => [[7], '*~?*'], 'c followed by character followed by b' => [[4, 6, 7], 'c?b'], 'one character followed by bc' => [[2], '?bc'], 'two characters followed by bc' => [[9], '??bc'], 'starts with z ends with c' => [[9], 'z*c'], 'starts with tilde' => [[11], '~~*'], 'contains tilde' => [[11, 12], '*~~*'], ]; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerCustomText')] public function testCustomTest(array $expectedVisible, string $pattern): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, $pattern ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals($expectedVisible, $this->getVisible()); } public function testCustomTestNotEqualBlank(): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, '' ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals([2, 3, 4, 6, 7, 8, 9, 10, 11, 12], $this->getVisible()); } public function testCustomTestNotEqualString(): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 'cba' ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals([2, 4, 5, 6, 7, 8, 9, 10, 11, 12], $this->getVisible()); } public function testEqualsListSimple(): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'c?b' // simple filter - no wildcards ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'a' ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_FILTER); self::assertEquals([7, 8], $this->getVisible()); } public function testEqualsList(): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->setJoin(Column::AUTOFILTER_COLUMN_JOIN_OR); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'a*' ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, '*c*' ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals([2, 3, 4, 6, 7, 8, 9, 10], $this->getVisible()); } public function testNotEqualsList(): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->setJoin(Column::AUTOFILTER_COLUMN_JOIN_AND); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 'a*' ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, '*c*' ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals([5, 11, 12], $this->getVisible()); } public static function providerCustomRule(): array { return [ 'equal to cba' => [[3], Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'cba'], 'not equal to cba' => [[2, 4, 5, 6, 7, 8, 9, 10, 11, 12], Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 'cba'], 'greater than cba' => [[9, 10, 11, 12], Rule::AUTOFILTER_COLUMN_RULE_GREATERTHAN, 'cba'], 'greater than or equal to cba' => [[3, 9, 10, 11, 12], Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL, 'cba'], 'less than cba' => [[2, 4, 5, 6, 7, 8], Rule::AUTOFILTER_COLUMN_RULE_LESSTHAN, 'cba'], 'less than or equal to cba' => [[2, 3, 4, 5, 6, 7, 8], Rule::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL, 'cba'], ]; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerCustomRule')] public function testCustomRuleTest(array $expectedVisible, string $rule, string $comparand): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( $rule, $comparand ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals($expectedVisible, $this->getVisible()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/RuleDateGroupTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/RuleDateGroupTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Exception as SpException; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class RuleDateGroupTest extends SetupTeardown { protected function initSheet(): Worksheet { $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue('Date'); $sheet->getCell('A2')->setValue('=DATE(2011,1,10)'); $sheet->getCell('A3')->setValue('=DATE(2012,1,10)'); $sheet->getCell('A4')->setValue('=DATE(2011,1,10)'); $sheet->getCell('A5')->setValue('=DATE(2012,2,10)'); $sheet->getCell('A6')->setValue('=DATE(2012,1,1)'); $sheet->getCell('A7')->setValue('=DATE(2012,12,31)'); $sheet->getCell('B1')->setValue('Heading2'); $sheet->getCell('B2')->setValue(1); $sheet->getCell('B3')->setValue(2); $sheet->getCell('B4')->setValue(3); $sheet->getCell('B5')->setValue(4); $sheet->getCell('B6')->setValue(5); $sheet->getCell('B7')->setValue(6); $this->maxRow = $maxRow = 7; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:B$maxRow"); return $sheet; } public function testYearGroup(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'year' => 2012, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([3, 5, 6, 7], $this->getVisible()); } public function testYearGroupWithInvalidIndex(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'year' => 2012, 'xyz' => 5, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([3, 5, 6, 7], $this->getVisible()); } public function testYearGroupNoValidIndexes(): void { $this->expectException(SpException::class); $this->expectExceptionMessage('Invalid rule value for column AutoFilter Rule.'); $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'zzyear' => 2012, 'xyz' => 5, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([3, 5, 6, 7], $this->getVisible()); } public function testYearGroupBadRuleType(): void { $this->expectException(SpException::class); $this->expectExceptionMessage('Invalid rule type for column AutoFilter Rule.'); $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'year' => 2012, ] ) ->setRuleType( 'xyz' ); self::assertEquals([3, 5, 6, 7], $this->getVisible()); } public function testYearMonthGroup(): void { $sheet = $this->initSheet(); $columnFilter = $sheet->getAutoFilter()->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'year' => 2012, 'month' => 1, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([3, 6], $this->getVisible()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterAverageTop10Test.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterAverageTop10Test.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; class AutoFilterAverageTop10Test extends SetupTeardown { public function initsheet(): Worksheet { $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue('Header'); $sheet->getCell('A2')->setValue(1); $sheet->getCell('A3')->setValue(3); $sheet->getCell('A4')->setValue(5); $sheet->getCell('A5')->setValue(7); $sheet->getCell('A6')->setValue(9); $sheet->getCell('A7')->setValue(2); $sheet->getCell('A8')->setValue(4); $sheet->getCell('A9')->setValue(6); $sheet->getCell('A10')->setValue(8); $this->maxRow = 10; return $sheet; } public static function providerAverage(): array { return [ [[5, 6, 9, 10], Rule::AUTOFILTER_RULETYPE_DYNAMIC_ABOVEAVERAGE], [[2, 3, 7, 8], Rule::AUTOFILTER_RULETYPE_DYNAMIC_BELOWAVERAGE], ]; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerAverage')] public function testAboveAverage(array $expectedVisible, string $rule): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, '', $rule ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); self::assertEquals($expectedVisible, $this->getVisible()); } public static function providerTop10(): array { return [ [[6, 10], Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP, 2], [[2, 3, 7], Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM, 3], [[6], Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP, 10], [[2, 3, 7], Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM, 40], ]; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerTop10')] public function testTop10(array $expectedVisible, string $rule, string $ruleType, int $count): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_TOPTENFILTER); $columnFilter->createRule() ->setRule( $rule, $count, $ruleType ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_TOPTENFILTER); self::assertEquals($expectedVisible, $this->getVisible()); } public function initsheetTies(): Worksheet { $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue('Header'); $sheet->getCell('A2')->setValue(1); $sheet->getCell('A3')->setValue(3); $sheet->getCell('A4')->setValue(3); $sheet->getCell('A5')->setValue(7); $sheet->getCell('A6')->setValue(9); $sheet->getCell('A7')->setValue(4); $sheet->getCell('A8')->setValue(4); $sheet->getCell('A9')->setValue(8); $sheet->getCell('A10')->setValue(8); $this->maxRow = 10; return $sheet; } public static function providerTop10Ties(): array { return [ [[2, 3, 4], Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM, 2], [[2], Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM, 1], [[5, 6, 7, 8, 9, 10], Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP, 5], [[6], Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP, 1], [[2, 3, 4], Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM, 25], [[6, 9, 10], Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP, 25], ]; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerTop10Ties')] public function testTop10Ties(array $expectedVisible, string $rule, string $ruleType, int $count): void { $sheet = $this->initSheetTies(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_TOPTENFILTER); $columnFilter->createRule() ->setRule( $rule, $count, $ruleType ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_TOPTENFILTER); self::assertEquals($expectedVisible, $this->getVisible()); } public function testTop10Exceeds500(): void { $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue('Heading'); for ($row = 2; $row < 602; ++$row) { $sheet->getCell("A$row")->setValue($row); } $maxRow = $this->maxRow = 601; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_TOPTENFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE, 550, Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_TOPTENFILTER); self::assertCount(500, $this->getVisible(), 'Top10 Filter limited to 500 items plus ties'); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterCustomNumericTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterCustomNumericTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; class AutoFilterCustomNumericTest extends SetupTeardown { public function initsheet(): Worksheet { $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue('Header'); $sheet->getCell('A2')->setValue(1); $sheet->getCell('A3')->setValue(3); $sheet->getCell('A4')->setValue(5); // nothing in cell A5 $sheet->getCell('A6')->setValue(7); $sheet->getCell('A7')->setValue(9); $sheet->getCell('A8')->setValue(7); $sheet->getCell('A9')->setValue(5); $sheet->getCell('A10')->setValue(3); $sheet->getCell('A11')->setValue(1); $sheet->getCell('A12')->setValue('x'); $this->maxRow = 12; return $sheet; } public static function providerCustomRule(): array { return [ 'equal to 3' => [[3, 10], Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3], 'not equal to 3' => [[2, 4, 5, 6, 7, 8, 9, 11, 12], Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 3], 'greater than 3' => [[4, 6, 7, 8, 9], Rule::AUTOFILTER_COLUMN_RULE_GREATERTHAN, 3], 'greater than or equal to 3' => [[3, 4, 6, 7, 8, 9, 10], Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL, 3], 'less than 3' => [[2, 11], Rule::AUTOFILTER_COLUMN_RULE_LESSTHAN, 3], 'less than or equal to 3' => [[2, 3, 10, 11], Rule::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL, 3], ]; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerCustomRule')] public function testCustomTest(array $expectedVisible, string $rule, int $comparand): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( $rule, $comparand ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals($expectedVisible, $this->getVisible()); } public function testEqualsListSimple(): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 5 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 7 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_FILTER); self::assertEquals([3, 4, 6, 8, 9, 10], $this->getVisible()); } public function testEqualsList(): void { $sheet = $this->initSheet(); $sheet->getRowDimension(4)->setRowHeight(25); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->setJoin(Column::AUTOFILTER_COLUMN_JOIN_OR); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 3 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 5 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals([3, 4, 9, 10], $this->getVisible()); self::assertTrue($sheet->rowDimensionExists(2)); self::assertFalse($sheet->rowDimensionExists(3), 'row visible by default'); self::assertTrue($sheet->rowDimensionExists(4), 'row is visible but height has been set'); self::assertTrue($sheet->rowDimensionExists(5)); self::assertTrue($sheet->rowDimensionExists(6)); self::assertTrue($sheet->rowDimensionExists(7)); self::assertTrue($sheet->rowDimensionExists(8)); self::assertFalse($sheet->rowDimensionExists(9), 'row visible by default'); self::assertFalse($sheet->rowDimensionExists(10), 'row visible by default'); self::assertTrue($sheet->rowDimensionExists(11)); self::assertTrue($sheet->rowDimensionExists(12)); } public function testNotEqualsList(): void { $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->setJoin(Column::AUTOFILTER_COLUMN_JOIN_AND); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 3 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 5 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals([2, 5, 6, 7, 8, 11, 12], $this->getVisible()); } public function testNotEqualsListWith3Members(): void { $this->expectException(PhpSpreadsheetException::class); $this->expectExceptionMessage('No more than 2 rules'); $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); $columnFilter->setJoin(Column::AUTOFILTER_COLUMN_JOIN_AND); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 3 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 5 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 7 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); self::assertEquals([2, 5, 7, 11, 12], $this->getVisible()); } public function testNotEqualsListWith3MembersFilterTypeAfterRules(): void { $this->expectException(PhpSpreadsheetException::class); $this->expectExceptionMessage('No more than 2 rules'); $sheet = $this->initSheet(); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setJoin(Column::AUTOFILTER_COLUMN_JOIN_AND); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 3 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 5 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL, 7 ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); self::assertEquals([2, 5, 7, 11, 12], $this->getVisible()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/DateGroupTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/DateGroupTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class DateGroupTest extends SetupTeardown { protected function initSheet(int $year): Worksheet { $sheet = $this->getSheet(); $sheet->getCell('A1')->setValue('Date'); $sheet->getCell('B1')->setValue('Time'); $sheet->getCell('C1')->setValue('DateTime'); $sheet->getCell('C1')->setValue('Row*10'); for ($row = 2; $row < 63; ++$row) { $sheet->getCell("A$row")->setValue("=DATE($year,11,30)+$row"); $hour = $row % 24; $minute = $row % 10; $second = $row % 20; $sheet->getCell("B$row")->setValue("=TIME($hour,$minute,$second)"); $sheet->getCell("C$row")->setValue("=A$row+B$row"); $sheet->getCell("D$row")->setValue("=10*$row"); } $this->maxRow = $maxRow = 62; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:C$maxRow"); return $sheet; } public function testYearMonthDayGroup(): void { $year = 2011; $sheet = $this->initSheet($year); $columnFilter = $sheet->getAutoFilter()->getColumn('C'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'year' => $year, 'month' => 12, 'day' => 6, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([6], $this->getVisible()); } public function testIssue4696(): void { $year = 2011; $sheet = $this->initSheet($year); $sheet->getCell('A2')->setValue(7000989091802000122); $columnFilter = $sheet->getAutoFilter()->getColumn('C'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'year' => $year, 'month' => 12, 'day' => 6, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([6], $this->getVisible()); } public function testYearMonthDayHourMinuteSecond1Group(): void { $year = 2011; $sheet = $this->initSheet($year); $columnFilter = $sheet->getAutoFilter()->getColumn('C'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'year' => $year, 'month' => 12, 'day' => 6, 'hour' => 6, 'minute' => 6, 'second' => 6, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); $sheet->getCell('C5')->setValue(''); // make an empty cell in range self::assertEquals([6], $this->getVisible()); } public function testYearMonthDayHourMinuteSecond2Group(): void { $year = 2011; $sheet = $this->initSheet($year); $columnFilter = $sheet->getAutoFilter()->getColumn('C'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'year' => $year, 'month' => 12, 'day' => 6, 'hour' => 6, 'minute' => 6, 'second' => 7, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([], $this->getVisible()); } public function testDayGroupEpoch(): void { $year = 2040; $sheet = $this->initSheet($year); $columnFilter = $sheet->getAutoFilter()->getColumn('C'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'year' => $year, 'month' => 12, 'day' => 6, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([6], $this->getVisible()); } public function testDayGroupNonArray(): void { $year = 2011; $sheet = $this->initSheet($year); /** @var int|string */ $cellA2 = $sheet->getCell('A2')->getCalculatedValue(); $columnFilter = $sheet->getAutoFilter()->getColumn('C'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, $cellA2 ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([], $this->getVisible()); } public function testHourGroup(): void { $year = 2011; $sheet = $this->initSheet($year); $columnFilter = $sheet->getAutoFilter()->getColumn('B'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'hour' => 14, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([14, 38, 62], $this->getVisible()); } public function testHourMinuteGroup(): void { $year = 2011; $sheet = $this->initSheet($year); $columnFilter = $sheet->getAutoFilter()->getColumn('B'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'hour' => 14, 'minute' => 8, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([38], $this->getVisible()); } public function testHourMinuteSecondGroup1(): void { $year = 2011; $sheet = $this->initSheet($year); $columnFilter = $sheet->getAutoFilter()->getColumn('B'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'hour' => 14, 'minute' => 8, 'second' => 18, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([38], $this->getVisible()); } public function testHourMinuteSecondGroup2(): void { $year = 2011; $sheet = $this->initSheet($year); $columnFilter = $sheet->getAutoFilter()->getColumn('B'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, [ 'hour' => 14, 'minute' => 8, 'second' => 19, ] ) ->setRuleType( Rule::AUTOFILTER_RULETYPE_DATEGROUP ); self::assertEquals([], $this->getVisible()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/DeleteAutoFilterTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/DeleteAutoFilterTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; class DeleteAutoFilterTest extends SetupTeardown { public function testDelete(): void { // Issue 2281 - deprecation in PHP81 when deleting filter $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange('H2:O256'); $sheet->removeAutoFilter(); self::assertSame('', $sheet->getAutoFilter()->getRange()); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterWeekTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterWeekTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use DateTimeImmutable; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; class AutoFilterWeekTest extends SetupTeardown { public static function providerWeek(): array { return [ [[2, 3], Rule::AUTOFILTER_RULETYPE_DYNAMIC_THISWEEK], [[4], Rule::AUTOFILTER_RULETYPE_DYNAMIC_NEXTWEEK], [[6], Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTWEEK], ]; } private function setCells(Worksheet $sheet): void { $sheet->getCell('A1')->setValue('Date'); $sheet->getCell('A2')->setValue('=TODAY()'); // cache result for consistency in later calculations $sheet->getCell('A2')->getCalculatedValue(); $sheet->getCell('B2')->setValue('=WEEKDAY(A2) - 1'); // subtract to get to Sunday $sheet->getCell('A3')->setValue('=DATE(YEAR(A2), MONTH(A2), DAY(A2) - B2)'); $sheet->getCell('A4')->setValue('=DATE(YEAR(A3), MONTH(A3), DAY(A3) + 8)'); $sheet->getCell('A5')->setValue('=DATE(YEAR(A3), MONTH(A3), DAY(A3) + 19)'); $sheet->getCell('A6')->setValue('=DATE(YEAR(A3), MONTH(A3), DAY(A3) - 6)'); $sheet->getCell('A7')->setValue('=DATE(YEAR(A3), MONTH(A3), DAY(A3) - 12)'); $sheet->getCell('A8')->setValue('=DATE(YEAR(A2) + 1, MONTH(A2), 1)'); $sheet->getCell('A9')->setValue('=DATE(YEAR(A2) - 1, MONTH(A2), 1)'); $this->maxRow = 9; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerWeek')] public function testWeek(array $expectedVisible, string $rule): void { // Loop to avoid rare edge case where first calculation // and second do not take place in same day. do { $sheet = $this->getSpreadsheet()->createSheet(); $dtStart = new DateTimeImmutable(); $startDay = (int) $dtStart->format('d'); $this->setCells($sheet); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, '', $rule ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); $autoFilter->showHideRows(); $dtEnd = new DateTimeImmutable(); $endDay = (int) $dtEnd->format('d'); } while ($startDay !== $endDay); self::assertEquals($expectedVisible, $this->getVisibleSheet($sheet)); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterQuarterTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterQuarterTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use DateTimeImmutable; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\Attributes\DataProvider; class AutoFilterQuarterTest extends SetupTeardown { public static function providerQuarter(): array { return [ [[2, 3], Rule::AUTOFILTER_RULETYPE_DYNAMIC_THISQUARTER], [[4], Rule::AUTOFILTER_RULETYPE_DYNAMIC_NEXTQUARTER], [[6], Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTQUARTER], ]; } private function setCells(Worksheet $sheet): void { $sheet->getCell('A1')->setValue('Date'); $sheet->getCell('A2')->setValue('=TODAY()'); // cache result for consistency in later calculations $sheet->getCell('A2')->getCalculatedValue(); $sheet->getCell('A3')->setValue('=DATE(YEAR(A2), MONTH(A2), 1)'); $sheet->getCell('A4')->setValue('=DATE(YEAR(A2), MONTH(A2) + 3, 1)'); $sheet->getCell('A5')->setValue('=DATE(YEAR(A2), MONTH(A2) + 6, 1)'); $sheet->getCell('A6')->setValue('=DATE(YEAR(A2), MONTH(A2) - 3, 1)'); $sheet->getCell('A7')->setValue('=DATE(YEAR(A2), MONTH(A2) - 6, 1)'); $sheet->getCell('A8')->setValue('=DATE(YEAR(A2) + 1, MONTH(A2), 1)'); $sheet->getCell('A9')->setValue('=DATE(YEAR(A2) - 1, MONTH(A2), 1)'); $this->maxRow = 9; } /** @param mixed[] $expectedVisible */ #[DataProvider('providerQuarter')] public function testQuarters(array $expectedVisible, string $rule): void { // Loop to avoid rare edge case where first calculation // and second do not take place in same day. do { $sheet = $this->getSpreadsheet()->createSheet(); $dtStart = new DateTimeImmutable(); $startDay = (int) $dtStart->format('d'); $this->setCells($sheet); $maxRow = $this->maxRow; $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange("A1:A$maxRow"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); $columnFilter->createRule() ->setRule( Rule::AUTOFILTER_COLUMN_RULE_EQUAL, '', $rule ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); $autoFilter->showHideRows(); $dtEnd = new DateTimeImmutable(); $endDay = (int) $dtEnd->format('d'); } while ($startDay !== $endDay); self::assertEquals($expectedVisible, $this->getVisibleSheet($sheet)); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false
PHPOffice/PhpSpreadsheet
https://github.com/PHPOffice/PhpSpreadsheet/blob/e33834b4ea2a02088becbb41fb8954d915b46b12/tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterTest.php
tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterTest.php
<?php declare(strict_types=1); namespace PhpOffice\PhpSpreadsheetTests\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter; use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class AutoFilterTest extends SetupTeardown { private const INITIAL_RANGE = 'H2:O256'; public function testToString(): void { $expectedResult = self::INITIAL_RANGE; $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); // magic __toString should return the active autofilter range $result = (string) $autoFilter; self::assertEquals($expectedResult, $result); } public function testGetParent(): void { $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $result = $autoFilter->getParent(); self::assertSame($sheet, $result); } public function testSetParent(): void { $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $spreadsheet = $this->getSpreadsheet(); $sheet2 = $spreadsheet->createSheet(); // Setters return the instance to implement the fluent interface $result = $autoFilter->setParent($sheet2); self::assertSame(self::INITIAL_RANGE, $result->getRange()); } public function testGetRange(): void { $expectedResult = self::INITIAL_RANGE; $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); // Result should be the active autofilter range $result = $autoFilter->getRange(); self::assertEquals($expectedResult, $result); } public function testSetRange(): void { $sheet = $this->getSheet(); $title = $sheet->getTitle(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $ranges = [ 'G1:J512' => "$title!G1:J512", 'K1:N20' => 'K1:N20', 'B10' => 'B10', ]; foreach ($ranges as $actualRange => $fullRange) { // Setters return the instance to implement the fluent interface $temp = $autoFilter->setRange($fullRange); // Result should be the new autofilter range $result = $temp->getRange(); self::assertEquals($actualRange, $result); } } public function testClearRange(): void { $expectedResult = ''; $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); // Setters return the instance to implement the fluent interface $temp = $autoFilter->setRange(''); // Result should be a clear range $result = $temp->getRange(); self::assertEquals($expectedResult, $result); } public function testSetRangeInvalidRowRange(): void { $this->expectException(PhpSpreadsheetException::class); $expectedResult = '999'; $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange($expectedResult); } public function testSetRangeInvalidColumnRange(): void { $this->expectException(PhpSpreadsheetException::class); $expectedResult = 'ABC'; $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange($expectedResult); } public function testGetColumnsEmpty(): void { // There should be no columns yet defined $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $result = $autoFilter->getColumns(); self::assertSame([], $result); } public function testGetColumnOffset(): void { $columnIndexes = [ 'H' => 0, 'K' => 3, 'M' => 5, ]; $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); // If we request a specific column by its column ID, we should get an // integer returned representing the column offset within the range foreach ($columnIndexes as $columnIndex => $columnOffset) { $result = $autoFilter->getColumnOffset($columnIndex); self::assertEquals($columnOffset, $result); } } public function testRemoveColumns(): void { $sheet = $this->getSheet(); $sheet->fromArray(range('H', 'O'), null, 'H2'); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $autoFilter->getColumn('L')->addRule((new Column\Rule())->setValue(5)); $sheet->removeColumn('K', 2); $result = $autoFilter->getRange(); self::assertEquals('H2:M256', $result); // Check that the rule that was set for column L is no longer set self::assertEmpty($autoFilter->getColumn('L')->getRule(0)->getValue()); } public function testRemoveRows(): void { $sheet = $this->getSheet(); $sheet->fromArray(range('H', 'O'), null, 'H2'); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $sheet->removeRow(42, 128); $result = $autoFilter->getRange(); self::assertEquals('H2:O128', $result); } public function testInsertColumns(): void { $sheet = $this->getSheet(); $sheet->fromArray(range('H', 'O'), null, 'H2'); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $autoFilter->getColumn('N')->addRule((new Column\Rule())->setValue(5)); $sheet->insertNewColumnBefore('N', 3); $result = $autoFilter->getRange(); self::assertEquals('H2:R256', $result); // Check that column N no longer has a rule set self::assertEmpty($autoFilter->getColumn('N')->getRule(0)->getValue()); // Check that the rule originally set in column N has been moved to column Q self::assertSame(5, $autoFilter->getColumn('Q')->getRule(0)->getValue()); } public function testInsertRows(): void { $sheet = $this->getSheet(); $sheet->fromArray(range('H', 'O'), null, 'H2'); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $sheet->insertNewRowBefore(3, 4); $result = $autoFilter->getRange(); self::assertEquals('H2:O260', $result); } public function testGetInvalidColumnOffset(): void { $this->expectException(PhpSpreadsheetException::class); $invalidColumn = 'G'; $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $autoFilter->getColumnOffset($invalidColumn); } public function testSetColumnWithString(): void { $expectedResult = 'L'; $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); // Setters return the instance to implement the fluent interface $temp = $autoFilter->setColumn($expectedResult); $result = $temp->getColumns(); // Result should be an array of \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\AutoFilter\Column // objects for each column we set indexed by the column ID self::assertCount(1, $result); self::assertArrayHasKey($expectedResult, $result); } public function testSetInvalidColumnWithString(): void { $this->expectException(PhpSpreadsheetException::class); $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $invalidColumn = 'A'; $autoFilter->setColumn($invalidColumn); } public function testSetColumnWithColumnObject(): void { $expectedResult = 'M'; $columnObject = new Column($expectedResult); $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); // Setters return the instance to implement the fluent interface $temp = $autoFilter->setColumn($columnObject); $result = $temp->getColumns(); // Result should be an array of \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\AutoFilter\Column // objects for each column we set indexed by the column ID self::assertCount(1, $result); self::assertArrayHasKey($expectedResult, $result); } public function testSetInvalidColumnWithObject(): void { $this->expectException(PhpSpreadsheetException::class); $invalidColumn = 'E'; $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $autoFilter->setColumn($invalidColumn); } public function testGetColumns(): void { $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $columnIndexes = ['L', 'M']; foreach ($columnIndexes as $columnIndex) { $autoFilter->setColumn($columnIndex); } $result = $autoFilter->getColumns(); // Result should be an array of \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\AutoFilter\Column // objects for each column we set indexed by the column ID self::assertCount(count($columnIndexes), $result); foreach ($columnIndexes as $columnIndex) { self::assertArrayHasKey($columnIndex, $result); } $autoFilter->setRange(''); self::assertCount(0, $autoFilter->getColumns()); self::assertSame('', $autoFilter->getRange()); } public function testGetColumn(): void { $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $columnIndexes = ['L', 'M']; foreach ($columnIndexes as $columnIndex) { $autoFilter->setColumn($columnIndex); } // If we request a specific column by its column ID, we should // get a \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\AutoFilter\Column object returned foreach ($columnIndexes as $columnIndex) { $result = $autoFilter->getColumn($columnIndex); self::assertSame($columnIndex, $result->getColumnIndex()); } } public function testGetColumnByOffset(): void { $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $columnIndexes = [ 0 => 'H', 3 => 'K', 5 => 'M', ]; // If we request a specific column by its offset, we should // get a \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\AutoFilter\Column object returned foreach ($columnIndexes as $columnIndex => $columnID) { $result = $autoFilter->getColumnByOffset($columnIndex); self::assertEquals($result->getColumnIndex(), $columnID); } } public function testGetColumnIfNotSet(): void { $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); // If we request a specific column by its column ID, we should // get a \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\AutoFilter\Column object returned $result = $autoFilter->getColumn('K'); self::assertSame('K', $result->getColumnIndex()); } public function testGetColumnWithoutRangeSet(): void { $this->expectException(PhpSpreadsheetException::class); $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); // Clear the range $autoFilter->setRange(''); $autoFilter->getColumn('A'); } public function testClearRangeWithExistingColumns(): void { $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $expectedResult = ''; $columnIndexes = ['L', 'M', 'N']; foreach ($columnIndexes as $columnIndex) { $autoFilter->setColumn($columnIndex); } // Setters return the instance to implement the fluent interface $temp = $autoFilter->setRange(''); // Range should be cleared $result = $temp->getRange(); self::assertEquals($expectedResult, $result); // Column array should be cleared $result = $autoFilter->getColumns(); self::assertSame([], $result); } public function testSetRangeWithExistingColumns(): void { $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $expectedResult = 'G1:J512'; // These columns should be retained $columnIndexes1 = ['I', 'J']; foreach ($columnIndexes1 as $columnIndex) { $autoFilter->setColumn($columnIndex); } // These columns should be discarded $columnIndexes2 = ['K', 'L', 'M']; foreach ($columnIndexes2 as $columnIndex) { $autoFilter->setColumn($columnIndex); } // Setters return the instance to implement the fluent interface $temp = $autoFilter->setRange($expectedResult); // Range should be correctly set $result = $temp->getRange(); self::assertEquals($expectedResult, $result); // Only columns that existed in the original range and that // still fall within the new range should be retained $result = $autoFilter->getColumns(); self::assertCount(count($columnIndexes1), $result); } public function testClone(): void { $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $columnIndexes = ['L', 'M']; foreach ($columnIndexes as $columnIndex) { $autoFilter->setColumn($columnIndex); } $result = clone $autoFilter; self::assertSame($autoFilter->getRange(), $result->getRange()); self::assertNull($result->getParent()); self::assertInstanceOf(Worksheet::class, $autoFilter->getParent()); $autoColumns = $autoFilter->getColumns(); $resultColumns = $result->getColumns(); self::assertCount(2, $autoColumns); self::assertCount(2, $resultColumns); self::assertArrayHasKey('L', $autoColumns); self::assertArrayHasKey('L', $resultColumns); self::assertArrayHasKey('M', $autoColumns); self::assertArrayHasKey('M', $resultColumns); } public function testNoWorksheet(): void { $autoFilter = new AutoFilter(); self::assertSame($autoFilter, $autoFilter->showHideRows()); } public function testClearColumn(): void { $sheet = $this->getSheet(); $autoFilter = $sheet->getAutoFilter(); $autoFilter->setRange(self::INITIAL_RANGE); $columnIndexes = ['J', 'K', 'L', 'M']; foreach ($columnIndexes as $columnIndex) { $autoFilter->setColumn($columnIndex); } $columns = $autoFilter->getColumns(); self::assertCount(4, $columns); self::assertArrayHasKey('J', $columns); self::assertArrayHasKey('K', $columns); self::assertArrayHasKey('L', $columns); self::assertArrayHasKey('M', $columns); $autoFilter->clearColumn('K'); $columns = $autoFilter->getColumns(); self::assertCount(3, $columns); self::assertArrayHasKey('J', $columns); self::assertArrayHasKey('L', $columns); self::assertArrayHasKey('M', $columns); $autoFilter->shiftColumn('L', 'K'); $columns = $autoFilter->getColumns(); self::assertCount(3, $columns); self::assertArrayHasKey('J', $columns); self::assertArrayHasKey('K', $columns); self::assertArrayHasKey('M', $columns); } public function testAutoExtendRange(): void { $spreadsheet = $this->getSpreadsheet(); $worksheet = $spreadsheet->addSheet(new Worksheet($spreadsheet, 'Autosized AutoFilter')); $worksheet->getCell('A1')->setValue('Col 1'); $worksheet->getCell('B1')->setValue('Col 2'); $worksheet->setAutoFilter('A1:B1'); $lastRow = $worksheet->getAutoFilter()->autoExtendRange(1, 1); self::assertSame(1, $lastRow, 'No data below AutoFilter, so there should ne no resize'); $lastRow = $worksheet->getAutoFilter()->autoExtendRange(1, 999); self::assertSame(999, $lastRow, 'Filter range is already correctly sized'); $data = [['A', 'A'], ['B', 'A'], ['A', 'B'], ['C', 'B'], ['B', null], [null, null], ['D', 'D'], ['E', 'E']]; $worksheet->fromArray($data, null, 'A2', true); $lastRow = $worksheet->getAutoFilter()->autoExtendRange(1, 1); self::assertSame(6, $lastRow, 'Filter range has been re-sized incorrectly'); } }
php
MIT
e33834b4ea2a02088becbb41fb8954d915b46b12
2026-01-04T15:02:44.305364Z
false