Merge pull request #1734 from oleibman/morecoverage

Improve Coverage in src/PhpSpreadsheet
This commit is contained in:
Adrien Crivelli 2020-12-02 09:46:22 +09:00 committed by GitHub
commit 40abd18fe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 166 additions and 1 deletions

Binary file not shown.

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheetTests;
use PhpOffice\PhpSpreadsheet\DefinedName;
use PhpOffice\PhpSpreadsheet\NamedFormula;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
@ -164,4 +165,24 @@ class DefinedNameFormulaTest extends TestCase
'utf-8 named ranges in a formula' => ['Здравствуй+мир', true],
];
}
public function testEmptyNamedFormula(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$spreadSheet = new Spreadsheet();
$workSheet1 = $spreadSheet->getActiveSheet();
new NamedFormula('namedformula', $workSheet1);
}
public function testChangeFormula(): void
{
$spreadSheet = new Spreadsheet();
$workSheet1 = $spreadSheet->getActiveSheet();
$namedFormula = new NamedFormula('namedformula', $workSheet1, '=1');
self::assertEquals('=1', $namedFormula->getFormula());
$namedFormula->setFormula('=2');
self::assertEquals('=2', $namedFormula->getFormula());
$namedFormula->setFormula('');
self::assertEquals('=2', $namedFormula->getFormula());
}
}

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheetTests;
use PhpOffice\PhpSpreadsheet\DefinedName;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
@ -121,4 +122,83 @@ class DefinedNameTest extends TestCase
$this->spreadsheet->getDefinedName('foo')->getValue()
);
}
public function testDefinedNameNoWorksheetNoScope(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
new NamedRange('xyz');
}
public function testSetAndGetRange(): void
{
$this->spreadsheet->addDefinedName(
DefinedName::createInstance('xyz', $this->spreadsheet->getActiveSheet(), 'A1')
);
$namedRange = $this->spreadsheet->getDefinedName('XYZ');
self::assertInstanceOf(NamedRange::class, $namedRange);
self::assertEquals('A1', $namedRange->getRange());
self::assertEquals('A1', $namedRange->getValue());
$namedRange->setRange('A2');
self::assertEquals('A2', $namedRange->getValue());
}
public function testChangeWorksheet(): void
{
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
$sheet1->getCell('A1')->setValue(1);
$sheet2->getCell('A1')->setValue(2);
$namedRange = new NamedRange('xyz', $sheet2, '$A$1');
$namedRange->setWorksheet($sheet1);
$this->spreadsheet->addNamedRange($namedRange);
$sheet1->getCell('B2')->setValue('=XYZ');
self::assertEquals(1, $sheet1->getCell('B2')->getCalculatedValue());
$sheet2->getCell('B2')->setValue('=XYZ');
self::assertEquals(1, $sheet2->getCell('B2')->getCalculatedValue());
}
public function testLocalOnly(): void
{
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
$sheet1->getCell('A1')->setValue(1);
$sheet2->getCell('A1')->setValue(2);
$namedRange = new NamedRange('abc', $sheet2, '$A$1');
$namedRange->setWorksheet($sheet1)->setLocalOnly(true);
$this->spreadsheet->addNamedRange($namedRange);
$sheet1->getCell('C2')->setValue('=ABC');
self::assertEquals(1, $sheet1->getCell('C2')->getCalculatedValue());
$sheet2->getCell('C2')->setValue('=ABC');
self::assertEquals('#NAME?', $sheet2->getCell('C2')->getCalculatedValue());
}
public function testScope(): void
{
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
$sheet1->getCell('A1')->setValue(1);
$sheet2->getCell('A1')->setValue(2);
$namedRange = new NamedRange('abc', $sheet2, '$A$1');
$namedRange->setScope($sheet1);
$this->spreadsheet->addNamedRange($namedRange);
$sheet1->getCell('C2')->setValue('=ABC');
self::assertEquals(2, $sheet1->getCell('C2')->getCalculatedValue());
$sheet2->getCell('C2')->setValue('=ABC');
self::assertEquals('#NAME?', $sheet2->getCell('C2')->getCalculatedValue());
}
public function testClone(): void
{
$sheet1 = $this->spreadsheet->getSheetByName('Sheet #1');
$sheet2 = $this->spreadsheet->getSheetByName('Sheet #2');
$sheet1->getCell('A1')->setValue(1);
$sheet2->getCell('A1')->setValue(2);
$namedRange = new NamedRange('abc', $sheet2, '$A$1');
$namedRangeClone = clone $namedRange;
$ss1 = $namedRange->getWorksheet();
$ss2 = $namedRangeClone->getWorksheet();
self::assertNotSame($ss1, $ss2);
self::assertEquals($ss1->getTitle(), $ss2->getTitle());
}
}

View File

@ -7,6 +7,7 @@ use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Logical;
use PhpOffice\PhpSpreadsheet\DocumentGenerator;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;
class DocumentGeneratorTest extends TestCase
{
@ -137,4 +138,13 @@ EXPECTED
],
];
}
public function testGenerateFunctionBadArray(): void
{
$this->expectException(UnexpectedValueException::class);
$phpSpreadsheetFunctions = [
'ABS' => ['category' => Cat::CATEGORY_MATH_AND_TRIG, 'functionCall' => 1],
];
DocumentGenerator::generateFunctionListByName($phpSpreadsheetFunctions);
}
}

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
class CommentsTest extends AbstractFunctional
{
@ -35,10 +36,22 @@ class CommentsTest extends AbstractFunctional
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
$commentsLoaded = $reloadedSpreadsheet->getSheet(0)->getComments();
$sheet = $reloadedSpreadsheet->getSheet(0);
$commentsLoaded = $sheet->getComments();
self::assertCount(1, $commentsLoaded);
$commentCoordinate = key($commentsLoaded);
self::assertSame('E10', $commentCoordinate);
$comment = $commentsLoaded[$commentCoordinate];
self::assertEquals('Comment to test', (string) $comment);
$commentClone = clone $comment;
self::assertEquals($comment, $commentClone);
self::assertNotSame($comment, $commentClone);
if ($format === 'Xlsx') {
self::assertEquals('feb0c24b880a8130262dadf801f85e94', $comment->getHashCode());
self::assertEquals(Alignment::HORIZONTAL_GENERAL, $comment->getAlignment());
$comment->setAlignment(Alignment::HORIZONTAL_RIGHT);
self::assertEquals(Alignment::HORIZONTAL_RIGHT, $comment->getAlignment());
}
}
}

View File

@ -161,4 +161,45 @@ class IOFactoryTest extends TestCase
IOFactory::registerReader('foo', 'bar');
}
public function testCreateInvalidWriter(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class);
$spreadsheet = new Spreadsheet();
IOFactory::createWriter($spreadsheet, 'bad');
}
public function testCreateInvalidReader(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
IOFactory::createReader('bad');
}
public function testCreateReaderUnknownExtension(): void
{
$filename = 'samples/Reader/sampleData/example1.tsv';
$reader = IOFactory::createReaderForFile($filename);
self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Csv', get_class($reader));
}
public function testCreateReaderCsvExtension(): void
{
$filename = 'samples/Reader/sampleData/example1.csv';
$reader = IOFactory::createReaderForFile($filename);
self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Csv', get_class($reader));
}
public function testCreateReaderNoExtension(): void
{
$filename = 'samples/Reader/sampleData/example1xls';
$reader = IOFactory::createReaderForFile($filename);
self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Xls', get_class($reader));
}
public function testCreateReaderNotSpreadsheet(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
$filename = __FILE__;
IOFactory::createReaderForFile($filename);
}
}