diff --git a/src/PhpWord/Style/TextBox.php b/src/PhpWord/Style/TextBox.php index e43e6fa2..341d9306 100644 --- a/src/PhpWord/Style/TextBox.php +++ b/src/PhpWord/Style/TextBox.php @@ -2,10 +2,8 @@ /** * This file is part of PHPWord - A pure PHP library for reading and writing * word processing documents. - * * PHPWord is free software distributed under the terms of the GNU Lesser * General Public License version 3 as published by the Free Software Foundation. - * * For the full copyright and license information, please read the LICENSE * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. @@ -27,121 +25,128 @@ class TextBox extends Image /** * margin top. * - * @var int + * @var null|int */ private $innerMarginTop; /** * margin left. * - * @var int + * @var null|int */ private $innerMarginLeft; /** * margin right. * - * @var int + * @var null|int */ private $innerMarginRight; /** * Cell margin bottom. * - * @var int + * @var null|int */ private $innerMarginBottom; /** * border size. * - * @var int + * @var null|int */ private $borderSize; /** * border color. * - * @var string + * @var null|string */ private $borderColor; /** - * Set margin top. + * background color. * - * @param int $value + * @var null|string */ - public function setInnerMarginTop($value = null): void + private $bgColor; + + /** + * Set background color. + */ + public function setBgColor(?string $value = null): void + { + $this->bgColor = $value; + } + + /** + * Get background color. + */ + public function getBgColor(): ?string + { + return $this->bgColor; + } + + /** + * Set margin top. + */ + public function setInnerMarginTop(?int $value = null): void { $this->innerMarginTop = $value; } /** * Get margin top. - * - * @return int */ - public function getInnerMarginTop() + public function getInnerMarginTop(): ?int { return $this->innerMarginTop; } /** * Set margin left. - * - * @param int $value */ - public function setInnerMarginLeft($value = null): void + public function setInnerMarginLeft(?int $value = null): void { $this->innerMarginLeft = $value; } /** * Get margin left. - * - * @return int */ - public function getInnerMarginLeft() + public function getInnerMarginLeft(): ?int { return $this->innerMarginLeft; } /** * Set margin right. - * - * @param int $value */ - public function setInnerMarginRight($value = null): void + public function setInnerMarginRight(?int $value = null): void { $this->innerMarginRight = $value; } /** * Get margin right. - * - * @return int */ - public function getInnerMarginRight() + public function getInnerMarginRight(): ?int { return $this->innerMarginRight; } /** * Set margin bottom. - * - * @param int $value */ - public function setInnerMarginBottom($value = null): void + public function setInnerMarginBottom(?int $value = null): void { $this->innerMarginBottom = $value; } /** * Get margin bottom. - * - * @return int */ - public function getInnerMarginBottom() + public function getInnerMarginBottom(): ?int { return $this->innerMarginBottom; } @@ -149,9 +154,9 @@ class TextBox extends Image /** * Set TLRB cell margin. * - * @param int $value Margin in twips + * @param null|int $value Margin in twips */ - public function setInnerMargin($value = null): void + public function setInnerMargin(?int $value = null): void { $this->setInnerMarginTop($value); $this->setInnerMarginLeft($value); @@ -164,17 +169,15 @@ class TextBox extends Image * * @return int[] */ - public function getInnerMargin() + public function getInnerMargin(): array { return [$this->innerMarginLeft, $this->innerMarginTop, $this->innerMarginRight, $this->innerMarginBottom]; } /** * Has inner margin? - * - * @return bool */ - public function hasInnerMargins() + public function hasInnerMargins(): bool { $hasInnerMargins = false; $margins = $this->getInnerMargin(); @@ -191,39 +194,33 @@ class TextBox extends Image /** * Set border size. * - * @param int $value Size in points + * @param null|int $value Size in points */ - public function setBorderSize($value = null): void + public function setBorderSize(?int $value = null): void { $this->borderSize = $value; } /** * Get border size. - * - * @return int */ - public function getBorderSize() + public function getBorderSize(): ?int { return $this->borderSize; } /** * Set border color. - * - * @param string $value */ - public function setBorderColor($value = null): void + public function setBorderColor(?string $value = null): void { $this->borderColor = $value; } /** * Get border color. - * - * @return string */ - public function getBorderColor() + public function getBorderColor(): ?string { return $this->borderColor; } diff --git a/src/PhpWord/Writer/Word2007/Element/TextBox.php b/src/PhpWord/Writer/Word2007/Element/TextBox.php index d264f084..ff94094d 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Element/TextBox.php @@ -2,10 +2,8 @@ /** * This file is part of PHPWord - A pure PHP library for reading and writing * word processing documents. - * * PHPWord is free software distributed under the terms of the GNU Lesser * General Public License version 3 as published by the Free Software Foundation. - * * For the full copyright and license information, please read the LICENSE * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. @@ -50,6 +48,10 @@ class TextBox extends Image $xmlWriter->startElement('v:shape'); $xmlWriter->writeAttribute('type', '#_x0000_t0202'); + if ($style->getBgColor()) { + $xmlWriter->writeAttribute('fillcolor', $style->getBgColor()); + } + $styleWriter->write(); $styleWriter->writeBorder(); diff --git a/src/PhpWord/Writer/Word2007/Style/TextBox.php b/src/PhpWord/Writer/Word2007/Style/TextBox.php index 6aa6b019..d5ccf7a8 100644 --- a/src/PhpWord/Writer/Word2007/Style/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Style/TextBox.php @@ -2,10 +2,8 @@ /** * This file is part of PHPWord - A pure PHP library for reading and writing * word processing documents. - * * PHPWord is free software distributed under the terms of the GNU Lesser * General Public License version 3 as published by the Free Software Foundation. - * * For the full copyright and license information, please read the LICENSE * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. diff --git a/tests/PhpWordTests/Style/TextBoxTest.php b/tests/PhpWordTests/Style/TextBoxTest.php index 3c1c2436..284829aa 100644 --- a/tests/PhpWordTests/Style/TextBoxTest.php +++ b/tests/PhpWordTests/Style/TextBoxTest.php @@ -2,10 +2,8 @@ /** * This file is part of PHPWord - A pure PHP library for reading and writing * word processing documents. - * * PHPWord is free software distributed under the terms of the GNU Lesser * General Public License version 3 as published by the Free Software Foundation. - * * For the full copyright and license information, please read the LICENSE * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. @@ -20,15 +18,15 @@ namespace PhpOffice\PhpWordTests\Style; use InvalidArgumentException; use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\Style\TextBox; +use PHPUnit\Framework\TestCase; /** * Test class for PhpOffice\PhpWord\Style\Image. * * @coversDefaultClass \PhpOffice\PhpWord\Style\Image - * * @runTestsInSeparateProcesses */ -class TextBoxTest extends \PHPUnit\Framework\TestCase +class TextBoxTest extends TestCase { /** * Test setting style with normal value. @@ -55,6 +53,7 @@ class TextBoxTest extends \PHPUnit\Framework\TestCase 'innerMarginLeft' => '5', 'borderSize' => '2', 'borderColor' => 'red', + 'bgColor' => 'blue', ]; foreach ($properties as $key => $value) { $set = "set{$key}"; @@ -89,6 +88,7 @@ class TextBoxTest extends \PHPUnit\Framework\TestCase 'innerMarginLeft' => '5', 'borderSize' => '2', 'borderColor' => 'red', + 'bgColor' => 'blue', ]; foreach ($properties as $key => $value) { $get = "get{$key}"; @@ -305,4 +305,15 @@ class TextBoxTest extends \PHPUnit\Framework\TestCase $object->setBorderColor($expected); self::assertEquals($expected, $object->getBorderColor()); } + + /** + * Test set/get bgColor. + */ + public function testSetGetBgColor(): void + { + $expected = 'blue'; + $object = new TextBox(); + $object->setBgColor($expected); + self::assertEquals($expected, $object->getBgColor()); + } } diff --git a/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php index 98449f3b..be8f8cc4 100644 --- a/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php @@ -2,10 +2,8 @@ /** * This file is part of PHPWord - A pure PHP library for reading and writing * word processing documents. - * * PHPWord is free software distributed under the terms of the GNU Lesser * General Public License version 3 as published by the Free Software Foundation. - * * For the full copyright and license information, please read the LICENSE * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. @@ -61,11 +59,11 @@ class DocumentTest extends \PHPUnit\Framework\TestCase $doc = TestHelperDOCX::getDocument($phpWord); self::assertNotNull($doc); -// $this->assertTrue($doc->elementExists('/Properties/property[name="key1"]/vt:lpwstr')); -// $this->assertTrue($doc->elementExists('/Properties/property[name="key2"]/vt:bool')); -// $this->assertTrue($doc->elementExists('/Properties/property[name="key3"]/vt:i4')); -// $this->assertTrue($doc->elementExists('/Properties/property[name="key4"]/vt:r8')); -// $this->assertTrue($doc->elementExists('/Properties/property[name="key5"]/vt:lpwstr')); + // $this->assertTrue($doc->elementExists('/Properties/property[name="key1"]/vt:lpwstr')); + // $this->assertTrue($doc->elementExists('/Properties/property[name="key2"]/vt:bool')); + // $this->assertTrue($doc->elementExists('/Properties/property[name="key3"]/vt:i4')); + // $this->assertTrue($doc->elementExists('/Properties/property[name="key4"]/vt:r8')); + // $this->assertTrue($doc->elementExists('/Properties/property[name="key5"]/vt:lpwstr')); } /** @@ -408,7 +406,13 @@ class DocumentTest extends \PHPUnit\Framework\TestCase // behind $element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape'); $style = $element->getAttribute('style'); - self::assertRegExp('/z\-index:\-[0-9]*/', $style); + + // Try to address CI coverage issue for PHP 7.1 and 7.2 when using regex match assertions + if (method_exists(static::class, 'assertRegExp')) { + self::assertRegExp('/z\-index:\-[0-9]*/', $style); + } else { + self::assertMatchesRegularExpression('/z\-index:\-[0-9]*/', $style); + } // square $element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:pict/v:shape/w10:wrap'); @@ -551,7 +555,13 @@ class DocumentTest extends \PHPUnit\Framework\TestCase $cell->addText('Test'); $doc = TestHelperDOCX::getDocument($phpWord); - self::assertEquals(Cell::DEFAULT_BORDER_COLOR, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcBorders/w:top', 'w:color')); + self::assertEquals( + Cell::DEFAULT_BORDER_COLOR, + $doc->getElementAttribute( + '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcBorders/w:top', + 'w:color' + ) + ); } /**