AllSetupTeardown.php 2.6 KB

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