Improve Coverage in src/PhpSpreadsheet
There are no changes to code. Additional tests are added, so that the following 6 items now have 100% test coverage: - Comment - DefinedName - DocumentGenerator - IOFactory - NamedFormula - NamedRange
This commit is contained in:
parent
cca43f0b5b
commit
1a0aab1a4f
Binary file not shown.
|
@ -3,6 +3,7 @@
|
||||||
namespace PhpOffice\PhpSpreadsheetTests;
|
namespace PhpOffice\PhpSpreadsheetTests;
|
||||||
|
|
||||||
use PhpOffice\PhpSpreadsheet\DefinedName;
|
use PhpOffice\PhpSpreadsheet\DefinedName;
|
||||||
|
use PhpOffice\PhpSpreadsheet\NamedFormula;
|
||||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
@ -164,4 +165,24 @@ class DefinedNameFormulaTest extends TestCase
|
||||||
'utf-8 named ranges in a formula' => ['Здравствуй+мир', true],
|
'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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace PhpOffice\PhpSpreadsheetTests;
|
namespace PhpOffice\PhpSpreadsheetTests;
|
||||||
|
|
||||||
use PhpOffice\PhpSpreadsheet\DefinedName;
|
use PhpOffice\PhpSpreadsheet\DefinedName;
|
||||||
|
use PhpOffice\PhpSpreadsheet\NamedRange;
|
||||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
@ -121,4 +122,85 @@ class DefinedNameTest extends TestCase
|
||||||
$this->spreadsheet->getDefinedName('foo')->getValue()
|
$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);
|
||||||
|
if ($namedRange instanceof 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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||||
use PhpOffice\PhpSpreadsheet\Calculation\Logical;
|
use PhpOffice\PhpSpreadsheet\Calculation\Logical;
|
||||||
use PhpOffice\PhpSpreadsheet\DocumentGenerator;
|
use PhpOffice\PhpSpreadsheet\DocumentGenerator;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use UnexpectedValueException;
|
||||||
|
|
||||||
class DocumentGeneratorTest extends TestCase
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace PhpOffice\PhpSpreadsheetTests\Functional;
|
namespace PhpOffice\PhpSpreadsheetTests\Functional;
|
||||||
|
|
||||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
||||||
|
|
||||||
class CommentsTest extends AbstractFunctional
|
class CommentsTest extends AbstractFunctional
|
||||||
{
|
{
|
||||||
|
@ -35,10 +36,22 @@ class CommentsTest extends AbstractFunctional
|
||||||
|
|
||||||
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
|
||||||
|
|
||||||
$commentsLoaded = $reloadedSpreadsheet->getSheet(0)->getComments();
|
$sheet = $reloadedSpreadsheet->getSheet(0);
|
||||||
|
$commentsLoaded = $sheet->getComments();
|
||||||
self::assertCount(1, $commentsLoaded);
|
self::assertCount(1, $commentsLoaded);
|
||||||
|
|
||||||
$commentCoordinate = key($commentsLoaded);
|
$commentCoordinate = key($commentsLoaded);
|
||||||
self::assertSame('E10', $commentCoordinate);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -161,4 +161,45 @@ class IOFactoryTest extends TestCase
|
||||||
|
|
||||||
IOFactory::registerReader('foo', 'bar');
|
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__;
|
||||||
|
$reader = IOFactory::createReaderForFile($filename);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue