Lookup functions additional unit tests (#2074)

* Additional unit tests for VLOOKUP() and HLOOKUP()
* Additional unit tests for CHOOSE()
* Unit tests for HYPERLINK() function
* Fix CHOOSE() test for spillage
This commit is contained in:
Mark Baker 2021-05-07 23:40:30 +02:00 committed by GitHub
parent f28eea7341
commit d2e6db71fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 142 additions and 2 deletions

View File

@ -11,7 +11,7 @@ class Hyperlink
* HYPERLINK. * HYPERLINK.
* *
* Excel Function: * Excel Function:
* =HYPERLINK(linkURL,displayName) * =HYPERLINK(linkURL, [displayName])
* *
* @param mixed $linkURL Expect string. Value to check, is also the value returned when no error * @param mixed $linkURL Expect string. Value to check, is also the value returned when no error
* @param mixed $displayName Expect string. Value to return when testValue is an error condition * @param mixed $displayName Expect string. Value to return when testValue is an error condition

View File

@ -284,7 +284,7 @@ class Xml extends BaseReader
$worksheet_ss = self::getAttributes($worksheet, $namespaces['ss']); $worksheet_ss = self::getAttributes($worksheet, $namespaces['ss']);
if ( if (
(isset($this->loadSheetsOnly)) && (isset($worksheet_ss['Name'])) && isset($this->loadSheetsOnly, $worksheet_ss['Name']) &&
(!in_array($worksheet_ss['Name'], $this->loadSheetsOnly)) (!in_array($worksheet_ss['Name'], $this->loadSheetsOnly))
) { ) {
continue; continue;

View File

@ -0,0 +1,51 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\Hyperlink;
use PHPUnit\Framework\TestCase;
class HyperlinkTest extends TestCase
{
/**
* @dataProvider providerHYPERLINK
*
* @param mixed $expectedResult
* @param null|string $linkUrl
* @param null|string $description
*/
public function testHYPERLINK($expectedResult, $linkUrl, $description): void
{
$hyperlink = new Hyperlink();
$cell = $this->getMockBuilder(Cell::class)
->onlyMethods(['getHyperlink'])
->disableOriginalConstructor()
->getMock();
$cell->method('getHyperlink')
->willReturn($hyperlink);
$result = LookupRef::HYPERLINK($linkUrl, $description, $cell);
if (!is_array($expectedResult)) {
self::assertSame($expectedResult, $result);
} else {
self::assertSame($expectedResult[1], $result);
self::assertSame($expectedResult[0], $hyperlink->getUrl());
self::assertSame($expectedResult[1], $hyperlink->getTooltip());
}
}
public function providerHYPERLINK(): array
{
return require 'tests/data/Calculation/LookupRef/HYPERLINK.php';
}
public function testHYPERLINKwithoutCell(): void
{
$result = LookupRef::HYPERLINK('https://phpspreadsheet.readthedocs.io/en/latest/', 'Read the Docs');
self::assertSame(Functions::REF(), $result);
}
}

View File

@ -25,4 +25,12 @@ return [
'#VALUE!', '#VALUE!',
0, 'red', 'blue', 'green', 'brown', 0, 'red', 'blue', 'green', 'brown',
], ],
[
'#VALUE!',
'NaN', 'red', 'blue', 'green', 'brown',
],
[
['blue', 'purple'],
3, ['red', 'orange'], ['yellow', 'green'], ['blue', 'purple'],
],
]; ];

View File

@ -328,4 +328,28 @@ return [
2, 2,
false, false,
], ],
[
0.61,
'Ed',
[
[null, 'Ann', 'Cara', 'Colin', 'Ed', 'Frank'],
['Math', 0.58, 0.90, 0.67, 0.76, 0.80],
['French', 0.61, 0.71, 0.59, 0.59, 0.76],
['Physics', 0.75, 0.45, 0.39, 0.52, 0.69],
['Bioogy', 0.39, 0.55, 0.77, 0.61, 0.45],
],
5,
false,
],
[
'Normal Weight',
23.5,
[
[null, 'Min', 0.0, 18.5, 25.0, 30.0],
['BMI', 'Max', 18.4, 24.9, 29.9, null],
[null, 'Body Type', 'Underweight', 'Normal Weight', 'Overweight', 'Obese'],
],
3,
true,
],
]; ];

View File

@ -0,0 +1,26 @@
<?php
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
return [
[
['https://phpspreadsheet.readthedocs.io/en/latest/', 'https://phpspreadsheet.readthedocs.io/en/latest/'],
'https://phpspreadsheet.readthedocs.io/en/latest/',
null,
],
[
['https://phpspreadsheet.readthedocs.io/en/latest/', 'Read the Docs'],
'https://phpspreadsheet.readthedocs.io/en/latest/',
'Read the Docs',
],
[
Functions::REF(),
null,
null,
],
[
Functions::REF(),
'',
null,
],
];

View File

@ -349,4 +349,35 @@ return [
], ],
2.0, 2.0,
], ],
[
3.50,
'Cornflakes',
[
['Item Description', 'Price'],
['Tinned Tomatoes', 0.90],
['Tinned Tuna', 1.50],
['Cornflakes', 3.50],
['Shortcake Biscuits', 1.00],
['Toothpaste', 4.10],
['Tinned Baked Beans', 0.99],
['White Sliced Bread', 0.80],
],
2,
false,
],
[
'E',
0.52,
[
['Lower', 'Upper', 'Grade'],
[0.00, 0.44, 'F'],
[0.45, 0.54, 'E'],
[0.55, 0.64, 'D'],
[0.65, 0.74, 'C'],
[0.75, 0.84, 'B'],
[0.85, 1.00, 'A'],
],
3,
true,
],
]; ];