Scrutinizer Clean Up Tests (#3061)

* Scrutinizer Clean Up Tests

No source code involved.

* Scrutinizer Whack-a-mole

Fixed 17, added 10. Trying again.

* Simplify Some Tests

Eliminate some null assertions.

* Dead Code

Remove 2 statements.
This commit is contained in:
oleibman 2022-09-14 07:11:20 -07:00 committed by GitHub
parent 3e8d50547c
commit 252474c1bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 292 additions and 281 deletions

View File

@ -41,7 +41,7 @@ class DocumentGenerator
private static function tableRow(array $lengths, ?array $values = null): string
{
$result = '';
foreach (array_map(null, $lengths, $values ?? []) as $i => [$length, $value]) {
foreach (array_map(/** @scrutinizer ignore-type */ null, $lengths, $values ?? []) as $i => [$length, $value]) {
$pad = $value === null ? '-' : ' ';
if ($i > 0) {
$result .= '|' . $pad;

View File

@ -11,5 +11,5 @@ $helper->log('Loading file ' . /** @scrutinizer ignore-type */ pathinfo($inputFi
try {
$spreadsheet = IOFactory::load($inputFileName);
} catch (ReaderException $e) {
$helper->log('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
$helper->log('Error loading file "' . /** @scrutinizer ignore-type */ pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}

View File

@ -724,6 +724,19 @@ class Spreadsheet
return null;
}
/**
* Get sheet by name, throwing exception if not found.
*/
public function getSheetByNameOrThrow(string $worksheetName): Worksheet
{
$worksheet = $this->getSheetByName($worksheetName);
if ($worksheet === null) {
throw new Exception("Sheet $worksheetName does not exist.");
}
return $worksheet;
}
/**
* Get index for sheet.
*

View File

@ -3,21 +3,15 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering;
use PhpOffice\PhpSpreadsheet\Calculation\Engineering;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PHPUnit\Framework\TestCase;
class ParseComplexTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
public function testParseComplex(): void
{
[$real, $imaginary, $suffix] = [1.23e-4, 5.67e+8, 'j'];
$result = Engineering::parseComplex('1.23e-4+5.67e+8j');
$result = /** @scrutinizer ignore-deprecated */ Engineering::parseComplex('1.23e-4+5.67e+8j');
self::assertArrayHasKey('real', $result);
self::assertEquals($real, $result['real']);
self::assertArrayHasKey('imaginary', $result);

View File

@ -2,26 +2,45 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Financial;
use PhpOffice\PhpSpreadsheet\Calculation\Financial;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PHPUnit\Framework\TestCase;
class IrrTest extends TestCase
class IrrTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerIRR
*
* @param mixed $expectedResult
* @param mixed $values
*/
public function testIRR($expectedResult, ...$args): void
public function testIRR($expectedResult, $values = null): void
{
$result = Financial::IRR(...$args);
self::assertEqualsWithDelta($expectedResult, $result, 1E-8);
$this->mightHaveException($expectedResult);
$sheet = $this->getSheet();
$formula = '=IRR(';
if ($values !== null) {
if (is_array($values)) {
$row = 0;
foreach ($values as $value) {
if (is_array($value)) {
foreach ($value as $arrayValue) {
++$row;
$sheet->getCell("A$row")->setValue($arrayValue);
}
} else {
++$row;
$sheet->getCell("A$row")->setValue($value);
}
}
$formula .= "A1:A$row";
} else {
$sheet->getCell('A1')->setValue($values);
$formula .= 'A1';
}
}
$formula .= ')';
$sheet->getCell('D1')->setValue($formula);
$result = $sheet->getCell('D1')->getCalculatedValue();
$this->adjustResult($result, $expectedResult);
self::assertEqualsWithDelta($expectedResult, $result, 0.1E-8);
}
public function providerIRR(): array

View File

@ -3,17 +3,11 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
use PHPUnit\Framework\TestCase;
class ColumnsTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerCOLUMNS
*
@ -21,7 +15,7 @@ class ColumnsTest extends TestCase
*/
public function testCOLUMNS($expectedResult, ...$args): void
{
$result = LookupRef::COLUMNS(...$args);
$result = LookupRef::COLUMNS(/** @scrutinizer ignore-type */ ...$args);
self::assertEquals($expectedResult, $result);
}

View File

@ -3,17 +3,11 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
use PHPUnit\Framework\TestCase;
class IndexTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerINDEX
*
@ -21,7 +15,7 @@ class IndexTest extends TestCase
*/
public function testINDEX($expectedResult, ...$args): void
{
$result = LookupRef::INDEX(...$args);
$result = LookupRef::INDEX(/** @scrutinizer ignore-type */ ...$args);
self::assertEquals($expectedResult, $result);
}

View File

@ -3,17 +3,11 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
use PHPUnit\Framework\TestCase;
class RowsTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerROWS
*
@ -21,7 +15,7 @@ class RowsTest extends TestCase
*/
public function testROWS($expectedResult, ...$args): void
{
$result = LookupRef::ROWS(...$args);
$result = LookupRef::ROWS(/** @scrutinizer ignore-type */ ...$args);
self::assertEquals($expectedResult, $result);
}

View File

@ -60,7 +60,7 @@ class RandBetweenTest extends AllSetupTeardown
$formula = "=RandBetween({$argument1}, {$argument2})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertIsArray($result);
self::assertCount($expectedRows, $result);
self::assertCount($expectedRows, /** @scrutinizer ignore-type */ $result);
self::assertIsArray($result[0]);
self::assertCount($expectedColumns, /** @scrutinizer ignore-type */ $result[0]);
}

View File

@ -58,7 +58,7 @@ class DefinedNameTest extends TestCase
DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1')
);
$this->spreadsheet->addDefinedName(
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true)
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=B1', true)
);
self::assertCount(2, $this->spreadsheet->getDefinedNames());
@ -66,7 +66,7 @@ class DefinedNameTest extends TestCase
self::assertNotNull($definedName1);
self::assertSame('=A1', $definedName1->getValue());
$definedName2 = $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getSheetByName('Sheet #2'));
$definedName2 = $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
self::assertNotNull($definedName2);
self::assertSame('=B1', $definedName2->getValue());
}
@ -103,13 +103,13 @@ class DefinedNameTest extends TestCase
DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1')
);
$this->spreadsheet->addDefinedName(
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true)
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=B1', true)
);
$this->spreadsheet->removeDefinedName('Foo', $this->spreadsheet->getActiveSheet());
self::assertCount(1, $this->spreadsheet->getDefinedNames());
$definedName = $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getSheetByName('Sheet #2'));
$definedName = $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
self::assertNotNull($definedName);
self::assertSame('=B1', $definedName->getValue());
}
@ -120,10 +120,10 @@ class DefinedNameTest extends TestCase
DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1')
);
$this->spreadsheet->addDefinedName(
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true)
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=B1', true)
);
$this->spreadsheet->removeDefinedName('Foo', $this->spreadsheet->getSheetByName('Sheet #2'));
$this->spreadsheet->removeDefinedName('Foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
self::assertCount(1, $this->spreadsheet->getDefinedNames());
$definedName = $this->spreadsheet->getDefinedName('foo');
@ -154,10 +154,8 @@ class DefinedNameTest extends TestCase
public function testChangeWorksheet(): void
{
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
self::assertNotNull($sheet1);
self::assertNotNull($sheet2);
$sheet1 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #2');
$sheet1->getCell('A1')->setValue(1);
$sheet2->getCell('A1')->setValue(2);
@ -172,10 +170,8 @@ class DefinedNameTest extends TestCase
public function testLocalOnly(): void
{
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
self::assertNotNull($sheet1);
self::assertNotNull($sheet2);
$sheet1 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #2');
$sheet1->getCell('A1')->setValue(1);
$sheet2->getCell('A1')->setValue(2);
@ -190,10 +186,8 @@ class DefinedNameTest extends TestCase
public function testScope(): void
{
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
self::assertNotNull($sheet1);
self::assertNotNull($sheet2);
$sheet1 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #2');
$sheet1->getCell('A1')->setValue(1);
$sheet2->getCell('A1')->setValue(2);
@ -208,10 +202,8 @@ class DefinedNameTest extends TestCase
public function testClone(): void
{
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
self::assertNotNull($sheet1);
self::assertNotNull($sheet2);
$sheet1 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #2');
$sheet1->getCell('A1')->setValue(1);
$sheet2->getCell('A1')->setValue(2);

View File

@ -58,8 +58,7 @@ class PrintAreaTest extends AbstractFunctional
private static function getPrintArea(Spreadsheet $spreadsheet, string $name): string
{
$sheet = $spreadsheet->getSheetByName($name);
self::assertNotNull($sheet, "Unable to get sheet $name");
$sheet = $spreadsheet->getSheetByNameOrThrow($name);
return $sheet->getPageSetup()->getPrintArea();
}

View File

@ -62,7 +62,7 @@ class NamedFormulaTest extends TestCase
new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
);
$this->spreadsheet->addNamedFormula(
new NamedFormula('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=16%', true)
new NamedFormula('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=16%', true)
);
self::assertCount(2, $this->spreadsheet->getNamedFormulae());
@ -72,7 +72,7 @@ class NamedFormulaTest extends TestCase
'=19%',
$formula->getValue()
);
$formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getSheetByName('Sheet #2'));
$formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
self::assertNotNull($formula);
self::assertSame(
'=16%',
@ -100,13 +100,13 @@ class NamedFormulaTest extends TestCase
new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
);
$this->spreadsheet->addNamedFormula(
new NamedFormula('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=16%', true)
new NamedFormula('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=16%', true)
);
$this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getActiveSheet());
self::assertCount(1, $this->spreadsheet->getNamedFormulae());
$formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getSheetByName('Sheet #2'));
$formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
self::assertNotNull($formula);
self::assertSame(
'=16%',
@ -120,10 +120,10 @@ class NamedFormulaTest extends TestCase
new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
);
$this->spreadsheet->addNamedFormula(
new NamedFormula('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=16%', true)
new NamedFormula('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=16%', true)
);
$this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getSheetByName('Sheet #2'));
$this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
self::assertCount(1, $this->spreadsheet->getNamedFormulae());
$formula = $this->spreadsheet->getNamedFormula('foo');

View File

@ -62,7 +62,7 @@ class NamedRangeTest extends TestCase
new NamedRange('Foo', $this->spreadsheet->getActiveSheet(), '=A1')
);
$this->spreadsheet->addNamedRange(
new NamedRange('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true)
new NamedRange('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=B1', true)
);
self::assertCount(2, $this->spreadsheet->getNamedRanges());
@ -72,7 +72,7 @@ class NamedRangeTest extends TestCase
'=A1',
$range->getValue()
);
$range = $this->spreadsheet->getNamedRange('foo', $this->spreadsheet->getSheetByName('Sheet #2'));
$range = $this->spreadsheet->getNamedRange('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
self::assertNotNull($range);
self::assertSame(
'=B1',
@ -100,13 +100,13 @@ class NamedRangeTest extends TestCase
new NamedRange('Foo', $this->spreadsheet->getActiveSheet(), '=A1')
);
$this->spreadsheet->addNamedRange(
new NamedRange('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true)
new NamedRange('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=B1', true)
);
$this->spreadsheet->removeNamedRange('Foo', $this->spreadsheet->getActiveSheet());
self::assertCount(1, $this->spreadsheet->getNamedRanges());
$sheet = $this->spreadsheet->getNamedRange('foo', $this->spreadsheet->getSheetByName('Sheet #2'));
$sheet = $this->spreadsheet->getNamedRange('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
self::assertNotNull($sheet);
self::assertSame(
'=B1',
@ -120,10 +120,10 @@ class NamedRangeTest extends TestCase
new NamedRange('Foo', $this->spreadsheet->getActiveSheet(), '=A1')
);
$this->spreadsheet->addNamedRange(
new NamedRange('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true)
new NamedRange('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=B1', true)
);
$this->spreadsheet->removeNamedRange('Foo', $this->spreadsheet->getSheetByName('Sheet #2'));
$this->spreadsheet->removeNamedRange('Foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
self::assertCount(1, $this->spreadsheet->getNamedRanges());
$range = $this->spreadsheet->getNamedRange('foo');

View File

@ -56,13 +56,11 @@ class CsvContiguousTest extends TestCase
private static function getCellValue(Spreadsheet $spreadsheet, string $sheetName, string $cellAddress): string
{
$sheet = $spreadsheet->getSheetByName($sheetName);
$sheet = $spreadsheet->getSheetByNameOrThrow($sheetName);
$result = '';
if ($sheet !== null) {
$value = $sheet->getCell($cellAddress)->getValue();
if (is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
$result = (string) $value;
}
$value = $sheet->getCell($cellAddress)->getValue();
if (is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
$result = (string) $value;
}
return $result;

View File

@ -15,20 +15,16 @@ class NumberFormatGeneralTest extends AbstractFunctional
$reader = new Xls();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getSheetByName('Blad1');
if ($sheet === null) {
self::fail('Expected to find sheet Blad1');
} else {
$array = $sheet->toArray();
self::assertSame('€ 2.95', $array[1][3]);
self::assertSame(2.95, $sheet->getCell('D2')->getValue());
self::assertSame(2.95, $sheet->getCell('D2')->getCalculatedValue());
self::assertSame('€ 2.95', $sheet->getCell('D2')->getFormattedValue());
self::assertSame(21, $array[1][4]);
self::assertSame(21, $sheet->getCell('E2')->getValue());
self::assertSame(21, $sheet->getCell('E2')->getCalculatedValue());
self::assertSame('21', $sheet->getCell('E2')->getFormattedValue());
}
$sheet = $spreadsheet->getSheetByNameOrThrow('Blad1');
$array = $sheet->toArray();
self::assertSame('€ 2.95', $array[1][3]);
self::assertSame(2.95, $sheet->getCell('D2')->getValue());
self::assertSame(2.95, $sheet->getCell('D2')->getCalculatedValue());
self::assertSame('€ 2.95', $sheet->getCell('D2')->getFormattedValue());
self::assertSame(21, $array[1][4]);
self::assertSame(21, $sheet->getCell('E2')->getValue());
self::assertSame(21, $sheet->getCell('E2')->getCalculatedValue());
self::assertSame('21', $sheet->getCell('E2')->getFormattedValue());
$spreadsheet->disconnectWorksheets();
}
}

View File

@ -12,8 +12,7 @@ class RichTextSizeTest extends AbstractFunctional
$filename = 'tests/data/Reader/XLS/RichTextFontSize.xls';
$reader = new Xls();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getSheetByName('橱柜门板');
self::assertNotNull($sheet);
$sheet = $spreadsheet->getSheetByNameOrThrow('橱柜门板');
$text = $sheet->getCell('L15')->getValue();
$elements = $text->getRichTextElements();
self::assertEquals(10, $elements[2]->getFont()->getSize());

View File

@ -28,8 +28,7 @@ class AutoFilter2Test extends TestCase
public function testReadDateRange(): void
{
$spreadsheet = IOFactory::load(self::TESTBOOK);
$sheet = $spreadsheet->getSheetByName('daterange');
self::assertNotNull($sheet);
$sheet = $spreadsheet->getSheetByNameOrThrow('daterange');
$filter = $sheet->getAutoFilter();
$maxRow = 30;
self::assertSame("A1:A$maxRow", $filter->getRange());
@ -61,8 +60,7 @@ class AutoFilter2Test extends TestCase
public function testReadTopTen(): void
{
$spreadsheet = IOFactory::load(self::TESTBOOK);
$sheet = $spreadsheet->getSheetByName('top10');
self::assertNotNull($sheet);
$sheet = $spreadsheet->getSheetByNameOrThrow('top10');
$filter = $sheet->getAutoFilter();
$maxRow = 65;
self::assertSame("A1:A$maxRow", $filter->getRange());
@ -87,8 +85,7 @@ class AutoFilter2Test extends TestCase
public function testReadDynamic(): void
{
$spreadsheet = IOFactory::load(self::TESTBOOK);
$sheet = $spreadsheet->getSheetByName('dynamic');
self::assertNotNull($sheet);
$sheet = $spreadsheet->getSheetByNameOrThrow('dynamic');
$filter = $sheet->getAutoFilter();
$maxRow = 30;
self::assertSame("A1:A$maxRow", $filter->getRange());

View File

@ -3,7 +3,6 @@
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
class ChartSheetTest extends TestCase
@ -16,8 +15,7 @@ class ChartSheetTest extends TestCase
$spreadsheet = $reader->load($filename);
self::assertCount(2, $spreadsheet->getAllSheets());
$chartSheet = $spreadsheet->getSheetByName('Chart1');
self::assertInstanceOf(Worksheet::class, $chartSheet);
$chartSheet = $spreadsheet->getSheetByNameOrThrow('Chart1');
self::assertSame(1, $chartSheet->getChartCount());
}
@ -29,7 +27,6 @@ class ChartSheetTest extends TestCase
$spreadsheet = $reader->load($filename);
self::assertCount(1, $spreadsheet->getAllSheets());
$chartSheet = $spreadsheet->getSheetByName('Chart1');
self::assertNull($chartSheet);
self::assertNull($spreadsheet->getSheetByName('Chart1'));
}
}

View File

@ -42,28 +42,20 @@ class Issue2501Test extends TestCase
$filename = self::$testbook;
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getSheetByName('Columns');
$sheet = $spreadsheet->getSheetByNameOrThrow('Columns');
$expected = [
'A1:A1048576',
'B1:D1048576',
'E2:E4',
];
if ($sheet === null) {
self::fail('Unable to find sheet Columns');
} else {
self::assertSame($expected, array_values($sheet->getMergeCells()));
}
$sheet = $spreadsheet->getSheetByName('Rows');
self::assertSame($expected, array_values($sheet->getMergeCells()));
$sheet = $spreadsheet->getSheetByNameOrThrow('Rows');
$expected = [
'A1:XFD1',
'A2:XFD4',
'B5:D5',
];
if ($sheet === null) {
self::fail('Unable to find sheet Rows');
} else {
self::assertSame($expected, array_values($sheet->getMergeCells()));
}
self::assertSame($expected, array_values($sheet->getMergeCells()));
$spreadsheet->disconnectWorksheets();
}

View File

@ -62,13 +62,9 @@ class NamespaceNonStdTest extends \PHPUnit\Framework\TestCase
self::assertSame('A2', $sheet->getFreezePane());
self::assertSame('A2', $sheet->getTopLeftCell());
self::assertSame('B3', $sheet->getSelectedCells());
$sheet = $spreadsheet->getSheetByName('SylkTest');
if ($sheet === null) {
self::fail('Unable to load expected sheet');
} else {
self::assertNull($sheet->getFreezePane());
self::assertNull($sheet->getTopLeftCell());
}
$sheet = $spreadsheet->getSheetByNameOrThrow('SylkTest');
self::assertNull($sheet->getFreezePane());
self::assertNull($sheet->getTopLeftCell());
}
public function testLoadXlsx(): void

View File

@ -100,13 +100,9 @@ class NamespaceOpenpyxl35Test extends \PHPUnit\Framework\TestCase
],
];
foreach ($expectedArray as $sheetName => $array1) {
$sheet = $spreadsheet->getSheetByName($sheetName);
if ($sheet === null) {
self::fail("Unable to find sheet $sheetName");
} else {
foreach ($array1 as $key => $value) {
self::assertSame($value, self::getCellValue($sheet, $key), "error in sheet $sheetName cell $key");
}
$sheet = $spreadsheet->getSheetByNameOrThrow($sheetName);
foreach ($array1 as $key => $value) {
self::assertSame($value, self::getCellValue($sheet, $key), "error in sheet $sheetName cell $key");
}
}
$spreadsheet->disconnectWorksheets();

View File

@ -62,13 +62,9 @@ class NamespaceStdTest extends \PHPUnit\Framework\TestCase
self::assertSame('A2', $sheet->getFreezePane());
self::assertSame('A2', $sheet->getTopLeftCell());
self::assertSame('B3', $sheet->getSelectedCells());
$sheet = $spreadsheet->getSheetByName('SylkTest');
if ($sheet === null) {
self::fail('Unable to load expected sheet');
} else {
self::assertNull($sheet->getFreezePane());
self::assertNull($sheet->getTopLeftCell());
}
$sheet = $spreadsheet->getSheetByNameOrThrow('SylkTest');
self::assertNull($sheet->getFreezePane());
self::assertNull($sheet->getTopLeftCell());
}
public function testLoadXlsx(): void

View File

@ -28,8 +28,7 @@ class PageSetup2Test extends TestCase
public function testColumnBreak(): void
{
$spreadsheet = IOFactory::load(self::TESTBOOK);
$sheet = $spreadsheet->getSheetByName('colbreak');
self::assertNotNull($sheet);
$sheet = $spreadsheet->getSheetByNameOrThrow('colbreak');
$breaks = $sheet->getBreaks();
self::assertCount(1, $breaks);
$break = $breaks['D1'] ?? null;

View File

@ -10,16 +10,27 @@ class PasswordHasherTest extends TestCase
{
/**
* @dataProvider providerHashPassword
*
* @param mixed $expectedResult
*/
public function testHashPassword($expectedResult, ...$args): void
{
public function testHashPassword(
string $expectedResult,
string $password,
?string $algorithm = null,
?string $salt = null,
?int $spinCount = null
): void {
if ($expectedResult === 'exception') {
$this->expectException(SpException::class);
}
$result = PasswordHasher::hashPassword(...$args);
self::assertEquals($expectedResult, $result);
if ($algorithm === null) {
$result = PasswordHasher::hashPassword($password);
} elseif ($salt === null) {
$result = PasswordHasher::hashPassword($password, $algorithm);
} elseif ($spinCount === null) {
$result = PasswordHasher::hashPassword($password, $algorithm, $salt);
} else {
$result = PasswordHasher::hashPassword($password, $algorithm, $salt, $spinCount);
}
self::assertSame($expectedResult, $result);
}
public function providerHashPassword(): array

View File

@ -2,28 +2,38 @@
namespace PhpOffice\PhpSpreadsheetTests;
use PhpOffice\PhpSpreadsheet\Exception as ssException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
class SpreadsheetTest extends TestCase
{
/** @var Spreadsheet */
private $object;
/** @var ?Spreadsheet */
private $spreadsheet;
protected function setUp(): void
protected function tearDown(): void
{
parent::setUp();
$this->object = new Spreadsheet();
$sheet = $this->object->getActiveSheet();
if ($this->spreadsheet !== null) {
$this->spreadsheet->disconnectWorksheets();
$this->spreadsheet = null;
}
}
private function getSpreadsheet(): Spreadsheet
{
$this->spreadsheet = $spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('someSheet1');
$sheet = new Worksheet();
$sheet->setTitle('someSheet2');
$this->object->addSheet($sheet);
$spreadsheet->addSheet($sheet);
$sheet = new Worksheet();
$sheet->setTitle('someSheet 3');
$this->object->addSheet($sheet);
$spreadsheet->addSheet($sheet);
return $spreadsheet;
}
public function dataProviderForSheetNames(): array
@ -35,6 +45,7 @@ class SpreadsheetTest extends TestCase
[1, "'someSheet2'"],
[2, 'someSheet 3'],
[2, "'someSheet 3'"],
[null, 'someSheet 33'],
];
return $array;
@ -43,135 +54,153 @@ class SpreadsheetTest extends TestCase
/**
* @dataProvider dataProviderForSheetNames
*/
public function testGetSheetByName(int $index, string $sheetName): void
public function testGetSheetByName(?int $index, string $sheetName): void
{
self::assertSame($this->object->getSheet($index), $this->object->getSheetByName($sheetName));
$spreadsheet = $this->getSpreadsheet();
if ($index === null) {
self::assertNull($spreadsheet->getSheetByName($sheetName));
} else {
self::assertSame($spreadsheet->getSheet($index), $spreadsheet->getSheetByName($sheetName));
}
}
public function testAddSheetDuplicateTitle(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$sheet = new Worksheet();
$sheet->setTitle('someSheet2');
$this->object->addSheet($sheet);
$spreadsheet->addSheet($sheet);
}
public function testAddSheetNoAdjustActive(): void
{
$this->object->setActiveSheetIndex(2);
self::assertEquals(2, $this->object->getActiveSheetIndex());
$spreadsheet = $this->getSpreadsheet();
$spreadsheet->setActiveSheetIndex(2);
self::assertEquals(2, $spreadsheet->getActiveSheetIndex());
$sheet = new Worksheet();
$sheet->setTitle('someSheet4');
$this->object->addSheet($sheet);
self::assertEquals(2, $this->object->getActiveSheetIndex());
$spreadsheet->addSheet($sheet);
self::assertEquals(2, $spreadsheet->getActiveSheetIndex());
}
public function testAddSheetAdjustActive(): void
{
$this->object->setActiveSheetIndex(2);
self::assertEquals(2, $this->object->getActiveSheetIndex());
$spreadsheet = $this->getSpreadsheet();
$spreadsheet->setActiveSheetIndex(2);
self::assertEquals(2, $spreadsheet->getActiveSheetIndex());
$sheet = new Worksheet();
$sheet->setTitle('someSheet0');
$this->object->addSheet($sheet, 0);
self::assertEquals(3, $this->object->getActiveSheetIndex());
$spreadsheet->addSheet($sheet, 0);
self::assertEquals(3, $spreadsheet->getActiveSheetIndex());
}
public function testRemoveSheetIndexTooHigh(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$this->object->removeSheetByIndex(4);
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$spreadsheet->removeSheetByIndex(4);
}
public function testRemoveSheetNoAdjustActive(): void
{
$this->object->setActiveSheetIndex(1);
self::assertEquals(1, $this->object->getActiveSheetIndex());
$this->object->removeSheetByIndex(2);
self::assertEquals(1, $this->object->getActiveSheetIndex());
$spreadsheet = $this->getSpreadsheet();
$spreadsheet->setActiveSheetIndex(1);
self::assertEquals(1, $spreadsheet->getActiveSheetIndex());
$spreadsheet->removeSheetByIndex(2);
self::assertEquals(1, $spreadsheet->getActiveSheetIndex());
}
public function testRemoveSheetAdjustActive(): void
{
$this->object->setActiveSheetIndex(2);
self::assertEquals(2, $this->object->getActiveSheetIndex());
$this->object->removeSheetByIndex(1);
self::assertEquals(1, $this->object->getActiveSheetIndex());
$spreadsheet = $this->getSpreadsheet();
$spreadsheet->setActiveSheetIndex(2);
self::assertEquals(2, $spreadsheet->getActiveSheetIndex());
$spreadsheet->removeSheetByIndex(1);
self::assertEquals(1, $spreadsheet->getActiveSheetIndex());
}
public function testGetSheetIndexTooHigh(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$this->object->getSheet(4);
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$spreadsheet->getSheet(4);
}
public function testGetIndexNonExistent(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$sheet = new Worksheet();
$sheet->setTitle('someSheet4');
$this->object->getIndex($sheet);
$spreadsheet->getIndex($sheet);
}
public function testSetIndexByName(): void
{
$this->object->setIndexByName('someSheet1', 1);
self::assertEquals('someSheet2', $this->object->getSheet(0)->getTitle());
self::assertEquals('someSheet1', $this->object->getSheet(1)->getTitle());
self::assertEquals('someSheet 3', $this->object->getSheet(2)->getTitle());
$spreadsheet = $this->getSpreadsheet();
$spreadsheet->setIndexByName('someSheet1', 1);
self::assertEquals('someSheet2', $spreadsheet->getSheet(0)->getTitle());
self::assertEquals('someSheet1', $spreadsheet->getSheet(1)->getTitle());
self::assertEquals('someSheet 3', $spreadsheet->getSheet(2)->getTitle());
}
public function testRemoveAllSheets(): void
{
$this->object->setActiveSheetIndex(2);
self::assertEquals(2, $this->object->getActiveSheetIndex());
$this->object->removeSheetByIndex(0);
self::assertEquals(1, $this->object->getActiveSheetIndex());
$this->object->removeSheetByIndex(0);
self::assertEquals(0, $this->object->getActiveSheetIndex());
$this->object->removeSheetByIndex(0);
self::assertEquals(-1, $this->object->getActiveSheetIndex());
$spreadsheet = $this->getSpreadsheet();
$spreadsheet->setActiveSheetIndex(2);
self::assertEquals(2, $spreadsheet->getActiveSheetIndex());
$spreadsheet->removeSheetByIndex(0);
self::assertEquals(1, $spreadsheet->getActiveSheetIndex());
$spreadsheet->removeSheetByIndex(0);
self::assertEquals(0, $spreadsheet->getActiveSheetIndex());
$spreadsheet->removeSheetByIndex(0);
self::assertEquals(-1, $spreadsheet->getActiveSheetIndex());
$sheet = new Worksheet();
$sheet->setTitle('someSheet4');
$this->object->addSheet($sheet);
self::assertEquals(0, $this->object->getActiveSheetIndex());
$spreadsheet->addSheet($sheet);
self::assertEquals(0, $spreadsheet->getActiveSheetIndex());
}
public function testBug1735(): void
{
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet->createSheet()->setTitle('addedsheet');
$spreadsheet->setActiveSheetIndex(1);
$spreadsheet->removeSheetByIndex(0);
$sheet = $spreadsheet->getActiveSheet();
$spreadsheet1 = new Spreadsheet();
$spreadsheet1->createSheet()->setTitle('addedsheet');
$spreadsheet1->setActiveSheetIndex(1);
$spreadsheet1->removeSheetByIndex(0);
$sheet = $spreadsheet1->getActiveSheet();
self::assertEquals('addedsheet', $sheet->getTitle());
}
public function testSetActiveSheetIndexTooHigh(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$this->object->setActiveSheetIndex(4);
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$spreadsheet->setActiveSheetIndex(4);
}
public function testSetActiveSheetNoSuchName(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$this->object->setActiveSheetIndexByName('unknown');
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$spreadsheet->setActiveSheetIndexByName('unknown');
}
public function testAddExternal(): void
{
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->createSheet()->setTitle('someSheet19');
$spreadsheet = $this->getSpreadsheet();
$spreadsheet1 = new Spreadsheet();
$sheet = $spreadsheet1->createSheet()->setTitle('someSheet19');
$sheet->getCell('A1')->setValue(1);
$sheet->getCell('A1')->getStyle()->getFont()->setBold(true);
$sheet->getCell('B1')->getStyle()->getFont()->setSuperscript(true);
$sheet->getCell('C1')->getStyle()->getFont()->setSubscript(true);
self::assertCount(4, $spreadsheet->getCellXfCollection());
self::assertCount(4, $spreadsheet1->getCellXfCollection());
self::assertEquals(1, $sheet->getCell('A1')->getXfIndex());
$this->object->getActiveSheet()->getCell('A1')->getStyle()->getFont()->setBold(true);
self::assertCount(2, $this->object->getCellXfCollection());
$sheet3 = $this->object->addExternalSheet($sheet);
self::assertCount(6, $this->object->getCellXfCollection());
$spreadsheet->getActiveSheet()->getCell('A1')->getStyle()->getFont()->setBold(true);
self::assertCount(2, $spreadsheet->getCellXfCollection());
$sheet3 = $spreadsheet->addExternalSheet($sheet);
self::assertCount(6, $spreadsheet->getCellXfCollection());
self::assertEquals('someSheet19', $sheet3->getTitle());
self::assertEquals(1, $sheet3->getCell('A1')->getValue());
self::assertTrue($sheet3->getCell('A1')->getStyle()->getFont()->getBold());
@ -181,17 +210,17 @@ class SpreadsheetTest extends TestCase
public function testAddExternalDuplicateName(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$this->expectException(ssException::class);
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->createSheet()->setTitle('someSheet1');
$sheet->getCell('A1')->setValue(1);
$sheet->getCell('A1')->getStyle()->getFont()->setBold(true);
$this->object->addExternalSheet($sheet);
$spreadsheet->addExternalSheet($sheet);
}
public function testAddExternalColumnDimensionStyles(): void
{
$spreadsheet1 = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet1 = new Spreadsheet();
$sheet1 = $spreadsheet1->createSheet()->setTitle('sheetWithColumnDimension');
$sheet1->getCell('A1')->setValue(1);
$sheet1->getCell('A1')->getStyle()->getFont()->setItalic(true);
@ -200,7 +229,7 @@ class SpreadsheetTest extends TestCase
self::assertEquals(1, $index);
self::assertCount(2, $spreadsheet1->getCellXfCollection());
$spreadsheet2 = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet2 = new Spreadsheet();
$sheet2 = $spreadsheet2->createSheet()->setTitle('sheetWithTwoStyles');
$sheet2->getCell('A1')->setValue(1);
$sheet2->getCell('A1')->getStyle()->getFont()->setBold(true);
@ -220,7 +249,7 @@ class SpreadsheetTest extends TestCase
public function testAddExternalRowDimensionStyles(): void
{
$spreadsheet1 = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet1 = new Spreadsheet();
$sheet1 = $spreadsheet1->createSheet()->setTitle('sheetWithColumnDimension');
$sheet1->getCell('A1')->setValue(1);
$sheet1->getCell('A1')->getStyle()->getFont()->setItalic(true);
@ -229,7 +258,7 @@ class SpreadsheetTest extends TestCase
self::assertEquals(1, $index);
self::assertCount(2, $spreadsheet1->getCellXfCollection());
$spreadsheet2 = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet2 = new Spreadsheet();
$sheet2 = $spreadsheet2->createSheet()->setTitle('sheetWithTwoStyles');
$sheet2->getCell('A1')->setValue(1);
$sheet2->getCell('A1')->getStyle()->getFont()->setBold(true);

View File

@ -2,9 +2,12 @@
namespace PhpOffice\PhpSpreadsheetTests\Style\ConditionalFormatting;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Exception as ssException;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\CellMatcher;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
class CellMatcherTest extends TestCase
@ -30,18 +33,26 @@ class CellMatcherTest extends TestCase
}
}
private function confirmString(Worksheet $worksheet, Cell $cell, string $cellAddress): string
{
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate()) ?? '';
if ($cfRange === '') {
self::fail("{$cellAddress} is not in a Conditional Format range");
}
return $cfRange;
}
/**
* @dataProvider basicCellIsComparisonDataProvider
*/
public function testBasicCellIsComparison(string $sheetname, string $cellAddress, array $expectedMatches): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::assertNotNull($cfRange, "{$cellAddress} is not in a Conditional Format range");
$cfRange = $this->confirmString($worksheet, $cell, $cellAddress);
$cfStyles = $worksheet->getConditionalStyles($cell->getCoordinate());
$matcher = new CellMatcher($cell, $cfRange);
@ -83,18 +94,35 @@ class CellMatcherTest extends TestCase
];
}
public function testNotInRange(): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$sheetname = 'cellIs Comparison';
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell('J20');
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::assertNull($cfRange);
}
public function testUnknownSheet(): void
{
$this->expectException(ssException::class);
$this->spreadsheet = $this->loadSpreadsheet();
$sheetname = 'cellIs Comparisonxxx';
$this->spreadsheet->getSheetByNameOrThrow($sheetname);
}
/**
* @dataProvider rangeCellIsComparisonDataProvider
*/
public function testRangeCellIsComparison(string $sheetname, string $cellAddress, bool $expectedMatch): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
$cfRange = $this->confirmString($worksheet, $cell, $cellAddress);
$cfStyle = $worksheet->getConditionalStyles($cell->getCoordinate());
$matcher = new CellMatcher($cell, $cfRange);
@ -132,12 +160,10 @@ class CellMatcherTest extends TestCase
public function testCellIsMultipleExpression(string $sheetname, string $cellAddress, array $expectedMatches): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
$cfRange = $this->confirmString($worksheet, $cell, $cellAddress);
$cfStyles = $worksheet->getConditionalStyles($cell->getCoordinate());
$matcher = new CellMatcher($cell, $cfRange);
@ -168,12 +194,10 @@ class CellMatcherTest extends TestCase
public function testCellIsExpression(string $sheetname, string $cellAddress, bool $expectedMatch): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
$cfRange = $this->confirmString($worksheet, $cell, $cellAddress);
$cfStyle = $worksheet->getConditionalStyles($cell->getCoordinate());
$matcher = new CellMatcher($cell, $cfRange);
@ -214,12 +238,10 @@ class CellMatcherTest extends TestCase
public function testTextExpressions(string $sheetname, string $cellAddress, bool $expectedMatch): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
$cfRange = $this->confirmString($worksheet, $cell, $cellAddress);
$cfStyle = $worksheet->getConditionalStyles($cell->getCoordinate());
$matcher = new CellMatcher($cell, $cfRange);
@ -324,12 +346,10 @@ class CellMatcherTest extends TestCase
public function testBlankExpressions(string $sheetname, string $cellAddress, array $expectedMatches): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
$cfRange = $this->confirmString($worksheet, $cell, $cellAddress);
$cfStyles = $worksheet->getConditionalStyles($cell->getCoordinate());
$matcher = new CellMatcher($cell, $cfRange);
@ -357,12 +377,10 @@ class CellMatcherTest extends TestCase
public function testErrorExpressions(string $sheetname, string $cellAddress, array $expectedMatches): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
$cfRange = $this->confirmString($worksheet, $cell, $cellAddress);
$cfStyles = $worksheet->getConditionalStyles($cell->getCoordinate());
$matcher = new CellMatcher($cell, $cfRange);
@ -389,12 +407,10 @@ class CellMatcherTest extends TestCase
public function testDateOccurringExpressions(string $sheetname, string $cellAddress, bool $expectedMatch): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
$cfRange = $this->confirmString($worksheet, $cell, $cellAddress);
$cfStyle = $worksheet->getConditionalStyles($cell->getCoordinate());
$matcher = new CellMatcher($cell, $cfRange);
@ -433,12 +449,10 @@ class CellMatcherTest extends TestCase
public function testDuplicatesExpressions(string $sheetname, string $cellAddress, array $expectedMatches): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::AssertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
$cfRange = $this->confirmString($worksheet, $cell, $cellAddress);
$cfStyles = $worksheet->getConditionalStyles($cell->getCoordinate());
$matcher = new CellMatcher($cell, $cfRange);
@ -469,12 +483,10 @@ class CellMatcherTest extends TestCase
public function testCrossWorksheetExpressions(string $sheetname, string $cellAddress, bool $expectedMatch): void
{
$this->spreadsheet = $this->loadSpreadsheet();
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
$worksheet = $this->spreadsheet->getSheetByNameOrThrow($sheetname);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
$cfRange = $this->confirmString($worksheet, $cell, $cellAddress);
$cfStyle = $worksheet->getConditionalStyles($cell->getCoordinate());
$matcher = new CellMatcher($cell, $cfRange);

View File

@ -24,9 +24,9 @@ class WizardFactoryTest extends TestCase
/**
* @dataProvider basicWizardFactoryProvider
*
* @param class-string<object> $expectedWizard
* @psalm-param class-string<object> $expectedWizard
*/
public function testBasicWizardFactory(string $ruleType, $expectedWizard): void
public function testBasicWizardFactory(string $ruleType, string $expectedWizard): void
{
$wizard = $this->wizardFactory->newRule($ruleType);
self::assertInstanceOf($expectedWizard, $wizard);
@ -54,10 +54,7 @@ class WizardFactoryTest extends TestCase
$filename = 'tests/data/Style/ConditionalFormatting/CellMatcher.xlsx';
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($filename);
$worksheet = $spreadsheet->getSheetByName($sheetName);
if ($worksheet === null) {
self::markTestSkipped("{$sheetName} not found in test workbook");
}
$worksheet = $spreadsheet->getSheetByNameOrThrow($sheetName);
$cell = $worksheet->getCell($cellAddress);
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());

View File

@ -79,7 +79,7 @@ class VisibilityTest extends AbstractFunctional
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xls');
foreach ($visibleSheets as $sheetName => $visibility) {
$reloadedWorksheet = $reloadedSpreadsheet->getSheetByName($sheetName) ?? new Worksheet();
$reloadedWorksheet = $reloadedSpreadsheet->getSheetByNameOrThrow($sheetName);
self::assertSame($visibility, $reloadedWorksheet->getSheetState());
}
}

View File

@ -77,19 +77,15 @@ class UnparsedDataCloneTest extends TestCase
$reader1 = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet1 = $reader1->load($resultFilename1);
unlink($resultFilename1);
$sheet1c = $spreadsheet1->getSheetByName('Clone');
self::assertNotNull($sheet1c);
$sheet1o = $spreadsheet1->getSheetByName('Original');
self::assertNotNull($sheet1o);
$sheet1c = $spreadsheet1->getSheetByNameOrThrow('Clone');
$sheet1o = $spreadsheet1->getSheetByNameOrThrow('Original');
$writer->save($resultFilename2);
$reader2 = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet2 = $reader2->load($resultFilename2);
unlink($resultFilename2);
$sheet2c = $spreadsheet2->getSheetByName('Clone');
self::assertNotNull($sheet2c);
$sheet2o = $spreadsheet2->getSheetByName('Original');
self::assertNotNull($sheet2o);
$sheet2c = $spreadsheet2->getSheetByNameOrThrow('Clone');
$sheet2o = $spreadsheet2->getSheetByNameOrThrow('Original');
self::assertEquals($spreadsheet1->getSheetCount(), $spreadsheet2->getSheetCount());
self::assertCount(1, $sheet1c->getDrawingCollection());

View File

@ -79,7 +79,7 @@ class VisibilityTest extends AbstractFunctional
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
foreach ($visibleSheets as $sheetName => $visibility) {
$reloadedWorksheet = $reloadedSpreadsheet->getSheetByName($sheetName) ?? new Worksheet();
$reloadedWorksheet = $reloadedSpreadsheet->getSheetByNameOrThrow($sheetName);
self::assertSame($visibility, $reloadedWorksheet->getSheetState());
}
}

View File

@ -83,4 +83,5 @@ return [
-21000,
],
],
'no arguments' => ['exception'],
];