Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Worksheet/CloneTest.php
T
oleibman 00191100ba Deprecate Worksheet::getHashInt and Spreadsheet::getId
Php8.5 *may* deprecate the use of `__wakeup` (it is planned, but not yet implemented, and it is convtroversial). We use it only twice. In the first instance, it just throws an exception to prevent a security exploit. This can be trivially replaced with `__unserialize`.

The other instance is merely to initialize a Worksheet instance variable. Converting this use to `__unserialize` is *not* trivial (one of the reasons for the controversy). However, I see no useful purpose for that variable. Since it has no use, there is no real need for `__wakeup`, so we will just remove the routine altogether, and deprecate the variable's getter (there is no setter).

A similar instance variable in Spreadsheet also serves no useful purpose, so we will deprecate its getter as well (again no setter).
2025-09-10 00:27:22 -07:00

72 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
class CloneTest extends TestCase
{
public function testUnattachedIndex(): void
{
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet1->getCell('A1')->setValue(10);
$sheet2 = clone $sheet1;
$sheet2->getCell('A1')->setValue(20);
self::assertSame(0, $spreadsheet->getIndex($sheet1));
$idx = $spreadsheet->getIndex($sheet2, true);
self::assertSame(-1, $idx);
$sheet2->setTitle('clone');
$spreadsheet->addSheet($sheet2);
$idx = $spreadsheet->getIndex($sheet2, true);
self::assertSame(1, $idx);
self::assertSame(10, $spreadsheet->getSheet(0)->getCell('A1')->getValue());
self::assertSame(20, $spreadsheet->getSheet(1)->getCell('A1')->getValue());
$spreadsheet->disconnectWorksheets();
}
public function testGetCloneIndex(): void
{
$this->expectException(SpreadsheetException::class);
$this->expectExceptionMessage('Sheet does not exist');
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet1->getCell('A1')->setValue(10);
$sheet2 = clone $sheet1;
$spreadsheet->getSheet($spreadsheet->getIndex($sheet2))
->setCellValue('A1', 100);
}
public function testSerialize1(): void
{
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet1->getCell('A1')->setValue(10);
$serialized = serialize($sheet1);
$newSheet = unserialize($serialized);
self::assertInstanceOf(Worksheet::class, $newSheet);
self::assertSame(10, $newSheet->getCell('A1')->getValue());
self::assertNotSame($newSheet, $sheet1);
self::assertNotNull($newSheet->getParent());
self::assertNotSame($newSheet->getParent(), $sheet1->getParent());
$newSheet->getParent()->disconnectWorksheets();
$spreadsheet->disconnectWorksheets();
}
public function testSerialize2(): void
{
$sheet1 = new Worksheet();
$sheet1->getCell('A1')->setValue(10);
$serialized = serialize($sheet1);
$newSheet = unserialize($serialized);
self::assertInstanceOf(Worksheet::class, $newSheet);
self::assertSame(10, $newSheet->getCell('A1')->getValue());
self::assertNotSame($newSheet, $sheet1);
}
}