AllSetupTeardown.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  5. use PhpOffice\PhpSpreadsheet\Shared\Date;
  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 int
  17. */
  18. private $excelCalendar;
  19. /**
  20. * @var string
  21. */
  22. private $returnDateType;
  23. /**
  24. * @var ?Spreadsheet
  25. */
  26. private $spreadsheet;
  27. /**
  28. * @var ?Worksheet
  29. */
  30. private $sheet;
  31. protected function setUp(): void
  32. {
  33. $this->compatibilityMode = Functions::getCompatibilityMode();
  34. $this->excelCalendar = Date::getExcelCalendar();
  35. $this->returnDateType = Functions::getReturnDateType();
  36. }
  37. protected function tearDown(): void
  38. {
  39. Date::setExcelCalendar($this->excelCalendar);
  40. Functions::setCompatibilityMode($this->compatibilityMode);
  41. Functions::setReturnDateType($this->returnDateType);
  42. $this->sheet = null;
  43. if ($this->spreadsheet !== null) {
  44. $this->spreadsheet->disconnectWorksheets();
  45. $this->spreadsheet = null;
  46. }
  47. }
  48. protected static function setMac1904(): void
  49. {
  50. Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
  51. }
  52. protected static function setUnixReturn(): void
  53. {
  54. Functions::setReturnDateType(Functions::RETURNDATE_UNIX_TIMESTAMP);
  55. }
  56. protected static function setObjectReturn(): void
  57. {
  58. Functions::setReturnDateType(Functions::RETURNDATE_PHP_DATETIME_OBJECT);
  59. }
  60. protected static function setOpenOffice(): void
  61. {
  62. Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
  63. }
  64. /**
  65. * @param mixed $expectedResult
  66. */
  67. protected function mightHaveException($expectedResult): void
  68. {
  69. if ($expectedResult === 'exception') {
  70. $this->expectException(CalcException::class);
  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. }