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
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Creator\Style;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Style\Border;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
|
||||
/**
|
||||
* Class StyleManagerTest
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Creator\Style
|
||||
*/
|
||||
class StyleBuilderTest extends \PHPUnit_Framework_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->assertEquals(null, $currentStyle->getBorder(), 'Current style has no border');
|
||||
$this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Style\Border', $baseStyle->getBorder(), 'Base style has a border');
|
||||
$this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Style\Border', $mergedStyle->getBorder(), 'Merged style has a border');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager\Style;
|
||||
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
|
||||
/**
|
||||
* Class StyleManagerTest
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Manager\Style
|
||||
*/
|
||||
class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @return StyleManager
|
||||
*/
|
||||
private function getStyleManager()
|
||||
{
|
||||
$style = (new StyleBuilder())->build();
|
||||
$styleRegistry = new StyleRegistry($style);
|
||||
|
||||
return new StyleManager($styleRegistry);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testApplyExtraStylesIfNeededShouldApplyWrapTextIfCellContainsNewLine()
|
||||
{
|
||||
$style = (new StyleBuilder())->build();
|
||||
$this->assertFalse($style->shouldWrapText());
|
||||
|
||||
$styleManager = $this->getStyleManager();
|
||||
$updatedStyle = $styleManager->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());
|
||||
|
||||
$styleManager = $this->getStyleManager();
|
||||
$updatedStyle = $styleManager->applyExtraStylesIfNeeded($style, ["multi\nlines"]);
|
||||
|
||||
$this->assertTrue($updatedStyle->shouldWrapText());
|
||||
}
|
||||
}
|
||||
+38
-56
@@ -1,43 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager;
|
||||
namespace Box\Spout\Writer\Common\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\Border;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
|
||||
/**
|
||||
* Class StyleManagerTest
|
||||
* Class StyleMergerTest
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Manager
|
||||
* @package Box\Spout\Writer\Common\Manager\Style
|
||||
*/
|
||||
class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
class StyleMergerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var StyleManager */
|
||||
private $styleManager;
|
||||
/** @var StyleMerger */
|
||||
private $styleMerger;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->styleManager = new StyleManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSerializeShouldNotTakeIntoAccountId()
|
||||
{
|
||||
$style1 = (new StyleBuilder())->setFontBold()->build();
|
||||
$style1->setId(1);
|
||||
|
||||
$style2 = (new StyleBuilder())->setFontBold()->build();
|
||||
$style2->setId(2);
|
||||
|
||||
$this->assertEquals($this->styleManager->serialize($style1), $this->styleManager->serialize($style2));
|
||||
$this->styleMerger = new StyleMerger();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,7 +31,7 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->build();
|
||||
$currentStyle = (new StyleBuilder())->build();
|
||||
$mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertNotSame($mergedStyle, $currentStyle);
|
||||
}
|
||||
@@ -57,17 +41,26 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testMergeWithShouldMergeSetProperties()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->setFontSize(99)->setFontBold()->build();
|
||||
$baseStyle = (new StyleBuilder())
|
||||
->setFontSize(99)
|
||||
->setFontBold()
|
||||
->setFontColor(Color::YELLOW)
|
||||
->setBackgroundColor(Color::BLUE)
|
||||
->build();
|
||||
$currentStyle = (new StyleBuilder())->setFontName('Font')->setFontUnderline()->build();
|
||||
$mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertNotEquals(99, $currentStyle->getFontSize());
|
||||
$this->assertFalse($currentStyle->isFontBold());
|
||||
$this->assertEquals(Style::DEFAULT_FONT_COLOR, $currentStyle->getFontColor());
|
||||
$this->assertEquals(null, $currentStyle->getBackgroundColor());
|
||||
|
||||
$this->assertEquals(99, $mergedStyle->getFontSize());
|
||||
$this->assertTrue($mergedStyle->isFontBold());
|
||||
$this->assertEquals('Font', $mergedStyle->getFontName());
|
||||
$this->assertTrue($mergedStyle->isFontUnderline());
|
||||
$this->assertEquals(Color::YELLOW, $mergedStyle->getFontColor());
|
||||
$this->assertEquals(Color::BLUE, $mergedStyle->getBackgroundColor());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,7 +70,7 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->setFontSize(10)->build();
|
||||
$currentStyle = (new StyleBuilder())->setFontSize(99)->build();
|
||||
$mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertEquals(99, $mergedStyle->getFontSize());
|
||||
}
|
||||
@@ -88,8 +81,12 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
public function testMergeWithShouldPreferCurrentStylePropertyIfSetOnCurrentButNotOnBase()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->build();
|
||||
$currentStyle = (new StyleBuilder())->setFontItalic()->setFontStrikethrough()->build();
|
||||
$mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
|
||||
$currentStyle = (new StyleBuilder())
|
||||
->setFontItalic()
|
||||
->setFontStrikethrough()
|
||||
->build();
|
||||
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertFalse($baseStyle->isFontItalic());
|
||||
$this->assertFalse($baseStyle->isFontStrikethrough());
|
||||
@@ -110,7 +107,7 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
->setShouldWrapText()
|
||||
->build();
|
||||
$currentStyle = (new StyleBuilder())->build();
|
||||
$mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertFalse($currentStyle->isFontUnderline());
|
||||
$this->assertTrue($mergedStyle->isFontUnderline());
|
||||
@@ -126,10 +123,10 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->build();
|
||||
$currentStyle = (new StyleBuilder())->build();
|
||||
$mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertTrue($this->styleManager->serialize($baseStyle) === $this->styleManager->serialize($currentStyle));
|
||||
$this->assertTrue($this->styleManager->serialize($currentStyle) === $this->styleManager->serialize($mergedStyle));
|
||||
$this->assertSameStyles($baseStyle, $currentStyle);
|
||||
$this->assertSameStyles($currentStyle, $mergedStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,36 +139,21 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
->setFontSize(Style::DEFAULT_FONT_SIZE)
|
||||
->build();
|
||||
$currentStyle = (new StyleBuilder())->build();
|
||||
$mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertTrue($this->styleManager->serialize($currentStyle) === $this->styleManager->serialize($mergedStyle));
|
||||
$this->assertSameStyles($currentStyle, $mergedStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style $style1
|
||||
* @param Style $style2
|
||||
* @return void
|
||||
*/
|
||||
public function testStyleBuilderShouldApplyBorders()
|
||||
private function assertSameStyles(Style $style1, Style $style2)
|
||||
{
|
||||
$border = (new BorderBuilder())
|
||||
->setBorderBottom()
|
||||
->build();
|
||||
$style = (new StyleBuilder())->setBorder($border)->build();
|
||||
$this->assertTrue($style->shouldApplyBorder());
|
||||
}
|
||||
$fakeStyle = (new StyleBuilder())->build();
|
||||
$styleRegistry = new StyleRegistry($fakeStyle);
|
||||
|
||||
/**
|
||||
* @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();
|
||||
$mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertEquals(null, $currentStyle->getBorder(), 'Current style has no border');
|
||||
$this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Style\Border', $baseStyle->getBorder(), 'Base style has a border');
|
||||
$this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Style\Border', $mergedStyle->getBorder(), 'Merged style has a border');
|
||||
$this->assertTrue($styleRegistry->serialize($style1) === $styleRegistry->serialize($style2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\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\Border;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
|
||||
/**
|
||||
* Class StyleRegistryTest
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Manager\Style
|
||||
*/
|
||||
class StyleRegistryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var Style */
|
||||
private $defaultStyle;
|
||||
|
||||
/** @var StyleRegistry */
|
||||
private $styleRegistry;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->defaultStyle = (new StyleBuilder())->build();
|
||||
$this->styleRegistry = new StyleRegistry($this->defaultStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSerializeShouldNotTakeIntoAccountId()
|
||||
{
|
||||
$style1 = (new StyleBuilder())->setFontBold()->build();
|
||||
$style1->setId(1);
|
||||
|
||||
$style2 = (new StyleBuilder())->setFontBold()->build();
|
||||
$style2->setId(2);
|
||||
|
||||
$this->assertEquals($this->styleRegistry->serialize($style1), $this->styleRegistry->serialize($style2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @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->styleRegistry->registerStyle($style1);
|
||||
$registeredStyle2 = $this->styleRegistry->registerStyle($style2);
|
||||
|
||||
$this->assertEquals(1, $registeredStyle1->getId());
|
||||
$this->assertEquals(2, $registeredStyle2->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRegisterStyleShouldReuseAlreadyRegisteredStyles()
|
||||
{
|
||||
$style = (new StyleBuilder())->setFontBold()->build();
|
||||
|
||||
$registeredStyle1 = $this->styleRegistry->registerStyle($style);
|
||||
$registeredStyle2 = $this->styleRegistry->registerStyle($style);
|
||||
|
||||
$this->assertEquals(1, $registeredStyle1->getId());
|
||||
$this->assertEquals(1, $registeredStyle2->getId());
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\ODS\Helper;
|
||||
|
||||
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\ODS\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 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,40 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\ODS\Manager\Style;
|
||||
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
|
||||
/**
|
||||
* Class StyleRegistryTest
|
||||
*
|
||||
* @package Box\Spout\Writer\ODS\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 testRegisterStyleKeepsTrackOfUsedFonts()
|
||||
{
|
||||
$styleRegistry = $this->getStyleRegistry();
|
||||
|
||||
$this->assertEquals(1, count($styleRegistry->getUsedFonts()), 'There should only be the default font name');
|
||||
|
||||
$style1 = (new StyleBuilder())->setFontName("MyFont1")->build();
|
||||
$styleRegistry->registerStyle($style1);
|
||||
|
||||
$style2 = (new StyleBuilder())->setFontName("MyFont2")->build();
|
||||
$styleRegistry->registerStyle($style2);
|
||||
|
||||
$this->assertEquals(3, count($styleRegistry->getUsedFonts()), 'There should be 3 fonts registered');
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,8 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
use TestUsingResource;
|
||||
|
||||
/** @var \Box\Spout\Writer\Common\Entity\Style\Style */
|
||||
protected $defaultStyle;
|
||||
/** @var Style */
|
||||
private $defaultStyle;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
|
||||
@@ -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