|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpSpreadsheetTests\Shared; |
| 6 | + |
| 7 | +use PhpOffice\PhpSpreadsheet\Cell\DataType; |
| 8 | +use PhpOffice\PhpSpreadsheet\Shared\Date; |
| 9 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +class Issue4696Test extends TestCase |
| 14 | +{ |
| 15 | + private ?Spreadsheet $spreadsheet = null; |
| 16 | + |
| 17 | + protected function tearDown(): void |
| 18 | + { |
| 19 | + if ($this->spreadsheet !== null) { |
| 20 | + $this->spreadsheet->disconnectWorksheets(); |
| 21 | + $this->spreadsheet = null; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + #[DataProvider('providerIsDateTime')] |
| 26 | + public function testIsDateTime(bool $expectedResult, string $expectedFormatted, int|float|string $value): void |
| 27 | + { |
| 28 | + $this->spreadsheet = new Spreadsheet(); |
| 29 | + $sheet = $this->spreadsheet->getActiveSheet(); |
| 30 | + if (is_string($value) && $value[0] !== '=') { |
| 31 | + $sheet->getCell('A1')->setValueExplicit($value, DataType::TYPE_STRING); |
| 32 | + } else { |
| 33 | + $sheet->getCell('A1')->setValue($value); |
| 34 | + } |
| 35 | + $sheet->getStyle('A1')->getNumberFormat() |
| 36 | + ->setFormatCode('yyyy-mm-dd'); |
| 37 | + self::assertSame( |
| 38 | + $expectedResult, |
| 39 | + Date::isDateTime($sheet->getCell('A1')) |
| 40 | + ); |
| 41 | + self::assertSame( |
| 42 | + $expectedFormatted, |
| 43 | + $sheet->getCell('A1')->getFormattedValue() |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public static function providerIsDateTime(): array |
| 48 | + { |
| 49 | + return [ |
| 50 | + 'valid integer' => [true, '1903-12-31', 1461], |
| 51 | + 'valid integer stored as string' => [true, '1904-01-01', '1462'], |
| 52 | + 'valid integer stored as concatenated string' => [true, '1904-01-01', '="14"&"62"'], |
| 53 | + 'valid float' => [true, '1903-12-31', 1461.5], |
| 54 | + 'valid float stored as string' => [true, '1903-12-31', '1461.5'], |
| 55 | + 'out-of-range integer' => [false, '7000989091802000122', 7000989091802000122], |
| 56 | + 'out-of-range float' => [false, '7.000989091802E+18', 7000989091802000122.1], |
| 57 | + 'out-of-range float stored as string' => [false, '7000989091802000122.1', '7000989091802000122.1'], |
| 58 | + 'non-numeric' => [false, 'xyz', 'xyz'], |
| 59 | + 'issue 917' => [false, '5e8630b8-603c-43fe-b038-6154a3f893ab', '5e8630b8-603c-43fe-b038-6154a3f893ab'], |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + #[DataProvider('providerOtherFunctions')] |
| 64 | + public function testOtherFunctions(string $function): void |
| 65 | + { |
| 66 | + $this->spreadsheet = new Spreadsheet(); |
| 67 | + $sheet = $this->spreadsheet->getActiveSheet(); |
| 68 | + $sheet->getCell('A1')->setValue(7000989091802000122); |
| 69 | + $sheet->getCell('A3')->setValue(39107); // 2007-01-25 |
| 70 | + $sheet->getCell('A4')->setValue(39767); // 2008-11-15 |
| 71 | + $sheet->getCell('A5')->setValue(2); |
| 72 | + $sheet->getCell('A6')->setValue(1); |
| 73 | + $sheet->getCell('B1')->setValue($function); |
| 74 | + self::assertSame( |
| 75 | + '#NUM!', |
| 76 | + $sheet->getCell('B1')->getFormattedValue() |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public static function providerOtherFunctions(): array |
| 81 | + { |
| 82 | + return [ |
| 83 | + ['=YEAR(A1)'], |
| 84 | + ['=MONTH(A1)'], |
| 85 | + ['=DAY(A1)'], |
| 86 | + ['=DAYS(A1,A1)'], |
| 87 | + ['=DAYS360(A1,A1)'], |
| 88 | + ['=DATEDIF(A1,A1,"D")'], |
| 89 | + ['=HOUR(A1)'], |
| 90 | + ['=MINUTE(A1)'], |
| 91 | + ['=SECOND(A1)'], |
| 92 | + ['=WEEKNUM(A1)'], |
| 93 | + ['=ISOWEEKNUM(A1)'], |
| 94 | + ['=WEEKDAY(A1)'], |
| 95 | + ['=COUPDAYBS(A1,A4,A5,A6)'], |
| 96 | + ['=COUPDAYS(A3,A2,A5,A6)'], |
| 97 | + ['=COUPDAYSNC(A3,A2,A5,A6)'], |
| 98 | + ['=COUPNCD(A3,A2,A5,A6)'], |
| 99 | + ['=COUPNUM(A3,A2,A5,A6)'], |
| 100 | + ['=COUPPCD(A3,A2,A5,A6)'], |
| 101 | + ]; |
| 102 | + } |
| 103 | +} |
0 commit comments