Skip to content

Commit 252474c

Browse files
authored
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.
1 parent 3e8d505 commit 252474c

31 files changed

+292
-281
lines changed

infra/DocumentGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private static function getCategories(): array
4141
private static function tableRow(array $lengths, ?array $values = null): string
4242
{
4343
$result = '';
44-
foreach (array_map(null, $lengths, $values ?? []) as $i => [$length, $value]) {
44+
foreach (array_map(/** @scrutinizer ignore-type */ null, $lengths, $values ?? []) as $i => [$length, $value]) {
4545
$pad = $value === null ? '-' : ' ';
4646
if ($i > 0) {
4747
$result .= '|' . $pad;

samples/Reader/16_Handling_loader_exceptions_using_TryCatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
try {
1212
$spreadsheet = IOFactory::load($inputFileName);
1313
} catch (ReaderException $e) {
14-
$helper->log('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
14+
$helper->log('Error loading file "' . /** @scrutinizer ignore-type */ pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
1515
}

src/PhpSpreadsheet/Spreadsheet.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,19 @@ public function getSheetByName($worksheetName)
724724
return null;
725725
}
726726

727+
/**
728+
* Get sheet by name, throwing exception if not found.
729+
*/
730+
public function getSheetByNameOrThrow(string $worksheetName): Worksheet
731+
{
732+
$worksheet = $this->getSheetByName($worksheetName);
733+
if ($worksheet === null) {
734+
throw new Exception("Sheet $worksheetName does not exist.");
735+
}
736+
737+
return $worksheet;
738+
}
739+
727740
/**
728741
* Get index for sheet.
729742
*

tests/PhpSpreadsheetTests/Calculation/Functions/Engineering/ParseComplexTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering;
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Engineering;
6-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
76
use PHPUnit\Framework\TestCase;
87

98
class ParseComplexTest extends TestCase
109
{
11-
protected function setUp(): void
12-
{
13-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
14-
}
15-
1610
public function testParseComplex(): void
1711
{
1812
[$real, $imaginary, $suffix] = [1.23e-4, 5.67e+8, 'j'];
1913

20-
$result = Engineering::parseComplex('1.23e-4+5.67e+8j');
14+
$result = /** @scrutinizer ignore-deprecated */ Engineering::parseComplex('1.23e-4+5.67e+8j');
2115
self::assertArrayHasKey('real', $result);
2216
self::assertEquals($real, $result['real']);
2317
self::assertArrayHasKey('imaginary', $result);

tests/PhpSpreadsheetTests/Calculation/Functions/Financial/IrrTest.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,45 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Financial;
44

5-
use PhpOffice\PhpSpreadsheet\Calculation\Financial;
6-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7-
use PHPUnit\Framework\TestCase;
8-
9-
class IrrTest extends TestCase
5+
class IrrTest extends AllSetupTeardown
106
{
11-
protected function setUp(): void
12-
{
13-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
14-
}
15-
167
/**
178
* @dataProvider providerIRR
189
*
1910
* @param mixed $expectedResult
11+
* @param mixed $values
2012
*/
21-
public function testIRR($expectedResult, ...$args): void
13+
public function testIRR($expectedResult, $values = null): void
2214
{
23-
$result = Financial::IRR(...$args);
24-
self::assertEqualsWithDelta($expectedResult, $result, 1E-8);
15+
$this->mightHaveException($expectedResult);
16+
$sheet = $this->getSheet();
17+
$formula = '=IRR(';
18+
if ($values !== null) {
19+
if (is_array($values)) {
20+
$row = 0;
21+
foreach ($values as $value) {
22+
if (is_array($value)) {
23+
foreach ($value as $arrayValue) {
24+
++$row;
25+
$sheet->getCell("A$row")->setValue($arrayValue);
26+
}
27+
} else {
28+
++$row;
29+
$sheet->getCell("A$row")->setValue($value);
30+
}
31+
}
32+
$formula .= "A1:A$row";
33+
} else {
34+
$sheet->getCell('A1')->setValue($values);
35+
$formula .= 'A1';
36+
}
37+
}
38+
$formula .= ')';
39+
$sheet->getCell('D1')->setValue($formula);
40+
$result = $sheet->getCell('D1')->getCalculatedValue();
41+
$this->adjustResult($result, $expectedResult);
42+
43+
self::assertEqualsWithDelta($expectedResult, $result, 0.1E-8);
2544
}
2645

2746
public function providerIRR(): array

tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnsTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
76
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
87
use PHPUnit\Framework\TestCase;
98

109
class ColumnsTest extends TestCase
1110
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
}
16-
1711
/**
1812
* @dataProvider providerCOLUMNS
1913
*
2014
* @param mixed $expectedResult
2115
*/
2216
public function testCOLUMNS($expectedResult, ...$args): void
2317
{
24-
$result = LookupRef::COLUMNS(...$args);
18+
$result = LookupRef::COLUMNS(/** @scrutinizer ignore-type */ ...$args);
2519
self::assertEquals($expectedResult, $result);
2620
}
2721

tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/IndexTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
76
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
87
use PHPUnit\Framework\TestCase;
98

109
class IndexTest extends TestCase
1110
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
}
16-
1711
/**
1812
* @dataProvider providerINDEX
1913
*
2014
* @param mixed $expectedResult
2115
*/
2216
public function testINDEX($expectedResult, ...$args): void
2317
{
24-
$result = LookupRef::INDEX(...$args);
18+
$result = LookupRef::INDEX(/** @scrutinizer ignore-type */ ...$args);
2519
self::assertEquals($expectedResult, $result);
2620
}
2721

tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowsTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
76
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
87
use PHPUnit\Framework\TestCase;
98

109
class RowsTest extends TestCase
1110
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
}
16-
1711
/**
1812
* @dataProvider providerROWS
1913
*
2014
* @param mixed $expectedResult
2115
*/
2216
public function testROWS($expectedResult, ...$args): void
2317
{
24-
$result = LookupRef::ROWS(...$args);
18+
$result = LookupRef::ROWS(/** @scrutinizer ignore-type */ ...$args);
2519
self::assertEquals($expectedResult, $result);
2620
}
2721

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/RandBetweenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testRandBetweenArray(
6060
$formula = "=RandBetween({$argument1}, {$argument2})";
6161
$result = $calculation->_calculateFormulaValue($formula);
6262
self::assertIsArray($result);
63-
self::assertCount($expectedRows, $result);
63+
self::assertCount($expectedRows, /** @scrutinizer ignore-type */ $result);
6464
self::assertIsArray($result[0]);
6565
self::assertCount($expectedColumns, /** @scrutinizer ignore-type */ $result[0]);
6666
}

tests/PhpSpreadsheetTests/DefinedNameTest.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ public function testAddScopedDefinedNameWithSameName(): void
5858
DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1')
5959
);
6060
$this->spreadsheet->addDefinedName(
61-
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true)
61+
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=B1', true)
6262
);
6363

6464
self::assertCount(2, $this->spreadsheet->getDefinedNames());
6565
$definedName1 = $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getActiveSheet());
6666
self::assertNotNull($definedName1);
6767
self::assertSame('=A1', $definedName1->getValue());
6868

69-
$definedName2 = $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getSheetByName('Sheet #2'));
69+
$definedName2 = $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
7070
self::assertNotNull($definedName2);
7171
self::assertSame('=B1', $definedName2->getValue());
7272
}
@@ -103,13 +103,13 @@ public function testRemoveGlobalDefinedNameWhenDuplicateNames(): void
103103
DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1')
104104
);
105105
$this->spreadsheet->addDefinedName(
106-
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true)
106+
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=B1', true)
107107
);
108108

109109
$this->spreadsheet->removeDefinedName('Foo', $this->spreadsheet->getActiveSheet());
110110

111111
self::assertCount(1, $this->spreadsheet->getDefinedNames());
112-
$definedName = $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getSheetByName('Sheet #2'));
112+
$definedName = $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
113113
self::assertNotNull($definedName);
114114
self::assertSame('=B1', $definedName->getValue());
115115
}
@@ -120,10 +120,10 @@ public function testRemoveScopedDefinedNameWhenDuplicateNames(): void
120120
DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1')
121121
);
122122
$this->spreadsheet->addDefinedName(
123-
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true)
123+
DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=B1', true)
124124
);
125125

126-
$this->spreadsheet->removeDefinedName('Foo', $this->spreadsheet->getSheetByName('Sheet #2'));
126+
$this->spreadsheet->removeDefinedName('Foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
127127

128128
self::assertCount(1, $this->spreadsheet->getDefinedNames());
129129
$definedName = $this->spreadsheet->getDefinedName('foo');
@@ -154,10 +154,8 @@ public function testSetAndGetRange(): void
154154

155155
public function testChangeWorksheet(): void
156156
{
157-
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
158-
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
159-
self::assertNotNull($sheet1);
160-
self::assertNotNull($sheet2);
157+
$sheet1 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #1');
158+
$sheet2 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #2');
161159

162160
$sheet1->getCell('A1')->setValue(1);
163161
$sheet2->getCell('A1')->setValue(2);
@@ -172,10 +170,8 @@ public function testChangeWorksheet(): void
172170

173171
public function testLocalOnly(): void
174172
{
175-
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
176-
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
177-
self::assertNotNull($sheet1);
178-
self::assertNotNull($sheet2);
173+
$sheet1 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #1');
174+
$sheet2 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #2');
179175

180176
$sheet1->getCell('A1')->setValue(1);
181177
$sheet2->getCell('A1')->setValue(2);
@@ -190,10 +186,8 @@ public function testLocalOnly(): void
190186

191187
public function testScope(): void
192188
{
193-
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
194-
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
195-
self::assertNotNull($sheet1);
196-
self::assertNotNull($sheet2);
189+
$sheet1 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #1');
190+
$sheet2 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #2');
197191

198192
$sheet1->getCell('A1')->setValue(1);
199193
$sheet2->getCell('A1')->setValue(2);
@@ -208,10 +202,8 @@ public function testScope(): void
208202

209203
public function testClone(): void
210204
{
211-
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
212-
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
213-
self::assertNotNull($sheet1);
214-
self::assertNotNull($sheet2);
205+
$sheet1 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #1');
206+
$sheet2 = $this->spreadsheet->getSheetByNameOrThrow('Sheet #2');
215207

216208
$sheet1->getCell('A1')->setValue(1);
217209
$sheet2->getCell('A1')->setValue(2);

0 commit comments

Comments
 (0)