CodeTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class CodeTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerCODE
  8. *
  9. * @param mixed $expectedResult
  10. * @param mixed $character
  11. */
  12. public function testCODE($expectedResult, $character = 'omitted'): void
  13. {
  14. $this->mightHaveException($expectedResult);
  15. $sheet = $this->getSheet();
  16. if ($character === 'omitted') {
  17. $sheet->getCell('B1')->setValue('=CODE()');
  18. } else {
  19. $this->setCell('A1', $character);
  20. $sheet->getCell('B1')->setValue('=CODE(A1)');
  21. }
  22. $result = $sheet->getCell('B1')->getCalculatedValue();
  23. self::assertEquals($expectedResult, $result);
  24. }
  25. public function providerCODE(): array
  26. {
  27. return require 'tests/data/Calculation/TextData/CODE.php';
  28. }
  29. /**
  30. * @dataProvider providerCodeArray
  31. */
  32. public function testCodeArray(array $expectedResult, string $array): void
  33. {
  34. $calculation = Calculation::getInstance();
  35. $formula = "=CODE({$array})";
  36. $result = $calculation->_calculateFormulaValue($formula);
  37. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  38. }
  39. public function providerCodeArray(): array
  40. {
  41. return [
  42. 'row vector' => [[[80, 72, 80]], '{"P", "H", "P"}'],
  43. 'column vector' => [[[80], [72], [80]], '{"P"; "H"; "P"}'],
  44. 'matrix' => [[[89, 111], [108, 111]], '{"Y", "o"; "l", "o"}'],
  45. ];
  46. }
  47. }