Add support for font color
This commit is contained in:
1 parent
2699ffbcae
commit
9467b5a810
8 files changed
+241
-5
No files matched your search
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Style;
|
||||
|
||||
/**
|
||||
* Class ColorTest
|
||||
*
|
||||
* @package Box\Spout\Writer\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, 'FF000000'],
|
||||
[255, 255, 255, 'FFFFFFFF'],
|
||||
[255, 0, 0, 'FFFF0000'],
|
||||
[0, 128, 0, 'FF008000'],
|
||||
[0, 255, 0, 'FF00FF00'],
|
||||
[0, 0, 255, 'FF0000FF'],
|
||||
[128, 22, 43, 'FF80162B'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ namespace Box\Spout\Writer\XLSX;
|
||||
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Style\Color;
|
||||
use Box\Spout\Writer\Style\Style;
|
||||
use Box\Spout\Writer\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
|
||||
@@ -110,6 +112,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
->build();
|
||||
$style2 = (new StyleBuilder())
|
||||
->setFontSize(15)
|
||||
->setFontColor(Color::RED)
|
||||
->setFontName('Arial')
|
||||
->build();
|
||||
|
||||
@@ -123,24 +126,27 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
// First font should be the default one
|
||||
$defaultFontElement = $fontElements->item(0);
|
||||
$this->assertChildrenNumEquals(2, $defaultFontElement, 'The default font should only have 2 properties.');
|
||||
$this->assertChildrenNumEquals(3, $defaultFontElement, 'The default font should only have 3 properties.');
|
||||
$this->assertFirstChildHasAttributeEquals((string) Writer::DEFAULT_FONT_SIZE, $defaultFontElement, 'sz', 'val');
|
||||
$this->assertFirstChildHasAttributeEquals(Style::DEFAULT_FONT_COLOR, $defaultFontElement, 'color', 'rgb');
|
||||
$this->assertFirstChildHasAttributeEquals(Writer::DEFAULT_FONT_NAME, $defaultFontElement, 'name', 'val');
|
||||
|
||||
// Second font should contain data from the first created style
|
||||
$secondFontElement = $fontElements->item(1);
|
||||
$this->assertChildrenNumEquals(6, $secondFontElement, 'The font should only have 6 properties (4 custom styles + 2 default styles).');
|
||||
$this->assertChildrenNumEquals(7, $secondFontElement, 'The font should only have 7 properties (4 custom styles + 3 default styles).');
|
||||
$this->assertChildExists($secondFontElement, 'b');
|
||||
$this->assertChildExists($secondFontElement, 'i');
|
||||
$this->assertChildExists($secondFontElement, 'u');
|
||||
$this->assertChildExists($secondFontElement, 'strike');
|
||||
$this->assertFirstChildHasAttributeEquals((string) Writer::DEFAULT_FONT_SIZE, $secondFontElement, 'sz', 'val');
|
||||
$this->assertFirstChildHasAttributeEquals(Style::DEFAULT_FONT_COLOR, $secondFontElement, 'color', 'rgb');
|
||||
$this->assertFirstChildHasAttributeEquals(Writer::DEFAULT_FONT_NAME, $secondFontElement, 'name', 'val');
|
||||
|
||||
// Third font should contain data from the second created style
|
||||
$thirdFontElement = $fontElements->item(2);
|
||||
$this->assertChildrenNumEquals(2, $thirdFontElement, 'The font should only have 2 properties.');
|
||||
$this->assertChildrenNumEquals(3, $thirdFontElement, 'The font should only have 3 properties.');
|
||||
$this->assertFirstChildHasAttributeEquals('15', $thirdFontElement, 'sz', 'val');
|
||||
$this->assertFirstChildHasAttributeEquals(Color::RED, $thirdFontElement, 'color', 'rgb');
|
||||
$this->assertFirstChildHasAttributeEquals('Arial', $thirdFontElement, 'name', 'val');
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user