AllSetupTeardown.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
  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. }