Hex2OctTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use PHPUnit\Framework\TestCase;
  8. class Hex2OctTest extends TestCase
  9. {
  10. /**
  11. * @var string
  12. */
  13. private $compatibilityMode;
  14. protected function setUp(): void
  15. {
  16. $this->compatibilityMode = Functions::getCompatibilityMode();
  17. }
  18. protected function tearDown(): void
  19. {
  20. Functions::setCompatibilityMode($this->compatibilityMode);
  21. }
  22. /**
  23. * @dataProvider providerHEX2OCT
  24. *
  25. * @param mixed $expectedResult
  26. * @param mixed $formula
  27. */
  28. public function testHEX2OCT($expectedResult, $formula): void
  29. {
  30. if ($expectedResult === 'exception') {
  31. $this->expectException(CalcExp::class);
  32. }
  33. $spreadsheet = new Spreadsheet();
  34. $sheet = $spreadsheet->getActiveSheet();
  35. $sheet->setCellValue('A2', 'B');
  36. $sheet->getCell('A1')->setValue("=HEX2OCT($formula)");
  37. $result = $sheet->getCell('A1')->getCalculatedValue();
  38. self::assertEquals($expectedResult, $result);
  39. }
  40. public function providerHEX2OCT(): array
  41. {
  42. return require 'tests/data/Calculation/Engineering/HEX2OCT.php';
  43. }
  44. /**
  45. * @dataProvider providerHEX2OCT
  46. *
  47. * @param mixed $expectedResult
  48. * @param mixed $formula
  49. */
  50. public function testHEX2OCTOds($expectedResult, $formula): void
  51. {
  52. Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
  53. if ($formula === 'true') {
  54. $expectedResult = 1;
  55. } elseif ($formula === 'false') {
  56. $expectedResult = 0;
  57. }
  58. $spreadsheet = new Spreadsheet();
  59. $sheet = $spreadsheet->getActiveSheet();
  60. $sheet->setCellValue('A2', 'B');
  61. $sheet->getCell('A1')->setValue("=HEX2OCT($formula)");
  62. $result = $sheet->getCell('A1')->getCalculatedValue();
  63. self::assertEquals($expectedResult, $result);
  64. }
  65. public function testHEX2OCTFrac(): void
  66. {
  67. $spreadsheet = new Spreadsheet();
  68. $sheet = $spreadsheet->getActiveSheet();
  69. Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
  70. $cell = 'G1';
  71. $sheet->setCellValue($cell, '=HEX2OCT(10.1)');
  72. self::assertEquals(20, $sheet->getCell($cell)->getCalculatedValue());
  73. $cell = 'F21';
  74. $sheet->setCellValue($cell, '=HEX2OCT("A.1")');
  75. self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue());
  76. Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
  77. $cell = 'O1';
  78. $sheet->setCellValue($cell, '=HEX2OCT(10.1)');
  79. self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue());
  80. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  81. $cell = 'E1';
  82. $sheet->setCellValue($cell, '=HEX2OCT(10.1)');
  83. self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue(), 'Excel');
  84. }
  85. /**
  86. * @dataProvider providerHex2OctArray
  87. */
  88. public function testHex2OctArray(array $expectedResult, string $value): void
  89. {
  90. $calculation = Calculation::getInstance();
  91. $formula = "=HEX2OCT({$value})";
  92. $result = $calculation->_calculateFormulaValue($formula);
  93. self::assertEquals($expectedResult, $result);
  94. }
  95. public function providerHex2OctArray(): array
  96. {
  97. return [
  98. 'row/column vector' => [
  99. [['4', '7', '77', '231', '314', '525']],
  100. '{"4", "7", "3F", "99", "CC", "155"}',
  101. ],
  102. ];
  103. }
  104. }