11
11
use PhpOffice \PhpSpreadsheet \Shared \Date ;
12
12
use PhpOffice \PhpSpreadsheet \Spreadsheet ;
13
13
use PhpOffice \PhpSpreadsheet \Style \NumberFormat ;
14
+ use PHPUnit \Framework \Attributes \DataProvider ;
14
15
use PHPUnit \Framework \TestCase ;
15
16
16
17
class DateTest extends TestCase
@@ -38,10 +39,15 @@ public function testSetExcelCalendar(): void
38
39
Date::CALENDAR_WINDOWS_1900 ,
39
40
];
40
41
42
+ $ spreadsheet = new Spreadsheet ();
41
43
foreach ($ calendarValues as $ calendarValue ) {
42
44
$ result = Date::setExcelCalendar ($ calendarValue );
43
45
self ::assertTrue ($ result );
46
+ $ result = $ spreadsheet ->setExcelCalendar ($ calendarValue );
47
+ self ::assertTrue ($ result );
44
48
}
49
+ self ::assertFalse ($ spreadsheet ->setExcelCalendar (0 ));
50
+ $ spreadsheet ->disconnectWorksheets ();
45
51
}
46
52
47
53
public function testSetExcelCalendarWithInvalidValue (): void
@@ -51,7 +57,7 @@ public function testSetExcelCalendarWithInvalidValue(): void
51
57
self ::assertFalse ($ result );
52
58
}
53
59
54
- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeExcelToTimestamp1900 ' )]
60
+ #[DataProvider('providerDateTimeExcelToTimestamp1900 ' )]
55
61
public function testDateTimeExcelToTimestamp1900 (float |int $ expectedResult , float |int $ excelDateTimeValue ): void
56
62
{
57
63
if ($ expectedResult > PHP_INT_MAX || $ expectedResult < PHP_INT_MIN ) {
@@ -68,7 +74,7 @@ public static function providerDateTimeExcelToTimestamp1900(): array
68
74
return require 'tests/data/Shared/Date/ExcelToTimestamp1900.php ' ;
69
75
}
70
76
71
- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeTimestampToExcel1900 ' )]
77
+ #[DataProvider('providerDateTimeTimestampToExcel1900 ' )]
72
78
public function testDateTimeTimestampToExcel1900 (float |int $ expectedResult , float |int |string $ unixTimestamp ): void
73
79
{
74
80
Date::setExcelCalendar (Date::CALENDAR_WINDOWS_1900 );
@@ -82,7 +88,7 @@ public static function providerDateTimeTimestampToExcel1900(): array
82
88
return require 'tests/data/Shared/Date/TimestampToExcel1900.php ' ;
83
89
}
84
90
85
- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeDateTimeToExcel ' )]
91
+ #[DataProvider('providerDateTimeDateTimeToExcel ' )]
86
92
public function testDateTimeDateTimeToExcel (float |int $ expectedResult , DateTimeInterface $ dateTimeObject ): void
87
93
{
88
94
Date::setExcelCalendar (Date::CALENDAR_WINDOWS_1900 );
@@ -99,7 +105,7 @@ public static function providerDateTimeDateTimeToExcel(): array
99
105
/**
100
106
* @param array{0: int, 1: int, 2: int, 3: int, 4: int, 5: float|int} $args Array containing year/month/day/hours/minutes/seconds
101
107
*/
102
- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeFormattedPHPToExcel1900 ' )]
108
+ #[DataProvider('providerDateTimeFormattedPHPToExcel1900 ' )]
103
109
public function testDateTimeFormattedPHPToExcel1900 (mixed $ expectedResult , ...$ args ): void
104
110
{
105
111
Date::setExcelCalendar (Date::CALENDAR_WINDOWS_1900 );
@@ -113,7 +119,7 @@ public static function providerDateTimeFormattedPHPToExcel1900(): array
113
119
return require 'tests/data/Shared/Date/FormattedPHPToExcel1900.php ' ;
114
120
}
115
121
116
- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeExcelToTimestamp1904 ' )]
122
+ #[DataProvider('providerDateTimeExcelToTimestamp1904 ' )]
117
123
public function testDateTimeExcelToTimestamp1904 (float |int $ expectedResult , float |int $ excelDateTimeValue ): void
118
124
{
119
125
if ($ expectedResult > PHP_INT_MAX || $ expectedResult < PHP_INT_MIN ) {
@@ -130,7 +136,7 @@ public static function providerDateTimeExcelToTimestamp1904(): array
130
136
return require 'tests/data/Shared/Date/ExcelToTimestamp1904.php ' ;
131
137
}
132
138
133
- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeTimestampToExcel1904 ' )]
139
+ #[DataProvider('providerDateTimeTimestampToExcel1904 ' )]
134
140
public function testDateTimeTimestampToExcel1904 (mixed $ expectedResult , float |int |string $ unixTimestamp ): void
135
141
{
136
142
Date::setExcelCalendar (Date::CALENDAR_MAC_1904 );
@@ -144,7 +150,7 @@ public static function providerDateTimeTimestampToExcel1904(): array
144
150
return require 'tests/data/Shared/Date/TimestampToExcel1904.php ' ;
145
151
}
146
152
147
- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerIsDateTimeFormatCode ' )]
153
+ #[DataProvider('providerIsDateTimeFormatCode ' )]
148
154
public function testIsDateTimeFormatCode (mixed $ expectedResult , string $ format ): void
149
155
{
150
156
$ result = Date::isDateTimeFormatCode ($ format );
@@ -156,7 +162,7 @@ public static function providerIsDateTimeFormatCode(): array
156
162
return require 'tests/data/Shared/Date/FormatCodes.php ' ;
157
163
}
158
164
159
- #[\ PHPUnit \ Framework \ Attributes \ DataProvider('providerDateTimeExcelToTimestamp1900Timezone ' )]
165
+ #[DataProvider('providerDateTimeExcelToTimestamp1900Timezone ' )]
160
166
public function testDateTimeExcelToTimestamp1900Timezone (float |int $ expectedResult , float |int $ excelDateTimeValue , string $ timezone ): void
161
167
{
162
168
if ($ expectedResult > PHP_INT_MAX || $ expectedResult < PHP_INT_MIN ) {
@@ -198,6 +204,9 @@ public function testVarious(): void
198
204
199
205
$ date = Date::PHPToExcel ('2020-01-01 ' );
200
206
self ::assertEquals (43831.0 , $ date );
207
+ $ phpDate = new DateTime ('2020-01-02T00:00Z ' );
208
+ $ date = Date::PHPToExcel ($ phpDate );
209
+ self ::assertEquals (43832.0 , $ date );
201
210
202
211
$ spreadsheet = new Spreadsheet ();
203
212
$ sheet = $ spreadsheet ->getActiveSheet ();
@@ -240,6 +249,27 @@ public function testVarious(): void
240
249
$ spreadsheet ->disconnectWorksheets ();
241
250
}
242
251
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
+
243
273
public function testRoundMicroseconds (): void
244
274
{
245
275
$ dti = new DateTime ('2000-01-02 03:04:05.999999 ' );
0 commit comments