|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpSpreadsheetTests; |
| 6 | + |
| 7 | +use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; |
| 8 | +use PhpOffice\PhpSpreadsheet\Helper\Sample; |
| 9 | +use PhpOffice\PhpSpreadsheet\NamedRange; |
| 10 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 11 | +use PHPUnit\Framework\Attributes; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class SpreadsheetSerializeTest extends TestCase |
| 15 | +{ |
| 16 | + private ?Spreadsheet $spreadsheet = null; |
| 17 | + |
| 18 | + protected function tearDown(): void |
| 19 | + { |
| 20 | + if ($this->spreadsheet !== null) { |
| 21 | + $this->spreadsheet->disconnectWorksheets(); |
| 22 | + $this->spreadsheet = null; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public function testSerialize(): void |
| 27 | + { |
| 28 | + $this->spreadsheet = new Spreadsheet(); |
| 29 | + $sheet = $this->spreadsheet->getActiveSheet(); |
| 30 | + $sheet->getCell('A1')->setValue(10); |
| 31 | + |
| 32 | + $serialized = serialize($this->spreadsheet); |
| 33 | + $newSpreadsheet = unserialize($serialized); |
| 34 | + self::assertInstanceOf(Spreadsheet::class, $newSpreadsheet); |
| 35 | + self::assertNotSame($this->spreadsheet, $newSpreadsheet); |
| 36 | + $newSheet = $newSpreadsheet->getActiveSheet(); |
| 37 | + self::assertSame(10, $newSheet->getCell('A1')->getValue()); |
| 38 | + $newSpreadsheet->disconnectWorksheets(); |
| 39 | + } |
| 40 | + |
| 41 | + public function testNotJsonEncodable(): void |
| 42 | + { |
| 43 | + $this->spreadsheet = new Spreadsheet(); |
| 44 | + |
| 45 | + $this->expectException(SpreadsheetException::class); |
| 46 | + $this->expectExceptionMessage('Spreadsheet objects cannot be json encoded'); |
| 47 | + json_encode($this->spreadsheet); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * These tests are a bit weird. |
| 52 | + * If prepareSerialize and readSerialize are run in the same |
| 53 | + * process, the latter's assertions will always succeed. |
| 54 | + * So to demonstrate that the |
| 55 | + * problem is solved, they need to run in separate processes. |
| 56 | + * But then they can't share the file name. So we need to send |
| 57 | + * the file to a semi-hard-coded destination. |
| 58 | + */ |
| 59 | + private static function getTempFileName(): string |
| 60 | + { |
| 61 | + $helper = new Sample(); |
| 62 | + |
| 63 | + return $helper->getTemporaryFolder() . '/spreadsheet.serialize.test.txt'; |
| 64 | + } |
| 65 | + |
| 66 | + public function testPrepareSerialize(): void |
| 67 | + { |
| 68 | + $this->spreadsheet = new Spreadsheet(); |
| 69 | + $sheet = $this->spreadsheet->getActiveSheet(); |
| 70 | + $this->spreadsheet->addNamedRange(new NamedRange('summedcells', $sheet, '$A$1:$A$5')); |
| 71 | + $sheet->setCellValue('A1', 1); |
| 72 | + $sheet->setCellValue('A2', 2); |
| 73 | + $sheet->setCellValue('A3', 3); |
| 74 | + $sheet->setCellValue('A4', 4); |
| 75 | + $sheet->setCellValue('A5', 5); |
| 76 | + $sheet->setCellValue('C1', '=SUM(summedcells)'); |
| 77 | + $ser = serialize($this->spreadsheet); |
| 78 | + $this->spreadsheet->disconnectWorksheets(); |
| 79 | + $outputFileName = self::getTempFileName(); |
| 80 | + self::assertNotFalse( |
| 81 | + file_put_contents($outputFileName, $ser) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + #[Attributes\RunInSeparateProcess] |
| 86 | + public function testReadSerialize(): void |
| 87 | + { |
| 88 | + $inputFileName = self::getTempFileName(); |
| 89 | + $ser = (string) file_get_contents($inputFileName); |
| 90 | + unlink($inputFileName); |
| 91 | + $this->spreadsheet = unserialize($ser); |
| 92 | + $sheet = $this->spreadsheet->getActiveSheet(); |
| 93 | + self::assertSame('=SUM(summedcells)', $sheet->getCell('C1')->getValue()); |
| 94 | + self::assertSame(15, $sheet->getCell('C1')->getCalculatedValue()); |
| 95 | + } |
| 96 | +} |
0 commit comments