PhpSpreadsheet/tests/PhpSpreadsheetTests/Style/ExportArrayTest.php

162 lines
9.0 KiB
PHP
Raw Permalink Normal View History

Add exportArray Method for Styles (#1580) Issue #580 has gone stale since I started work on this. Nevertheless, this implements an exportArray function as an exact counterpart of applyFromArry. I chose the name exportArray to avoid confusion with the existing method getStyleArray, which does something completely different. This change also increases coverage for all the Style classes to 100%, with the exception of Style.php itself. There were several (unchanged) places in Style.php where I did not have sufficient understanding of what was supposed to be happening, so could not create tests. All properties used by applyFromArray are exported by this method. Note that conditional styles are not covered; this is consistent with the fact that they are not covered by applyFromArray. The method is implemented as a final public function in Style/Supervisor, which calls abstract protected function exportArray1, which is implemented in each of the subclasses, and which calls final protected function exportArray2 in Style/Supervisor. So exportArray is usable for any of the subclasses as well. The new method is added to the documentation. The existing documentation for applyFromArray was alphabetized to make it easier to follow. One property (Style quotePrefix) was added to the documentation. Some Borders pseudo-properties (vertical, horizontal, and outline) were documented as usable by applyFromArray, but aren't actually supported - they were removed. The documentation of the properties seemed to use setProperty and getProperty fairly randomly - it now uses setProperty exclusively. New constants were added for the textRotation "angles" used to create a "stacked" cell. I felt that changing the readers and writers to use these constants was beyond the scope of this change, but it is on my to-do list.
2020-10-26 19:56:24 +00:00
<?php
namespace PhpOffice\PhpSpreadsheetTests\Style;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\Protection;
use PHPUnit\Framework\TestCase;
class ExportArrayTest extends TestCase
{
public function testStyleCopy(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$cell1 = $sheet->getCell('A1');
$cell1->setValue('Cell A1');
$cell1style = $cell1->getStyle();
$cell1style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
$cell1style->getFont()->getColor()->setARGB('FFFF0000');
$cell1style->getFont()->setBold(true);
$cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
$cell1style->getFill()->setStartColor(new Color('FF0000FF'));
$cell1style->getFill()->setEndColor(new Color('FF00FF00'));
$cell1style->getFont()->setUnderline(true);
self::assertEquals(Font::UNDERLINE_SINGLE, $cell1style->getFont()->getUnderline());
$cell1style->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
$cell1style->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
$styleArray = $cell1style->exportArray();
$cell2 = $sheet->getCell('B1');
$cell2->setValue('Cell B1');
$cell2style = $cell2->getStyle();
$cell2style->applyFromArray($styleArray);
self::AssertEquals($cell1style->getAlignment()->getHorizontal(), $cell2style->getAlignment()->getHorizontal());
self::AssertEquals($cell1style->getFont()->getColor()->getARGB(), $cell2style->getFont()->getColor()->getARGB());
self::AssertEquals($cell1style->getFont()->getBold(), $cell2style->getFont()->getBold());
self::AssertEquals($cell1style->getFont()->getUnderline(), $cell2style->getFont()->getUnderline());
self::AssertEquals($cell1style->getFill()->getFillType(), $cell2style->getFill()->getFillType());
self::AssertEquals($cell1style->getFill()->getStartColor()->getARGB(), $cell2style->getFill()->getStartColor()->getARGB());
self::AssertEquals($cell1style->getFill()->getEndColor()->getARGB(), $cell2style->getFill()->getEndColor()->getARGB());
self::AssertEquals($cell1style->getProtection()->getLocked(), $cell2style->getProtection()->getLocked());
self::AssertEquals($cell1style->getProtection()->getHidden(), $cell2style->getProtection()->getHidden());
self::AssertEquals($cell1style->getHashCode(), $cell2style->getHashCode());
self::AssertEquals($cell1style->getAlignment()->getHashCode(), $cell2style->getAlignment()->getHashCode());
self::AssertEquals($cell1style->getFont()->getHashCode(), $cell2style->getFont()->getHashCode());
self::AssertEquals($cell1style->getFill()->getHashCode(), $cell2style->getFill()->getHashCode());
self::AssertEquals($cell1style->getProtection()->getHashCode(), $cell2style->getProtection()->getHashCode());
}
public function testStyleFromArrayCopy(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$cell1 = $sheet->getCell('A1');
$cell1->setValue('Cell A1');
$cell1style = $cell1->getStyle();
$cell1style->getAlignment()->applyFromArray(['horizontal' => Alignment::HORIZONTAL_RIGHT]);
$cell1style->getFont()->getColor()->setARGB('FFFF0000');
$cell1style->getFont()->applyFromArray(['bold' => true]);
$cell1style->getFill()->applyFromArray(['fillType' => Fill::FILL_PATTERN_GRAY125]);
$cell1style->getFill()->getStartColor()->applyFromArray(['argb' => 'FF0000FF']);
$cell1style->getFill()->getEndColor()->setRGB('00FF00');
$cell1style->getFill()->setRotation(45);
$cell1style->getFont()->setUnderline(true);
self::assertEquals(Font::UNDERLINE_SINGLE, $cell1style->getFont()->getUnderline());
$cell1style->getProtection()->applyFromArray(['hidden' => Protection::PROTECTION_UNPROTECTED, 'locked' => Protection::PROTECTION_UNPROTECTED]);
$styleArray = $cell1style->exportArray();
$cell2 = $sheet->getCell('B1');
$cell2->setValue('Cell B1');
$cell2style = $cell2->getStyle();
$cell2style->applyFromArray($styleArray);
self::AssertEquals($cell1style->getAlignment()->getHorizontal(), $cell2style->getAlignment()->getHorizontal());
self::AssertEquals($cell1style->getFont()->getColor()->getARGB(), $cell2style->getFont()->getColor()->getARGB());
self::AssertEquals($cell1style->getFont()->getBold(), $cell2style->getFont()->getBold());
self::AssertEquals($cell1style->getFont()->getUnderline(), $cell2style->getFont()->getUnderline());
self::AssertEquals($cell1style->getFill()->getFillType(), $cell2style->getFill()->getFillType());
self::AssertEquals($cell1style->getFill()->getRotation(), $cell2style->getFill()->getRotation());
self::AssertEquals($cell1style->getFill()->getStartColor()->getARGB(), $cell2style->getFill()->getStartColor()->getARGB());
self::AssertEquals($cell1style->getFill()->getEndColor()->getARGB(), $cell2style->getFill()->getEndColor()->getARGB());
self::AssertEquals($cell1style->getProtection()->getLocked(), $cell2style->getProtection()->getLocked());
self::AssertEquals($cell1style->getProtection()->getHidden(), $cell2style->getProtection()->getHidden());
self::AssertEquals($cell1style->getFill()->getStartColor()->getHashCode(), $cell2style->getFill()->getStartColor()->getHashCode());
self::AssertEquals($cell1style->getFill()->getEndColor()->getHashCode(), $cell2style->getFill()->getEndColor()->getHashCode());
}
public function testNumberFormat(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$cell1 = $sheet->getCell('A1');
$cell1style = $cell1->getStyle();
$fmt2 = '$ #,##0.000';
$cell1style->getNumberFormat()->setFormatCode($fmt2);
$cell1style->getFont()->setUnderline('');
self::assertEquals(Font::UNDERLINE_NONE, $cell1style->getFont()->getUnderline());
$cell1->setValue(2345.679);
$styleArray = $cell1style->exportArray();
self::assertEquals('$ 2,345.679', $cell1->getFormattedValue());
$cell2 = $sheet->getCell('B1');
$cell2->setValue(12345.679);
$cell2style = $cell2->getStyle();
$cell2style->applyFromArray($styleArray);
self::assertEquals('$ 12,345.679', $cell2->getFormattedValue());
self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode());
}
public function testNumberFormatFromArray(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$cell1 = $sheet->getCell('A1');
$cell1style = $cell1->getStyle();
$fmt2 = '$ #,##0.000';
$cell1style->getNumberFormat()->applyFromArray(['formatCode' => $fmt2]);
$cell1style->getFont()->setUnderline('');
self::assertEquals(Font::UNDERLINE_NONE, $cell1style->getFont()->getUnderline());
$cell1style->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN);
$cell1->setValue(2345.679);
$styleArray = $cell1style->exportArray();
self::assertEquals('$ 2,345.679', $cell1->getFormattedValue());
$cell2 = $sheet->getCell('B1');
$cell2->setValue(12345.679);
$cell2style = $cell2->getStyle();
$cell2style->applyFromArray($styleArray);
self::assertEquals('$ 12,345.679', $cell2->getFormattedValue());
self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode());
self::AssertEquals($cell1style->getBorders()->getHashCode(), $cell2style->getBorders()->getHashCode());
self::AssertEquals($cell1style->getBorders()->getTop()->getHashCode(), $cell2style->getBorders()->getTop()->getHashCode());
self::AssertEquals($cell1style->getBorders()->getTop()->getBorderStyle(), $cell2style->getBorders()->getTop()->getBorderStyle());
}
public function testStackedRotation(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$cell1 = $sheet->getCell('A1');
$cell1->setValue('Cell A1');
$cell1style = $cell1->getStyle();
$cell1style->getAlignment()->setTextRotation(Alignment::TEXTROTATION_STACK_EXCEL);
self::assertEquals(Alignment::TEXTROTATION_STACK_PHPSPREADSHEET, $cell1style->getAlignment()->getTextRotation());
$styleArray = $cell1style->exportArray();
$cell2 = $sheet->getCell('B1');
$cell2->setValue('Cell B1');
$cell2style = $cell2->getStyle();
$cell2style->applyFromArray($styleArray);
self::AssertEquals($cell1style->getAlignment()->getTextRotation(), $cell2style->getAlignment()->getTextRotation());
}
}