Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/HyperlinkTest.php
T
Adrien Crivelli ec4098c8fd Strict mode for all tests
While we might never be able to have 100% of our code strict, we can at
the very least do it for all of our tests. This ensures that our tests
are using our API with the types as intended by the test author, and not
silently be cast to what our API requires.
2023-09-07 17:44:56 +08:00

101 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
class HyperlinkTest extends AllSetupTeardown
{
/** @var bool */
protected $issue2464 = true;
/**
* @dataProvider providerHYPERLINK
*/
public function testHYPERLINK(array|string $expectedResult, ?string $linkUrl, ?string $description): void
{
$this->mightHaveException($expectedResult);
$sheet = $this->getSheet();
$formula = '=HYPERLINK(';
if ($linkUrl !== null) {
$formula .= '"' . $linkUrl . '"';
if ($description !== null) {
$formula .= ',"' . $description . '"';
}
}
$formula .= ')';
$sheet->getCell('A1')->setValue($formula);
$result = $sheet->getCell('A1')->getCalculatedValue();
if (!is_array($expectedResult)) {
self::assertSame($expectedResult, $result);
} else {
self::assertSame($expectedResult[1], $result);
$hyperlink = $sheet->getCell('A1')->getHyperlink();
self::assertSame($expectedResult[0], $hyperlink->getUrl());
self::assertSame($expectedResult[1], $hyperlink->getTooltip());
}
}
/**
* @dataProvider providerHYPERLINK
*/
public function testHYPERLINKcellRef(array|string $expectedResult, ?string $linkUrl, ?string $description): void
{
$this->mightHaveException($expectedResult);
$sheet = $this->getSheet();
$formula = '=HYPERLINK(';
if ($linkUrl !== null) {
$sheet->getCell('A2')->setValue($linkUrl);
$formula .= 'A2';
if ($description !== null) {
$sheet->getCell('A3')->setValue($description);
$formula .= ',A3';
}
}
$formula .= ')';
$sheet->getCell('A1')->setValue($formula);
$result = $sheet->getCell('A1')->getCalculatedValue();
if (!is_array($expectedResult)) {
self::assertSame($expectedResult, $result);
} else {
self::assertSame($expectedResult[1], $result);
if (!$this->issue2464) {
$hyperlink = $sheet->getCell('A1')->getHyperlink();
self::assertSame($expectedResult[0], $hyperlink->getUrl());
self::assertSame($expectedResult[1], $hyperlink->getTooltip());
}
}
}
public function testLen(): void
{
$sheet = $this->getSheet();
$sheet->getCell('A1')->setValue('=LEN(HYPERLINK("http://www.example.com","Example"))');
$value = $sheet->getCell('A1')->getCalculatedValue();
self::assertSame(7, $value);
if ($this->issue2464) {
self::markTestIncomplete('testLen and testHYPERLINKcellRef incomplete due to issue 2464');
} else {
$hyperlink = $sheet->getCell('A1')->getHyperlink();
self::assertSame('', $hyperlink->getUrl());
self::assertSame('', $hyperlink->getTooltip());
}
}
public static function providerHYPERLINK(): array
{
return require 'tests/data/Calculation/LookupRef/HYPERLINK.php';
}
public function testHYPERLINKwithoutCell(): void
{
$result = LookupRef\Hyperlink::set('https://phpspreadsheet.readthedocs.io/en/latest/', 'Read the Docs');
self::assertSame(ExcelError::REF(), $result);
}
}