Hex2DecTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Hex2DecTest 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 providerHEX2DEC
  24. *
  25. * @param mixed $expectedResult
  26. * @param mixed $formula
  27. */
  28. public function testHEX2DEC($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("=HEX2DEC($formula)");
  37. $result = $sheet->getCell('A1')->getCalculatedValue();
  38. self::assertEquals($expectedResult, $result);
  39. }
  40. public function providerHEX2DEC(): array
  41. {
  42. return require 'tests/data/Calculation/Engineering/HEX2DEC.php';
  43. }
  44. /**
  45. * @dataProvider providerHEX2DEC
  46. *
  47. * @param mixed $expectedResult
  48. * @param mixed $formula
  49. */
  50. public function testHEX2DECOds($expectedResult, $formula): void
  51. {
  52. Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
  53. if ($expectedResult === 'exception') {
  54. $this->expectException(CalcExp::class);
  55. }
  56. if ($formula === 'true') {
  57. $expectedResult = 1;
  58. } elseif ($formula === 'false') {
  59. $expectedResult = 0;
  60. }
  61. $spreadsheet = new Spreadsheet();
  62. $sheet = $spreadsheet->getActiveSheet();
  63. $sheet->setCellValue('A2', 'B');
  64. $sheet->getCell('A1')->setValue("=HEX2DEC($formula)");
  65. $result = $sheet->getCell('A1')->getCalculatedValue();
  66. self::assertEquals($expectedResult, $result);
  67. }
  68. public function testHEX2DECFrac(): void
  69. {
  70. $spreadsheet = new Spreadsheet();
  71. $sheet = $spreadsheet->getActiveSheet();
  72. Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
  73. $cell = 'G1';
  74. $sheet->setCellValue($cell, '=HEX2DEC(10.1)');
  75. self::assertEquals(16, $sheet->getCell($cell)->getCalculatedValue());
  76. $cell = 'F21';
  77. $sheet->setCellValue($cell, '=HEX2DEC("A.1")');
  78. self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue());
  79. Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
  80. $cell = 'O1';
  81. $sheet->setCellValue($cell, '=HEX2DEC(10.1)');
  82. self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue());
  83. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  84. $cell = 'E1';
  85. $sheet->setCellValue($cell, '=HEX2DEC(10.1)');
  86. self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue(), 'Excel');
  87. }
  88. /**
  89. * @dataProvider providerHex2DecArray
  90. */
  91. public function testHex2DecArray(array $expectedResult, string $value): void
  92. {
  93. $calculation = Calculation::getInstance();
  94. $formula = "=HEX2DEC({$value})";
  95. $result = $calculation->_calculateFormulaValue($formula);
  96. self::assertEquals($expectedResult, $result);
  97. }
  98. public function providerHex2DecArray(): array
  99. {
  100. return [
  101. 'row/column vector' => [
  102. [[4, 7, 63, 153, 204, 341]],
  103. '{"4", "7", "3F", "99", "CC", "155"}',
  104. ],
  105. ];
  106. }
  107. }