#729 - fix shrink to fit + add test

shrinkToFit was not handled in StyleMerger so it was overwritten by the default cell style
StyleManager didn't add the property to the xml if shrinkToFit was used without alignment or text wrap.
Unit test
This commit is contained in:
István Ignácz 2020-08-14 16:31:07 +02:00
parent f702127d3a
commit 817e4f1f01
5 changed files with 36 additions and 4 deletions

View File

@ -66,8 +66,10 @@ class Style
/** @var bool Whether the wrap text property was set */
private $hasSetWrapText = false;
private $shrinkToFit = false;
/** @var bool Whether the cell should shrink to fit to content */
private $shouldShrinkToFit = false;
/** @var bool Whether the shouldShrinkToFit text property was set */
private $hasSetShrinkToFit = false;
/** @var Border */
private $border;
@ -474,7 +476,7 @@ class Style
*/
public function setShouldShrinkToFit($shrinkToFit = true)
{
$this->shrinkToFit = $shrinkToFit;
$this->hasSetShrinkToFit = true;
$this->shouldShrinkToFit = $shrinkToFit;
return $this;
@ -487,4 +489,12 @@ class Style
{
return $this->shouldShrinkToFit;
}
/**
* @return bool
*/
public function hasSetShrinkToFit()
{
return $this->hasSetShrinkToFit;
}
}

View File

@ -186,7 +186,8 @@ class StyleBuilder
/**
* Set should shrink to fit
* @param bool $shrinkToFit
* @return void
* @return StyleBuilder
* @api
*/
public function setShouldShrinkToFit($shrinkToFit = true)
{

View File

@ -85,6 +85,9 @@ class StyleMerger
if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) {
$styleToUpdate->setShouldWrapText();
}
if (!$style->hasSetShrinkToFit() && $baseStyle->shouldShrinkToFit()) {
$styleToUpdate->setShouldShrinkToFit();
}
if (!$style->hasSetCellAlignment() && $baseStyle->shouldApplyCellAlignment()) {
$styleToUpdate->setCellAlignment($baseStyle->getCellAlignment());
}

View File

@ -249,7 +249,7 @@ EOD;
$content .= \sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0);
if ($style->shouldApplyCellAlignment() || $style->shouldWrapText()) {
if ($style->shouldApplyCellAlignment() || $style->shouldWrapText() || $style->shouldShrinkToFit()) {
$content .= ' applyAlignment="1">';
$content .= '<alignment';
if ($style->shouldApplyCellAlignment()) {

View File

@ -306,6 +306,24 @@ class WriterWithStyleTest extends TestCase
$this->assertFirstChildHasAttributeEquals(CellAlignment::RIGHT, $xfElement, 'alignment', 'horizontal');
}
/**
* @return void
*/
public function testAddRowShouldApplyShrinkToFit()
{
$fileName = 'test_add_row_should_apply_shrink_to_fit.xlsx';
$shrinkToFitStyle = (new StyleBuilder())->setShouldShrinkToFit()->build();
$dataRows = $this->createStyledRowsFromValues([['xlsx--11']], $shrinkToFitStyle);
$this->writeToXLSXFile($dataRows, $fileName);
$cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1);
$this->assertEquals(1, $xfElement->getAttribute('applyAlignment'));
$this->assertFirstChildHasAttributeEquals("true", $xfElement, 'alignment', 'shrinkToFit');
}
/**
* @return void
*/