From 74e0f3b2d1fb038d2a0fe36bd01feacf026c14fa Mon Sep 17 00:00:00 2001 From: Florian Meyer Date: Wed, 17 Aug 2016 11:36:21 +0200 Subject: [PATCH] Added some unit tests --- .../ExcelCreator/ConfiguredSheet.php | 13 ++++++++--- .../ExcelCreator/Formatter/YesNoFormatter.php | 2 +- .../Formatter/YesNoFormatterTest.php | 23 +++++++++++++++++++ .../ExcelCreator/IntegrationTest.php | 4 ++++ 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 tests/StingerSoft/ExcelCreator/Formatter/YesNoFormatterTest.php diff --git a/src/StingerSoft/ExcelCreator/ConfiguredSheet.php b/src/StingerSoft/ExcelCreator/ConfiguredSheet.php index 326a239..6662f6e 100644 --- a/src/StingerSoft/ExcelCreator/ConfiguredSheet.php +++ b/src/StingerSoft/ExcelCreator/ConfiguredSheet.php @@ -178,10 +178,14 @@ public function applyData($startColumn = 0, $headerRow = 1) { protected function renderHeaderRow($startColumn = 0, $headerRow = 1) { $this->sheet->getStyle(\PHPExcel_Cell::stringFromColumnIndex($startColumn) . $headerRow . ':' . \PHPExcel_Cell::stringFromColumnIndex($startColumn + $this->bindings->count() - 1) . $headerRow)->applyFromArray($this->getDefaultHeaderStyling()); foreach($this->bindings as $binding) { - $this->sheet->setCellValueByColumnAndRow($startColumn, $headerRow, $this->decodeHtmlEntity($this->translate($binding->getLabel(), $binding->getLabelTranslationDomain()))); + $cell = $this->sheet->getCellByColumnAndRow($startColumn, $headerRow); + $cell->setValue($this->decodeHtmlEntity($this->translate($binding->getLabel(), $binding->getLabelTranslationDomain()))); if($binding->getOutline()) { $this->sheet->getColumnDimensionByColumn($startColumn)->setOutlineLevel($binding->getOutline()); } + if($binding->getHeaderFontColor() || $binding->getHeaderBackgroundColor()) { + $cell->getStyle()->applyFromArray($this->getDefaultHeaderStyling($binding->getHeaderFontColor(), $binding->getHeaderBackgroundColor())); + } $startColumn++; } } @@ -370,17 +374,20 @@ protected function getDefaultDataStyling($fontColor, $bgColor) { * * @return string[] */ - protected function getDefaultHeaderStyling() { + protected function getDefaultHeaderStyling($fontColor = null, $bgColor = null) { return array( 'font' => array( 'name' => $this->defaultFontFamily, 'size' => $this->defaultHeaderFontSize, + 'color'=> array( + 'rgb' => $fontColor ?: $this->defaultHeaderFontColor, + ), 'bold' => true ), 'fill' => array( 'type' => \PHPExcel_Style_Fill::FILL_SOLID, 'color' => array( - 'rgb' => $this->defaultHeaderBackgroundColor + 'rgb' => $bgColor ?: $this->defaultHeaderBackgroundColor ) ), 'alignment' => array( diff --git a/src/StingerSoft/ExcelCreator/Formatter/YesNoFormatter.php b/src/StingerSoft/ExcelCreator/Formatter/YesNoFormatter.php index 64f748d..59acaf1 100644 --- a/src/StingerSoft/ExcelCreator/Formatter/YesNoFormatter.php +++ b/src/StingerSoft/ExcelCreator/Formatter/YesNoFormatter.php @@ -15,7 +15,7 @@ abstract class YesNoFormatter { public static function createYesNoFormatter($yesLabel, $noLabel) { return function($value) use ($yesLabel, $noLabel) { - return $value == true ? $yesLabel : $noLabel; + return filter_var($value, FILTER_VALIDATE_BOOLEAN) === true ? $yesLabel : $noLabel; }; } } \ No newline at end of file diff --git a/tests/StingerSoft/ExcelCreator/Formatter/YesNoFormatterTest.php b/tests/StingerSoft/ExcelCreator/Formatter/YesNoFormatterTest.php new file mode 100644 index 0000000..f6cf6b0 --- /dev/null +++ b/tests/StingerSoft/ExcelCreator/Formatter/YesNoFormatterTest.php @@ -0,0 +1,23 @@ + + * (c) Florian Meyer + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace StingerSoft\ExcelCreator\Formatter; + +class YesNoFormatterTest extends \PHPUnit_Framework_TestCase { + + public function testCreateTranslationFormatter() { + $pirateYesNoformatter = YesNoFormatter::createYesNoFormatter('Arrrr!', 'Avast!'); + + $this->assertEquals('Arrrr!', $pirateYesNoformatter(true)); + $this->assertEquals('Avast!', $pirateYesNoformatter(false)); + $this->assertEquals('Avast!', $pirateYesNoformatter('nonono')); + } +} \ No newline at end of file diff --git a/tests/StingerSoft/ExcelCreator/IntegrationTest.php b/tests/StingerSoft/ExcelCreator/IntegrationTest.php index 98306f6..2660d0d 100644 --- a/tests/StingerSoft/ExcelCreator/IntegrationTest.php +++ b/tests/StingerSoft/ExcelCreator/IntegrationTest.php @@ -30,6 +30,10 @@ public function testSimpleCycle() { $binding->setColumnWidth('auto'); $binding->setWrapText(true); $binding->setOutline(1); + $binding->setHeaderBackgroundColor('000000'); + $binding->setHeaderFontColor('FFFFFF'); + $binding->setDataFontColor('$FFFFFF'); + $binding->setDataBackgroundColor('$FAFAFA'); $binding->setFormatter(function ($value) { return strtoupper($value); });