HyperlinkTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  4. use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
  5. class HyperlinkTest extends AllSetupTeardown
  6. {
  7. /** @var bool */
  8. protected $issue2464 = true;
  9. /**
  10. * @dataProvider providerHYPERLINK
  11. *
  12. * @param array|string $expectedResult
  13. */
  14. public function testHYPERLINK($expectedResult, ?string $linkUrl, ?string $description): void
  15. {
  16. $this->mightHaveException($expectedResult);
  17. $sheet = $this->getSheet();
  18. $formula = '=HYPERLINK(';
  19. if ($linkUrl !== null) {
  20. $formula .= '"' . $linkUrl . '"';
  21. if ($description !== null) {
  22. $formula .= ',"' . $description . '"';
  23. }
  24. }
  25. $formula .= ')';
  26. $sheet->getCell('A1')->setValue($formula);
  27. $result = $sheet->getCell('A1')->getCalculatedValue();
  28. if (!is_array($expectedResult)) {
  29. self::assertSame($expectedResult, $result);
  30. } else {
  31. self::assertSame($expectedResult[1], $result);
  32. $hyperlink = $sheet->getCell('A1')->getHyperlink();
  33. self::assertSame($expectedResult[0], $hyperlink->getUrl());
  34. self::assertSame($expectedResult[1], $hyperlink->getTooltip());
  35. }
  36. }
  37. /**
  38. * @dataProvider providerHYPERLINK
  39. *
  40. * @param array|string $expectedResult
  41. */
  42. public function testHYPERLINKcellRef($expectedResult, ?string $linkUrl, ?string $description): void
  43. {
  44. $this->mightHaveException($expectedResult);
  45. $sheet = $this->getSheet();
  46. $formula = '=HYPERLINK(';
  47. if ($linkUrl !== null) {
  48. $sheet->getCell('A2')->setValue($linkUrl);
  49. $formula .= 'A2';
  50. if ($description !== null) {
  51. $sheet->getCell('A3')->setValue($description);
  52. $formula .= ',A3';
  53. }
  54. }
  55. $formula .= ')';
  56. $sheet->getCell('A1')->setValue($formula);
  57. $result = $sheet->getCell('A1')->getCalculatedValue();
  58. if (!is_array($expectedResult)) {
  59. self::assertSame($expectedResult, $result);
  60. } else {
  61. self::assertSame($expectedResult[1], $result);
  62. if (!$this->issue2464) {
  63. $hyperlink = $sheet->getCell('A1')->getHyperlink();
  64. self::assertSame($expectedResult[0], $hyperlink->getUrl());
  65. self::assertSame($expectedResult[1], $hyperlink->getTooltip());
  66. }
  67. }
  68. }
  69. public function testLen(): void
  70. {
  71. $sheet = $this->getSheet();
  72. $sheet->getCell('A1')->setValue('=LEN(HYPERLINK("http://www.example.com","Example"))');
  73. $value = $sheet->getCell('A1')->getCalculatedValue();
  74. self::assertSame(7, $value);
  75. if ($this->issue2464) {
  76. self::markTestIncomplete('testLen and testHYPERLINKcellRef incomplete due to issue 2464');
  77. } else {
  78. $hyperlink = $sheet->getCell('A1')->getHyperlink();
  79. self::assertSame('', $hyperlink->getUrl());
  80. self::assertSame('', $hyperlink->getTooltip());
  81. }
  82. }
  83. public function providerHYPERLINK(): array
  84. {
  85. return require 'tests/data/Calculation/LookupRef/HYPERLINK.php';
  86. }
  87. public function testHYPERLINKwithoutCell(): void
  88. {
  89. $result = LookupRef\Hyperlink::set('https://phpspreadsheet.readthedocs.io/en/latest/', 'Read the Docs');
  90. self::assertSame(ExcelError::REF(), $result);
  91. }
  92. }