CalculationFunctionListTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PHPUnit\Framework\TestCase;
  7. class CalculationFunctionListTest extends TestCase
  8. {
  9. /**
  10. * @var string
  11. */
  12. private $compatibilityMode;
  13. /**
  14. * @var string
  15. */
  16. private $locale;
  17. protected function setUp(): void
  18. {
  19. $this->compatibilityMode = Functions::getCompatibilityMode();
  20. $calculation = Calculation::getInstance();
  21. $this->locale = $calculation->getLocale();
  22. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  23. }
  24. protected function tearDown(): void
  25. {
  26. Functions::setCompatibilityMode($this->compatibilityMode);
  27. $calculation = Calculation::getInstance();
  28. $calculation->setLocale($this->locale);
  29. }
  30. /**
  31. * @dataProvider providerGetFunctions
  32. *
  33. * @param string $category
  34. * @param array|string $functionCall
  35. * @param string $argumentCount
  36. */
  37. public function testGetFunctions(/** @scrutinizer ignore-unused */ $category, $functionCall, /** @scrutinizer ignore-unused */ $argumentCount): void
  38. {
  39. self::assertIsCallable($functionCall);
  40. }
  41. public function providerGetFunctions(): array
  42. {
  43. return Calculation::getInstance()->getFunctions();
  44. }
  45. public function testIsImplemented(): void
  46. {
  47. $calculation = Calculation::getInstance();
  48. self::assertFalse($calculation->isImplemented('non-existing-function'));
  49. self::assertFalse($calculation->isImplemented('AREAS'));
  50. self::assertTrue($calculation->isImplemented('coUNt'));
  51. self::assertTrue($calculation->isImplemented('abs'));
  52. }
  53. public function testUnknownFunction(): void
  54. {
  55. $workbook = new Spreadsheet();
  56. $sheet = $workbook->getActiveSheet();
  57. $sheet->setCellValue('A1', '=gzorg()');
  58. $sheet->setCellValue('A2', '=mode.gzorg(1)');
  59. $sheet->setCellValue('A3', '=gzorg(1,2)');
  60. $sheet->setCellValue('A4', '=3+IF(gzorg(),1,2)');
  61. self::assertEquals('#NAME?', $sheet->getCell('A1')->getCalculatedValue());
  62. self::assertEquals('#NAME?', $sheet->getCell('A2')->getCalculatedValue());
  63. self::assertEquals('#NAME?', $sheet->getCell('A3')->getCalculatedValue());
  64. self::assertEquals('#NAME?', $sheet->getCell('A4')->getCalculatedValue());
  65. }
  66. }