mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-31 04:28:51 +00:00
Increase pivot table test coverage
Cover the previously-unexercised code paths flagged by Coveralls: - writer: month and year date grouping, and a fractional numeric interval (the non-integer num() path); - reader: reading an explicit dataField subtotal attribute back from a saved file; - worksheet: the getPivotTables() alias, addPivotTable(), case-insensitive getPivotTableByName(), the no-match lookup, and removePivotTableCollection().
This commit is contained in:
@@ -141,6 +141,70 @@ class PivotTableBuilderTest extends TestCase
|
||||
self::assertStringContainsString('<s v="Qtr4"/>', $cacheDefinition);
|
||||
}
|
||||
|
||||
public function testDateMonthGroupingIsEmitted(): void
|
||||
{
|
||||
$spreadsheet = $this->groupingSpreadsheet();
|
||||
$builder = new PivotTableBuilder($spreadsheet->getSheetByNameOrThrow('Data'), 'A1:D5');
|
||||
$builder
|
||||
->groupFieldByDate('OrderDate', PivotFieldGroup::GROUP_BY_MONTHS)
|
||||
->addRowField('OrderDate')
|
||||
->addDataField('Amount', PivotField::SUBTOTAL_SUM)
|
||||
->build($spreadsheet->getSheetByNameOrThrow('Pivot'), 'A3', 'ByMonth');
|
||||
|
||||
$outputFile = $this->save($spreadsheet);
|
||||
|
||||
$zip = new ZipArchive();
|
||||
self::assertTrue($zip->open($outputFile) === true);
|
||||
$cacheDefinition = (string) $zip->getFromName('xl/pivotCache/pivotCacheDefinition1.xml');
|
||||
$zip->close();
|
||||
|
||||
self::assertStringContainsString('<rangePr groupBy="months"/>', $cacheDefinition);
|
||||
self::assertStringContainsString('<s v="Jan"/>', $cacheDefinition);
|
||||
self::assertStringContainsString('<s v="Dec"/>', $cacheDefinition);
|
||||
}
|
||||
|
||||
public function testDateYearGroupingIsEmitted(): void
|
||||
{
|
||||
$spreadsheet = $this->groupingSpreadsheet();
|
||||
$builder = new PivotTableBuilder($spreadsheet->getSheetByNameOrThrow('Data'), 'A1:D5');
|
||||
$builder
|
||||
->groupFieldByDate('OrderDate', PivotFieldGroup::GROUP_BY_YEARS)
|
||||
->addRowField('OrderDate')
|
||||
->addDataField('Amount', PivotField::SUBTOTAL_SUM)
|
||||
->build($spreadsheet->getSheetByNameOrThrow('Pivot'), 'A3', 'ByYear');
|
||||
|
||||
$outputFile = $this->save($spreadsheet);
|
||||
|
||||
$zip = new ZipArchive();
|
||||
self::assertTrue($zip->open($outputFile) === true);
|
||||
$cacheDefinition = (string) $zip->getFromName('xl/pivotCache/pivotCacheDefinition1.xml');
|
||||
$zip->close();
|
||||
|
||||
self::assertStringContainsString('<rangePr groupBy="years"/>', $cacheDefinition);
|
||||
}
|
||||
|
||||
public function testNumericGroupingWithFractionalIntervalIsEmitted(): void
|
||||
{
|
||||
$spreadsheet = $this->groupingSpreadsheet();
|
||||
$builder = new PivotTableBuilder($spreadsheet->getSheetByNameOrThrow('Data'), 'A1:D5');
|
||||
$builder
|
||||
->groupFieldByNumericRange('Amount', 2.5, 0.0, 5.0)
|
||||
->addRowField('Amount')
|
||||
->addDataField('Amount', PivotField::SUBTOTAL_SUM)
|
||||
->build($spreadsheet->getSheetByNameOrThrow('Pivot'), 'A3', 'FractionGroups');
|
||||
|
||||
$outputFile = $this->save($spreadsheet);
|
||||
|
||||
$zip = new ZipArchive();
|
||||
self::assertTrue($zip->open($outputFile) === true);
|
||||
$cacheDefinition = (string) $zip->getFromName('xl/pivotCache/pivotCacheDefinition1.xml');
|
||||
$zip->close();
|
||||
|
||||
// Fractional interval must keep its decimal (num() non-integer path).
|
||||
self::assertStringContainsString('groupInterval="2.5"', $cacheDefinition);
|
||||
self::assertStringContainsString('<s v="0-2.5"/>', $cacheDefinition);
|
||||
}
|
||||
|
||||
public function testGroupingRejectsUnknownField(): void
|
||||
{
|
||||
$spreadsheet = $this->groupingSpreadsheet();
|
||||
@@ -273,6 +337,15 @@ class PivotTableBuilderTest extends TestCase
|
||||
|
||||
self::assertStringContainsString('subtotal="average"', $definition);
|
||||
self::assertStringContainsString('name="Average of Amount"', $definition);
|
||||
|
||||
// Reading the file back must recover the explicit subtotal attribute.
|
||||
$reloaded = (new XlsxReader())->load($outputFile);
|
||||
$pivotTable = $reloaded->getSheetByNameOrThrow('Pivot')->getPivotTableByName('AvgPivot');
|
||||
self::assertNotNull($pivotTable);
|
||||
$dataFields = $pivotTable->getDataFields();
|
||||
self::assertCount(1, $dataFields);
|
||||
self::assertSame(PivotField::SUBTOTAL_AVERAGE, $dataFields[0]->getSubtotal());
|
||||
$reloaded->disconnectWorksheets();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\PivotTable\PivotCacheDefinition;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\PivotTable\PivotField;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\PivotTable\PivotFieldGroup;
|
||||
@@ -82,6 +83,34 @@ class PivotTableModelTest extends TestCase
|
||||
self::assertSame('MyPivot', $pivotTable->__toString());
|
||||
}
|
||||
|
||||
public function testWorksheetPivotTableCollection(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
self::assertCount(0, $worksheet->getPivotTableCollection());
|
||||
self::assertSame([], $worksheet->getPivotTableNames());
|
||||
self::assertNull($worksheet->getPivotTableByName('Missing'));
|
||||
|
||||
$pivotTable = new PivotTable('First');
|
||||
self::assertSame($worksheet, $worksheet->addPivotTable($pivotTable));
|
||||
|
||||
// addPivotTable attaches the worksheet to the pivot table.
|
||||
self::assertSame($worksheet, $pivotTable->getWorksheet());
|
||||
|
||||
// getPivotTables() is an alias of getPivotTableCollection().
|
||||
self::assertCount(1, $worksheet->getPivotTables());
|
||||
self::assertSame(['First'], $worksheet->getPivotTableNames());
|
||||
self::assertSame($pivotTable, $worksheet->getPivotTableByName('First'));
|
||||
// Name lookup is case-insensitive.
|
||||
self::assertSame($pivotTable, $worksheet->getPivotTableByName('FIRST'));
|
||||
|
||||
self::assertSame($worksheet, $worksheet->removePivotTableCollection());
|
||||
self::assertCount(0, $worksheet->getPivotTableCollection());
|
||||
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function testPivotFieldSetters(): void
|
||||
{
|
||||
$field = new PivotField(2, 'Amount');
|
||||
|
||||
Reference in New Issue
Block a user