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,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');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user