Refactory Writer Styles to match new code organization (#433)
Decomposed old StyleHelper into StyleManager, StyleRegistry and StyleMerger.
This commit is contained in:
1 parent
238756ab6e
commit
cc9a0b526b
34 files changed
+1005
-762
No files matched your search
@@ -1,114 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\XLSX\Helper;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Style\Border;
|
||||
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Manager\StyleManager;
|
||||
|
||||
/**
|
||||
* Class StyleHelperTest
|
||||
*
|
||||
* @package Box\Spout\Writer\XLSX\Helper
|
||||
*/
|
||||
class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var Style */
|
||||
protected $defaultStyle;
|
||||
|
||||
/** @var StyleHelper */
|
||||
private $styleHelper;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->defaultStyle = (new StyleBuilder())->build();
|
||||
$this->styleHelper = new StyleHelper($this->defaultStyle, new StyleManager());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRegisterStyleShouldUpdateId()
|
||||
{
|
||||
$style1 = (new StyleBuilder())->setFontBold()->build();
|
||||
$style2 = (new StyleBuilder())->setFontUnderline()->build();
|
||||
|
||||
$this->assertEquals(0, $this->defaultStyle->getId(), 'Default style ID should be 0');
|
||||
$this->assertNull($style1->getId());
|
||||
$this->assertNull($style2->getId());
|
||||
|
||||
$registeredStyle1 = $this->styleHelper->registerStyle($style1);
|
||||
$registeredStyle2 = $this->styleHelper->registerStyle($style2);
|
||||
|
||||
$this->assertEquals(1, $registeredStyle1->getId());
|
||||
$this->assertEquals(2, $registeredStyle2->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRegisterStyleShouldReuseAlreadyRegisteredStyles()
|
||||
{
|
||||
$style = (new StyleBuilder())->setFontBold()->build();
|
||||
|
||||
$registeredStyle1 = $this->styleHelper->registerStyle($style);
|
||||
$registeredStyle2 = $this->styleHelper->registerStyle($style);
|
||||
|
||||
$this->assertEquals(1, $registeredStyle1->getId());
|
||||
$this->assertEquals(1, $registeredStyle2->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldApplyStyleOnEmptyCell()
|
||||
{
|
||||
$styleWithFont = (new StyleBuilder())->setFontBold()->build();
|
||||
$styleWithBackground = (new StyleBuilder())->setBackgroundColor(Color::BLUE)->build();
|
||||
|
||||
$border = (new BorderBuilder())->setBorderBottom(Color::GREEN)->build();
|
||||
$styleWithBorder = (new StyleBuilder())->setBorder($border)->build();
|
||||
|
||||
$this->styleHelper->registerStyle($styleWithFont);
|
||||
$this->styleHelper->registerStyle($styleWithBackground);
|
||||
$this->styleHelper->registerStyle($styleWithBorder);
|
||||
|
||||
$this->assertFalse($this->styleHelper->shouldApplyStyleOnEmptyCell($styleWithFont->getId()));
|
||||
$this->assertTrue($this->styleHelper->shouldApplyStyleOnEmptyCell($styleWithBackground->getId()));
|
||||
$this->assertTrue($this->styleHelper->shouldApplyStyleOnEmptyCell($styleWithBorder->getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testApplyExtraStylesIfNeededShouldApplyWrapTextIfCellContainsNewLine()
|
||||
{
|
||||
$style = clone $this->defaultStyle;
|
||||
|
||||
$this->assertFalse($style->shouldWrapText());
|
||||
|
||||
$updatedStyle = $this->styleHelper->applyExtraStylesIfNeeded($style, [12, 'single line', "multi\nlines", null]);
|
||||
|
||||
$this->assertTrue($updatedStyle->shouldWrapText());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testApplyExtraStylesIfNeededShouldDoNothingIfWrapTextAlreadyApplied()
|
||||
{
|
||||
$style = (new StyleBuilder())->setShouldWrapText()->build();
|
||||
|
||||
$this->assertTrue($style->shouldWrapText());
|
||||
|
||||
$updatedStyle = $this->styleHelper->applyExtraStylesIfNeeded($style, ["multi\nlines"]);
|
||||
|
||||
$this->assertTrue($updatedStyle->shouldWrapText());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\XLSX\Manager\Style;
|
||||
|
||||
/**
|
||||
* Class StyleManagerTest
|
||||
*
|
||||
* @package Box\Spout\Writer\XLSX\Manager\Style
|
||||
*/
|
||||
class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForTestShouldApplyStyleOnEmptyCell()
|
||||
{
|
||||
return [
|
||||
// fillId, borderId, expected result
|
||||
[null, null, false],
|
||||
[0, null, false],
|
||||
[null, 0, false],
|
||||
[0, 0, false],
|
||||
[12, null, true],
|
||||
[null, 12, true],
|
||||
[12, 0, true],
|
||||
[0, 12, true],
|
||||
[12, 13, true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForTestShouldApplyStyleOnEmptyCell
|
||||
*
|
||||
* @param int|null $fillId
|
||||
* @param int|null $borderId
|
||||
* @param bool $expectedResult
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldApplyStyleOnEmptyCell($fillId, $borderId, $expectedResult)
|
||||
{
|
||||
$styleRegistryMock = $this->getMockBuilder(StyleRegistry::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(['getFillIdForStyleId', 'getBorderIdForStyleId'])
|
||||
->getMock();
|
||||
|
||||
$styleRegistryMock
|
||||
->method('getFillIdForStyleId')
|
||||
->willReturn($fillId);
|
||||
|
||||
$styleRegistryMock
|
||||
->method('getBorderIdForStyleId')
|
||||
->willReturn($borderId);
|
||||
|
||||
$styleManager = new StyleManager($styleRegistryMock);
|
||||
$shouldApply = $styleManager->shouldApplyStyleOnEmptyCell(99);
|
||||
|
||||
$this->assertEquals($expectedResult, $shouldApply);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\XLSX\Manager\Style;
|
||||
|
||||
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
|
||||
/**
|
||||
* Class StyleRegistryTest
|
||||
*
|
||||
* @package Box\Spout\Writer\XLSX\Manager\Style
|
||||
*/
|
||||
class StyleRegistryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @return StyleRegistry
|
||||
*/
|
||||
private function getStyleRegistry()
|
||||
{
|
||||
$defaultStyle = (new StyleBuilder())->build();
|
||||
return new StyleRegistry($defaultStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRegisterStyleAlsoRegistersFills()
|
||||
{
|
||||
$styleRegistry = $this->getStyleRegistry();
|
||||
|
||||
$styleBlack = (new StyleBuilder())->setBackgroundColor(Color::BLACK)->build();
|
||||
$styleOrange = (new StyleBuilder())->setBackgroundColor(Color::ORANGE)->build();
|
||||
$styleOrangeBold = (new StyleBuilder())->setBackgroundColor(Color::ORANGE)->setFontBold()->build();
|
||||
$styleNoBackgroundColor = (new StyleBuilder())->setFontItalic()->build();
|
||||
|
||||
$styleRegistry->registerStyle($styleBlack);
|
||||
$styleRegistry->registerStyle($styleOrange);
|
||||
$styleRegistry->registerStyle($styleOrangeBold);
|
||||
$styleRegistry->registerStyle($styleNoBackgroundColor);
|
||||
|
||||
$this->assertEquals(2, count($styleRegistry->getRegisteredFills()), 'There should be 2 registered fills');
|
||||
|
||||
$this->assertEquals(2, $styleRegistry->getFillIdForStyleId($styleBlack->getId()), 'First style with background color set should have index 2 (0 and 1 being reserved)');
|
||||
$this->assertEquals(3, $styleRegistry->getFillIdForStyleId($styleOrange->getId()), 'Second style with background color set - different from first style - should have index 3');
|
||||
$this->assertEquals(3, $styleRegistry->getFillIdForStyleId($styleOrangeBold->getId()), 'Style with background color already set should have the same index');
|
||||
$this->assertEquals(0, $styleRegistry->getFillIdForStyleId($styleNoBackgroundColor->getId()), 'Style with no background color should have index 0');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRegisterStyleAlsoRegistersBorders()
|
||||
{
|
||||
$styleRegistry = $this->getStyleRegistry();
|
||||
|
||||
$borderLeft = (new BorderBuilder())->setBorderLeft()->build();
|
||||
$borderRight = (new BorderBuilder())->setBorderRight()->build();
|
||||
|
||||
$styleBorderLeft = (new StyleBuilder())->setBorder($borderLeft)->build();
|
||||
$styleBoderRight = (new StyleBuilder())->setBorder($borderRight)->build();
|
||||
$styleBoderRightBold = (new StyleBuilder())->setBorder($borderRight)->setFontBold()->build();
|
||||
$styleNoBorder = (new StyleBuilder())->setFontItalic()->build();
|
||||
|
||||
$styleRegistry->registerStyle($styleBorderLeft);
|
||||
$styleRegistry->registerStyle($styleBoderRight);
|
||||
$styleRegistry->registerStyle($styleBoderRightBold);
|
||||
$styleRegistry->registerStyle($styleNoBorder);
|
||||
|
||||
$this->assertEquals(2, count($styleRegistry->getRegisteredBorders()), 'There should be 2 registered borders');
|
||||
|
||||
$this->assertEquals(1, $styleRegistry->getBorderIdForStyleId($styleBorderLeft->getId()), 'First style with border set should have index 1 (0 is for the default style)');
|
||||
$this->assertEquals(2, $styleRegistry->getBorderIdForStyleId($styleBoderRight->getId()), 'Second style with border set - different from first style - should have index 2');
|
||||
$this->assertEquals(2, $styleRegistry->getBorderIdForStyleId($styleBoderRightBold->getId()), 'Style with border already set should have the same index');
|
||||
$this->assertEquals(0, $styleRegistry->getBorderIdForStyleId($styleNoBorder->getId()), 'Style with no border should have index 0');
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\Common\Manager\StyleManager;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
use Box\Spout\Writer\XLSX\Manager\OptionsManager;
|
||||
|
||||
@@ -24,7 +24,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
use TestUsingResource;
|
||||
|
||||
/** @var \Box\Spout\Writer\Common\Entity\Style\Style */
|
||||
protected $defaultStyle;
|
||||
private $defaultStyle;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
@@ -463,7 +463,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$fontStyle = (new StyleBuilder())->setFontBold()->build();
|
||||
$emptyStyle = (new StyleBuilder())->build();
|
||||
|
||||
$borderRightFontBoldStyle = (new StyleManager())->merge($borderRightStyle, $fontStyle);
|
||||
$borderRightFontBoldStyle = (new StyleMerger())->merge($borderRightStyle, $fontStyle);
|
||||
|
||||
$dataRows = [
|
||||
['Border-Left'],
|
||||
|
||||
Reference in new issue
Block a user