BinaryComparisonTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation;
  3. use PhpOffice\PhpSpreadsheet\Calculation\BinaryComparison;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Exception;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  6. use PHPUnit\Framework\TestCase;
  7. class BinaryComparisonTest extends TestCase
  8. {
  9. /**
  10. * @var string
  11. */
  12. private $compatibilityMode;
  13. protected function setUp(): void
  14. {
  15. $this->compatibilityMode = Functions::getCompatibilityMode();
  16. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  17. }
  18. protected function tearDown(): void
  19. {
  20. Functions::setCompatibilityMode($this->compatibilityMode);
  21. }
  22. /**
  23. * @dataProvider providerBinaryComparison
  24. *
  25. * @param mixed $operand1
  26. * @param mixed $operand2
  27. */
  28. public function testBinaryComparisonOperation(
  29. $operand1,
  30. $operand2,
  31. string $operator,
  32. bool $expectedResultExcel,
  33. bool $expectedResultOpenOffice
  34. ): void {
  35. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  36. $resultExcel = BinaryComparison::compare($operand1, $operand2, $operator);
  37. self::assertEquals($expectedResultExcel, $resultExcel, 'should be Excel compatible');
  38. Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
  39. $resultOpenOffice = BinaryComparison::compare($operand1, $operand2, $operator);
  40. self::assertEquals($expectedResultOpenOffice, $resultOpenOffice, 'should be OpenOffice compatible');
  41. }
  42. public function providerBinaryComparison(): array
  43. {
  44. return require 'tests/data/Calculation/BinaryComparisonOperations.php';
  45. }
  46. public function testInvalidOperator(): void
  47. {
  48. $this->expectException(Exception::class);
  49. $this->expectExceptionMessage('Unsupported binary comparison operator');
  50. BinaryComparison::compare(1, 2, '!=');
  51. }
  52. }