mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-25 11:18:22 +00:00
c0809b0c6c
* Fix Spreadsheet Copy, Disable Clone, Improve Coverage This PR was supposed to be merely to increase coverage in Spreadsheet. However, in doing so, I discovered that neither clone nor copy worked correctly. Neither had been covered in the test suite. Copy not only did not work, it broke the source spreadsheet as well. I tried to debug and got nowhere; I even tried using myclabs/deep-copy which is already in use in the test suite, but it failed as well. However, write and reload ought to work just fine for copy. It can't be used for clone; however, since copy does what clone ought to do, there's no reason why clone needs to be used, so __clone is changed to throw an exception if attempted. One other source change was needed, an obvious bug where an if condition uses 'or' when it should use 'and'. Also, one docblock declaration needed a change. Aside from that, the rest of this PR is test cases, and overall coverage passes 89% for the first time. * Clone is Okay After All But copy wasn't, changing it to just return clone. Perhaps save and reload will be needed instead at some point, but not yet. * An Error I Cannot Reproduce PHP8.1 unit test says error because GdImage can't be serialized. I can't reproduce this error on any of my test systems. I have no idea why GdImage is even involved. Using try/catch to see if it helps. * Weird Failures in Github I thought restoring clone was a good idea. That left me in a state where, after one change, copy/clone no longer worked on Github (unable to reproduce on any of my test systems). After a second change, copy worked but clone didn't, again unable to reproduce. So, reverting to original version - copy does save and reload, clone throws exception.
109 lines
3.9 KiB
PHP
109 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Style;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Font;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class FontTest extends TestCase
|
|
{
|
|
public function testSuperSubScript(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$cell = $sheet->getCell('A1');
|
|
$cell->setValue('Cell A1');
|
|
$font = $cell->getStyle()->getFont();
|
|
$font->setSuperscript(true);
|
|
$font->setSubscript(true);
|
|
self::assertFalse($font->getSuperscript(), 'Earlier set true loses');
|
|
self::assertTrue($font->getSubscript(), 'Last set true wins');
|
|
$font->setSubscript(true);
|
|
$font->setSuperscript(true);
|
|
self::assertTrue($font->getSuperscript(), 'Last set true wins');
|
|
self::assertFalse($font->getSubscript(), 'Earlier set true loses');
|
|
$font->setSuperscript(false);
|
|
$font->setSubscript(false);
|
|
self::assertFalse($font->getSuperscript(), 'False remains unchanged');
|
|
self::assertFalse($font->getSubscript(), 'False remains unchanged');
|
|
$font->setSubscript(false);
|
|
$font->setSuperscript(false);
|
|
self::assertFalse($font->getSuperscript(), 'False remains unchanged');
|
|
self::assertFalse($font->getSubscript(), 'False remains unchanged');
|
|
$font->setSubscript(true);
|
|
$font->setSuperscript(false);
|
|
self::assertFalse($font->getSuperscript(), 'False remains unchanged');
|
|
self::assertTrue($font->getSubscript(), 'True remains unchanged');
|
|
$font->setSubscript(false);
|
|
$font->setSuperscript(true);
|
|
self::assertTrue($font->getSuperscript());
|
|
self::assertFalse($font->getSubscript(), 'False remains unchanged');
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public function testSize(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$cell = $sheet->getCell('A1');
|
|
$cell->setValue('Cell A1');
|
|
$font = $cell->getStyle()->getFont();
|
|
|
|
self::assertEquals(11, $font->getSize(), 'The default is 11');
|
|
|
|
$font->setSize(12);
|
|
self::assertEquals(12, $font->getSize(), 'Accepted new font size');
|
|
|
|
$invalidFontSizeValues = [
|
|
'',
|
|
false,
|
|
true,
|
|
'non_numeric_string',
|
|
'-1.0',
|
|
-1.0,
|
|
0,
|
|
[],
|
|
(object) [],
|
|
null,
|
|
];
|
|
foreach ($invalidFontSizeValues as $invalidFontSizeValue) {
|
|
$font->setSize(12);
|
|
$font->setSize($invalidFontSizeValue);
|
|
self::assertEquals(10, $font->getSize(), 'Set to 10 after trying to set an invalid value.');
|
|
}
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public function testName(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$cell = $sheet->getCell('A1');
|
|
$cell->setValue('Cell A1');
|
|
$font = $cell->getStyle()->getFont();
|
|
self::assertEquals('Calibri', $font->getName(), 'The default is Calibri');
|
|
$font->setName('whatever');
|
|
self::assertEquals('whatever', $font->getName(), 'The default is Calibri');
|
|
$font->setName('');
|
|
self::assertEquals('Calibri', $font->getName(), 'Null string changed to default');
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public function testUnderlineHash(): void
|
|
{
|
|
$font1 = new Font();
|
|
$font2 = new Font();
|
|
$font2aHash = $font2->getHashCode();
|
|
self::assertSame($font1->getHashCode(), $font2aHash);
|
|
$font2->setUnderlineColor(
|
|
[
|
|
'type' => 'srgbClr',
|
|
'value' => 'FF0000',
|
|
]
|
|
);
|
|
$font2bHash = $font2->getHashCode();
|
|
self::assertNotEquals($font1->getHashCode(), $font2bHash);
|
|
}
|
|
}
|