1111use PhpOffice \PhpSpreadsheet \Shared \Date ;
1212use PhpOffice \PhpSpreadsheet \Spreadsheet ;
1313use PhpOffice \PhpSpreadsheet \Style \NumberFormat ;
14+ use PHPUnit \Framework \Attributes \DataProvider ;
1415use PHPUnit \Framework \TestCase ;
1516
1617class DateTest extends TestCase
@@ -38,10 +39,15 @@ public function testSetExcelCalendar(): void
3839 Date::CALENDAR_WINDOWS_1900 ,
3940 ];
4041
42+ $ spreadsheet = new Spreadsheet ();
4143 foreach ($ calendarValues as $ calendarValue ) {
4244 $ result = Date::setExcelCalendar ($ calendarValue );
4345 self ::assertTrue ($ result );
46+ $ result = $ spreadsheet ->setExcelCalendar ($ calendarValue );
47+ self ::assertTrue ($ result );
4448 }
49+ self ::assertFalse ($ spreadsheet ->setExcelCalendar (0 ));
50+ $ spreadsheet ->disconnectWorksheets ();
4551 }
4652
4753 public function testSetExcelCalendarWithInvalidValue (): void
@@ -51,7 +57,7 @@ public function testSetExcelCalendarWithInvalidValue(): void
5157 self ::assertFalse ($ result );
5258 }
5359
54- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeExcelToTimestamp1900 ' )]
60+ #[DataProvider('providerDateTimeExcelToTimestamp1900 ' )]
5561 public function testDateTimeExcelToTimestamp1900 (float |int $ expectedResult , float |int $ excelDateTimeValue ): void
5662 {
5763 if ($ expectedResult > PHP_INT_MAX || $ expectedResult < PHP_INT_MIN ) {
@@ -68,7 +74,7 @@ public static function providerDateTimeExcelToTimestamp1900(): array
6874 return require 'tests/data/Shared/Date/ExcelToTimestamp1900.php ' ;
6975 }
7076
71- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeTimestampToExcel1900 ' )]
77+ #[DataProvider('providerDateTimeTimestampToExcel1900 ' )]
7278 public function testDateTimeTimestampToExcel1900 (float |int $ expectedResult , float |int |string $ unixTimestamp ): void
7379 {
7480 Date::setExcelCalendar (Date::CALENDAR_WINDOWS_1900 );
@@ -82,7 +88,7 @@ public static function providerDateTimeTimestampToExcel1900(): array
8288 return require 'tests/data/Shared/Date/TimestampToExcel1900.php ' ;
8389 }
8490
85- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeDateTimeToExcel ' )]
91+ #[DataProvider('providerDateTimeDateTimeToExcel ' )]
8692 public function testDateTimeDateTimeToExcel (float |int $ expectedResult , DateTimeInterface $ dateTimeObject ): void
8793 {
8894 Date::setExcelCalendar (Date::CALENDAR_WINDOWS_1900 );
@@ -99,7 +105,7 @@ public static function providerDateTimeDateTimeToExcel(): array
99105 /**
100106 * @param array{0: int, 1: int, 2: int, 3: int, 4: int, 5: float|int} $args Array containing year/month/day/hours/minutes/seconds
101107 */
102- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeFormattedPHPToExcel1900 ' )]
108+ #[DataProvider('providerDateTimeFormattedPHPToExcel1900 ' )]
103109 public function testDateTimeFormattedPHPToExcel1900 (mixed $ expectedResult , ...$ args ): void
104110 {
105111 Date::setExcelCalendar (Date::CALENDAR_WINDOWS_1900 );
@@ -113,7 +119,7 @@ public static function providerDateTimeFormattedPHPToExcel1900(): array
113119 return require 'tests/data/Shared/Date/FormattedPHPToExcel1900.php ' ;
114120 }
115121
116- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeExcelToTimestamp1904 ' )]
122+ #[DataProvider('providerDateTimeExcelToTimestamp1904 ' )]
117123 public function testDateTimeExcelToTimestamp1904 (float |int $ expectedResult , float |int $ excelDateTimeValue ): void
118124 {
119125 if ($ expectedResult > PHP_INT_MAX || $ expectedResult < PHP_INT_MIN ) {
@@ -130,7 +136,7 @@ public static function providerDateTimeExcelToTimestamp1904(): array
130136 return require 'tests/data/Shared/Date/ExcelToTimestamp1904.php ' ;
131137 }
132138
133- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeTimestampToExcel1904 ' )]
139+ #[DataProvider('providerDateTimeTimestampToExcel1904 ' )]
134140 public function testDateTimeTimestampToExcel1904 (mixed $ expectedResult , float |int |string $ unixTimestamp ): void
135141 {
136142 Date::setExcelCalendar (Date::CALENDAR_MAC_1904 );
@@ -144,7 +150,7 @@ public static function providerDateTimeTimestampToExcel1904(): array
144150 return require 'tests/data/Shared/Date/TimestampToExcel1904.php ' ;
145151 }
146152
147- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerIsDateTimeFormatCode ' )]
153+ #[DataProvider('providerIsDateTimeFormatCode ' )]
148154 public function testIsDateTimeFormatCode (mixed $ expectedResult , string $ format ): void
149155 {
150156 $ result = Date::isDateTimeFormatCode ($ format );
@@ -156,7 +162,7 @@ public static function providerIsDateTimeFormatCode(): array
156162 return require 'tests/data/Shared/Date/FormatCodes.php ' ;
157163 }
158164
159- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeExcelToTimestamp1900Timezone ' )]
165+ #[DataProvider('providerDateTimeExcelToTimestamp1900Timezone ' )]
160166 public function testDateTimeExcelToTimestamp1900Timezone (float |int $ expectedResult , float |int $ excelDateTimeValue , string $ timezone ): void
161167 {
162168 if ($ expectedResult > PHP_INT_MAX || $ expectedResult < PHP_INT_MIN ) {
@@ -198,6 +204,9 @@ public function testVarious(): void
198204
199205 $ date = Date::PHPToExcel ('2020-01-01 ' );
200206 self ::assertEquals (43831.0 , $ date );
207+ $ phpDate = new DateTime ('2020-01-02T00:00Z ' );
208+ $ date = Date::PHPToExcel ($ phpDate );
209+ self ::assertEquals (43832.0 , $ date );
201210
202211 $ spreadsheet = new Spreadsheet ();
203212 $ sheet = $ spreadsheet ->getActiveSheet ();
@@ -240,6 +249,27 @@ public function testVarious(): void
240249 $ spreadsheet ->disconnectWorksheets ();
241250 }
242251
252+ public function testArray (): void
253+ {
254+ $ spreadsheet = new Spreadsheet ();
255+ $ spreadsheet ->returnArrayAsArray ();
256+ $ sheet = $ spreadsheet ->getActiveSheet ();
257+ $ sheet ->setCellValue ('A1 ' , 45000 );
258+ $ sheet ->setCellValue ('A2 ' , 44000 );
259+ $ sheet ->setCellValue ('A3 ' , 46000 );
260+ $ sheet ->setCellValue ('C1 ' , '=SORT(A1:A3) ' );
261+ $ sheet ->setCellValue ('D1 ' , '=SORT(A1:A3) ' );
262+ $ sheet ->getStyle ('C1 ' )
263+ ->getNumberFormat ()
264+ ->setFormatCode ('yyyy-mm-dd ' );
265+ self ::assertTrue (Date::isDateTime ($ sheet ->getCell ('C1 ' )));
266+ self ::assertFalse (Date::isDateTime ($ sheet ->getCell ('D1 ' )));
267+ self ::assertIsArray (
268+ $ sheet ->getCell ('C1 ' )->getCalculatedValue ()
269+ );
270+ $ spreadsheet ->disconnectWorksheets ();
271+ }
272+
243273 public function testRoundMicroseconds (): void
244274 {
245275 $ dti = new DateTime ('2000-01-02 03:04:05.999999 ' );
0 commit comments