Skip to content

Commit

Permalink
Lookup functions additional unit tests (#2074)
Browse files Browse the repository at this point in the history
* Additional unit tests for VLOOKUP() and HLOOKUP()
* Additional unit tests for CHOOSE()
* Unit tests for HYPERLINK() function
* Fix CHOOSE() test for spillage
  • Loading branch information
Mark Baker committed May 7, 2021
1 parent f28eea7 commit d2e6db7
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/LookupRef/Hyperlink.php
Expand Up @@ -11,7 +11,7 @@ class Hyperlink
* HYPERLINK.
*
* 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 $displayName Expect string. Value to return when testValue is an error condition
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Reader/Xml.php
Expand Up @@ -284,7 +284,7 @@ public function loadIntoExisting($filename, Spreadsheet $spreadsheet)
$worksheet_ss = self::getAttributes($worksheet, $namespaces['ss']);

if (
(isset($this->loadSheetsOnly)) && (isset($worksheet_ss['Name'])) &&
isset($this->loadSheetsOnly, $worksheet_ss['Name']) &&
(!in_array($worksheet_ss['Name'], $this->loadSheetsOnly))
) {
continue;
Expand Down
@@ -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);
}
}
8 changes: 8 additions & 0 deletions tests/data/Calculation/LookupRef/CHOOSE.php
Expand Up @@ -25,4 +25,12 @@
'#VALUE!',
0, 'red', 'blue', 'green', 'brown',
],
[
'#VALUE!',
'NaN', 'red', 'blue', 'green', 'brown',
],
[
['blue', 'purple'],
3, ['red', 'orange'], ['yellow', 'green'], ['blue', 'purple'],
],
];
24 changes: 24 additions & 0 deletions tests/data/Calculation/LookupRef/HLOOKUP.php
Expand Up @@ -328,4 +328,28 @@
2,
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,
],
];
26 changes: 26 additions & 0 deletions tests/data/Calculation/LookupRef/HYPERLINK.php
@@ -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,
],
];
31 changes: 31 additions & 0 deletions tests/data/Calculation/LookupRef/VLOOKUP.php
Expand Up @@ -349,4 +349,35 @@
],
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,
],
];

0 comments on commit d2e6db7

Please sign in to comment.