mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-12 11:06:28 +00:00
e9cf27354d
Successor to PR #3523. There are 494 single-line changes (`public function provider` to `public static function provider`) in this PR. None of these were made manually; they were all created with the following script (adapted from https://stackoverflow.com/questions/25909820/how-to-recursively-iterate-through-files-in-php): ```php $dir = 'C:/git/unit10prep2/tests/PhpSpreadsheetTests'; $it = new RecursiveDirectoryIterator($dir); // Loop through files foreach(new RecursiveIteratorIterator($it) as $file) { if ($file->getExtension() === 'php') { $contents = file_get_contents($file); $new = preg_replace('/public function (\\w*)([Pp])rovider/', 'public static function $1$2rovider', $contents); if ($new !== $contents) { echo "changing $file\n"; file_put_contents($file, $new); } } } ``` After this PR, there will be one more, with a small number of test changes, and enabling PhpUnit 10 for Php 8.1+.
103 lines
3.5 KiB
PHP
103 lines
3.5 KiB
PHP
<?php
|
|
|
|
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
|
|
*
|
|
* @param array|string $expectedResult
|
|
*/
|
|
public function testHYPERLINK($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
|
|
*
|
|
* @param array|string $expectedResult
|
|
*/
|
|
public function testHYPERLINKcellRef($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);
|
|
}
|
|
}
|