AllSetupTeardown.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Financial;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  5. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  8. use PHPUnit\Framework\TestCase;
  9. class AllSetupTeardown extends TestCase
  10. {
  11. /**
  12. * @var string
  13. */
  14. private $compatibilityMode;
  15. /**
  16. * @var ?Spreadsheet
  17. */
  18. private $spreadsheet;
  19. /**
  20. * @var ?Worksheet
  21. */
  22. private $sheet;
  23. protected function setUp(): void
  24. {
  25. $this->compatibilityMode = Functions::getCompatibilityMode();
  26. }
  27. protected function tearDown(): void
  28. {
  29. Functions::setCompatibilityMode($this->compatibilityMode);
  30. $this->sheet = null;
  31. if ($this->spreadsheet !== null) {
  32. $this->spreadsheet->disconnectWorksheets();
  33. $this->spreadsheet = null;
  34. }
  35. }
  36. protected static function setOpenOffice(): void
  37. {
  38. Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
  39. }
  40. protected static function setGnumeric(): void
  41. {
  42. Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
  43. }
  44. /**
  45. * @param mixed $expectedResult
  46. */
  47. protected function mightHaveException($expectedResult): void
  48. {
  49. if ($expectedResult === 'exception') {
  50. $this->expectException(CalcException::class);
  51. }
  52. }
  53. /**
  54. * @param mixed $value
  55. */
  56. protected function setCell(string $cell, $value): void
  57. {
  58. if ($value !== null) {
  59. if (is_string($value) && is_numeric($value)) {
  60. $this->getSheet()->getCell($cell)->setValueExplicit($value, DataType::TYPE_STRING);
  61. } else {
  62. $this->getSheet()->getCell($cell)->setValue($value);
  63. }
  64. }
  65. }
  66. protected function getSpreadsheet(): Spreadsheet
  67. {
  68. if ($this->spreadsheet !== null) {
  69. return $this->spreadsheet;
  70. }
  71. $this->spreadsheet = new Spreadsheet();
  72. return $this->spreadsheet;
  73. }
  74. protected function getSheet(): Worksheet
  75. {
  76. if ($this->sheet !== null) {
  77. return $this->sheet;
  78. }
  79. $this->sheet = $this->getSpreadsheet()->getActiveSheet();
  80. return $this->sheet;
  81. }
  82. /**
  83. * Adjust result if it is close enough to expected by ratio
  84. * rather than offset.
  85. *
  86. * @param mixed $result
  87. * @param mixed $expectedResult
  88. */
  89. protected function adjustResult(&$result, $expectedResult): void
  90. {
  91. if (is_numeric($result) && is_numeric($expectedResult)) {
  92. if ($expectedResult != 0) {
  93. $frac = $result / $expectedResult;
  94. if ($frac > 0.999999 && $frac < 1.000001) {
  95. $result = $expectedResult;
  96. }
  97. }
  98. }
  99. }
  100. }