FunctionsTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  5. use PHPUnit\Framework\TestCase;
  6. class FunctionsTest extends TestCase
  7. {
  8. /**
  9. * @var string
  10. */
  11. private $compatibilityMode;
  12. /**
  13. * @var string
  14. */
  15. private $returnDate;
  16. protected function setUp(): void
  17. {
  18. $this->compatibilityMode = Functions::getCompatibilityMode();
  19. $this->returnDate = Functions::getReturnDateType();
  20. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  21. Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
  22. }
  23. protected function tearDown(): void
  24. {
  25. Functions::setCompatibilityMode($this->compatibilityMode);
  26. Functions::setReturnDateType($this->returnDate);
  27. }
  28. public function testCompatibilityMode(): void
  29. {
  30. $result = Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
  31. // Test for a true response for success
  32. self::assertTrue($result);
  33. // Test that mode has been changed
  34. self::assertEquals(Functions::COMPATIBILITY_GNUMERIC, Functions::getCompatibilityMode());
  35. }
  36. public function testInvalidCompatibilityMode(): void
  37. {
  38. $result = Functions::setCompatibilityMode('INVALIDMODE');
  39. // Test for a false response for failure
  40. self::assertFalse($result);
  41. // Test that mode has not been changed
  42. self::assertEquals(Functions::COMPATIBILITY_EXCEL, Functions::getCompatibilityMode());
  43. }
  44. public function testReturnDateType(): void
  45. {
  46. $result = Functions::setReturnDateType(Functions::RETURNDATE_PHP_OBJECT);
  47. // Test for a true response for success
  48. self::assertTrue($result);
  49. // Test that mode has been changed
  50. self::assertEquals(Functions::RETURNDATE_PHP_OBJECT, Functions::getReturnDateType());
  51. }
  52. public function testInvalidReturnDateType(): void
  53. {
  54. $result = Functions::setReturnDateType('INVALIDTYPE');
  55. // Test for a false response for failure
  56. self::assertFalse($result);
  57. // Test that mode has not been changed
  58. self::assertEquals(Functions::RETURNDATE_EXCEL, Functions::getReturnDateType());
  59. }
  60. public function testDUMMY(): void
  61. {
  62. $result = Functions::DUMMY();
  63. self::assertEquals('#Not Yet Implemented', $result);
  64. }
  65. /**
  66. * @dataProvider providerIfCondition
  67. */
  68. public function testIfCondition(string $expectedResult, string $args): void
  69. {
  70. $result = Functions::ifCondition($args);
  71. self::assertEquals($expectedResult, $result);
  72. }
  73. public function providerIfCondition(): array
  74. {
  75. return require 'tests/data/Calculation/Functions/IF_CONDITION.php';
  76. }
  77. public function testDeprecatedIsFormula(): void
  78. {
  79. $result = Functions::isFormula('="STRING"');
  80. self::assertEquals(ExcelError::REF(), $result);
  81. }
  82. public function testScalar(): void
  83. {
  84. $value = 'scalar';
  85. $result = Functions::scalar([[$value]]);
  86. self::assertSame($value, $result);
  87. }
  88. }