NamedFormulaTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests;
  3. use PhpOffice\PhpSpreadsheet\NamedFormula;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  6. use PHPUnit\Framework\TestCase;
  7. class NamedFormulaTest extends TestCase
  8. {
  9. /** @var Spreadsheet */
  10. private $spreadsheet;
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. $this->spreadsheet = new Spreadsheet();
  15. $this->spreadsheet->getActiveSheet()
  16. ->setTitle('Sheet #1');
  17. $worksheet2 = new Worksheet();
  18. $worksheet2->setTitle('Sheet #2');
  19. $this->spreadsheet->addSheet($worksheet2);
  20. $this->spreadsheet->setActiveSheetIndex(0);
  21. }
  22. public function testAddNamedRange(): void
  23. {
  24. $this->spreadsheet->addNamedFormula(
  25. new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
  26. );
  27. self::assertCount(1, $this->spreadsheet->getDefinedNames());
  28. self::assertCount(1, $this->spreadsheet->getNamedFormulae());
  29. self::assertCount(0, $this->spreadsheet->getNamedRanges());
  30. }
  31. public function testAddDuplicateNamedRange(): void
  32. {
  33. $this->spreadsheet->addNamedFormula(
  34. new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
  35. );
  36. $this->spreadsheet->addNamedFormula(
  37. new NamedFormula('FOO', $this->spreadsheet->getActiveSheet(), '=16%')
  38. );
  39. self::assertCount(1, $this->spreadsheet->getNamedFormulae());
  40. $formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getActiveSheet());
  41. self::assertNotNull($formula);
  42. self::assertSame(
  43. '=16%',
  44. $formula->getValue()
  45. );
  46. }
  47. public function testAddScopedNamedFormulaWithSameName(): void
  48. {
  49. $this->spreadsheet->addNamedFormula(
  50. new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
  51. );
  52. $this->spreadsheet->addNamedFormula(
  53. new NamedFormula('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=16%', true)
  54. );
  55. self::assertCount(2, $this->spreadsheet->getNamedFormulae());
  56. $formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getActiveSheet());
  57. self::assertNotNull($formula);
  58. self::assertSame(
  59. '=19%',
  60. $formula->getValue()
  61. );
  62. $formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
  63. self::assertNotNull($formula);
  64. self::assertSame(
  65. '=16%',
  66. $formula->getValue()
  67. );
  68. }
  69. public function testRemoveNamedFormula(): void
  70. {
  71. $this->spreadsheet->addDefinedName(
  72. new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=16%')
  73. );
  74. $this->spreadsheet->addDefinedName(
  75. new NamedFormula('Bar', $this->spreadsheet->getActiveSheet(), '=19%')
  76. );
  77. $this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getActiveSheet());
  78. self::assertCount(1, $this->spreadsheet->getNamedFormulae());
  79. }
  80. public function testRemoveGlobalNamedFormulaWhenDuplicateNames(): void
  81. {
  82. $this->spreadsheet->addNamedFormula(
  83. new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
  84. );
  85. $this->spreadsheet->addNamedFormula(
  86. new NamedFormula('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=16%', true)
  87. );
  88. $this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getActiveSheet());
  89. self::assertCount(1, $this->spreadsheet->getNamedFormulae());
  90. $formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
  91. self::assertNotNull($formula);
  92. self::assertSame(
  93. '=16%',
  94. $formula->getValue()
  95. );
  96. }
  97. public function testRemoveScopedNamedFormulaWhenDuplicateNames(): void
  98. {
  99. $this->spreadsheet->addNamedFormula(
  100. new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
  101. );
  102. $this->spreadsheet->addNamedFormula(
  103. new NamedFormula('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=16%', true)
  104. );
  105. $this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
  106. self::assertCount(1, $this->spreadsheet->getNamedFormulae());
  107. $formula = $this->spreadsheet->getNamedFormula('foo');
  108. self::assertNotNull($formula);
  109. self::assertSame(
  110. '=19%',
  111. $formula->getValue()
  112. );
  113. }
  114. public function testRemoveNonExistentNamedFormula(): void
  115. {
  116. self::assertCount(0, $this->spreadsheet->getNamedFormulae());
  117. $this->spreadsheet->removeNamedFormula('Any');
  118. }
  119. }