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,159 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager\Style;
|
||||
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
|
||||
/**
|
||||
* Class StyleMergerTest
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Manager\Style
|
||||
*/
|
||||
class StyleMergerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var StyleMerger */
|
||||
private $styleMerger;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->styleMerger = new StyleMerger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeWithShouldReturnACopy()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->build();
|
||||
$currentStyle = (new StyleBuilder())->build();
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertNotSame($mergedStyle, $currentStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeWithShouldMergeSetProperties()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())
|
||||
->setFontSize(99)
|
||||
->setFontBold()
|
||||
->setFontColor(Color::YELLOW)
|
||||
->setBackgroundColor(Color::BLUE)
|
||||
->build();
|
||||
$currentStyle = (new StyleBuilder())->setFontName('Font')->setFontUnderline()->build();
|
||||
$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());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeWithShouldPreferCurrentStylePropertyIfSetOnCurrentAndOnBase()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->setFontSize(10)->build();
|
||||
$currentStyle = (new StyleBuilder())->setFontSize(99)->build();
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertEquals(99, $mergedStyle->getFontSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeWithShouldPreferCurrentStylePropertyIfSetOnCurrentButNotOnBase()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->build();
|
||||
$currentStyle = (new StyleBuilder())
|
||||
->setFontItalic()
|
||||
->setFontStrikethrough()
|
||||
->build();
|
||||
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertFalse($baseStyle->isFontItalic());
|
||||
$this->assertFalse($baseStyle->isFontStrikethrough());
|
||||
|
||||
$this->assertTrue($mergedStyle->isFontItalic());
|
||||
$this->assertTrue($mergedStyle->isFontStrikethrough());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeWithShouldPreferBaseStylePropertyIfSetOnBaseButNotOnCurrent()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())
|
||||
->setFontItalic()
|
||||
->setFontUnderline()
|
||||
->setFontStrikethrough()
|
||||
->setShouldWrapText()
|
||||
->build();
|
||||
$currentStyle = (new StyleBuilder())->build();
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertFalse($currentStyle->isFontUnderline());
|
||||
$this->assertTrue($mergedStyle->isFontUnderline());
|
||||
|
||||
$this->assertFalse($currentStyle->shouldWrapText());
|
||||
$this->assertTrue($mergedStyle->shouldWrapText());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeWithShouldDoNothingIfStylePropertyNotSetOnBaseNorCurrent()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->build();
|
||||
$currentStyle = (new StyleBuilder())->build();
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertSameStyles($baseStyle, $currentStyle);
|
||||
$this->assertSameStyles($currentStyle, $mergedStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeWithShouldDoNothingIfStylePropertyNotSetOnCurrentAndIsDefaultValueOnBase()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())
|
||||
->setFontName(Style::DEFAULT_FONT_NAME)
|
||||
->setFontSize(Style::DEFAULT_FONT_SIZE)
|
||||
->build();
|
||||
$currentStyle = (new StyleBuilder())->build();
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertSameStyles($currentStyle, $mergedStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style $style1
|
||||
* @param Style $style2
|
||||
* @return void
|
||||
*/
|
||||
private function assertSameStyles(Style $style1, Style $style2)
|
||||
{
|
||||
$fakeStyle = (new StyleBuilder())->build();
|
||||
$styleRegistry = new StyleRegistry($fakeStyle);
|
||||
|
||||
$this->assertTrue($styleRegistry->serialize($style1) === $styleRegistry->serialize($style2));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user