Explicitly prevent serialization of the Spreadsheet object

This commit is contained in:
MarkBaker
2022-11-22 00:12:09 +01:00
parent 8955859925
commit a7259de437
2 changed files with 46 additions and 9 deletions
+20 -1
View File
@@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheet;
use JsonSerializable;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Shared\File;
@@ -11,7 +12,7 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Iterator;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
class Spreadsheet
class Spreadsheet implements JsonSerializable
{
// Allowable values for workbook window visilbity
const VISIBILITY_VISIBLE = 'visible';
@@ -1637,4 +1638,22 @@ class Spreadsheet
{
return new Style();
}
/**
* @throws Exception
*
* @return mixed
*/
public function __serialize()
{
throw new Exception('Spreadsheet objects cannot be serialized');
}
/**
* @throws Exception
*/
public function jsonSerialize(): mixed
{
throw new Exception('Spreadsheet objects cannot be json encoded');
}
}
+26 -8
View File
@@ -2,7 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests;
use PhpOffice\PhpSpreadsheet\Exception as ssException;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
@@ -67,7 +67,7 @@ class SpreadsheetTest extends TestCase
public function testAddSheetDuplicateTitle(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$this->expectException(Exception::class);
$sheet = new Worksheet();
$sheet->setTitle('someSheet2');
$spreadsheet->addSheet($sheet);
@@ -98,7 +98,7 @@ class SpreadsheetTest extends TestCase
public function testRemoveSheetIndexTooHigh(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$this->expectException(Exception::class);
$spreadsheet->removeSheetByIndex(4);
}
@@ -123,14 +123,14 @@ class SpreadsheetTest extends TestCase
public function testGetSheetIndexTooHigh(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$this->expectException(Exception::class);
$spreadsheet->getSheet(4);
}
public function testGetIndexNonExistent(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$this->expectException(Exception::class);
$sheet = new Worksheet();
$sheet->setTitle('someSheet4');
$spreadsheet->getIndex($sheet);
@@ -175,14 +175,14 @@ class SpreadsheetTest extends TestCase
public function testSetActiveSheetIndexTooHigh(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$this->expectException(Exception::class);
$spreadsheet->setActiveSheetIndex(4);
}
public function testSetActiveSheetNoSuchName(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(ssException::class);
$this->expectException(Exception::class);
$spreadsheet->setActiveSheetIndexByName('unknown');
}
@@ -210,7 +210,7 @@ class SpreadsheetTest extends TestCase
public function testAddExternalDuplicateName(): void
{
$this->expectException(ssException::class);
$this->expectException(Exception::class);
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->createSheet()->setTitle('someSheet1');
$sheet->getCell('A1')->setValue(1);
@@ -275,4 +275,22 @@ class SpreadsheetTest extends TestCase
self::assertEquals($countXfs + $index, $sheet3->getCell('A2')->getXfIndex());
self::assertEquals($countXfs + $index, $sheet3->getRowDimension(2)->getXfIndex());
}
public function testNotSerializable(): void
{
$this->spreadsheet = $spreadsheet = new Spreadsheet();
$this->expectException(Exception::class);
$this->expectExceptionMessage('Spreadsheet objects cannot be serialized');
serialize($this->spreadsheet);
}
public function testNotJsonEncodable(): void
{
$this->spreadsheet = $spreadsheet = new Spreadsheet();
$this->expectException(Exception::class);
$this->expectExceptionMessage('Spreadsheet objects cannot be json encoded');
json_encode($this->spreadsheet);
}
}