Move Style classes into Common folder (#432)
This commit is contained in:
1 parent
c4e25a168e
commit
238756ab6e
38 files changed
+463
-298
No files matched your search
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\EntityStyle;
|
||||
|
||||
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Border;
|
||||
use Box\Spout\Writer\Common\Entity\Style\BorderPart;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
|
||||
/**
|
||||
* Class BorderTest
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\EntityStyle
|
||||
*/
|
||||
class BorderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testValidInstance()
|
||||
{
|
||||
$noConstructorParams = new Border();
|
||||
$withConstructorParams = new Border([
|
||||
new BorderPart(Border::LEFT)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Writer\Exception\Border\InvalidNameException
|
||||
*/
|
||||
public function testInvalidBorderPart()
|
||||
{
|
||||
new BorderPart('invalid');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Writer\Exception\Border\InvalidStyleException
|
||||
*/
|
||||
public function testInvalidBorderPartStyle()
|
||||
{
|
||||
new BorderPart(Border::LEFT, Color::BLACK, Border::WIDTH_THIN, 'invalid');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Writer\Exception\Border\InvalidWidthException
|
||||
*/
|
||||
public function testInvalidBorderPartWidth()
|
||||
{
|
||||
new BorderPart(Border::LEFT, Color::BLACK, 'invalid', Border::STYLE_DASHED);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNotMoreThanFourPartsPossible()
|
||||
{
|
||||
$border = new Border();
|
||||
$border
|
||||
->addPart(new BorderPart(Border::LEFT))
|
||||
->addPart(new BorderPart(Border::RIGHT))
|
||||
->addPart(new BorderPart(Border::TOP))
|
||||
->addPart(new BorderPart(Border::BOTTOM))
|
||||
->addPart(new BorderPart(Border::LEFT));
|
||||
|
||||
$this->assertEquals(4, count($border->getParts()), 'There should never be more than 4 border parts');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSetParts()
|
||||
{
|
||||
$border = new Border();
|
||||
$border->setParts([
|
||||
new BorderPart(Border::LEFT)
|
||||
]);
|
||||
|
||||
$this->assertEquals(1, count($border->getParts()), 'It should be possible to set the border parts');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testBorderBuilderFluent()
|
||||
{
|
||||
$border = (new BorderBuilder())
|
||||
->setBorderBottom()
|
||||
->setBorderTop()
|
||||
->setBorderLeft()
|
||||
->setBorderRight()
|
||||
->build();
|
||||
$this->assertEquals(4, count($border->getParts()), 'The border builder exposes a fluent interface');
|
||||
}
|
||||
|
||||
/**
|
||||
* :D :S
|
||||
* @return void
|
||||
*/
|
||||
public function testAnyCombinationOfAllowedBorderPartsParams()
|
||||
{
|
||||
$color = Color::BLACK;
|
||||
foreach (BorderPart::getAllowedNames() as $allowedName) {
|
||||
foreach (BorderPart::getAllowedStyles() as $allowedStyle) {
|
||||
foreach (BorderPart::getAllowedWidths() as $allowedWidth) {
|
||||
$borderPart = new BorderPart($allowedName, $color, $allowedWidth, $allowedStyle);
|
||||
$border = new Border();
|
||||
$border->addPart($borderPart);
|
||||
$this->assertEquals(1, count($border->getParts()));
|
||||
|
||||
/** @var $part BorderPart */
|
||||
$part = $border->getParts()[$allowedName];
|
||||
|
||||
$this->assertEquals($allowedStyle, $part->getStyle());
|
||||
$this->assertEquals($allowedWidth, $part->getWidth());
|
||||
$this->assertEquals($color, $part->getColor());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Entity\Style;
|
||||
|
||||
/**
|
||||
* Class ColorTest
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Entity\Style
|
||||
*/
|
||||
class ColorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForTestRGB()
|
||||
{
|
||||
|
||||
return [
|
||||
[0, 0, 0, Color::BLACK],
|
||||
[255, 255, 255, Color::WHITE],
|
||||
[255, 0, 0, Color::RED],
|
||||
[192, 0, 0, Color::DARK_RED],
|
||||
[255, 192, 0, Color::ORANGE],
|
||||
[255, 255, 0, Color::YELLOW],
|
||||
[146, 208, 64, Color::LIGHT_GREEN],
|
||||
[0, 176, 80, Color::GREEN],
|
||||
[0, 176, 224, Color::LIGHT_BLUE],
|
||||
[0, 112, 192, Color::BLUE],
|
||||
[0, 32, 96, Color::DARK_BLUE],
|
||||
[112, 48, 160, Color::PURPLE],
|
||||
[0, 0, 0, '000000'],
|
||||
[255, 255, 255, 'FFFFFF'],
|
||||
[255, 0, 0, 'FF0000'],
|
||||
[0, 128, 0, '008000'],
|
||||
[0, 255, 0, '00FF00'],
|
||||
[0, 0, 255, '0000FF'],
|
||||
[128, 22, 43, '80162B'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForTestRGB
|
||||
*
|
||||
* @param int $red
|
||||
* @param int $green
|
||||
* @param int $blue
|
||||
* @param string $expectedColor
|
||||
* @return void
|
||||
*/
|
||||
public function testRGB($red, $green, $blue, $expectedColor)
|
||||
{
|
||||
$color = Color::rgb($red, $green, $blue);
|
||||
$this->assertEquals($expectedColor, $color);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForTestRGBAInvalidColorComponents()
|
||||
{
|
||||
return [
|
||||
[-1, 0, 0],
|
||||
[0, -1, 0],
|
||||
[0, 0, -1],
|
||||
[999, 0, 0],
|
||||
[0, 999, 0],
|
||||
[0, 0, 999],
|
||||
[null, 0, 0],
|
||||
[0, null, 0],
|
||||
[0, 0, null],
|
||||
['1', 0, 0],
|
||||
[0, '1', 0],
|
||||
[0, 0, '1'],
|
||||
[true, 0, 0],
|
||||
[0, true, 0],
|
||||
[0, 0, true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForTestRGBAInvalidColorComponents
|
||||
* @expectedException \Box\Spout\Writer\Exception\InvalidColorException
|
||||
*
|
||||
* @param int $red
|
||||
* @param int $green
|
||||
* @param int $blue
|
||||
* @return void
|
||||
*/
|
||||
public function testRGBInvalidColorComponents($red, $green, $blue)
|
||||
{
|
||||
Color::rgb($red, $green, $blue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager;
|
||||
|
||||
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
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Manager
|
||||
*/
|
||||
class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var StyleManager */
|
||||
private $styleManager;
|
||||
|
||||
/**
|
||||
* @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));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeWithShouldReturnACopy()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->build();
|
||||
$currentStyle = (new StyleBuilder())->build();
|
||||
$mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertNotSame($mergedStyle, $currentStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeWithShouldMergeSetProperties()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->setFontSize(99)->setFontBold()->build();
|
||||
$currentStyle = (new StyleBuilder())->setFontName('Font')->setFontUnderline()->build();
|
||||
$mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertNotEquals(99, $currentStyle->getFontSize());
|
||||
$this->assertFalse($currentStyle->isFontBold());
|
||||
|
||||
$this->assertEquals(99, $mergedStyle->getFontSize());
|
||||
$this->assertTrue($mergedStyle->isFontBold());
|
||||
$this->assertEquals('Font', $mergedStyle->getFontName());
|
||||
$this->assertTrue($mergedStyle->isFontUnderline());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeWithShouldPreferCurrentStylePropertyIfSetOnCurrentAndOnBase()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())->setFontSize(10)->build();
|
||||
$currentStyle = (new StyleBuilder())->setFontSize(99)->build();
|
||||
$mergedStyle = $this->styleManager->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->styleManager->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->styleManager->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->styleManager->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertTrue($this->styleManager->serialize($baseStyle) === $this->styleManager->serialize($currentStyle));
|
||||
$this->assertTrue($this->styleManager->serialize($currentStyle) === $this->styleManager->serialize($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->styleManager->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertTrue($this->styleManager->serialize($currentStyle) === $this->styleManager->serialize($mergedStyle));
|
||||
}
|
||||
|
||||
/**
|
||||
* @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();
|
||||
$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');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user