Oct2HexTest.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 Oct2HexTest 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 providerOCT2HEX
  24. *
  25. * @param mixed $expectedResult
  26. * @param mixed $formula
  27. */
  28. public function testOCT2HEX($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', 101);
  36. $sheet->getCell('A1')->setValue("=OCT2HEX($formula)");
  37. $result = $sheet->getCell('A1')->getCalculatedValue();
  38. self::assertEquals($expectedResult, $result);
  39. }
  40. public function providerOCT2HEX(): array
  41. {
  42. return require 'tests/data/Calculation/Engineering/OCT2HEX.php';
  43. }
  44. /**
  45. * @dataProvider providerOCT2HEX
  46. *
  47. * @param mixed $expectedResult
  48. * @param mixed $formula
  49. */
  50. public function testOCT2HEXOds($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', 101);
  64. $sheet->getCell('A1')->setValue("=OCT2HEX($formula)");
  65. $result = $sheet->getCell('A1')->getCalculatedValue();
  66. self::assertEquals($expectedResult, $result);
  67. }
  68. public function testOCT2HEXFrac(): void
  69. {
  70. $spreadsheet = new Spreadsheet();
  71. $sheet = $spreadsheet->getActiveSheet();
  72. Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
  73. $cell = 'G1';
  74. $sheet->setCellValue($cell, '=OCT2HEX(20.1)');
  75. self::assertEquals(10, $sheet->getCell($cell)->getCalculatedValue());
  76. Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
  77. $cell = 'O1';
  78. $sheet->setCellValue($cell, '=OCT2HEX(20.1)');
  79. self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue());
  80. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  81. $cell = 'E1';
  82. $sheet->setCellValue($cell, '=OCT2HEX(20.1)');
  83. self::assertEquals('#NUM!', $sheet->getCell($cell)->getCalculatedValue());
  84. }
  85. /**
  86. * @dataProvider providerOct2HexArray
  87. */
  88. public function testOct2HexArray(array $expectedResult, string $value): void
  89. {
  90. $calculation = Calculation::getInstance();
  91. $formula = "=OCT2HEX({$value})";
  92. $result = $calculation->_calculateFormulaValue($formula);
  93. self::assertEquals($expectedResult, $result);
  94. }
  95. public function providerOct2HexArray(): array
  96. {
  97. return [
  98. 'row/column vector' => [
  99. [['4', '7', '3F', '99', 'CC', '155']],
  100. '{"4", "7", "77", "231", "314", "525"}',
  101. ],
  102. ];
  103. }
  104. }