spout/tests/Spout/Writer/Common/Creator/StyleBuilderTest.php
Jon Nott 887d6ef033 Add setCellVerticalAlignment() & allow setShouldWrapText() to be explicitly set as false
- add Common\Entity\Style\CellVerticalAlignment
- duplicate get/set/hasSet/shouldApply methods for CellAlignment in Common\Entity\Style\Style for CellVerticalAlignment instead, plus corresponding properties
- add setCellVerticalAlignment method to Common\Creator\Style\StyleBuilder
- add vertical alignment to StyleMerger:: mergeCellProperties()
- adjust wrapText logic in mergeCellProperties() to fix issue https://github.com/box/spout/issues/829
- apply vertical cell styling for both XLSX and ODS, via corresponding StyleManager classes
- transform vertical alignment  ‘center’ to ‘middle’ for ODS
- fix logic around wrapText such that the choice whether to include wrapping styles depends on hasSetWrapText() being true, and then use shouldWrapText() thereafter to either set wrapping or no wrapping (for XLSX, wrapText=“1” or wrapText=“0”, for ODS, wrap-option=wrap or wrap-option=no-wrap). previously there was no way to set wrapping to be OFF, only to set it to be ON.
- add new tests to ensure shouldWrapText(false) results in the correct negated wrapText (XLSX) / wrap-option (ODS) styles
- add new tests to StyleBuilderTest for vertical alignment
- add vertical alignment to documentation.md
2022-02-12 19:37:05 +00:00

84 lines
2.6 KiB
PHP

<?php
namespace Box\Spout\Writer\Common\Creator\Style;
use Box\Spout\Common\Entity\Style\Border;
use Box\Spout\Common\Entity\Style\CellAlignment;
use Box\Spout\Common\Entity\Style\CellVerticalAlignment;
use Box\Spout\Common\Entity\Style\Color;
use Box\Spout\Common\Exception\InvalidArgumentException;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use PHPUnit\Framework\TestCase;
/**
* Class StyleManagerTest
*/
class StyleBuilderTest extends TestCase
{
/**
* @return void
*/
public function testStyleBuilderShouldApplyBorders()
{
$border = (new BorderBuilder())
->setBorderBottom()
->build();
$style = (new StyleBuilder())->setBorder($border)->build();
$this->assertTrue($style->shouldApplyBorder());
}
/**
* @return void
*/
public function testStyleBuilderShouldMergeBorders()
{
$border = (new BorderBuilder())->setBorderBottom(Color::RED, Border::WIDTH_THIN, Border::STYLE_DASHED)->build();
$baseStyle = (new StyleBuilder())->setBorder($border)->build();
$currentStyle = (new StyleBuilder())->build();
$styleMerger = new StyleMerger();
$mergedStyle = $styleMerger->merge($currentStyle, $baseStyle);
$this->assertNull($currentStyle->getBorder(), 'Current style has no border');
$this->assertInstanceOf(Border::class, $baseStyle->getBorder(), 'Base style has a border');
$this->assertInstanceOf(Border::class, $mergedStyle->getBorder(), 'Merged style has a border');
}
/**
* @return void
*/
public function testStyleBuilderShouldApplyCellAlignment()
{
$style = (new StyleBuilder())->setCellAlignment(CellAlignment::CENTER)->build();
$this->assertTrue($style->shouldApplyCellAlignment());
}
/**
* @return void
*/
public function testStyleBuilderShouldApplyCellVerticalAlignment()
{
$style = (new StyleBuilder())->setCellVerticalAlignment(CellVerticalAlignment::CENTER)->build();
$this->assertTrue($style->shouldApplyCellVerticalAlignment());
}
/**
* @return void
*/
public function testStyleBuilderShouldThrowOnInvalidCellAlignment()
{
$this->expectException(InvalidArgumentException::class);
(new StyleBuilder())->setCellAlignment('invalid_cell_alignment')->build();
}
/**
* @return void
*/
public function testStyleBuilderShouldThrowOnInvalidCellVerticalAlignment()
{
$this->expectException(InvalidArgumentException::class);
(new StyleBuilder())->setCellVerticalAlignment('invalid_cell_alignment')->build();
}
}