PhpSpreadsheet/tests/PhpSpreadsheetTests/Style/BorderTest.php

248 lines
15 KiB
PHP
Raw Normal View History

<?php
namespace PhpOffice\PhpSpreadsheetTests\Style;
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
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
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
use PhpOffice\PhpSpreadsheet\Style\Borders;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PHPUnit\Framework\TestCase;
class BorderTest extends TestCase
{
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
public function testAllBorders(): void
{
$spreadsheet = new Spreadsheet();
$borders = $spreadsheet->getActiveSheet()->getStyle('A1')->getBorders();
$allBorders = $borders->getAllBorders();
$bottom = $borders->getBottom();
$actual = $bottom->getBorderStyle();
self::assertSame(Border::BORDER_NONE, $actual, 'should default to none');
$allBorders->setBorderStyle(Border::BORDER_THIN);
$actual = $bottom->getBorderStyle();
self::assertSame(Border::BORDER_THIN, $actual, 'should have been set via allBorders');
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
self::assertSame(Border::BORDER_THIN, $borders->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $borders->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $borders->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $borders->getDiagonal()->getBorderStyle());
}
public function testAllBordersArray(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getStyle('A1')->getBorders()->applyFromArray(['allBorders' => ['borderStyle' => Border::BORDER_THIN]]);
$borders = $sheet->getCell('A1')->getStyle()->getBorders();
self::assertSame(Border::BORDER_THIN, $borders->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $borders->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $borders->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $borders->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $borders->getDiagonal()->getBorderStyle());
}
public function testAllBordersArrayNotSupervisor(): void
{
$borders = new Borders();
$borders->applyFromArray(['allBorders' => ['borderStyle' => Border::BORDER_THIN]]);
self::assertSame(Border::BORDER_THIN, $borders->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $borders->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $borders->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $borders->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $borders->getDiagonal()->getBorderStyle());
}
public function testOutline(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$borders = $sheet->getStyle('A1:B2')->getBorders();
$outline = $borders->getOutline();
$outline->setBorderStyle(Border::BORDER_THIN);
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle());
}
public function testInside(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$borders = $sheet->getStyle('A1:B2')->getBorders();
$inside = $borders->getInside();
$inside->setBorderStyle(Border::BORDER_THIN);
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle());
}
public function testHorizontal(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$borders = $sheet->getStyle('A1:B2')->getBorders();
$horizontal = $borders->getHorizontal();
$horizontal->setBorderStyle(Border::BORDER_THIN);
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle());
}
public function testVertical(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$borders = $sheet->getStyle('A1:B2')->getBorders();
$vertical = $borders->getVertical();
$vertical->setBorderStyle(Border::BORDER_THIN);
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle());
}
public function testNoSupervisorAllBorders(): void
{
$this->expectException(PhpSpreadsheetException::class);
$borders = new Borders();
$borders->getAllBorders();
}
public function testNoSupervisorOutline(): void
{
$this->expectException(PhpSpreadsheetException::class);
$borders = new Borders();
$borders->getOutline();
}
public function testNoSupervisorInside(): void
{
$this->expectException(PhpSpreadsheetException::class);
$borders = new Borders();
$borders->getInside();
}
public function testNoSupervisorVertical(): void
{
$this->expectException(PhpSpreadsheetException::class);
$borders = new Borders();
$borders->getVertical();
}
public function testNoSupervisorHorizontal(): void
{
$this->expectException(PhpSpreadsheetException::class);
$borders = new Borders();
$borders->getHorizontal();
}
public function testGetSharedComponentPseudo(): void
{
$this->expectException(PhpSpreadsheetException::class);
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getStyle('A1')->getBorders()->getHorizontal()->setBorderStyle(Border::BORDER_MEDIUM);
$sheet->getStyle('A1')->getBorders()->getHorizontal()->getSharedComponent();
}
public function testBorderStyle(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getStyle('A1')->getBorders()->getTop()->setBorderStyle(false);
$sheet->getStyle('A2')->getBorders()->getTop()->setBorderStyle(true);
self::assertEquals(Border::BORDER_NONE, $sheet->getStyle('A1')->getBorders()->getTop()->getBorderStyle());
self::assertEquals(Border::BORDER_MEDIUM, $sheet->getStyle('A2')->getBorders()->getTop()->getBorderStyle());
$sheet->getStyle('A3')->getBorders()->getTop()->applyFromArray(['borderStyle' => Border::BORDER_MEDIUM]);
self::assertEquals(Border::BORDER_MEDIUM, $sheet->getStyle('A3')->getBorders()->getTop()->getBorderStyle());
$border = new Border();
$border->setBorderStyle(Border::BORDER_THIN)->setColor(new Color('FFFF0000'));
self::assertEquals('FFFF0000', $border->getColor()->getARGB());
self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle());
}
public function testDiagonalDirection(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getStyle('A1')->getBorders()->getDiagonal()->setBorderStyle(Border::BORDER_MEDIUM);
$sheet->getStyle('A1')->getBorders()->setDiagonalDirection(Borders::DIAGONAL_BOTH);
$borders = $sheet->getStyle('A1')->getBorders();
self::assertSame(Border::BORDER_MEDIUM, $borders->getDiagonal()->getBorderStyle());
self::assertSame(Borders::DIAGONAL_BOTH, $borders->getDiagonalDirection());
}
}