Add ability to pass a Style object to a Section element (#1416)

* Add ability to pass a Style object to a Section
* Fix typo
* update changelog
This commit is contained in:
Nathan Dench 2018-12-09 08:35:32 +10:00 committed by troosan
parent c2b54cc343
commit cf3132acac
4 changed files with 43 additions and 3 deletions

View File

@ -7,6 +7,7 @@ v0.16.0 (xx dec 2018)
----------------------
### Added
- Add setting Chart Title and Legend visibility @Tom-Magill #1433
- Add ability to pass a Style object in Section constructor @ndench #1416
- Add support for hidden text @Alexmg86 #1527
### Fixed

View File

@ -59,14 +59,16 @@ class Section extends AbstractContainer
* Create new instance
*
* @param int $sectionCount
* @param array $style
* @param null|array|\PhpOffice\PhpWord\Style $style
*/
public function __construct($sectionCount, $style = null)
{
$this->sectionId = $sectionCount;
$this->setDocPart($this->container, $this->sectionId);
$this->style = new SectionStyle();
$this->setStyle($style);
if (null === $style) {
$style = new SectionStyle();
}
$this->style = $this->setNewStyle(new SectionStyle(), $style);
}
/**

View File

@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Section as SectionStyle;
/**
* @covers \PhpOffice\PhpWord\Element\Section
@ -27,6 +28,27 @@ use PhpOffice\PhpWord\Style;
*/
class SectionTest extends \PHPUnit\Framework\TestCase
{
public function testConstructorWithDefaultStyle()
{
$section = new Section(0);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $section->getStyle());
}
public function testConstructorWithArrayStyle()
{
$section = new Section(0, array('orientation' => 'landscape'));
$style = $section->getStyle();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $style);
$this->assertEquals('landscape', $style->getOrientation());
}
public function testConstructorWithObjectStyle()
{
$style = new SectionStyle();
$section = new Section(0, $style);
$this->assertSame($style, $section->getStyle());
}
/**
* @covers ::setStyle
*/

View File

@ -492,4 +492,19 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[2]/w:pPr/w:pStyle'));
$this->assertEquals('Heading1', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:pStyle', 'w:val'));
}
/**
* Test correct writing of text with ampersant in it
*/
public function testTextWithAmpersant()
{
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('this text contains an & (ampersant)');
$doc = TestHelperDOCX::getDocument($phpWord);
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
$this->assertEquals('this text contains an & (ampersant)', $doc->getElement('/w:document/w:body/w:p/w:r/w:t')->nodeValue);
}
}